| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package bingocloud
- import (
- "fmt"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/errors"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- )
- type SRegion struct {
- multicloud.SRegion
- multicloud.SRegionEipBase
- multicloud.SRegionLbBase
- multicloud.SRegionOssBase
- multicloud.SRegionSecurityGroupBase
- multicloud.SRegionVpcBase
- multicloud.SRegionZoneBase
- client *SBingoCloudClient
- RegionId string
- RegionName string
- Hypervisor string
- NetworkMode string
- RegionEndpoint string
- }
- func (self *SRegion) GetId() string {
- return self.RegionId
- }
- func (self *SRegion) GetName() string {
- return self.RegionName
- }
- func (self *SRegion) GetGlobalId() string {
- return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_BINGO_CLOUD, self.RegionId)
- }
- func (self *SRegion) GetClient() *SBingoCloudClient {
- return self.client
- }
- func (self *SRegion) GetCapabilities() []string {
- return self.client.GetCapabilities()
- }
- func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
- return cloudprovider.SModelI18nTable{}
- }
- func (self *SRegion) GetProvider() string {
- return api.CLOUD_PROVIDER_BINGO_CLOUD
- }
- func (self *SRegion) GetStatus() string {
- return api.CLOUD_REGION_STATUS_INSERVER
- }
- func (self *SRegion) GetCloudEnv() string {
- return ""
- }
- func (self *SRegion) getAccountUser() string {
- quotas, err := self.GetQuotas()
- if err != nil {
- return ""
- }
- ownerId := ""
- if len(quotas) > 0 {
- ownerId = quotas[0].OwnerId
- }
- return ownerId
- }
- func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
- return cloudprovider.SGeographicInfo{}
- }
- func (self *SRegion) invoke(action string, params map[string]string) (jsonutils.JSONObject, error) {
- return self.client.invoke(action, params)
- }
- func (self *SBingoCloudClient) GetRegions() ([]SRegion, error) {
- resp, err := self.invoke("DescribeRegions", nil)
- if err != nil {
- return nil, err
- }
- var ret []SRegion
- return ret, resp.Unmarshal(&ret, "regionInfo")
- }
- func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
- storages, err := self.getStorages()
- if err != nil {
- return nil, err
- }
- for i := range storages {
- if storages[i].GetGlobalId() == id {
- storages[i].cluster = &SCluster{region: self}
- return &storages[i], nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
- }
- func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
- var storages []SStorage
- part, nextToken, err := self.GetStorages("")
- if err != nil {
- return nil, err
- }
- storages = append(storages, part...)
- for len(nextToken) > 0 {
- part, nextToken, err = self.GetStorages(nextToken)
- if err != nil {
- return nil, err
- }
- storages = append(storages, part...)
- }
- var ret []cloudprovider.ICloudStoragecache
- for i := range storages {
- cache := SStoragecache{
- region: self,
- storageName: storages[i].StorageName,
- storageId: storages[i].StorageId,
- }
- ret = append(ret, &cache)
- }
- return ret, nil
- }
- func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
- caches, err := self.GetIStoragecaches()
- if err != nil {
- return nil, err
- }
- for i := range caches {
- if caches[i].GetGlobalId() == id {
- return caches[i], nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
- }
- func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
- zones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- var ret []cloudprovider.ICloudHost
- for i := range zones {
- hosts, err := zones[i].GetIHosts()
- if err != nil {
- return nil, err
- }
- ret = append(ret, hosts...)
- }
- return ret, nil
- }
- func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
- zones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := range zones {
- hosts, err := zones[i].GetIHosts()
- if err != nil {
- return nil, err
- }
- for i := range hosts {
- if hosts[i].GetGlobalId() == id {
- return hosts[i], nil
- }
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
- }
- func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
- snapshots, err := self.getSnapshots("", "")
- if err != nil {
- return nil, err
- }
- iSnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots))
- for i := 0; i < len(snapshots); i++ {
- snapshots[i].region = self
- iSnapshots[i] = snapshots[i]
- }
- return iSnapshots, nil
- }
- func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
- snapshots, err := self.getSnapshots(id, "")
- if err != nil {
- return nil, err
- }
- for i := range snapshots {
- if snapshots[i].GetGlobalId() == id {
- snapshots[i].region = self
- return &snapshots[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
- var groups []SSecurityGroup
- nextToken := ""
- for {
- part, _nextToken, err := self.GetSecurityGroups("", "", nextToken)
- if err != nil {
- return nil, err
- }
- groups = append(groups, part...)
- if len(_nextToken) == 0 || len(part) == 0 {
- break
- }
- nextToken = _nextToken
- }
- var ret []cloudprovider.ICloudSecurityGroup
- for i := range groups {
- groups[i].region = self
- ret = append(ret, &groups[i])
- }
- return ret, nil
- }
- func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
- var vms []SInstance
- nextToken := ""
- for {
- part, _nextToken, err := region.GetInstances("", "", MAX_RESULT, nextToken)
- if err != nil {
- return nil, err
- }
- vms = append(vms, part...)
- if len(part) == 0 || len(_nextToken) == 0 {
- break
- }
- nextToken = _nextToken
- }
- var ret []cloudprovider.ICloudVM
- for i := range vms {
- ret = append(ret, &vms[i])
- }
- return ret, nil
- }
|