globaleip.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/log"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SGlobalAddress struct {
  25. region *SGlobalRegion
  26. SResourceBase
  27. multicloud.SEipBase
  28. GoogleTags
  29. CreationTimestamp time.Time
  30. Description string
  31. Address string
  32. Status string
  33. Region string
  34. Users []string
  35. NetworkTier string
  36. AddressType string
  37. Kind string
  38. }
  39. func (addr *SGlobalAddress) GetStatus() string {
  40. switch addr.Status {
  41. case "RESERVING":
  42. return api.EIP_STATUS_ASSOCIATE
  43. case "RESERVED":
  44. return api.EIP_STATUS_READY
  45. case "IN_USE":
  46. return api.EIP_STATUS_READY
  47. default:
  48. log.Errorf("Unknown eip status: %s", addr.Status)
  49. return api.EIP_STATUS_UNKNOWN
  50. }
  51. }
  52. func (addr *SGlobalAddress) GetIpAddr() string {
  53. return addr.Address
  54. }
  55. func (addr *SGlobalAddress) GetMode() string {
  56. if addr.IsEmulated() {
  57. return api.EIP_MODE_INSTANCE_PUBLICIP
  58. }
  59. return api.EIP_MODE_STANDALONE_EIP
  60. }
  61. func (addr *SGlobalAddress) GetBandwidth() int {
  62. return 0
  63. }
  64. func (addr *SGlobalAddress) GetInternetChargeType() string {
  65. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  66. }
  67. func (addr *SGlobalAddress) Delete() error {
  68. return addr.region.Delete(addr.SelfLink)
  69. }
  70. func (addr *SGlobalAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  71. return cloudprovider.ErrNotImplemented
  72. }
  73. func (addr *SGlobalAddress) Dissociate() error {
  74. return cloudprovider.ErrNotImplemented
  75. }
  76. func (addr *SGlobalAddress) ChangeBandwidth(bw int) error {
  77. return cloudprovider.ErrNotSupported
  78. }
  79. func (addr *SGlobalAddress) GetProjectId() string {
  80. return ""
  81. }
  82. func (region *SGlobalRegion) GetEips(address string) ([]SGlobalAddress, error) {
  83. eips := []SGlobalAddress{}
  84. params := map[string]string{}
  85. filters := []string{"addressType=EXTERNAL"}
  86. if len(address) > 0 {
  87. filters = append(filters, fmt.Sprintf(`address="%s"`, address))
  88. }
  89. params["filter"] = strings.Join(filters, " ADN ")
  90. resource := "global/addresses"
  91. err := region.ListAll(resource, params, &eips)
  92. if err != nil {
  93. return nil, err
  94. }
  95. for i := range eips {
  96. eips[i].region = region
  97. }
  98. return eips, nil
  99. }
  100. func (region *SGlobalRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  101. eips, err := region.GetEips("")
  102. if err != nil {
  103. return nil, err
  104. }
  105. ret := []cloudprovider.ICloudEIP{}
  106. for i := range eips {
  107. eips[i].region = region
  108. ret = append(ret, &eips[i])
  109. }
  110. return ret, nil
  111. }
  112. func (region *SGlobalRegion) GetEip(id string) (*SGlobalAddress, error) {
  113. eip := &SGlobalAddress{region: region}
  114. return eip, region.Get("addresses", id, eip)
  115. }
  116. func (region *SGlobalRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
  117. eip, err := region.GetEip(id)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return eip, nil
  122. }
  123. func (region *SGlobalRegion) CreateEIP(args *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  124. return nil, cloudprovider.ErrNotImplemented
  125. }
  126. func (addr *SGlobalAddress) GetAssociationExternalId() string {
  127. associateType := addr.GetAssociationType()
  128. for _, user := range addr.Users {
  129. if associateType == api.EIP_ASSOCIATE_TYPE_LOADBALANCER {
  130. forword := &SForwardingRule{}
  131. err := addr.region.GetBySelfId(user, forword)
  132. if err != nil {
  133. return ""
  134. }
  135. proxy := &STargetHttpProxy{}
  136. err = addr.region.GetBySelfId(forword.Target, proxy)
  137. if err != nil {
  138. return ""
  139. }
  140. return getGlobalId(proxy.URLMap)
  141. }
  142. }
  143. return ""
  144. }
  145. func (addr *SGlobalAddress) GetAssociationType() string {
  146. for _, user := range addr.Users {
  147. if strings.Contains(user, "global/forwardingRules") {
  148. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  149. }
  150. return api.EIP_ASSOCIATE_TYPE_SERVER
  151. }
  152. return ""
  153. }
  154. func (region *SGlobalRegion) listAll(method string, resource string, params map[string]string, retval interface{}) error {
  155. return region.client._ecsListAll(method, resource, params, retval)
  156. }
  157. func (region *SGlobalRegion) ListAll(resource string, params map[string]string, retval interface{}) error {
  158. return region.listAll("GET", resource, params, retval)
  159. }