wire.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 qcloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  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. QcloudTags
  25. zone *SZone
  26. vpc *SVpc
  27. }
  28. func (self *SWire) GetId() string {
  29. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.zone.GetId())
  30. }
  31. func (self *SWire) GetName() string {
  32. return self.GetId()
  33. }
  34. func (self *SWire) IsEmulated() bool {
  35. return true
  36. }
  37. func (self *SWire) GetStatus() string {
  38. return api.WIRE_STATUS_AVAILABLE
  39. }
  40. func (self *SWire) Refresh() error {
  41. return nil
  42. }
  43. func (self *SWire) GetGlobalId() string {
  44. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.zone.GetGlobalId())
  45. }
  46. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  47. return self.vpc
  48. }
  49. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  50. return self.zone
  51. }
  52. func (self *SWire) GetBandwidth() int {
  53. return 10000
  54. }
  55. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  56. networkId, err := self.zone.region.CreateNetwork(self.zone.Zone, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
  57. if err != nil {
  58. return nil, errors.Wrapf(err, "CreateNetwork")
  59. }
  60. network, err := self.vpc.region.GetNetwork(networkId)
  61. if err != nil {
  62. return nil, errors.Wrapf(err, "GetNetwork")
  63. }
  64. network.wire = self
  65. return network, nil
  66. }
  67. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  68. network, err := self.zone.region.GetNetwork(netid)
  69. if err != nil {
  70. return nil, err
  71. }
  72. network.wire = self
  73. return network, nil
  74. }
  75. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  76. networks, err := self.zone.region.GetNetworks(nil, self.vpc.VpcId, self.zone.Zone)
  77. if err != nil {
  78. return nil, err
  79. }
  80. ret := []cloudprovider.ICloudNetwork{}
  81. for i := range networks {
  82. networks[i].wire = self
  83. ret = append(ret, &networks[i])
  84. }
  85. return ret, nil
  86. }