wire.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "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. OpenStackTags
  24. vpc *SVpc
  25. }
  26. func (wire *SWire) GetId() string {
  27. return wire.vpc.GetId()
  28. }
  29. func (wire *SWire) GetName() string {
  30. return wire.GetId()
  31. }
  32. func (wire *SWire) IsEmulated() bool {
  33. return true
  34. }
  35. func (wire *SWire) GetStatus() string {
  36. return api.WIRE_STATUS_AVAILABLE
  37. }
  38. func (wire *SWire) Refresh() error {
  39. return nil
  40. }
  41. func (wire *SWire) GetGlobalId() string {
  42. return wire.vpc.GetGlobalId()
  43. }
  44. func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc {
  45. return wire.vpc
  46. }
  47. func (wire *SWire) GetIZone() cloudprovider.ICloudZone {
  48. return nil
  49. }
  50. func (wire *SWire) GetBandwidth() int {
  51. return 10000
  52. }
  53. func (wire *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  54. network, err := wire.vpc.region.CreateNetwork(wire.vpc.Id, opts.ProjectId, opts.Name, opts.Cidr, opts.Desc)
  55. if err != nil {
  56. return nil, errors.Wrap(err, "CreateNetwork")
  57. }
  58. network.wire = wire
  59. return network, nil
  60. }
  61. func (wire *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  62. networks, err := wire.GetINetworks()
  63. if err != nil {
  64. return nil, err
  65. }
  66. for i := 0; i < len(networks); i++ {
  67. if networks[i].GetGlobalId() == netid {
  68. return networks[i], nil
  69. }
  70. }
  71. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", netid)
  72. }
  73. func (wire *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  74. _networks, err := wire.vpc.region.GetNetworks(wire.vpc.Id)
  75. if err != nil {
  76. return nil, errors.Wrapf(err, "GetNetworks(%s)", wire.vpc.Id)
  77. }
  78. networks := []SNetwork{}
  79. for i := range _networks {
  80. pools := _networks[i].AllocationPools
  81. for j := range pools {
  82. if !pools[j].IsValid() {
  83. continue
  84. }
  85. _networks[i].AllocationPools = []AllocationPool{pools[j]}
  86. networks = append(networks, _networks[i])
  87. }
  88. }
  89. inetworks := []cloudprovider.ICloudNetwork{}
  90. for i := range networks {
  91. networks[i].wire = wire
  92. inetworks = append(inetworks, &networks[i])
  93. }
  94. return inetworks, nil
  95. }