globalregion.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 google
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SGlobalRegion struct {
  26. cloudprovider.SFakeOnPremiseRegion
  27. multicloud.SNoObjectStorageRegion
  28. multicloud.SRegion
  29. client *SGoogleClient
  30. Description string
  31. ID string
  32. Kind string
  33. Name string
  34. Status string
  35. SelfLink string
  36. CreationTimestamp time.Time
  37. }
  38. func (region *SGlobalRegion) GetClient() *SGoogleClient {
  39. return region.client
  40. }
  41. func (region *SGlobalRegion) GetName() string {
  42. if name, ok := RegionNames[region.Name]; ok {
  43. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE_CN, name)
  44. }
  45. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE_CN, region.Name)
  46. }
  47. func (self *SGlobalRegion) GetI18n() cloudprovider.SModelI18nTable {
  48. en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE, self.Name)
  49. table := cloudprovider.SModelI18nTable{}
  50. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  51. return table
  52. }
  53. func (region *SGlobalRegion) GetId() string {
  54. return region.Name
  55. }
  56. func (region *SGlobalRegion) GetGlobalId() string {
  57. return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_GOOGLE, region.Name)
  58. }
  59. func (region *SGlobalRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  60. if geoInfo, ok := LatitudeAndLongitude[region.Name]; ok {
  61. return geoInfo
  62. }
  63. return cloudprovider.SGeographicInfo{}
  64. }
  65. func (self *SGlobalRegion) GetCreatedAt() time.Time {
  66. return self.CreationTimestamp
  67. }
  68. func (region *SGlobalRegion) GetProvider() string {
  69. return CLOUD_PROVIDER_GOOGLE
  70. }
  71. func (region *SGlobalRegion) GetStatus() string {
  72. return api.CLOUD_REGION_STATUS_INSERVER
  73. }
  74. func (self *SGlobalRegion) GetCapabilities() []string {
  75. return []string{
  76. cloudprovider.CLOUD_CAPABILITY_EIP + cloudprovider.READ_ONLY_SUFFIX,
  77. cloudprovider.CLOUD_CAPABILITY_LOADBALANCER + cloudprovider.READ_ONLY_SUFFIX,
  78. }
  79. }
  80. func (self *SGlobalRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
  81. lbs, err := self.GetGlobalLoadbalancers()
  82. if err != nil {
  83. return nil, errors.Wrap(err, "GetGlobalLoadbalancers")
  84. }
  85. ilbs := []cloudprovider.ICloudLoadbalancer{}
  86. for i := range lbs {
  87. lbs[i].region = self
  88. ilbs = append(ilbs, &lbs[i])
  89. }
  90. return ilbs, nil
  91. }
  92. func (self *SGlobalRegion) Delete(id string) error {
  93. operation := &SOperation{}
  94. err := self.client.ecsDelete(id, operation)
  95. if err != nil {
  96. return errors.Wrap(err, "client.ecsDelete")
  97. }
  98. _, err = self.client.WaitOperation(operation.SelfLink, id, "delete")
  99. if err != nil {
  100. return errors.Wrapf(err, "region.WaitOperation(%s)", operation.SelfLink)
  101. }
  102. return nil
  103. }
  104. func (self *SGlobalRegion) GetProjectId() string {
  105. return self.client.projectId
  106. }
  107. func (self *SGlobalRegion) GetBySelfId(id string, retval interface{}) error {
  108. return self.client.GetBySelfId(id, retval)
  109. }
  110. func (region *SGlobalRegion) Do(id string, action string, params map[string]string, body jsonutils.JSONObject) error {
  111. opId, err := region.client.ecsDo(id, action, params, body)
  112. if err != nil {
  113. return err
  114. }
  115. if strings.Index(opId, "/operations/") > 0 {
  116. _, err = region.client.WaitOperation(opId, id, action)
  117. return err
  118. }
  119. return nil
  120. }
  121. func (region *SGlobalRegion) Get(resourceType, id string, retval interface{}) error {
  122. return region.client.ecsGet(resourceType, id, retval)
  123. }
  124. func (self *SGlobalRegion) getLoadbalancerComponents(resource string, filter string, result interface{}) error {
  125. url := fmt.Sprintf("%s/%s", self.Name, resource)
  126. params := map[string]string{}
  127. if len(filter) > 0 {
  128. params["filter"] = filter
  129. }
  130. err := self.ListAll(url, params, result)
  131. if err != nil {
  132. return errors.Wrap(err, "ListAll")
  133. }
  134. return nil
  135. }