yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/region.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package incloudsphere 16 17 import ( 18 "fmt" 19 "net/url" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SRegion struct { 30 multicloud.SRegion 31 multicloud.SRegionEipBase 32 multicloud.SNoObjectStorageRegion 33 multicloud.SRegionLbBase 34 35 client *SphereClient 36 } 37 38 func (self *SRegion) GetClient() *SphereClient { 39 return self.client 40 } 41 42 func (self *SRegion) GetName() string { 43 return self.client.cpcfg.Name 44 } 45 46 func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable { 47 table := cloudprovider.SModelI18nTable{} 48 table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()) 49 return table 50 } 51 52 func (self *SRegion) GetId() string { 53 return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_INCLOUD_SPHERE, self.client.cpcfg.Id) 54 } 55 56 func (self *SRegion) GetGlobalId() string { 57 return self.GetId() 58 } 59 60 func (self *SRegion) GetProvider() string { 61 return CLOUD_PROVIDER_INCLOUD_SPHERE 62 } 63 64 func (self *SRegion) GetCloudEnv() string { 65 return "" 66 } 67 68 func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { 69 return cloudprovider.SGeographicInfo{} 70 } 71 72 func (self *SRegion) GetStatus() string { 73 return api.CLOUD_REGION_STATUS_INSERVER 74 } 75 76 func (self *SRegion) Refresh() error { 77 return nil 78 } 79 80 func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) { 81 return nil, cloudprovider.ErrNotSupported 82 } 83 84 func (self *SRegion) GetISecurityGroupByName(opts *cloudprovider.SecurityGroupFilterOptions) (cloudprovider.ICloudSecurityGroup, error) { 85 return nil, cloudprovider.ErrNotSupported 86 } 87 88 func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) { 89 return nil, cloudprovider.ErrNotSupported 90 } 91 92 func (self *SRegion) CreateIVpc(conf *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) { 93 return nil, cloudprovider.ErrNotSupported 94 } 95 96 func (self *SRegion) GetCapabilities() []string { 97 return self.client.GetCapabilities() 98 } 99 100 func (self *SRegion) getVpc() *SVpc { 101 return &SVpc{region: self} 102 } 103 104 func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { 105 vpc := self.getVpc() 106 return []cloudprovider.ICloudVpc{vpc}, nil 107 } 108 109 func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) { 110 vpcs, err := self.GetIVpcs() 111 if err != nil { 112 return nil, err 113 } 114 for i := range vpcs { 115 if vpcs[i].GetGlobalId() == id { 116 return vpcs[i], nil 117 } 118 } 119 return nil, cloudprovider.ErrNotFound 120 } 121 122 func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) { 123 zone, err := self.GetZone(id) 124 if err != nil { 125 return nil, errors.Wrapf(err, "GetZone(%s)", id) 126 } 127 return zone, nil 128 } 129 130 func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) { 131 zones, err := self.GetZones() 132 if err != nil { 133 return nil, err 134 } 135 ret := []cloudprovider.ICloudZone{} 136 for i := range zones { 137 zones[i].region = self 138 ret = append(ret, &zones[i]) 139 } 140 return ret, nil 141 } 142 143 func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) { 144 vm, err := self.GetInstance(id) 145 if err != nil { 146 return nil, err 147 } 148 return vm, nil 149 } 150 151 func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 152 host, err := self.GetHost(id) 153 if err != nil { 154 return nil, err 155 } 156 zone, err := self.GetZone(host.DataCenterId) 157 if err != nil { 158 return nil, err 159 } 160 host.zone = zone 161 return host, nil 162 } 163 164 func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) { 165 zones, err := self.GetZones() 166 if err != nil { 167 return nil, err 168 } 169 ret := []cloudprovider.ICloudStoragecache{} 170 for i := range zones { 171 cache := &SStoragecache{zone: &zones[i]} 172 ret = append(ret, cache) 173 } 174 return ret, nil 175 } 176 177 func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) { 178 caches, err := self.GetIStoragecaches() 179 if err != nil { 180 return nil, err 181 } 182 for i := range caches { 183 if caches[i].GetGlobalId() == id { 184 return caches[i], nil 185 } 186 } 187 return nil, cloudprovider.ErrNotFound 188 } 189 190 func (self *SRegion) _list(res string, params url.Values) (jsonutils.JSONObject, error) { 191 return self.client._list(res, params) 192 } 193 194 func (self *SRegion) list(res string, params url.Values, retVal interface{}) error { 195 return self.client.list(res, params, retVal) 196 } 197 198 func (self *SRegion) get(res string, params url.Values, retVal interface{}) error { 199 return self.client.get(res, params, retVal) 200 } 201 202 func (self *SRegion) post(res string, params interface{}) (jsonutils.JSONObject, error) { 203 return self.client.post(res, params) 204 } 205 206 func (self *SRegion) put(res string, params url.Values, body jsonutils.JSONObject, retVal interface{}) error { 207 return self.client.put(res, params, body, retVal) 208 } 209 210 func (self *SRegion) del(res string, params url.Values, retVal interface{}) error { 211 return self.client.del(res, params, retVal) 212 }