eip.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright 2023 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 volcengine
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/pkg/utils"
  25. )
  26. type TBillingType int
  27. const (
  28. BillingByPrePaid TBillingType = iota + 1
  29. BillingByBandwidth
  30. BillingByTraffic
  31. )
  32. const (
  33. EIP_STATUS_ATTACHING = "Attaching"
  34. EIP_STATUS_DETACHING = "Detaching"
  35. EIP_STATUS_ATTACHED = "Attached"
  36. EIP_STATUS_AVAILABLE = "Available"
  37. EIP_STATUS_DELETING = "Deleting"
  38. EIP_INSTANCE_TYPE_NAT = "Nat" // NAT网关
  39. EIP_INSTANCE_TYPE_ENI = "NetworkInterface" // 辅助网卡
  40. EIP_INSTANCE_TYPE_CLB = "ClbInstance" // 负载均衡
  41. EIP_INSTANCE_TYPE_ALB = "Albinstance" // 应用型负载均衡
  42. EIP_INSTANCE_TYPE_ECS = "EcsInstance" // 云服务器
  43. EIP_INSTANCE_TYPE_HAVIP = "HaVip" // 高可用虚拟IP
  44. )
  45. type SEipAddress struct {
  46. region *SRegion
  47. multicloud.SEipBase
  48. VolcEngineTags
  49. Name string
  50. AllocationId string
  51. BillingType TBillingType
  52. EipAddress string
  53. Status string
  54. InstanceType string
  55. InstanceId string
  56. Bandwidth int /* Mbps */
  57. BusinessStatus string
  58. AllocationTime time.Time
  59. Description string
  60. ISP string
  61. LockReason string
  62. ExpiredTime time.Time
  63. ProjectName string
  64. }
  65. func (eipaddr *SEipAddress) GetId() string {
  66. return eipaddr.AllocationId
  67. }
  68. func (eipaddr *SEipAddress) GetName() string {
  69. if len(eipaddr.Name) > 0 {
  70. return eipaddr.Name
  71. }
  72. return eipaddr.EipAddress
  73. }
  74. func (eipaddr *SEipAddress) GetGlobalId() string {
  75. return eipaddr.AllocationId
  76. }
  77. func (eipaddr *SEipAddress) GetStatus() string {
  78. switch eipaddr.Status {
  79. case EIP_STATUS_ATTACHED, EIP_STATUS_AVAILABLE:
  80. return api.EIP_STATUS_READY
  81. case EIP_STATUS_ATTACHING:
  82. return api.EIP_STATUS_ASSOCIATE
  83. case EIP_STATUS_DETACHING:
  84. return api.EIP_STATUS_DISSOCIATE
  85. case EIP_STATUS_DELETING:
  86. return api.EIP_STATUS_DEALLOCATE
  87. default:
  88. return api.EIP_STATUS_UNKNOWN
  89. }
  90. }
  91. func (eipaddr *SEipAddress) Refresh() error {
  92. if eipaddr.IsEmulated() {
  93. return nil
  94. }
  95. new, err := eipaddr.region.GetEip(eipaddr.AllocationId)
  96. if err != nil {
  97. return err
  98. }
  99. return jsonutils.Update(eipaddr, new)
  100. }
  101. func (eipaddr *SEipAddress) GetProjectId() string {
  102. return eipaddr.ProjectName
  103. }
  104. func (eipaddr *SEipAddress) GetIpAddr() string {
  105. return eipaddr.EipAddress
  106. }
  107. func (eipaddr *SEipAddress) GetMode() string {
  108. if eipaddr.InstanceId == eipaddr.AllocationId {
  109. return api.EIP_MODE_INSTANCE_PUBLICIP
  110. } else {
  111. return api.EIP_MODE_STANDALONE_EIP
  112. }
  113. }
  114. func (eipaddr *SEipAddress) GetINetworkId() string {
  115. return ""
  116. }
  117. func (eipaddr *SEipAddress) GetAssociationType() string {
  118. switch eipaddr.InstanceType {
  119. case EIP_INSTANCE_TYPE_ECS, EIP_INSTANCE_TYPE_ENI:
  120. return api.EIP_ASSOCIATE_TYPE_SERVER
  121. case EIP_INSTANCE_TYPE_NAT:
  122. return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
  123. case EIP_INSTANCE_TYPE_ALB, EIP_INSTANCE_TYPE_CLB:
  124. return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
  125. default:
  126. return eipaddr.InstanceType
  127. }
  128. }
  129. func (eipaddr *SEipAddress) GetAssociationExternalId() string {
  130. return eipaddr.InstanceId
  131. }
  132. func (eipaddr *SEipAddress) GetBandwidth() int {
  133. return eipaddr.Bandwidth
  134. }
  135. func (eipaddr *SEipAddress) GetInternetChargeType() string {
  136. switch eipaddr.BillingType {
  137. case BillingByPrePaid, BillingByBandwidth:
  138. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  139. case BillingByTraffic:
  140. return api.EIP_CHARGE_TYPE_BY_TRAFFIC
  141. default:
  142. return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
  143. }
  144. }
  145. func (eipaddr *SEipAddress) Delete() error {
  146. return eipaddr.region.DeallocateEIP(eipaddr.AllocationId)
  147. }
  148. func (eipaddr *SEipAddress) Associate(conf *cloudprovider.AssociateConfig) error {
  149. _ = cloudprovider.Wait(20*time.Second, time.Minute, func() (bool, error) {
  150. err := eipaddr.region.AssociateEip(eipaddr.AllocationId, conf.InstanceId)
  151. if err != nil {
  152. if isError(err, "IncorrectInstanceStatus") {
  153. return false, nil
  154. }
  155. return false, errors.Wrapf(err, "region.AssociateEip")
  156. }
  157. return true, nil
  158. })
  159. err := cloudprovider.WaitStatus(eipaddr, api.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
  160. return err
  161. }
  162. func (eipaddr *SEipAddress) Dissociate() error {
  163. err := eipaddr.region.DissociateEip(eipaddr.AllocationId, eipaddr.InstanceId)
  164. if err != nil {
  165. return err
  166. }
  167. err = cloudprovider.WaitStatus(eipaddr, api.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
  168. return err
  169. }
  170. func (eipaddr *SEipAddress) ChangeBandwidth(bw int) error {
  171. return eipaddr.region.UpdateEipBandwidth(eipaddr.AllocationId, bw)
  172. }
  173. func getInstanceType(instanceId string) (string, error) {
  174. prefixMap := map[string]string{
  175. "i-": EIP_INSTANCE_TYPE_ECS,
  176. "clb-": EIP_INSTANCE_TYPE_CLB,
  177. "alb-": EIP_INSTANCE_TYPE_ALB,
  178. "ngw-": EIP_INSTANCE_TYPE_NAT,
  179. "eni-": EIP_INSTANCE_TYPE_ENI,
  180. "havip-": EIP_INSTANCE_TYPE_HAVIP,
  181. }
  182. for prefix, instanceType := range prefixMap {
  183. if strings.HasPrefix(instanceId, prefix) {
  184. return instanceType, nil
  185. }
  186. }
  187. return "", errors.Errorf("Unknown instance type for %s", instanceId)
  188. }
  189. func (region *SRegion) GetEips(eipIds []string, associatedId string, addresses []string, pageNumber int, pageSize int) ([]SEipAddress, int, error) {
  190. if pageSize > 100 || pageSize <= 0 {
  191. pageSize = 100
  192. }
  193. params := make(map[string]string)
  194. params["PageSize"] = fmt.Sprintf("%d", pageSize)
  195. params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
  196. for index, addr := range addresses {
  197. params[fmt.Sprintf("EipAddresses.%d", index+1)] = addr
  198. }
  199. for index, eipId := range eipIds {
  200. params[fmt.Sprintf("AllocationIds.%d", index+1)] = eipId
  201. }
  202. if len(associatedId) > 0 {
  203. params["AssociatedInstanceId"] = associatedId
  204. associatedType, err := getInstanceType(associatedId)
  205. if err != nil {
  206. return nil, 0, errors.Wrapf(err, "Unknown associated type")
  207. }
  208. params["AssociatedInstanceType"] = associatedType
  209. }
  210. body, err := region.vpcRequest("DescribeEipAddresses", params)
  211. if err != nil {
  212. return nil, 0, errors.Wrapf(err, "DescribeEipAddresses fail")
  213. }
  214. eips := make([]SEipAddress, 0)
  215. err = body.Unmarshal(&eips, "EipAddresses")
  216. if err != nil {
  217. return nil, 0, errors.Wrapf(err, "Unmarshal EipAddress details fail")
  218. }
  219. total, _ := body.Int("TotalCount")
  220. for i := 0; i < len(eips); i += 1 {
  221. eips[i].region = region
  222. }
  223. return eips, int(total), nil
  224. }
  225. func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
  226. eips, _, err := region.GetEips([]string{eipId}, "", make([]string, 0), 1, 1)
  227. if err != nil {
  228. return nil, err
  229. }
  230. for i := range eips {
  231. if eips[i].AllocationId == eipId {
  232. eips[i].region = region
  233. return &eips[i], nil
  234. }
  235. }
  236. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", eipId)
  237. }
  238. func (region *SRegion) AllocateEIP(opts *cloudprovider.SEip) (*SEipAddress, error) {
  239. params := make(map[string]string)
  240. if len(opts.Name) > 0 {
  241. params["Name"] = opts.Name
  242. }
  243. params["Bandwidth"] = fmt.Sprintf("%d", opts.BandwidthMbps)
  244. switch opts.ChargeType {
  245. case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
  246. params["BillingType"] = fmt.Sprintf("%d", BillingByTraffic)
  247. case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
  248. params["BillingType"] = fmt.Sprintf("%d", BillingByBandwidth)
  249. }
  250. params["ClientToken"] = utils.GenRequestId(20)
  251. if len(opts.ProjectId) > 0 {
  252. params["ProjectName"] = opts.ProjectId
  253. }
  254. params["ISP"] = "BGP"
  255. index := 1
  256. for key, value := range opts.Tags {
  257. params[fmt.Sprintf("Tags.%d.Key", index)] = key
  258. params[fmt.Sprintf("Tags.%d.Value", index)] = value
  259. index++
  260. }
  261. body, err := region.vpcRequest("AllocateEipAddress", params)
  262. if err != nil {
  263. return nil, errors.Wrapf(err, "AllocateEipAddress fail")
  264. }
  265. eipId, err := body.GetString("AllocationId")
  266. if err != nil {
  267. return nil, errors.Wrapf(err, "get AllocationId after created fail")
  268. }
  269. err = cloudprovider.Wait(5*time.Second, time.Minute, func() (bool, error) {
  270. _, err := region.GetEip(eipId)
  271. if errors.Cause(err) == cloudprovider.ErrNotFound {
  272. return false, nil
  273. } else {
  274. return true, err
  275. }
  276. })
  277. if err != nil {
  278. return nil, errors.Wrapf(err, "cannot find eip after create")
  279. }
  280. return region.GetEip(eipId)
  281. }
  282. func (region *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  283. return region.AllocateEIP(opts)
  284. }
  285. func (region *SRegion) DeallocateEIP(eipId string) error {
  286. params := make(map[string]string)
  287. params["AllocationId"] = eipId
  288. _, err := region.vpcRequest("ReleaseEipAddress", params)
  289. if err != nil {
  290. err = errors.Wrapf(err, "ReleaseEipAddress fail")
  291. }
  292. return err
  293. }
  294. func (region *SRegion) AssociateEip(eipId string, instanceId string) error {
  295. params := make(map[string]string)
  296. params["AllocationId"] = eipId
  297. params["InstanceId"] = instanceId
  298. instanceType, err := getInstanceType(instanceId)
  299. if err != nil {
  300. return errors.Wrapf(err, "Unknown instance type")
  301. }
  302. params["InstanceType"] = instanceType
  303. _, err = region.vpcRequest("AssociateEipAddress", params)
  304. return errors.Wrapf(err, "AssociateEipAddress fail")
  305. }
  306. func (region *SRegion) DissociateEip(eipId string, instanceId string) error {
  307. params := make(map[string]string)
  308. params["AllocationId"] = eipId
  309. params["InstanceId"] = instanceId
  310. instanceType, err := getInstanceType(instanceId)
  311. if err != nil {
  312. return errors.Wrapf(err, "Unknown instance type")
  313. }
  314. params["InstanceType"] = instanceType
  315. _, err = region.vpcRequest("DisassociateEipAddress", params)
  316. if err != nil {
  317. err = errors.Wrapf(err, "DisassociateEipAddress fail")
  318. }
  319. return err
  320. }
  321. func (region *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
  322. params := make(map[string]string)
  323. params["AllocationId"] = eipId
  324. params["Bandwidth"] = fmt.Sprintf("%d", bw)
  325. _, err := region.vpcRequest("ModifyEipAddressAttributes", params)
  326. if err != nil {
  327. err = errors.Wrapf(err, "ModifyEipAddressAttributes fail")
  328. }
  329. return err
  330. }