eip.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/log"
  21. "yunion.io/x/pkg/errors"
  22. billing "yunion.io/x/cloudmux/pkg/apis/billing"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type SAddress struct {
  28. region *SRegion
  29. SResourceBase
  30. multicloud.SEipBase
  31. GoogleTags
  32. instanceId string
  33. CreationTimestamp time.Time
  34. Description string
  35. Address string
  36. Status string
  37. Region string
  38. Users []string
  39. NetworkTier string
  40. AddressType string
  41. Kind string
  42. }
  43. func (region *SRegion) GetEips(address string, maxResults int, pageToken string) ([]SAddress, error) {
  44. eips := []SAddress{}
  45. params := map[string]string{}
  46. filters := []string{`(addressType eq "EXTERNAL")`}
  47. if len(address) > 0 {
  48. filters = append(filters, fmt.Sprintf(`(address eq "%s")`, address))
  49. }
  50. params["filter"] = strings.Join(filters, "")
  51. resource := fmt.Sprintf("regions/%s/addresses", region.Name)
  52. return eips, region.List(resource, params, maxResults, pageToken, &eips)
  53. }
  54. func (region *SRegion) GetEip(id string) (*SAddress, error) {
  55. eip := &SAddress{region: region}
  56. return eip, region.Get("addresses", id, eip)
  57. }
  58. func (addr *SAddress) GetStatus() string {
  59. switch addr.Status {
  60. case "RESERVING":
  61. return api.EIP_STATUS_ASSOCIATE
  62. case "RESERVED":
  63. return api.EIP_STATUS_READY
  64. case "IN_USE":
  65. return api.EIP_STATUS_READY
  66. default:
  67. log.Errorf("Unknown eip status: %s", addr.Status)
  68. return api.EIP_STATUS_UNKNOWN
  69. }
  70. }
  71. func (addr *SAddress) GetProjectId() string {
  72. return addr.region.GetProjectId()
  73. }
  74. func (addr *SAddress) IsEmulated() bool {
  75. return len(addr.instanceId) > 0
  76. }
  77. func (addr *SAddress) GetCreatedAt() time.Time {
  78. return addr.CreationTimestamp
  79. }
  80. func (addr *SAddress) GetExpiredAt() time.Time {
  81. return time.Time{}
  82. }
  83. func (addr *SAddress) GetBillingType() string {
  84. return billing.BILLING_TYPE_POSTPAID
  85. }
  86. func (self *SAddress) Refresh() error {
  87. if self.IsEmulated() {
  88. return nil
  89. }
  90. addr, err := self.region.GetEip(self.Id)
  91. if err != nil {
  92. return err
  93. }
  94. return jsonutils.Update(self, addr)
  95. }
  96. func (addr *SAddress) GetIpAddr() string {
  97. return addr.Address
  98. }
  99. func (addr *SAddress) GetMode() string {
  100. if addr.IsEmulated() {
  101. return api.EIP_MODE_INSTANCE_PUBLICIP
  102. }
  103. return api.EIP_MODE_STANDALONE_EIP
  104. }
  105. func (addr *SAddress) GetINetworkId() string {
  106. return ""
  107. }
  108. func (addr *SAddress) GetAssociationType() string {
  109. if len(addr.instanceId) > 0 {
  110. return api.EIP_ASSOCIATE_TYPE_SERVER
  111. }
  112. for _, user := range addr.Users {
  113. if strings.Contains(user, "/instances/") {
  114. return api.EIP_ASSOCIATE_TYPE_SERVER
  115. }
  116. if strings.Contains(user, "/forwardingRules/") {
  117. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  118. }
  119. if strings.Contains(user, "/routers/") {
  120. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  121. }
  122. }
  123. return ""
  124. }
  125. func (addr *SAddress) GetAssociationExternalId() string {
  126. if len(addr.instanceId) > 0 {
  127. return addr.instanceId
  128. }
  129. if len(addr.Users) > 0 {
  130. res := &SResourceBase{}
  131. err := addr.region.GetBySelfId(addr.Users[0], res)
  132. if err != nil {
  133. return ""
  134. }
  135. return res.GetGlobalId()
  136. }
  137. return ""
  138. }
  139. func (addr *SAddress) GetBandwidth() int {
  140. return 0
  141. }
  142. func (addr *SAddress) GetInternetChargeType() string {
  143. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  144. }
  145. func (addr *SAddress) Delete() error {
  146. return addr.region.Delete(addr.SelfLink)
  147. }
  148. func (addr *SAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  149. return addr.region.AssociateInstanceEip(conf.InstanceId, addr.Address)
  150. }
  151. func (addr *SAddress) Dissociate() error {
  152. if len(addr.Users) > 0 {
  153. return addr.region.DissociateInstanceEip(addr.Users[0], addr.Address)
  154. }
  155. return nil
  156. }
  157. func (addr *SAddress) ChangeBandwidth(bw int) error {
  158. return cloudprovider.ErrNotSupported
  159. }
  160. func (region *SRegion) CreateEip(name string, desc string) (*SAddress, error) {
  161. body := map[string]string{
  162. "name": normalizeString(name),
  163. "description": desc,
  164. }
  165. resource := fmt.Sprintf("regions/%s/addresses", region.Name)
  166. addr := &SAddress{region: region}
  167. err := region.Insert(resource, jsonutils.Marshal(body), addr)
  168. if err != nil {
  169. return nil, err
  170. }
  171. return addr, nil
  172. }
  173. func (region *SRegion) AssociateInstanceEip(instanceId string, eip string) error {
  174. instance, err := region.GetInstance(instanceId)
  175. if err != nil {
  176. return errors.Wrap(err, "region.GetInstance")
  177. }
  178. for _, networkInterface := range instance.NetworkInterfaces {
  179. body := map[string]interface{}{
  180. "type": "ONE_TO_ONE_NAT",
  181. "name": "External NAT",
  182. "natIP": eip,
  183. }
  184. params := map[string]string{"networkInterface": networkInterface.Name}
  185. return region.Do(instance.SelfLink, "addAccessConfig", params, jsonutils.Marshal(body))
  186. }
  187. return fmt.Errorf("no valid networkinterface to associate")
  188. }
  189. func (self *SRegion) DissociateInstanceEip(instanceId string, eip string) error {
  190. instance := SInstance{}
  191. err := self.GetBySelfId(instanceId, &instance)
  192. if err != nil {
  193. return errors.Wrap(err, "region.GetInstance")
  194. }
  195. for _, networkInterface := range instance.NetworkInterfaces {
  196. for _, accessConfig := range networkInterface.AccessConfigs {
  197. if accessConfig.NatIP == eip {
  198. body := map[string]string{}
  199. params := map[string]string{
  200. "networkInterface": networkInterface.Name,
  201. "accessConfig": accessConfig.Name,
  202. }
  203. return self.Do(instance.SelfLink, "deleteAccessConfig", params, jsonutils.Marshal(body))
  204. }
  205. }
  206. }
  207. return nil
  208. }