wire.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 ucloud
  15. import (
  16. "fmt"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. )
  21. // 子网在整个region可用
  22. type SWire struct {
  23. multicloud.SResourceBase
  24. UcloudTags
  25. region *SRegion
  26. vpc *SVPC
  27. inetworks []cloudprovider.ICloudNetwork
  28. }
  29. func (self *SWire) GetId() string {
  30. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.region.GetId())
  31. }
  32. func (self *SWire) GetName() string {
  33. return self.GetId()
  34. }
  35. func (self *SWire) GetGlobalId() string {
  36. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.region.GetGlobalId())
  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) IsEmulated() bool {
  45. return true
  46. }
  47. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  48. return self.vpc
  49. }
  50. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  51. return nil
  52. }
  53. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  54. if self.inetworks == nil {
  55. err := self.vpc.fetchNetworks()
  56. if err != nil {
  57. return nil, err
  58. }
  59. }
  60. return self.inetworks, nil
  61. }
  62. func (self *SWire) GetBandwidth() int {
  63. return 10000
  64. }
  65. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  66. networks, err := self.GetINetworks()
  67. if err != nil {
  68. return nil, err
  69. }
  70. for i := 0; i < len(networks); i += 1 {
  71. if networks[i].GetGlobalId() == netid {
  72. return networks[i], nil
  73. }
  74. }
  75. return nil, cloudprovider.ErrNotFound
  76. }
  77. // https://docs.ucloud.cn/api/vpc2.0-api/create_subnet
  78. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  79. return self.region.CreateNetwork(self.vpc.GetId(), opts.Name, opts.Cidr, opts.Desc)
  80. }
  81. func (self *SWire) addNetwork(network *SNetwork) {
  82. if self.inetworks == nil {
  83. self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
  84. }
  85. find := false
  86. for i := 0; i < len(self.inetworks); i += 1 {
  87. if self.inetworks[i].GetId() == network.GetId() {
  88. find = true
  89. break
  90. }
  91. }
  92. if !find {
  93. self.inetworks = append(self.inetworks, network)
  94. }
  95. }