wire.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 ctyun
  15. import (
  16. "yunion.io/x/pkg/errors"
  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. type SWire struct {
  22. multicloud.SResourceBase
  23. CtyunTags
  24. vpc *SVpc
  25. }
  26. func (self *SWire) GetId() string {
  27. return self.vpc.GetId()
  28. }
  29. func (self *SWire) GetName() string {
  30. return self.vpc.GetName()
  31. }
  32. func (self *SWire) GetGlobalId() string {
  33. return self.vpc.GetGlobalId()
  34. }
  35. func (self *SWire) GetStatus() string {
  36. return api.WIRE_STATUS_AVAILABLE
  37. }
  38. func (self *SWire) IsEmulated() bool {
  39. return true
  40. }
  41. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  42. return self.vpc
  43. }
  44. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  45. return nil
  46. }
  47. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  48. networks, err := self.vpc.region.GetNetwroks(self.vpc.VpcId)
  49. if err != nil {
  50. return nil, err
  51. }
  52. ret := []cloudprovider.ICloudNetwork{}
  53. for i := range networks {
  54. networks[i].wire = self
  55. ret = append(ret, &networks[i])
  56. }
  57. return ret, nil
  58. }
  59. func (self *SWire) GetBandwidth() int {
  60. return 10000
  61. }
  62. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  63. networks, err := self.GetINetworks()
  64. if err != nil {
  65. return nil, err
  66. }
  67. for i := 0; i < len(networks); i += 1 {
  68. if networks[i].GetGlobalId() == netid {
  69. return networks[i], nil
  70. }
  71. }
  72. return nil, cloudprovider.ErrNotFound
  73. }
  74. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  75. network, err := self.vpc.region.CreateNetwork(self.vpc.VpcId, opts)
  76. if err != nil {
  77. return nil, errors.Wrap(err, "CreateNetwork")
  78. }
  79. network.wire = self
  80. return network, nil
  81. }