instancenic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/pkg/errors"
  21. )
  22. type SInstanceNic struct {
  23. instance *SInstance
  24. id string
  25. ipAddr string
  26. macAddr string
  27. }
  28. type SNetworkInterface struct {
  29. cloudprovider.DummyICloudNic
  30. multicloud.SNetworkInterfaceBase
  31. VolcEngineTags
  32. region *SRegion
  33. InstanceId string
  34. NetworkInterfaceId string
  35. VpcId string
  36. SubnetId string
  37. PrimaryIpAddress string
  38. Type string
  39. MacAddress string
  40. CreationTime time.Time
  41. NetworkInterfaceName string
  42. PrivateIpSets SPrivateIpSets
  43. ResourceGroupId string
  44. SecurityGroupIds SSecurityGroupIds
  45. Status string
  46. ZoneId string
  47. PrivateIpAddresses []string
  48. AssociatedElasticIp SAssociatedElasticIp
  49. IPv6Sets []string
  50. }
  51. func (nic *SNetworkInterface) GetIP() string {
  52. return nic.PrimaryIpAddress
  53. }
  54. func (nic *SNetworkInterface) GetIP6() string {
  55. for _, ip := range nic.IPv6Sets {
  56. return ip
  57. }
  58. return ""
  59. }
  60. func (nic *SNetworkInterface) GetMAC() string {
  61. return nic.MacAddress
  62. }
  63. func (nic *SNetworkInterface) InClassicNetwork() bool {
  64. return false
  65. }
  66. func (nic *SNetworkInterface) GetDriver() string {
  67. return "virtio"
  68. }
  69. func (nic *SNetworkInterface) GetINetworkId() string {
  70. return nic.SubnetId
  71. }
  72. func (nic *SNetworkInterface) GetSubAddress() ([]string, error) {
  73. return nic.region.GetSubAddress(nic.NetworkInterfaceId)
  74. }
  75. func (nic *SNetworkInterface) AssignAddress(ipAddrs []string) error {
  76. return nic.region.AssignAddres(nic.NetworkInterfaceId, ipAddrs)
  77. }
  78. func (nic *SNetworkInterface) UnassignAddress(ipAddrs []string) error {
  79. return nic.region.UnassignAddress(nic.NetworkInterfaceId, ipAddrs)
  80. }
  81. func (region *SRegion) GetSubAddress(nicId string) ([]string, error) {
  82. nics, err := region.GetNetworkInterfaces(nicId, "")
  83. if err != nil {
  84. return nil, errors.Wrapf(err, "GetNetworkInterfaces")
  85. }
  86. ipAddrs := []string{}
  87. for _, net := range nics {
  88. if net.NetworkInterfaceId != nicId {
  89. continue
  90. }
  91. for _, addr := range net.PrivateIpSets.PrivateIpSet {
  92. if !addr.Primary {
  93. ipAddrs = append(ipAddrs, addr.PrivateIpAddress)
  94. }
  95. }
  96. }
  97. return ipAddrs, nil
  98. }
  99. func (region *SRegion) GetNetworkInterfaces(nicId, instanceId string) ([]SNetworkInterface, error) {
  100. params := map[string]string{
  101. "PageSize": "100",
  102. }
  103. if len(nicId) > 0 {
  104. params["NetworkInterfaceId.1"] = nicId
  105. }
  106. if len(instanceId) > 0 {
  107. params["InstanceId"] = instanceId
  108. }
  109. ret := []SNetworkInterface{}
  110. for {
  111. resp, err := region.vpcRequest("DescribeNetworkInterfaces", params)
  112. if err != nil {
  113. return nil, errors.Wrapf(err, "DescribeNetworkInterfaces")
  114. }
  115. part := struct {
  116. NetworkInterfaceSets []SNetworkInterface
  117. NextToken string
  118. }{}
  119. err = resp.Unmarshal(&part)
  120. if err != nil {
  121. return nil, err
  122. }
  123. ret = append(ret, part.NetworkInterfaceSets...)
  124. if len(part.NextToken) == 0 {
  125. break
  126. }
  127. params["NextToken"] = part.NextToken
  128. }
  129. return ret, nil
  130. }
  131. func (region *SRegion) AssignAddres(nicId string, ipAddrs []string) error {
  132. params := make(map[string]string)
  133. params["NetworkInterfaceId"] = nicId
  134. for idx, addr := range ipAddrs {
  135. params[fmt.Sprintf("PrivateIpAddress.%d", idx+1)] = addr
  136. }
  137. _, err := region.vpcRequest("AssignPrivateIpAddresses", params)
  138. if err != nil {
  139. return errors.Wrapf(err, "AssignPrivateIpAddresses")
  140. }
  141. return nil
  142. }
  143. func (region *SRegion) UnassignAddress(nicId string, ipAddrs []string) error {
  144. params := make(map[string]string)
  145. params["NetworkInterfaceId"] = nicId
  146. for idx, addr := range ipAddrs {
  147. params[fmt.Sprintf("PrivateIpAddress.%d", idx+1)] = addr
  148. }
  149. _, err := region.vpcRequest("UnassignPrivateAddress", params)
  150. if err != nil {
  151. return errors.Wrapf(err, "UnassignPrivateAdress")
  152. }
  153. return nil
  154. }