| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104 |
- // 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 apsara
- import (
- "fmt"
- "strings"
- "time"
- "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "yunion.io/x/jsonutils"
- "yunion.io/x/log"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/util/regutils"
- "yunion.io/x/pkg/utils"
- 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
- client *SApsaraClient
- sdkClient *sdk.Client
- RegionId string
- LocalName string
- izones []cloudprovider.ICloudZone
- ivpcs []cloudprovider.ICloudVpc
- lbEndpints map[string]string
- storageCache *SStoragecache
- instanceTypes []SInstanceType
- latitude float64
- longitude float64
- fetchLocation bool
- ossEndpoint string
- }
- func (self *SRegion) GetClient() *SApsaraClient {
- return self.client
- }
- func (self *SRegion) getSdkClient() (*sdk.Client, error) {
- if self.sdkClient == nil {
- var err error
- self.sdkClient, err = self.GetClient().getDefaultClient(self.RegionId)
- if err != nil {
- return nil, err
- }
- }
- return self.sdkClient, nil
- }
- func (self *SRegion) GetOssClient() (*oss.Client, error) {
- if len(self.ossEndpoint) == 0 {
- _, err := self.GetBuckets()
- if err != nil {
- return nil, err
- }
- if len(self.ossEndpoint) == 0 {
- return nil, fmt.Errorf("no available buckets")
- }
- }
- return self.client.getOssClient(self.ossEndpoint)
- }
- func (self *SRegion) productRequest(client *sdk.Client, product, domain, apiVersion, apiName string, params map[string]string, debug bool) (jsonutils.JSONObject, error) {
- params["Product"] = product
- return jsonRequest(client, domain, apiVersion, apiName, params, debug)
- }
- func (self *SRegion) ossRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
- params["RegionId"] = self.RegionId
- return self.client.ossRequest(apiName, params)
- }
- func (self *SRegion) ecsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
- client, err := self.getSdkClient()
- if err != nil {
- return nil, err
- }
- domain := self.client.getDomain(APSARA_PRODUCT_ECS)
- params["Product"] = APSARA_PRODUCT_ECS
- return jsonRequest(client, domain, APSARA_API_VERSION, apiName, params, self.client.debug)
- }
- func (self *SRegion) rdsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
- client, err := self.getSdkClient()
- if err != nil {
- return nil, err
- }
- domain := self.client.getDomain(APSARA_PRODUCT_RDS)
- return self.productRequest(client, APSARA_PRODUCT_RDS, domain, APSARA_API_VERION_RDS, apiName, params, self.client.debug)
- }
- func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
- client, err := self.getSdkClient()
- if err != nil {
- return nil, err
- }
- domain := self.client.getDomain(APSARA_PRODUCT_VPC)
- return self.productRequest(client, APSARA_PRODUCT_VPC, domain, APSARA_API_VERSION_VPC, action, params, self.client.debug)
- }
- func (self *SRegion) kvsRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
- client, err := self.getSdkClient()
- if err != nil {
- return nil, err
- }
- domain := self.client.getDomain(APSARA_PRODUCT_KVSTORE)
- return self.productRequest(client, APSARA_PRODUCT_KVSTORE, domain, APSARA_API_VERSION_KVS, action, params, self.client.debug)
- }
- func (self *SRegion) tagRequest(serviceType string, action string, params map[string]string) (jsonutils.JSONObject, error) {
- switch serviceType {
- case APSARA_PRODUCT_ECS:
- return self.ecsRequest(action, params)
- case APSARA_PRODUCT_RDS:
- return self.rdsRequest(action, params)
- case APSARA_PRODUCT_SLB:
- return self.lbRequest(action, params)
- case APSARA_PRODUCT_KVSTORE:
- return self.kvsRequest(action, params)
- default:
- return nil, errors.Wrapf(errors.ErrNotSupported, "not support %s service tag", serviceType)
- }
- }
- func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
- client, err := self.getSdkClient()
- if err != nil {
- return nil, err
- }
- domain := self.client.getDomain(APSARA_PRODUCT_SLB)
- return self.productRequest(client, APSARA_PRODUCT_SLB, domain, APSARA_API_VERSION_LB, apiName, params, self.client.debug)
- }
- // ///////////////////////////////////////////////////////////////////////////
- func (self *SRegion) GetId() string {
- return self.RegionId
- }
- func (self *SRegion) GetName() string {
- switch self.client.cpcfg.Vendor {
- case api.CLOUD_PROVIDER_APSARA:
- return fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_CN, self.LocalName)
- default:
- return fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_CN, self.LocalName)
- }
- }
- func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
- en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_EN, self.LocalName)
- table := cloudprovider.SModelI18nTable{}
- table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
- return table
- }
- func (self *SRegion) GetGlobalId() string {
- return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_APSARA, self.RegionId)
- }
- func (self *SRegion) IsEmulated() bool {
- return false
- }
- func (self *SRegion) GetProvider() string {
- return self.client.cpcfg.Vendor
- }
- func (self *SRegion) GetCloudEnv() string {
- return ""
- }
- func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
- return cloudprovider.SGeographicInfo{}
- }
- func (self *SRegion) GetStatus() string {
- return api.CLOUD_REGION_STATUS_INSERVER
- }
- func (self *SRegion) Refresh() error {
- // do nothing
- return nil
- }
- func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
- if self.izones == nil {
- var err error
- err = self.fetchInfrastructure()
- if err != nil {
- return nil, err
- }
- }
- return self.izones, nil
- }
- func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- if izones[i].GetGlobalId() == id {
- return izones[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) getStoragecache() *SStoragecache {
- if self.storageCache == nil {
- self.storageCache = &SStoragecache{region: self}
- }
- return self.storageCache
- }
- func (self *SRegion) _fetchZones(chargeType TChargeType, spotStrategy SpotStrategyType) error {
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- if len(chargeType) > 0 {
- params["InstanceChargeType"] = string(chargeType)
- }
- if len(spotStrategy) > 0 {
- params["SpotStrategy"] = string(spotStrategy)
- }
- body, err := self.ecsRequest("DescribeZones", params)
- if err != nil {
- return err
- }
- zones := make([]SZone, 0)
- err = body.Unmarshal(&zones, "Zones", "Zone")
- if err != nil {
- return err
- }
- self.izones = make([]cloudprovider.ICloudZone, len(zones))
- for i := 0; i < len(zones); i += 1 {
- zones[i].region = self
- self.izones[i] = &zones[i]
- }
- return nil
- }
- func (self *SRegion) getZoneById(id string) (*SZone, error) {
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- zone := izones[i].(*SZone)
- if zone.ZoneId == id {
- return zone, nil
- }
- }
- return nil, fmt.Errorf("no such zone %s", id)
- }
- func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
- return self.GetInstance(id)
- }
- func (self *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
- return self.getDisk(id)
- }
- func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
- if self.ivpcs == nil {
- err := self.fetchInfrastructure()
- if err != nil {
- return nil, err
- }
- }
- return self.ivpcs, nil
- }
- func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
- ivpcs, err := self.GetIVpcs()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(ivpcs); i += 1 {
- if ivpcs[i].GetGlobalId() == id {
- return ivpcs[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) fetchIVpcs() error {
- vpcs := make([]SVpc, 0)
- for {
- part, total, err := self.GetVpcs(nil, len(vpcs), 50)
- if err != nil {
- return err
- }
- vpcs = append(vpcs, part...)
- if len(vpcs) >= total || len(part) == 0 {
- break
- }
- }
- self.ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs))
- for i := 0; i < len(vpcs); i += 1 {
- vpcs[i].region = self
- self.ivpcs[i] = &vpcs[i]
- }
- return nil
- }
- func (self *SRegion) fetchInfrastructure() error {
- err := self._fetchZones(PostPaidInstanceChargeType, NoSpotStrategy)
- if err != nil {
- return err
- }
- err = self.fetchIVpcs()
- if err != nil {
- return err
- }
- for i := 0; i < len(self.ivpcs); i += 1 {
- for j := 0; j < len(self.izones); j += 1 {
- zone := self.izones[j].(*SZone)
- vpc := self.ivpcs[i].(*SVpc)
- wire := SWire{zone: zone, vpc: vpc}
- zone.addWire(&wire)
- vpc.addWire(&wire)
- }
- }
- return nil
- }
- func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int, error) {
- if limit > 50 || limit <= 0 {
- limit = 50
- }
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["PageSize"] = fmt.Sprintf("%d", limit)
- params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
- if vpcId != nil && len(vpcId) > 0 {
- params["VpcId"] = strings.Join(vpcId, ",")
- }
- body, err := self.ecsRequest("DescribeVpcs", params)
- if err != nil {
- log.Errorf("GetVpcs fail %s", err)
- return nil, 0, err
- }
- vpcs := make([]SVpc, 0)
- err = body.Unmarshal(&vpcs, "Vpcs", "Vpc")
- if err != nil {
- log.Errorf("Unmarshal vpc fail %s", err)
- return nil, 0, err
- }
- total, _ := body.Int("TotalCount")
- return vpcs, int(total), nil
- }
- func (self *SRegion) getVpc(vpcId string) (*SVpc, error) {
- vpcs, total, err := self.GetVpcs([]string{vpcId}, 0, 1)
- if err != nil {
- return nil, err
- }
- if total != 1 {
- return nil, cloudprovider.ErrNotFound
- }
- vpcs[0].region = self
- return &vpcs[0], nil
- }
- func (self *SRegion) GetVRouters(offset int, limit int) ([]SVRouter, int, error) {
- if limit > 50 || limit <= 0 {
- limit = 50
- }
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["PageSize"] = fmt.Sprintf("%d", limit)
- params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
- body, err := self.ecsRequest("DescribeVRouters", params)
- if err != nil {
- log.Errorf("GetVRouters fail %s", err)
- return nil, 0, err
- }
- vrouters := make([]SVRouter, 0)
- err = body.Unmarshal(&vrouters, "VRouters", "VRouter")
- if err != nil {
- log.Errorf("Unmarshal vrouter fail %s", err)
- return nil, 0, err
- }
- total, _ := body.Int("TotalCount")
- return vrouters, int(total), nil
- }
- func (self *SRegion) GetRouteTables(ids []string, offset int, limit int) ([]SRouteTable, int, error) {
- if limit > 50 || limit <= 0 {
- limit = 50
- }
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["PageSize"] = fmt.Sprintf("%d", limit)
- params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
- if ids != nil && len(ids) > 0 {
- params["RouteTableId"] = strings.Join(ids, ",")
- }
- body, err := self.ecsRequest("DescribeRouteTables", params)
- if err != nil {
- log.Errorf("GetRouteTables fail %s", err)
- return nil, 0, err
- }
- routetables := make([]SRouteTable, 0)
- err = body.Unmarshal(&routetables, "RouteTables", "RouteTable")
- if err != nil {
- log.Errorf("Unmarshal routetables fail %s", err)
- return nil, 0, err
- }
- total, _ := body.Int("TotalCount")
- return routetables, int(total), nil
- }
- func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, gpu int, zoneId string) ([]SInstanceType, error) {
- if self.instanceTypes == nil {
- types, err := self.GetInstanceTypes()
- if err != nil {
- log.Errorf("GetInstanceTypes %s", err)
- return nil, err
- }
- self.instanceTypes = types
- }
- var available []string
- if len(zoneId) > 0 {
- zone, err := self.getZoneById(zoneId)
- if err != nil {
- return nil, err
- }
- available = zone.AvailableInstanceTypes.InstanceTypes
- }
- ret := make([]SInstanceType, 0)
- for _, t := range self.instanceTypes {
- if t.CpuCoreCount == cpu && memMB == t.memoryMB() && fmt.Sprintf("%d", gpu) == t.GPUAmount {
- if available == nil || utils.IsInStringArray(t.InstanceTypeId, available) {
- ret = append(ret, t)
- }
- }
- }
- return ret, nil
- }
- func (self *SRegion) CreateInstanceSimple(name string, imgId string, cpu int, memGB int, storageType string, dataDiskSizesGB []int, vswitchId string, passwd string, publicKey string) (*SInstance, error) {
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- z := izones[i].(*SZone)
- log.Debugf("Search in zone %s", z.LocalName)
- net := z.getNetworkById(vswitchId)
- if net != nil {
- desc := &cloudprovider.SManagedVMCreateConfig{
- Name: name,
- ExternalImageId: imgId,
- SysDisk: cloudprovider.SDiskInfo{SizeGB: 0, StorageType: storageType},
- Cpu: cpu,
- MemoryMB: memGB * 1024,
- ExternalNetworkId: vswitchId,
- Password: passwd,
- DataDisks: []cloudprovider.SDiskInfo{},
- PublicKey: publicKey,
- }
- for _, sizeGB := range dataDiskSizesGB {
- desc.DataDisks = append(desc.DataDisks, cloudprovider.SDiskInfo{SizeGB: sizeGB, StorageType: storageType})
- }
- inst, err := z.getHost().CreateVM(desc)
- if err != nil {
- return nil, err
- }
- return inst.(*SInstance), nil
- }
- }
- return nil, fmt.Errorf("cannot find vswitch %s", vswitchId)
- }
- func (self *SRegion) instanceOperation(instanceId string, opname string, extra map[string]string) error {
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["InstanceId"] = instanceId
- if extra != nil && len(extra) > 0 {
- for k, v := range extra {
- params[k] = v
- }
- }
- _, err := self.ecsRequest(opname, params)
- return err
- }
- func (self *SRegion) GetInstanceStatus(instanceId string) (string, error) {
- instance, err := self.GetInstance(instanceId)
- if err != nil {
- return "", err
- }
- return instance.Status, nil
- }
- func (self *SRegion) GetInstanceVNCUrl(instanceId string) (string, error) {
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["InstanceId"] = instanceId
- body, err := self.ecsRequest("DescribeInstanceVncUrl", params)
- if err != nil {
- return "", err
- }
- return body.GetString("VncUrl")
- }
- func (self *SRegion) ModifyInstanceVNCUrlPassword(instanceId string, passwd string) error {
- params := make(map[string]string)
- params["RegionId"] = self.RegionId
- params["InstanceId"] = instanceId
- params["VncPassword"] = passwd // must be 6 digital + alphabet
- _, err := self.ecsRequest("ModifyInstanceVncPasswd", params)
- return err
- }
- func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
- params := make(map[string]string)
- if len(opts.CIDR) > 0 {
- params["CidrBlock"] = opts.CIDR
- }
- if len(opts.NAME) > 0 {
- params["VpcName"] = opts.NAME
- }
- if len(opts.Desc) > 0 {
- params["Description"] = opts.Desc
- }
- params["ClientToken"] = utils.GenRequestId(20)
- body, err := self.ecsRequest("CreateVpc", params)
- if err != nil {
- return nil, err
- }
- vpcId, err := body.GetString("VpcId")
- if err != nil {
- return nil, err
- }
- err = self.fetchInfrastructure()
- if err != nil {
- return nil, err
- }
- return self.GetIVpcById(vpcId)
- }
- func (self *SRegion) DeleteVpc(vpcId string) error {
- params := make(map[string]string)
- params["VpcId"] = vpcId
- _, err := self.ecsRequest("DeleteVpc", params)
- return err
- }
- func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- ihost, err := izones[i].GetIHostById(id)
- if err == nil {
- return ihost, nil
- } else if err != cloudprovider.ErrNotFound {
- return nil, err
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- istore, err := izones[i].GetIStorageById(id)
- if err == nil {
- return istore, nil
- } else if err != cloudprovider.ErrNotFound {
- return nil, err
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
- iHosts := make([]cloudprovider.ICloudHost, 0)
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- iZoneHost, err := izones[i].GetIHosts()
- if err != nil {
- return nil, err
- }
- iHosts = append(iHosts, iZoneHost...)
- }
- return iHosts, nil
- }
- func (self *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
- iStores := make([]cloudprovider.ICloudStorage, 0)
- izones, err := self.GetIZones()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(izones); i += 1 {
- iZoneStores, err := izones[i].GetIStorages()
- if err != nil {
- return nil, err
- }
- iStores = append(iStores, iZoneStores...)
- }
- return iStores, nil
- }
- func (self *SRegion) updateInstance(instId string, name, desc, passwd, hostname, userData string) error {
- params := make(map[string]string)
- params["InstanceId"] = instId
- if len(name) > 0 {
- params["InstanceName"] = name
- }
- if len(desc) > 0 {
- params["Description"] = desc
- }
- if len(passwd) > 0 {
- params["Password"] = passwd
- }
- if len(hostname) > 0 {
- params["HostName"] = hostname
- }
- if len(userData) > 0 {
- params["UserData"] = userData
- }
- _, err := self.ecsRequest("ModifyInstanceAttribute", params)
- return err
- }
- func (self *SRegion) UpdateInstancePassword(instId string, passwd string) error {
- return self.updateInstance(instId, "", "", passwd, "", "")
- }
- // func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
- // eips, total, err := self.GetSnapshots("", 0, 50)
- // if err != nil {
- // return nil, err
- // }
- // for len(eips) < total {
- // var parts []SEipAddress
- // parts, total, err = self.GetEips("", len(eips), 50)
- // if err != nil {
- // return nil, err
- // }
- // eips = append(eips, parts...)
- // }
- // ret := make([]cloudprovider.ICloudEIP, len(eips))
- // for i := 0; i < len(eips); i += 1 {
- // ret[i] = &eips[i]
- // }
- // return ret, nil
- // }
- func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
- eips, total, err := self.GetEips("", "", 0, 50)
- if err != nil {
- return nil, err
- }
- for len(eips) < total {
- var parts []SEipAddress
- parts, total, err = self.GetEips("", "", len(eips), 50)
- if err != nil {
- return nil, err
- }
- eips = append(eips, parts...)
- if len(parts) == 0 {
- break
- }
- }
- ret := make([]cloudprovider.ICloudEIP, len(eips))
- for i := 0; i < len(eips); i += 1 {
- ret[i] = &eips[i]
- }
- return ret, nil
- }
- func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
- eips, total, err := self.GetEips(eipId, "", 0, 1)
- if err != nil {
- return nil, err
- }
- if total == 0 {
- return nil, cloudprovider.ErrNotFound
- }
- if total > 1 {
- return nil, cloudprovider.ErrDuplicateId
- }
- return &eips[0], nil
- }
- func (region *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
- secgroup, err := region.GetSecurityGroup(secgroupId)
- if err != nil {
- return nil, err
- }
- secgroup.region = region
- return secgroup, nil
- }
- func (region *SRegion) CreateISecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
- externalId, err := region.CreateSecurityGroup(opts.VpcId, opts.Name, opts.Desc, opts.ProjectId)
- if err != nil {
- return nil, err
- }
- return region.GetISecurityGroupById(externalId)
- }
- func (region *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
- lbs, err := region.GetLoadbalancers(nil)
- if err != nil {
- return nil, err
- }
- ilbs := []cloudprovider.ICloudLoadbalancer{}
- for i := 0; i < len(lbs); i++ {
- lbs[i].region = region
- ilbs = append(ilbs, &lbs[i])
- }
- return ilbs, nil
- }
- func (region *SRegion) GetILoadBalancerById(loadbalancerId string) (cloudprovider.ICloudLoadbalancer, error) {
- return region.GetLoadbalancerDetail(loadbalancerId)
- }
- func (region *SRegion) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) {
- certs, err := region.GetLoadbalancerServerCertificates()
- if err != nil {
- return nil, err
- }
- for i := 0; i < len(certs); i++ {
- if certs[i].GetGlobalId() == certId {
- certs[i].region = region
- return &certs[i], nil
- }
- }
- return nil, cloudprovider.ErrNotFound
- }
- func (region *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) {
- params := map[string]string{}
- params["RegionId"] = region.RegionId
- params["ServerCertificateName"] = cert.Name
- params["PrivateKey"] = cert.PrivateKey
- params["ServerCertificate"] = cert.Certificate
- body, err := region.lbRequest("UploadServerCertificate", params)
- if err != nil {
- return nil, err
- }
- certID, err := body.GetString("ServerCertificateId")
- if err != nil {
- return nil, err
- }
- return region.GetILoadBalancerCertificateById(certID)
- }
- func (region *SRegion) GetILoadBalancerAclById(aclId string) (cloudprovider.ICloudLoadbalancerAcl, error) {
- return nil, cloudprovider.ErrNotFound
- }
- func (region *SRegion) GetILoadBalancerAcls() ([]cloudprovider.ICloudLoadbalancerAcl, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (region *SRegion) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadbalancerCertificate, error) {
- certificates, err := region.GetLoadbalancerServerCertificates()
- if err != nil {
- return nil, err
- }
- iCertificates := []cloudprovider.ICloudLoadbalancerCertificate{}
- for i := 0; i < len(certificates); i++ {
- certificates[i].region = region
- iCertificates = append(iCertificates, &certificates[i])
- }
- return iCertificates, nil
- }
- func (region *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalancerCreateOptions) (cloudprovider.ICloudLoadbalancer, error) {
- params := map[string]string{}
- params["RegionId"] = region.RegionId
- params["LoadBalancerName"] = loadbalancer.Name
- if len(loadbalancer.ZoneId) > 0 {
- params["MasterZoneId"] = loadbalancer.ZoneId
- }
- if len(loadbalancer.VpcId) > 0 {
- params["VpcId"] = loadbalancer.VpcId
- }
- if len(loadbalancer.NetworkIds) > 0 {
- params["VSwitchId"] = loadbalancer.NetworkIds[0]
- }
- if len(loadbalancer.Address) > 0 {
- params["Address"] = loadbalancer.Address
- }
- if len(loadbalancer.AddressType) > 0 {
- params["AddressType"] = loadbalancer.AddressType
- }
- if len(loadbalancer.LoadbalancerSpec) > 0 {
- params["LoadBalancerSpec"] = loadbalancer.LoadbalancerSpec
- }
- if len(loadbalancer.ChargeType) > 0 {
- params["InternetChargeType"] = "payby" + loadbalancer.ChargeType
- }
- if len(loadbalancer.ProjectId) > 0 {
- params["ResourceGroupId"] = loadbalancer.ProjectId
- }
- if loadbalancer.ChargeType == api.LB_CHARGE_TYPE_BY_BANDWIDTH && loadbalancer.EgressMbps > 0 {
- params["Bandwidth"] = fmt.Sprintf("%d", loadbalancer.EgressMbps)
- }
- body, err := region.lbRequest("CreateLoadBalancer", params)
- if err != nil {
- return nil, err
- }
- loadBalancerID, err := body.GetString("LoadBalancerId")
- if err != nil {
- return nil, err
- }
- region.SetResourceTags("slb", "instance", []string{loadBalancerID}, loadbalancer.Tags, false)
- iLoadbalancer, err := region.GetLoadbalancerDetail(loadBalancerID)
- if err != nil {
- return nil, err
- }
- return iLoadbalancer, cloudprovider.WaitStatus(iLoadbalancer, api.LB_STATUS_ENABLED, time.Second*5, time.Minute*5)
- }
- func (region *SRegion) AddAccessControlListEntry(aclId string, entrys []cloudprovider.SLoadbalancerAccessControlListEntry) error {
- params := map[string]string{}
- params["RegionId"] = region.RegionId
- params["AclId"] = aclId
- aclArray := jsonutils.NewArray()
- for i := 0; i < len(entrys); i++ {
- //阿里云AclEntrys参数必须是CIDR格式的。
- if regutils.MatchIPAddr(entrys[i].CIDR) {
- entrys[i].CIDR += "/32"
- }
- aclArray.Add(jsonutils.Marshal(map[string]string{"entry": entrys[i].CIDR, "comment": entrys[i].Comment}))
- }
- if aclArray.Length() == 0 {
- return nil
- }
- params["AclEntrys"] = aclArray.String()
- _, err := region.lbRequest("AddAccessControlListEntry", params)
- return err
- }
- func (region *SRegion) CreateILoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (cloudprovider.ICloudLoadbalancerAcl, error) {
- params := map[string]string{}
- params["RegionId"] = region.RegionId
- params["AclName"] = acl.Name
- body, err := region.lbRequest("CreateAccessControlList", params)
- if err != nil {
- return nil, err
- }
- aclId, err := body.GetString("AclId")
- if err != nil {
- return nil, err
- }
- iAcl, err := region.GetLoadbalancerAclDetail(aclId)
- if err != nil {
- return nil, err
- }
- return iAcl, region.AddAccessControlListEntry(aclId, acl.Entrys)
- }
- func (self *SRegion) GetBuckets() ([]SBucket, error) {
- params := map[string]string{
- "AccountInfo": self.client.getAccountInfo(),
- }
- resp, err := self.ossRequest("GetService", params)
- if err != nil {
- return nil, err
- }
- if !resp.Contains("Data") {
- if self.client.cpcfg.UpdatePermission != nil {
- self.client.cpcfg.UpdatePermission("oss", "ListBuckets")
- }
- return []SBucket{}, nil
- }
- ret := []SBucket{}
- err = resp.Unmarshal(&ret, "Data", "ListAllMyBucketsResult", "Buckets", "Bucket")
- if err != nil {
- return nil, errors.Wrapf(err, "resp.Unmarshal")
- }
- if len(ret) > 0 {
- self.ossEndpoint = ret[0].IntranetEndpoint
- }
- return ret, nil
- }
- func (self *SRegion) GetIBuckets() ([]cloudprovider.ICloudBucket, error) {
- buckets, err := self.GetBuckets()
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudBucket{}
- for i := range buckets {
- buckets[i].region = self
- ret = append(ret, &buckets[i])
- }
- return ret, nil
- }
- func str2StorageClass(storageClassStr string) (oss.StorageClassType, error) {
- storageClass := oss.StorageStandard
- if strings.EqualFold(storageClassStr, string(oss.StorageStandard)) {
- //
- } else if strings.EqualFold(storageClassStr, string(oss.StorageIA)) {
- storageClass = oss.StorageIA
- } else if strings.EqualFold(storageClassStr, string(oss.StorageArchive)) {
- storageClass = oss.StorageArchive
- } else {
- return storageClass, errors.Error("not supported storageClass")
- }
- return storageClass, nil
- }
- func str2Acl(aclStr string) (oss.ACLType, error) {
- acl := oss.ACLPrivate
- if strings.EqualFold(aclStr, string(oss.ACLPrivate)) {
- // private, default
- } else if strings.EqualFold(aclStr, string(oss.ACLPublicRead)) {
- acl = oss.ACLPublicRead
- } else if strings.EqualFold(aclStr, string(oss.ACLPublicReadWrite)) {
- acl = oss.ACLPublicReadWrite
- } else {
- return acl, errors.Error("not supported acl")
- }
- return acl, nil
- }
- func (self *SRegion) CreateIBucket(name string, storageClassStr string, aclStr string) error {
- osscli, err := self.GetOssClient()
- if err != nil {
- return errors.Wrap(err, "region.GetOssClient")
- }
- opts := make([]oss.Option, 0)
- if len(storageClassStr) > 0 {
- storageClass, err := str2StorageClass(storageClassStr)
- if err != nil {
- return err
- }
- opts = append(opts, oss.StorageClass(storageClass))
- }
- if len(aclStr) > 0 {
- acl, err := str2Acl(aclStr)
- if err != nil {
- return err
- }
- opts = append(opts, oss.ACL(acl))
- }
- return osscli.CreateBucket(name, opts...)
- }
- func ossErrorCode(err error) int {
- if srvErr, ok := err.(oss.ServiceError); ok {
- return srvErr.StatusCode
- }
- if srvErr, ok := err.(*oss.ServiceError); ok {
- return srvErr.StatusCode
- }
- return -1
- }
- func (self *SRegion) DeleteIBucket(name string) error {
- cli, err := self.GetOssClient()
- if err != nil {
- return err
- }
- return cli.DeleteBucket(name)
- }
- func (self *SRegion) GetBucket(name string) (*SBucket, error) {
- params := map[string]string{
- "AccountInfo": self.client.getAccountInfo(),
- "x-acs-instanceid": name,
- "Params": jsonutils.Marshal(map[string]string{"BucketName": name}).String(),
- }
- resp, err := self.ossRequest("GetBucketInfo", params)
- if err != nil {
- return nil, errors.Wrapf(err, "GetBucketInfo")
- }
- bucket := &SBucket{region: self}
- return bucket, resp.Unmarshal(bucket, "Data", "BucketInfo", "Bucket")
- }
- func (self *SRegion) IBucketExist(name string) (bool, error) {
- _, err := self.GetBucket(name)
- if err != nil {
- return false, err
- }
- return true, nil
- }
- func (self *SRegion) GetIBucketById(name string) (cloudprovider.ICloudBucket, error) {
- buckets, err := self.GetBuckets()
- if err != nil {
- return nil, err
- }
- for i := range buckets {
- if buckets[i].GetName() == name {
- buckets[i].region = self
- return &buckets[i], nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", name)
- }
- func (region *SRegion) GetIBucketByName(name string) (cloudprovider.ICloudBucket, error) {
- return region.GetIBucketById(name)
- }
- func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) {
- caches, err := self.GetElasticCaches(nil)
- if err != nil {
- return nil, err
- }
- icaches := make([]cloudprovider.ICloudElasticcache, len(caches))
- for i := range caches {
- caches[i].region = self
- icaches[i] = &caches[i]
- }
- return icaches, nil
- }
- func (region *SRegion) GetCapabilities() []string {
- return region.client.GetCapabilities()
- }
- func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
- vms := make([]SInstance, 0)
- for {
- parts, total, err := region.GetInstances("", nil, len(vms), 50)
- if err != nil {
- return nil, err
- }
- vms = append(vms, parts...)
- if len(vms) >= total || len(parts) == 0 {
- break
- }
- }
- ivms := make([]cloudprovider.ICloudVM, len(vms))
- for i := 0; i < len(vms); i += 1 {
- ivms[i] = &vms[i]
- }
- return ivms, nil
- }
|