instancenic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 openstack
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/regutils"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. )
  21. type SInstanceNic struct {
  22. MacAddr string `json:"OS-EXT-IPS-MAC:mac_addr"`
  23. Version int `json:"version"`
  24. Addr string `json:"addr"`
  25. Type string `json:"OS-EXT-IPS:type"`
  26. }
  27. type SFixedIp struct {
  28. IpAddress string
  29. SubnetId string
  30. }
  31. type SInstancePort struct {
  32. region *SRegion
  33. FixedIps []SFixedIp
  34. MacAddr string
  35. NetId string
  36. PortId string
  37. PortState string
  38. cloudprovider.DummyICloudNic
  39. }
  40. func (region *SRegion) GetInstancePorts(instanceId string) ([]SInstancePort, error) {
  41. resource := fmt.Sprintf("/servers/%s/os-interface", instanceId)
  42. resp, err := region.ecsList(resource, nil)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "ecsList")
  45. }
  46. ports := []SInstancePort{}
  47. err = resp.Unmarshal(&ports, "interfaceAttachments")
  48. if err != nil {
  49. return nil, errors.Wrap(err, "resp.Unmarshal")
  50. }
  51. return ports, nil
  52. }
  53. func (nic *SInstancePort) GetId() string {
  54. return ""
  55. }
  56. func (nic *SInstancePort) GetIP() string {
  57. for i := range nic.FixedIps {
  58. if regutils.MatchIPAddr(nic.FixedIps[i].IpAddress) {
  59. return nic.FixedIps[i].IpAddress
  60. }
  61. }
  62. return ""
  63. }
  64. func (nic *SInstancePort) GetMAC() string {
  65. return nic.MacAddr
  66. }
  67. func (nic *SInstancePort) GetDriver() string {
  68. return "virtio"
  69. }
  70. func (nic *SInstancePort) InClassicNetwork() bool {
  71. return false
  72. }
  73. func (nic *SInstancePort) GetINetworkId() string {
  74. for i := range nic.FixedIps {
  75. if regutils.MatchIPAddr(nic.FixedIps[i].IpAddress) {
  76. network, err := nic.region.GetNetwork(nic.FixedIps[i].SubnetId)
  77. if err != nil {
  78. continue
  79. }
  80. pools := network.AllocationPools
  81. for j := range pools {
  82. if !pools[j].IsValid() {
  83. continue
  84. }
  85. network.AllocationPools = []AllocationPool{pools[j]}
  86. if network.Contains(nic.FixedIps[i].IpAddress) {
  87. return network.GetGlobalId()
  88. }
  89. }
  90. }
  91. }
  92. return ""
  93. }