instancenic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 hcso
  15. import (
  16. "yunion.io/x/log"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud/hcso/client/modules"
  19. )
  20. // ===========================================
  21. type Interface struct {
  22. PortState string `json:"port_state"`
  23. FixedIPS []FixedIP `json:"fixed_ips"`
  24. NetID string `json:"net_id"` // 网络ID. 与 SNetwork里的ID对应。统一使用这个ID
  25. PortID string `json:"port_id"`
  26. MACAddr string `json:"mac_addr"`
  27. }
  28. /*
  29. subnet: {id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233", name: "subnet-149c", cidr: "192.168.0.0/24",…}
  30. availability_zone: "cn-north-1b"
  31. cidr: "192.168.0.0/24"
  32. dhcp_enable: true
  33. dnsList: ["100.125.1.250", "100.125.21.250"]
  34. gateway_ip: "192.168.0.1"
  35. id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233"
  36. ipv6_enable: false
  37. name: "subnet-149c"
  38. neutron_network_id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233"
  39. neutron_subnet_id: "81fcfaa0-8e73-4472-9eba-3b2b7736d3a7"
  40. primary_dns: "100.125.1.250"
  41. secondary_dns: "100.125.21.250"
  42. status: "ACTIVE"
  43. tags: []
  44. vpc_id: "877f1feb-3dc8-4c2d-92e9-0d94fd7d79dd"}
  45. */
  46. type FixedIP struct {
  47. SubnetID string `json:"subnet_id"` // 子网ID, 与SNetwork中的 neutron_subnet_id对应. 注意!!! 并不是SNetwork ID。
  48. IPAddress string `json:"ip_address"`
  49. }
  50. // ===========================================
  51. type SInstanceNic struct {
  52. instance *SInstance
  53. ipAddr string
  54. macAddr string
  55. cloudprovider.DummyICloudNic
  56. }
  57. func (self *SInstanceNic) GetId() string {
  58. return ""
  59. }
  60. func (self *SInstanceNic) GetIP() string {
  61. return self.ipAddr
  62. }
  63. func (self *SInstanceNic) GetMAC() string {
  64. return self.macAddr
  65. }
  66. func (self *SInstanceNic) GetDriver() string {
  67. return "virtio"
  68. }
  69. func (self *SInstanceNic) InClassicNetwork() bool {
  70. return false
  71. }
  72. func (self *SInstanceNic) GetINetworkId() string {
  73. instanceId := self.instance.GetId()
  74. subnets, err := self.instance.host.zone.region.getSubnetIdsByInstanceId(instanceId)
  75. if err != nil || len(subnets) == 0 {
  76. log.Errorf("getSubnetIdsByInstanceId error: %s", err.Error())
  77. return ""
  78. }
  79. return subnets[0]
  80. }
  81. func (self *SRegion) getSubnetIdsByInstanceId(instanceId string) ([]string, error) {
  82. ctx := &modules.SManagerContext{InstanceManager: self.ecsClient.NovaServers, InstanceId: instanceId}
  83. interfaces := make([]Interface, 0)
  84. err := DoListInContext(self.ecsClient.Interface.ListInContext, ctx, nil, &interfaces)
  85. if err != nil {
  86. return nil, err
  87. }
  88. subnets := make([]string, 0)
  89. for _, i := range interfaces {
  90. subnets = append(subnets, i.NetID)
  91. }
  92. return subnets, nil
  93. }