wire.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 apsara
  15. import (
  16. "fmt"
  17. "yunion.io/x/log"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SWire struct {
  23. multicloud.SResourceBase
  24. ApsaraTags
  25. zone *SZone
  26. vpc *SVpc
  27. inetworks []cloudprovider.ICloudNetwork
  28. }
  29. func (self *SWire) GetId() string {
  30. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.zone.GetId())
  31. }
  32. func (self *SWire) GetName() string {
  33. return self.GetId()
  34. }
  35. func (self *SWire) IsEmulated() bool {
  36. return true
  37. }
  38. func (self *SWire) GetStatus() string {
  39. return api.WIRE_STATUS_AVAILABLE
  40. }
  41. func (self *SWire) Refresh() error {
  42. return nil
  43. }
  44. func (self *SWire) GetGlobalId() string {
  45. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.zone.GetGlobalId())
  46. }
  47. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  48. return self.vpc
  49. }
  50. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  51. return self.zone
  52. }
  53. func (self *SWire) addNetwork(vswitch *SVSwitch) {
  54. if self.inetworks == nil {
  55. self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
  56. }
  57. find := false
  58. for i := 0; i < len(self.inetworks); i += 1 {
  59. if self.inetworks[i].GetId() == vswitch.VSwitchId {
  60. find = true
  61. break
  62. }
  63. }
  64. if !find {
  65. self.inetworks = append(self.inetworks, vswitch)
  66. }
  67. }
  68. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  69. if self.inetworks == nil {
  70. err := self.vpc.fetchVSwitches()
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. return self.inetworks, nil
  76. }
  77. func (self *SWire) getNetworkById(vswitchId string) *SVSwitch {
  78. networks, err := self.GetINetworks()
  79. if err != nil {
  80. return nil
  81. }
  82. log.Debugf("search for networks %d", len(networks))
  83. for i := 0; i < len(networks); i += 1 {
  84. log.Debugf("search %s", networks[i].GetName())
  85. network := networks[i].(*SVSwitch)
  86. if network.VSwitchId == vswitchId {
  87. return network
  88. }
  89. }
  90. return nil
  91. }
  92. func (self *SWire) GetBandwidth() int {
  93. return 10000
  94. }
  95. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  96. vswitchId, err := self.zone.region.createVSwitch(self.zone.ZoneId, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
  97. if err != nil {
  98. log.Errorf("createVSwitch error %s", err)
  99. return nil, err
  100. }
  101. self.inetworks = nil
  102. vswitch := self.getNetworkById(vswitchId)
  103. if vswitch == nil {
  104. log.Errorf("cannot find vswitch after create????")
  105. return nil, cloudprovider.ErrNotFound
  106. }
  107. return vswitch, nil
  108. }
  109. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  110. networks, err := self.GetINetworks()
  111. if err != nil {
  112. return nil, err
  113. }
  114. for i := 0; i < len(networks); i += 1 {
  115. if networks[i].GetGlobalId() == netid {
  116. return networks[i], nil
  117. }
  118. }
  119. return nil, cloudprovider.ErrNotFound
  120. }