region.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. package apsara
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  20. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/pkg/util/regutils"
  25. "yunion.io/x/pkg/utils"
  26. api "yunion.io/x/cloudmux/pkg/apis/compute"
  27. "yunion.io/x/cloudmux/pkg/cloudprovider"
  28. "yunion.io/x/cloudmux/pkg/multicloud"
  29. )
  30. type SRegion struct {
  31. multicloud.SRegion
  32. client *SApsaraClient
  33. sdkClient *sdk.Client
  34. RegionId string
  35. LocalName string
  36. izones []cloudprovider.ICloudZone
  37. ivpcs []cloudprovider.ICloudVpc
  38. lbEndpints map[string]string
  39. storageCache *SStoragecache
  40. instanceTypes []SInstanceType
  41. latitude float64
  42. longitude float64
  43. fetchLocation bool
  44. ossEndpoint string
  45. }
  46. func (self *SRegion) GetClient() *SApsaraClient {
  47. return self.client
  48. }
  49. func (self *SRegion) getSdkClient() (*sdk.Client, error) {
  50. if self.sdkClient == nil {
  51. var err error
  52. self.sdkClient, err = self.GetClient().getDefaultClient(self.RegionId)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. return self.sdkClient, nil
  58. }
  59. func (self *SRegion) GetOssClient() (*oss.Client, error) {
  60. if len(self.ossEndpoint) == 0 {
  61. _, err := self.GetBuckets()
  62. if err != nil {
  63. return nil, err
  64. }
  65. if len(self.ossEndpoint) == 0 {
  66. return nil, fmt.Errorf("no available buckets")
  67. }
  68. }
  69. return self.client.getOssClient(self.ossEndpoint)
  70. }
  71. func (self *SRegion) productRequest(client *sdk.Client, product, domain, apiVersion, apiName string, params map[string]string, debug bool) (jsonutils.JSONObject, error) {
  72. params["Product"] = product
  73. return jsonRequest(client, domain, apiVersion, apiName, params, debug)
  74. }
  75. func (self *SRegion) ossRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  76. params["RegionId"] = self.RegionId
  77. return self.client.ossRequest(apiName, params)
  78. }
  79. func (self *SRegion) ecsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  80. client, err := self.getSdkClient()
  81. if err != nil {
  82. return nil, err
  83. }
  84. domain := self.client.getDomain(APSARA_PRODUCT_ECS)
  85. params["Product"] = APSARA_PRODUCT_ECS
  86. return jsonRequest(client, domain, APSARA_API_VERSION, apiName, params, self.client.debug)
  87. }
  88. func (self *SRegion) rdsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  89. client, err := self.getSdkClient()
  90. if err != nil {
  91. return nil, err
  92. }
  93. domain := self.client.getDomain(APSARA_PRODUCT_RDS)
  94. return self.productRequest(client, APSARA_PRODUCT_RDS, domain, APSARA_API_VERION_RDS, apiName, params, self.client.debug)
  95. }
  96. func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
  97. client, err := self.getSdkClient()
  98. if err != nil {
  99. return nil, err
  100. }
  101. domain := self.client.getDomain(APSARA_PRODUCT_VPC)
  102. return self.productRequest(client, APSARA_PRODUCT_VPC, domain, APSARA_API_VERSION_VPC, action, params, self.client.debug)
  103. }
  104. func (self *SRegion) kvsRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
  105. client, err := self.getSdkClient()
  106. if err != nil {
  107. return nil, err
  108. }
  109. domain := self.client.getDomain(APSARA_PRODUCT_KVSTORE)
  110. return self.productRequest(client, APSARA_PRODUCT_KVSTORE, domain, APSARA_API_VERSION_KVS, action, params, self.client.debug)
  111. }
  112. func (self *SRegion) tagRequest(serviceType string, action string, params map[string]string) (jsonutils.JSONObject, error) {
  113. switch serviceType {
  114. case APSARA_PRODUCT_ECS:
  115. return self.ecsRequest(action, params)
  116. case APSARA_PRODUCT_RDS:
  117. return self.rdsRequest(action, params)
  118. case APSARA_PRODUCT_SLB:
  119. return self.lbRequest(action, params)
  120. case APSARA_PRODUCT_KVSTORE:
  121. return self.kvsRequest(action, params)
  122. default:
  123. return nil, errors.Wrapf(errors.ErrNotSupported, "not support %s service tag", serviceType)
  124. }
  125. }
  126. func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  127. client, err := self.getSdkClient()
  128. if err != nil {
  129. return nil, err
  130. }
  131. domain := self.client.getDomain(APSARA_PRODUCT_SLB)
  132. return self.productRequest(client, APSARA_PRODUCT_SLB, domain, APSARA_API_VERSION_LB, apiName, params, self.client.debug)
  133. }
  134. // ///////////////////////////////////////////////////////////////////////////
  135. func (self *SRegion) GetId() string {
  136. return self.RegionId
  137. }
  138. func (self *SRegion) GetName() string {
  139. switch self.client.cpcfg.Vendor {
  140. case api.CLOUD_PROVIDER_APSARA:
  141. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_CN, self.LocalName)
  142. default:
  143. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_CN, self.LocalName)
  144. }
  145. }
  146. func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  147. en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_APSARA_EN, self.LocalName)
  148. table := cloudprovider.SModelI18nTable{}
  149. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  150. return table
  151. }
  152. func (self *SRegion) GetGlobalId() string {
  153. return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_APSARA, self.RegionId)
  154. }
  155. func (self *SRegion) IsEmulated() bool {
  156. return false
  157. }
  158. func (self *SRegion) GetProvider() string {
  159. return self.client.cpcfg.Vendor
  160. }
  161. func (self *SRegion) GetCloudEnv() string {
  162. return ""
  163. }
  164. func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  165. return cloudprovider.SGeographicInfo{}
  166. }
  167. func (self *SRegion) GetStatus() string {
  168. return api.CLOUD_REGION_STATUS_INSERVER
  169. }
  170. func (self *SRegion) Refresh() error {
  171. // do nothing
  172. return nil
  173. }
  174. func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
  175. if self.izones == nil {
  176. var err error
  177. err = self.fetchInfrastructure()
  178. if err != nil {
  179. return nil, err
  180. }
  181. }
  182. return self.izones, nil
  183. }
  184. func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
  185. izones, err := self.GetIZones()
  186. if err != nil {
  187. return nil, err
  188. }
  189. for i := 0; i < len(izones); i += 1 {
  190. if izones[i].GetGlobalId() == id {
  191. return izones[i], nil
  192. }
  193. }
  194. return nil, cloudprovider.ErrNotFound
  195. }
  196. func (self *SRegion) getStoragecache() *SStoragecache {
  197. if self.storageCache == nil {
  198. self.storageCache = &SStoragecache{region: self}
  199. }
  200. return self.storageCache
  201. }
  202. func (self *SRegion) _fetchZones(chargeType TChargeType, spotStrategy SpotStrategyType) error {
  203. params := make(map[string]string)
  204. params["RegionId"] = self.RegionId
  205. if len(chargeType) > 0 {
  206. params["InstanceChargeType"] = string(chargeType)
  207. }
  208. if len(spotStrategy) > 0 {
  209. params["SpotStrategy"] = string(spotStrategy)
  210. }
  211. body, err := self.ecsRequest("DescribeZones", params)
  212. if err != nil {
  213. return err
  214. }
  215. zones := make([]SZone, 0)
  216. err = body.Unmarshal(&zones, "Zones", "Zone")
  217. if err != nil {
  218. return err
  219. }
  220. self.izones = make([]cloudprovider.ICloudZone, len(zones))
  221. for i := 0; i < len(zones); i += 1 {
  222. zones[i].region = self
  223. self.izones[i] = &zones[i]
  224. }
  225. return nil
  226. }
  227. func (self *SRegion) getZoneById(id string) (*SZone, error) {
  228. izones, err := self.GetIZones()
  229. if err != nil {
  230. return nil, err
  231. }
  232. for i := 0; i < len(izones); i += 1 {
  233. zone := izones[i].(*SZone)
  234. if zone.ZoneId == id {
  235. return zone, nil
  236. }
  237. }
  238. return nil, fmt.Errorf("no such zone %s", id)
  239. }
  240. func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  241. return self.GetInstance(id)
  242. }
  243. func (self *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
  244. return self.getDisk(id)
  245. }
  246. func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  247. if self.ivpcs == nil {
  248. err := self.fetchInfrastructure()
  249. if err != nil {
  250. return nil, err
  251. }
  252. }
  253. return self.ivpcs, nil
  254. }
  255. func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  256. ivpcs, err := self.GetIVpcs()
  257. if err != nil {
  258. return nil, err
  259. }
  260. for i := 0; i < len(ivpcs); i += 1 {
  261. if ivpcs[i].GetGlobalId() == id {
  262. return ivpcs[i], nil
  263. }
  264. }
  265. return nil, cloudprovider.ErrNotFound
  266. }
  267. func (self *SRegion) fetchIVpcs() error {
  268. vpcs := make([]SVpc, 0)
  269. for {
  270. part, total, err := self.GetVpcs(nil, len(vpcs), 50)
  271. if err != nil {
  272. return err
  273. }
  274. vpcs = append(vpcs, part...)
  275. if len(vpcs) >= total || len(part) == 0 {
  276. break
  277. }
  278. }
  279. self.ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs))
  280. for i := 0; i < len(vpcs); i += 1 {
  281. vpcs[i].region = self
  282. self.ivpcs[i] = &vpcs[i]
  283. }
  284. return nil
  285. }
  286. func (self *SRegion) fetchInfrastructure() error {
  287. err := self._fetchZones(PostPaidInstanceChargeType, NoSpotStrategy)
  288. if err != nil {
  289. return err
  290. }
  291. err = self.fetchIVpcs()
  292. if err != nil {
  293. return err
  294. }
  295. for i := 0; i < len(self.ivpcs); i += 1 {
  296. for j := 0; j < len(self.izones); j += 1 {
  297. zone := self.izones[j].(*SZone)
  298. vpc := self.ivpcs[i].(*SVpc)
  299. wire := SWire{zone: zone, vpc: vpc}
  300. zone.addWire(&wire)
  301. vpc.addWire(&wire)
  302. }
  303. }
  304. return nil
  305. }
  306. func (self *SRegion) GetVpcs(vpcId []string, offset int, limit int) ([]SVpc, int, error) {
  307. if limit > 50 || limit <= 0 {
  308. limit = 50
  309. }
  310. params := make(map[string]string)
  311. params["RegionId"] = self.RegionId
  312. params["PageSize"] = fmt.Sprintf("%d", limit)
  313. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  314. if vpcId != nil && len(vpcId) > 0 {
  315. params["VpcId"] = strings.Join(vpcId, ",")
  316. }
  317. body, err := self.ecsRequest("DescribeVpcs", params)
  318. if err != nil {
  319. log.Errorf("GetVpcs fail %s", err)
  320. return nil, 0, err
  321. }
  322. vpcs := make([]SVpc, 0)
  323. err = body.Unmarshal(&vpcs, "Vpcs", "Vpc")
  324. if err != nil {
  325. log.Errorf("Unmarshal vpc fail %s", err)
  326. return nil, 0, err
  327. }
  328. total, _ := body.Int("TotalCount")
  329. return vpcs, int(total), nil
  330. }
  331. func (self *SRegion) getVpc(vpcId string) (*SVpc, error) {
  332. vpcs, total, err := self.GetVpcs([]string{vpcId}, 0, 1)
  333. if err != nil {
  334. return nil, err
  335. }
  336. if total != 1 {
  337. return nil, cloudprovider.ErrNotFound
  338. }
  339. vpcs[0].region = self
  340. return &vpcs[0], nil
  341. }
  342. func (self *SRegion) GetVRouters(offset int, limit int) ([]SVRouter, int, error) {
  343. if limit > 50 || limit <= 0 {
  344. limit = 50
  345. }
  346. params := make(map[string]string)
  347. params["RegionId"] = self.RegionId
  348. params["PageSize"] = fmt.Sprintf("%d", limit)
  349. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  350. body, err := self.ecsRequest("DescribeVRouters", params)
  351. if err != nil {
  352. log.Errorf("GetVRouters fail %s", err)
  353. return nil, 0, err
  354. }
  355. vrouters := make([]SVRouter, 0)
  356. err = body.Unmarshal(&vrouters, "VRouters", "VRouter")
  357. if err != nil {
  358. log.Errorf("Unmarshal vrouter fail %s", err)
  359. return nil, 0, err
  360. }
  361. total, _ := body.Int("TotalCount")
  362. return vrouters, int(total), nil
  363. }
  364. func (self *SRegion) GetRouteTables(ids []string, offset int, limit int) ([]SRouteTable, int, error) {
  365. if limit > 50 || limit <= 0 {
  366. limit = 50
  367. }
  368. params := make(map[string]string)
  369. params["RegionId"] = self.RegionId
  370. params["PageSize"] = fmt.Sprintf("%d", limit)
  371. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  372. if ids != nil && len(ids) > 0 {
  373. params["RouteTableId"] = strings.Join(ids, ",")
  374. }
  375. body, err := self.ecsRequest("DescribeRouteTables", params)
  376. if err != nil {
  377. log.Errorf("GetRouteTables fail %s", err)
  378. return nil, 0, err
  379. }
  380. routetables := make([]SRouteTable, 0)
  381. err = body.Unmarshal(&routetables, "RouteTables", "RouteTable")
  382. if err != nil {
  383. log.Errorf("Unmarshal routetables fail %s", err)
  384. return nil, 0, err
  385. }
  386. total, _ := body.Int("TotalCount")
  387. return routetables, int(total), nil
  388. }
  389. func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, gpu int, zoneId string) ([]SInstanceType, error) {
  390. if self.instanceTypes == nil {
  391. types, err := self.GetInstanceTypes()
  392. if err != nil {
  393. log.Errorf("GetInstanceTypes %s", err)
  394. return nil, err
  395. }
  396. self.instanceTypes = types
  397. }
  398. var available []string
  399. if len(zoneId) > 0 {
  400. zone, err := self.getZoneById(zoneId)
  401. if err != nil {
  402. return nil, err
  403. }
  404. available = zone.AvailableInstanceTypes.InstanceTypes
  405. }
  406. ret := make([]SInstanceType, 0)
  407. for _, t := range self.instanceTypes {
  408. if t.CpuCoreCount == cpu && memMB == t.memoryMB() && fmt.Sprintf("%d", gpu) == t.GPUAmount {
  409. if available == nil || utils.IsInStringArray(t.InstanceTypeId, available) {
  410. ret = append(ret, t)
  411. }
  412. }
  413. }
  414. return ret, nil
  415. }
  416. func (self *SRegion) CreateInstanceSimple(name string, imgId string, cpu int, memGB int, storageType string, dataDiskSizesGB []int, vswitchId string, passwd string, publicKey string) (*SInstance, error) {
  417. izones, err := self.GetIZones()
  418. if err != nil {
  419. return nil, err
  420. }
  421. for i := 0; i < len(izones); i += 1 {
  422. z := izones[i].(*SZone)
  423. log.Debugf("Search in zone %s", z.LocalName)
  424. net := z.getNetworkById(vswitchId)
  425. if net != nil {
  426. desc := &cloudprovider.SManagedVMCreateConfig{
  427. Name: name,
  428. ExternalImageId: imgId,
  429. SysDisk: cloudprovider.SDiskInfo{SizeGB: 0, StorageType: storageType},
  430. Cpu: cpu,
  431. MemoryMB: memGB * 1024,
  432. ExternalNetworkId: vswitchId,
  433. Password: passwd,
  434. DataDisks: []cloudprovider.SDiskInfo{},
  435. PublicKey: publicKey,
  436. }
  437. for _, sizeGB := range dataDiskSizesGB {
  438. desc.DataDisks = append(desc.DataDisks, cloudprovider.SDiskInfo{SizeGB: sizeGB, StorageType: storageType})
  439. }
  440. inst, err := z.getHost().CreateVM(desc)
  441. if err != nil {
  442. return nil, err
  443. }
  444. return inst.(*SInstance), nil
  445. }
  446. }
  447. return nil, fmt.Errorf("cannot find vswitch %s", vswitchId)
  448. }
  449. func (self *SRegion) instanceOperation(instanceId string, opname string, extra map[string]string) error {
  450. params := make(map[string]string)
  451. params["RegionId"] = self.RegionId
  452. params["InstanceId"] = instanceId
  453. if extra != nil && len(extra) > 0 {
  454. for k, v := range extra {
  455. params[k] = v
  456. }
  457. }
  458. _, err := self.ecsRequest(opname, params)
  459. return err
  460. }
  461. func (self *SRegion) GetInstanceStatus(instanceId string) (string, error) {
  462. instance, err := self.GetInstance(instanceId)
  463. if err != nil {
  464. return "", err
  465. }
  466. return instance.Status, nil
  467. }
  468. func (self *SRegion) GetInstanceVNCUrl(instanceId string) (string, error) {
  469. params := make(map[string]string)
  470. params["RegionId"] = self.RegionId
  471. params["InstanceId"] = instanceId
  472. body, err := self.ecsRequest("DescribeInstanceVncUrl", params)
  473. if err != nil {
  474. return "", err
  475. }
  476. return body.GetString("VncUrl")
  477. }
  478. func (self *SRegion) ModifyInstanceVNCUrlPassword(instanceId string, passwd string) error {
  479. params := make(map[string]string)
  480. params["RegionId"] = self.RegionId
  481. params["InstanceId"] = instanceId
  482. params["VncPassword"] = passwd // must be 6 digital + alphabet
  483. _, err := self.ecsRequest("ModifyInstanceVncPasswd", params)
  484. return err
  485. }
  486. func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  487. params := make(map[string]string)
  488. if len(opts.CIDR) > 0 {
  489. params["CidrBlock"] = opts.CIDR
  490. }
  491. if len(opts.NAME) > 0 {
  492. params["VpcName"] = opts.NAME
  493. }
  494. if len(opts.Desc) > 0 {
  495. params["Description"] = opts.Desc
  496. }
  497. params["ClientToken"] = utils.GenRequestId(20)
  498. body, err := self.ecsRequest("CreateVpc", params)
  499. if err != nil {
  500. return nil, err
  501. }
  502. vpcId, err := body.GetString("VpcId")
  503. if err != nil {
  504. return nil, err
  505. }
  506. err = self.fetchInfrastructure()
  507. if err != nil {
  508. return nil, err
  509. }
  510. return self.GetIVpcById(vpcId)
  511. }
  512. func (self *SRegion) DeleteVpc(vpcId string) error {
  513. params := make(map[string]string)
  514. params["VpcId"] = vpcId
  515. _, err := self.ecsRequest("DeleteVpc", params)
  516. return err
  517. }
  518. func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  519. izones, err := self.GetIZones()
  520. if err != nil {
  521. return nil, err
  522. }
  523. for i := 0; i < len(izones); i += 1 {
  524. ihost, err := izones[i].GetIHostById(id)
  525. if err == nil {
  526. return ihost, nil
  527. } else if err != cloudprovider.ErrNotFound {
  528. return nil, err
  529. }
  530. }
  531. return nil, cloudprovider.ErrNotFound
  532. }
  533. func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  534. izones, err := self.GetIZones()
  535. if err != nil {
  536. return nil, err
  537. }
  538. for i := 0; i < len(izones); i += 1 {
  539. istore, err := izones[i].GetIStorageById(id)
  540. if err == nil {
  541. return istore, nil
  542. } else if err != cloudprovider.ErrNotFound {
  543. return nil, err
  544. }
  545. }
  546. return nil, cloudprovider.ErrNotFound
  547. }
  548. func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  549. iHosts := make([]cloudprovider.ICloudHost, 0)
  550. izones, err := self.GetIZones()
  551. if err != nil {
  552. return nil, err
  553. }
  554. for i := 0; i < len(izones); i += 1 {
  555. iZoneHost, err := izones[i].GetIHosts()
  556. if err != nil {
  557. return nil, err
  558. }
  559. iHosts = append(iHosts, iZoneHost...)
  560. }
  561. return iHosts, nil
  562. }
  563. func (self *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  564. iStores := make([]cloudprovider.ICloudStorage, 0)
  565. izones, err := self.GetIZones()
  566. if err != nil {
  567. return nil, err
  568. }
  569. for i := 0; i < len(izones); i += 1 {
  570. iZoneStores, err := izones[i].GetIStorages()
  571. if err != nil {
  572. return nil, err
  573. }
  574. iStores = append(iStores, iZoneStores...)
  575. }
  576. return iStores, nil
  577. }
  578. func (self *SRegion) updateInstance(instId string, name, desc, passwd, hostname, userData string) error {
  579. params := make(map[string]string)
  580. params["InstanceId"] = instId
  581. if len(name) > 0 {
  582. params["InstanceName"] = name
  583. }
  584. if len(desc) > 0 {
  585. params["Description"] = desc
  586. }
  587. if len(passwd) > 0 {
  588. params["Password"] = passwd
  589. }
  590. if len(hostname) > 0 {
  591. params["HostName"] = hostname
  592. }
  593. if len(userData) > 0 {
  594. params["UserData"] = userData
  595. }
  596. _, err := self.ecsRequest("ModifyInstanceAttribute", params)
  597. return err
  598. }
  599. func (self *SRegion) UpdateInstancePassword(instId string, passwd string) error {
  600. return self.updateInstance(instId, "", "", passwd, "", "")
  601. }
  602. // func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  603. // eips, total, err := self.GetSnapshots("", 0, 50)
  604. // if err != nil {
  605. // return nil, err
  606. // }
  607. // for len(eips) < total {
  608. // var parts []SEipAddress
  609. // parts, total, err = self.GetEips("", len(eips), 50)
  610. // if err != nil {
  611. // return nil, err
  612. // }
  613. // eips = append(eips, parts...)
  614. // }
  615. // ret := make([]cloudprovider.ICloudEIP, len(eips))
  616. // for i := 0; i < len(eips); i += 1 {
  617. // ret[i] = &eips[i]
  618. // }
  619. // return ret, nil
  620. // }
  621. func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  622. eips, total, err := self.GetEips("", "", 0, 50)
  623. if err != nil {
  624. return nil, err
  625. }
  626. for len(eips) < total {
  627. var parts []SEipAddress
  628. parts, total, err = self.GetEips("", "", len(eips), 50)
  629. if err != nil {
  630. return nil, err
  631. }
  632. eips = append(eips, parts...)
  633. if len(parts) == 0 {
  634. break
  635. }
  636. }
  637. ret := make([]cloudprovider.ICloudEIP, len(eips))
  638. for i := 0; i < len(eips); i += 1 {
  639. ret[i] = &eips[i]
  640. }
  641. return ret, nil
  642. }
  643. func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
  644. eips, total, err := self.GetEips(eipId, "", 0, 1)
  645. if err != nil {
  646. return nil, err
  647. }
  648. if total == 0 {
  649. return nil, cloudprovider.ErrNotFound
  650. }
  651. if total > 1 {
  652. return nil, cloudprovider.ErrDuplicateId
  653. }
  654. return &eips[0], nil
  655. }
  656. func (region *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
  657. secgroup, err := region.GetSecurityGroup(secgroupId)
  658. if err != nil {
  659. return nil, err
  660. }
  661. secgroup.region = region
  662. return secgroup, nil
  663. }
  664. func (region *SRegion) CreateISecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  665. externalId, err := region.CreateSecurityGroup(opts.VpcId, opts.Name, opts.Desc, opts.ProjectId)
  666. if err != nil {
  667. return nil, err
  668. }
  669. return region.GetISecurityGroupById(externalId)
  670. }
  671. func (region *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
  672. lbs, err := region.GetLoadbalancers(nil)
  673. if err != nil {
  674. return nil, err
  675. }
  676. ilbs := []cloudprovider.ICloudLoadbalancer{}
  677. for i := 0; i < len(lbs); i++ {
  678. lbs[i].region = region
  679. ilbs = append(ilbs, &lbs[i])
  680. }
  681. return ilbs, nil
  682. }
  683. func (region *SRegion) GetILoadBalancerById(loadbalancerId string) (cloudprovider.ICloudLoadbalancer, error) {
  684. return region.GetLoadbalancerDetail(loadbalancerId)
  685. }
  686. func (region *SRegion) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) {
  687. certs, err := region.GetLoadbalancerServerCertificates()
  688. if err != nil {
  689. return nil, err
  690. }
  691. for i := 0; i < len(certs); i++ {
  692. if certs[i].GetGlobalId() == certId {
  693. certs[i].region = region
  694. return &certs[i], nil
  695. }
  696. }
  697. return nil, cloudprovider.ErrNotFound
  698. }
  699. func (region *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) {
  700. params := map[string]string{}
  701. params["RegionId"] = region.RegionId
  702. params["ServerCertificateName"] = cert.Name
  703. params["PrivateKey"] = cert.PrivateKey
  704. params["ServerCertificate"] = cert.Certificate
  705. body, err := region.lbRequest("UploadServerCertificate", params)
  706. if err != nil {
  707. return nil, err
  708. }
  709. certID, err := body.GetString("ServerCertificateId")
  710. if err != nil {
  711. return nil, err
  712. }
  713. return region.GetILoadBalancerCertificateById(certID)
  714. }
  715. func (region *SRegion) GetILoadBalancerAclById(aclId string) (cloudprovider.ICloudLoadbalancerAcl, error) {
  716. return nil, cloudprovider.ErrNotFound
  717. }
  718. func (region *SRegion) GetILoadBalancerAcls() ([]cloudprovider.ICloudLoadbalancerAcl, error) {
  719. return nil, cloudprovider.ErrNotSupported
  720. }
  721. func (region *SRegion) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadbalancerCertificate, error) {
  722. certificates, err := region.GetLoadbalancerServerCertificates()
  723. if err != nil {
  724. return nil, err
  725. }
  726. iCertificates := []cloudprovider.ICloudLoadbalancerCertificate{}
  727. for i := 0; i < len(certificates); i++ {
  728. certificates[i].region = region
  729. iCertificates = append(iCertificates, &certificates[i])
  730. }
  731. return iCertificates, nil
  732. }
  733. func (region *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalancerCreateOptions) (cloudprovider.ICloudLoadbalancer, error) {
  734. params := map[string]string{}
  735. params["RegionId"] = region.RegionId
  736. params["LoadBalancerName"] = loadbalancer.Name
  737. if len(loadbalancer.ZoneId) > 0 {
  738. params["MasterZoneId"] = loadbalancer.ZoneId
  739. }
  740. if len(loadbalancer.VpcId) > 0 {
  741. params["VpcId"] = loadbalancer.VpcId
  742. }
  743. if len(loadbalancer.NetworkIds) > 0 {
  744. params["VSwitchId"] = loadbalancer.NetworkIds[0]
  745. }
  746. if len(loadbalancer.Address) > 0 {
  747. params["Address"] = loadbalancer.Address
  748. }
  749. if len(loadbalancer.AddressType) > 0 {
  750. params["AddressType"] = loadbalancer.AddressType
  751. }
  752. if len(loadbalancer.LoadbalancerSpec) > 0 {
  753. params["LoadBalancerSpec"] = loadbalancer.LoadbalancerSpec
  754. }
  755. if len(loadbalancer.ChargeType) > 0 {
  756. params["InternetChargeType"] = "payby" + loadbalancer.ChargeType
  757. }
  758. if len(loadbalancer.ProjectId) > 0 {
  759. params["ResourceGroupId"] = loadbalancer.ProjectId
  760. }
  761. if loadbalancer.ChargeType == api.LB_CHARGE_TYPE_BY_BANDWIDTH && loadbalancer.EgressMbps > 0 {
  762. params["Bandwidth"] = fmt.Sprintf("%d", loadbalancer.EgressMbps)
  763. }
  764. body, err := region.lbRequest("CreateLoadBalancer", params)
  765. if err != nil {
  766. return nil, err
  767. }
  768. loadBalancerID, err := body.GetString("LoadBalancerId")
  769. if err != nil {
  770. return nil, err
  771. }
  772. region.SetResourceTags("slb", "instance", []string{loadBalancerID}, loadbalancer.Tags, false)
  773. iLoadbalancer, err := region.GetLoadbalancerDetail(loadBalancerID)
  774. if err != nil {
  775. return nil, err
  776. }
  777. return iLoadbalancer, cloudprovider.WaitStatus(iLoadbalancer, api.LB_STATUS_ENABLED, time.Second*5, time.Minute*5)
  778. }
  779. func (region *SRegion) AddAccessControlListEntry(aclId string, entrys []cloudprovider.SLoadbalancerAccessControlListEntry) error {
  780. params := map[string]string{}
  781. params["RegionId"] = region.RegionId
  782. params["AclId"] = aclId
  783. aclArray := jsonutils.NewArray()
  784. for i := 0; i < len(entrys); i++ {
  785. //阿里云AclEntrys参数必须是CIDR格式的。
  786. if regutils.MatchIPAddr(entrys[i].CIDR) {
  787. entrys[i].CIDR += "/32"
  788. }
  789. aclArray.Add(jsonutils.Marshal(map[string]string{"entry": entrys[i].CIDR, "comment": entrys[i].Comment}))
  790. }
  791. if aclArray.Length() == 0 {
  792. return nil
  793. }
  794. params["AclEntrys"] = aclArray.String()
  795. _, err := region.lbRequest("AddAccessControlListEntry", params)
  796. return err
  797. }
  798. func (region *SRegion) CreateILoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (cloudprovider.ICloudLoadbalancerAcl, error) {
  799. params := map[string]string{}
  800. params["RegionId"] = region.RegionId
  801. params["AclName"] = acl.Name
  802. body, err := region.lbRequest("CreateAccessControlList", params)
  803. if err != nil {
  804. return nil, err
  805. }
  806. aclId, err := body.GetString("AclId")
  807. if err != nil {
  808. return nil, err
  809. }
  810. iAcl, err := region.GetLoadbalancerAclDetail(aclId)
  811. if err != nil {
  812. return nil, err
  813. }
  814. return iAcl, region.AddAccessControlListEntry(aclId, acl.Entrys)
  815. }
  816. func (self *SRegion) GetBuckets() ([]SBucket, error) {
  817. params := map[string]string{
  818. "AccountInfo": self.client.getAccountInfo(),
  819. }
  820. resp, err := self.ossRequest("GetService", params)
  821. if err != nil {
  822. return nil, err
  823. }
  824. if !resp.Contains("Data") {
  825. if self.client.cpcfg.UpdatePermission != nil {
  826. self.client.cpcfg.UpdatePermission("oss", "ListBuckets")
  827. }
  828. return []SBucket{}, nil
  829. }
  830. ret := []SBucket{}
  831. err = resp.Unmarshal(&ret, "Data", "ListAllMyBucketsResult", "Buckets", "Bucket")
  832. if err != nil {
  833. return nil, errors.Wrapf(err, "resp.Unmarshal")
  834. }
  835. if len(ret) > 0 {
  836. self.ossEndpoint = ret[0].IntranetEndpoint
  837. }
  838. return ret, nil
  839. }
  840. func (self *SRegion) GetIBuckets() ([]cloudprovider.ICloudBucket, error) {
  841. buckets, err := self.GetBuckets()
  842. if err != nil {
  843. return nil, err
  844. }
  845. ret := []cloudprovider.ICloudBucket{}
  846. for i := range buckets {
  847. buckets[i].region = self
  848. ret = append(ret, &buckets[i])
  849. }
  850. return ret, nil
  851. }
  852. func str2StorageClass(storageClassStr string) (oss.StorageClassType, error) {
  853. storageClass := oss.StorageStandard
  854. if strings.EqualFold(storageClassStr, string(oss.StorageStandard)) {
  855. //
  856. } else if strings.EqualFold(storageClassStr, string(oss.StorageIA)) {
  857. storageClass = oss.StorageIA
  858. } else if strings.EqualFold(storageClassStr, string(oss.StorageArchive)) {
  859. storageClass = oss.StorageArchive
  860. } else {
  861. return storageClass, errors.Error("not supported storageClass")
  862. }
  863. return storageClass, nil
  864. }
  865. func str2Acl(aclStr string) (oss.ACLType, error) {
  866. acl := oss.ACLPrivate
  867. if strings.EqualFold(aclStr, string(oss.ACLPrivate)) {
  868. // private, default
  869. } else if strings.EqualFold(aclStr, string(oss.ACLPublicRead)) {
  870. acl = oss.ACLPublicRead
  871. } else if strings.EqualFold(aclStr, string(oss.ACLPublicReadWrite)) {
  872. acl = oss.ACLPublicReadWrite
  873. } else {
  874. return acl, errors.Error("not supported acl")
  875. }
  876. return acl, nil
  877. }
  878. func (self *SRegion) CreateIBucket(name string, storageClassStr string, aclStr string) error {
  879. osscli, err := self.GetOssClient()
  880. if err != nil {
  881. return errors.Wrap(err, "region.GetOssClient")
  882. }
  883. opts := make([]oss.Option, 0)
  884. if len(storageClassStr) > 0 {
  885. storageClass, err := str2StorageClass(storageClassStr)
  886. if err != nil {
  887. return err
  888. }
  889. opts = append(opts, oss.StorageClass(storageClass))
  890. }
  891. if len(aclStr) > 0 {
  892. acl, err := str2Acl(aclStr)
  893. if err != nil {
  894. return err
  895. }
  896. opts = append(opts, oss.ACL(acl))
  897. }
  898. return osscli.CreateBucket(name, opts...)
  899. }
  900. func ossErrorCode(err error) int {
  901. if srvErr, ok := err.(oss.ServiceError); ok {
  902. return srvErr.StatusCode
  903. }
  904. if srvErr, ok := err.(*oss.ServiceError); ok {
  905. return srvErr.StatusCode
  906. }
  907. return -1
  908. }
  909. func (self *SRegion) DeleteIBucket(name string) error {
  910. cli, err := self.GetOssClient()
  911. if err != nil {
  912. return err
  913. }
  914. return cli.DeleteBucket(name)
  915. }
  916. func (self *SRegion) GetBucket(name string) (*SBucket, error) {
  917. params := map[string]string{
  918. "AccountInfo": self.client.getAccountInfo(),
  919. "x-acs-instanceid": name,
  920. "Params": jsonutils.Marshal(map[string]string{"BucketName": name}).String(),
  921. }
  922. resp, err := self.ossRequest("GetBucketInfo", params)
  923. if err != nil {
  924. return nil, errors.Wrapf(err, "GetBucketInfo")
  925. }
  926. bucket := &SBucket{region: self}
  927. return bucket, resp.Unmarshal(bucket, "Data", "BucketInfo", "Bucket")
  928. }
  929. func (self *SRegion) IBucketExist(name string) (bool, error) {
  930. _, err := self.GetBucket(name)
  931. if err != nil {
  932. return false, err
  933. }
  934. return true, nil
  935. }
  936. func (self *SRegion) GetIBucketById(name string) (cloudprovider.ICloudBucket, error) {
  937. buckets, err := self.GetBuckets()
  938. if err != nil {
  939. return nil, err
  940. }
  941. for i := range buckets {
  942. if buckets[i].GetName() == name {
  943. buckets[i].region = self
  944. return &buckets[i], nil
  945. }
  946. }
  947. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", name)
  948. }
  949. func (region *SRegion) GetIBucketByName(name string) (cloudprovider.ICloudBucket, error) {
  950. return region.GetIBucketById(name)
  951. }
  952. func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) {
  953. caches, err := self.GetElasticCaches(nil)
  954. if err != nil {
  955. return nil, err
  956. }
  957. icaches := make([]cloudprovider.ICloudElasticcache, len(caches))
  958. for i := range caches {
  959. caches[i].region = self
  960. icaches[i] = &caches[i]
  961. }
  962. return icaches, nil
  963. }
  964. func (region *SRegion) GetCapabilities() []string {
  965. return region.client.GetCapabilities()
  966. }
  967. func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  968. vms := make([]SInstance, 0)
  969. for {
  970. parts, total, err := region.GetInstances("", nil, len(vms), 50)
  971. if err != nil {
  972. return nil, err
  973. }
  974. vms = append(vms, parts...)
  975. if len(vms) >= total || len(parts) == 0 {
  976. break
  977. }
  978. }
  979. ivms := make([]cloudprovider.ICloudVM, len(vms))
  980. for i := 0; i < len(vms); i += 1 {
  981. ivms[i] = &vms[i]
  982. }
  983. return ivms, nil
  984. }