region.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 cloudpods
  15. import (
  16. "fmt"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/jsonutils"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. )
  22. type SRegion struct {
  23. multicloud.SRegion
  24. multicloud.SRegionOssBase
  25. multicloud.SRegionLbBase
  26. multicloud.SRegionSecurityGroupBase
  27. cli *SCloudpodsClient
  28. api.CloudregionDetails
  29. }
  30. func (self *SRegion) GetClient() *SCloudpodsClient {
  31. return self.cli
  32. }
  33. func (self *SRegion) GetId() string {
  34. return self.Id
  35. }
  36. func (self *SRegion) GetName() string {
  37. return fmt.Sprintf("%s-%s", self.cli.cpcfg.Name, self.Name)
  38. }
  39. func (self *SRegion) GetGlobalId() string {
  40. return fmt.Sprintf("%s/%s/%s", CLOUD_PROVIDER_CLOUDPODS, self.cli.cpcfg.Id, self.Name)
  41. }
  42. func (self *SRegion) GetStatus() string {
  43. return self.Status
  44. }
  45. func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  46. table := cloudprovider.SModelI18nTable{}
  47. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
  48. return table
  49. }
  50. func (self *SRegion) GetCapabilities() []string {
  51. return self.cli.GetCapabilities()
  52. }
  53. func (self *SRegion) GetCloudEnv() string {
  54. return api.CLOUD_ENV_PRIVATE_CLOUD
  55. }
  56. func (self *SRegion) GetProvider() string {
  57. return api.CLOUD_PROVIDER_CLOUDPODS
  58. }
  59. func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  60. return cloudprovider.SGeographicInfo{
  61. Latitude: self.Latitude,
  62. Longitude: self.Longitude,
  63. City: self.City,
  64. CountryCode: self.CountryCode,
  65. }
  66. }
  67. func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  68. ins, err := self.GetInstance(id)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return ins, nil
  73. }
  74. func (self *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
  75. disk, err := self.GetDisk(id)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return disk, nil
  80. }
  81. func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  82. host, err := self.GetHost(id)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if host.CloudregionId != self.Id {
  87. return nil, cloudprovider.ErrNotFound
  88. }
  89. zone, err := self.GetZone(host.ZoneId)
  90. if err != nil {
  91. return nil, err
  92. }
  93. host.zone = zone
  94. return host, nil
  95. }
  96. func (self *SRegion) create(manager ModelManager, params interface{}, retVal interface{}) error {
  97. return self.cli.create(manager, params, retVal)
  98. }
  99. func (self *SRegion) perform(manager ModelManager, id, action string, params interface{}) (jsonutils.JSONObject, error) {
  100. return self.cli.perform(manager, id, action, params)
  101. }
  102. func (self *SRegion) list(manager ModelManager, params map[string]interface{}, retVal interface{}) error {
  103. if params == nil {
  104. params = map[string]interface{}{}
  105. }
  106. params["cloudregion_id"] = self.Id
  107. return self.cli.list(manager, params, retVal)
  108. }