instancenic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 nutanix
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. )
  20. type SInstanceNic struct {
  21. cloudprovider.DummyICloudNic
  22. ins *SInstance
  23. MacAddress string `json:"mac_address"`
  24. NetworkUUID string `json:"network_uuid"`
  25. NicUUID string `json:"nic_uuid"`
  26. Model string `json:"model"`
  27. IPAddress string `json:"ip_address"`
  28. IPAddresses []string `json:"ip_addresses"`
  29. VlanMode string `json:"vlan_mode"`
  30. IsConnected bool `json:"is_connected"`
  31. }
  32. func (self *SInstanceNic) GetId() string {
  33. return self.NicUUID
  34. }
  35. func (self *SInstanceNic) GetIP() string {
  36. return self.IPAddress
  37. }
  38. func (self *SInstanceNic) GetMAC() string {
  39. return self.MacAddress
  40. }
  41. func (self *SInstanceNic) GetDriver() string {
  42. return "virtio"
  43. }
  44. func (self *SInstanceNic) InClassicNetwork() bool {
  45. return false
  46. }
  47. func (self *SInstanceNic) GetSubAddress() ([]string, error) {
  48. ret := []string{}
  49. for _, addr := range self.IPAddresses {
  50. if addr != self.IPAddress {
  51. ret = append(ret, addr)
  52. }
  53. }
  54. return ret, nil
  55. }
  56. func (self *SInstanceNic) GetINetworkId() string {
  57. if len(self.IPAddress) == 0 {
  58. return self.NetworkUUID
  59. }
  60. vpc, err := self.ins.host.zone.region.GetVpc(self.NetworkUUID)
  61. if err != nil {
  62. return self.NetworkUUID
  63. }
  64. wires, err := vpc.GetIWires()
  65. if err != nil {
  66. return self.NetworkUUID
  67. }
  68. for i := range wires {
  69. networks, err := wires[i].GetINetworks()
  70. if err != nil {
  71. continue
  72. }
  73. for j := range networks {
  74. network := networks[j].(*SNetwork)
  75. if network.Contains(self.IPAddress) {
  76. return network.GetGlobalId()
  77. }
  78. }
  79. }
  80. return self.NetworkUUID
  81. }
  82. func (self *SInstanceNic) AssignAddress(ipAddrs []string) error {
  83. return cloudprovider.ErrNotImplemented
  84. }
  85. func (self *SRegion) GetInstanceNics(id string) ([]SInstanceNic, error) {
  86. nics := []SInstanceNic{}
  87. res := fmt.Sprintf("vms/%s/nics", id)
  88. params := url.Values{}
  89. params.Set("include_address_assignments", "true")
  90. _, err := self.list(res, params, &nics)
  91. return nics, err
  92. }