instancenic.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 aws
  15. import (
  16. "fmt"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. )
  19. type SInstanceNic struct {
  20. instance *SInstance
  21. id string
  22. ipAddr string
  23. ip6Addr string
  24. macAddr string
  25. cloudprovider.DummyICloudNic
  26. }
  27. func (self *SInstanceNic) GetId() string {
  28. return self.id
  29. }
  30. func (self *SInstanceNic) GetIP() string {
  31. return self.ipAddr
  32. }
  33. func (self *SInstanceNic) GetIP6() string {
  34. return self.ip6Addr
  35. }
  36. func (self *SInstanceNic) GetMAC() string {
  37. return self.macAddr
  38. }
  39. func (self *SInstanceNic) InClassicNetwork() bool {
  40. return false
  41. }
  42. func (self *SInstanceNic) GetDriver() string {
  43. return "virtio"
  44. }
  45. func (self *SInstanceNic) GetINetworkId() string {
  46. return self.instance.SubnetId
  47. }
  48. func (self *SRegion) GetSubAddress(nicId string) ([]string, error) {
  49. params := map[string]string{
  50. "NetworkInterfaceId.1": nicId,
  51. }
  52. ret := struct {
  53. NextToken string `xml:"nextToken"`
  54. NetworkInterfaceSet []struct {
  55. NetworkInterfaceId string `xml:"networkInterfaceId"`
  56. PrivateIpAddress string `xml:"privateIpAddress"`
  57. PrivateIpAddressesSet []struct {
  58. PrivateIpAddress string `xml:"privateIpAddress"`
  59. Primary bool `xml:"primary"`
  60. } `xml:"privateIpAddressesSet>item"`
  61. } `xml:"networkInterfaceSet>item"`
  62. }{}
  63. err := self.ec2Request("DescribeNetworkInterfaces", params, &ret)
  64. if err != nil {
  65. return nil, err
  66. }
  67. ipAddrs := []string{}
  68. for _, net := range ret.NetworkInterfaceSet {
  69. if net.NetworkInterfaceId != nicId {
  70. continue
  71. }
  72. for _, addr := range net.PrivateIpAddressesSet {
  73. if !addr.Primary {
  74. ipAddrs = append(ipAddrs, addr.PrivateIpAddress)
  75. }
  76. }
  77. }
  78. return ipAddrs, nil
  79. }
  80. func (self *SInstanceNic) GetSubAddress() ([]string, error) {
  81. return self.instance.host.zone.region.GetSubAddress(self.id)
  82. }
  83. func (self *SRegion) AssignAddres(nicId string, ipAddrs []string) error {
  84. params := map[string]string{
  85. "NetworkInterfaceId": nicId,
  86. }
  87. for i, addr := range ipAddrs {
  88. params[fmt.Sprintf("PrivateIpAddress.%d", i+1)] = addr
  89. }
  90. ret := struct{}{}
  91. return self.ec2Request("AssignPrivateIpAddresses", params, &ret)
  92. }
  93. func (self *SInstanceNic) AssignAddress(ipAddrs []string) error {
  94. return self.instance.host.zone.region.AssignAddres(self.id, ipAddrs)
  95. }
  96. func (self *SRegion) UnassignAddress(nicId string, ipAddrs []string) error {
  97. params := map[string]string{
  98. "NetworkInterfaceId": nicId,
  99. }
  100. for i, addr := range ipAddrs {
  101. params[fmt.Sprintf("PrivateIpAddress.%d", i+1)] = addr
  102. }
  103. ret := struct{}{}
  104. return self.ec2Request("UnassignPrivateIpAddresses", params, &ret)
  105. }
  106. func (self *SInstanceNic) UnassignAddress(ipAddrs []string) error {
  107. return self.instance.host.zone.region.UnassignAddress(self.id, ipAddrs)
  108. }