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 nutanix
  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. multicloud.STagBase
  24. vpc *SVpc
  25. }
  26. func (self *SWire) GetName() string {
  27. return self.vpc.GetName()
  28. }
  29. func (self *SWire) GetId() string {
  30. return self.vpc.GetId()
  31. }
  32. func (self *SWire) GetGlobalId() string {
  33. return self.vpc.GetGlobalId()
  34. }
  35. func (self *SWire) IsEmulated() bool {
  36. return true
  37. }
  38. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  39. network, err := self.vpc.region.CreateNetwork(self.vpc.UUID, opts)
  40. if err != nil {
  41. return nil, errors.Wrapf(err, "CreateNetwork")
  42. }
  43. return network, nil
  44. }
  45. func (self *SWire) GetBandwidth() int {
  46. return 10000
  47. }
  48. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  49. ret := []cloudprovider.ICloudNetwork{}
  50. if len(self.vpc.IPConfig.Pool) == 0 {
  51. network := &SNetwork{wire: self}
  52. ret = append(ret, network)
  53. }
  54. for _, pool := range self.vpc.IPConfig.Pool {
  55. network := &SNetwork{wire: self}
  56. network.Range = pool.Range
  57. ret = append(ret, network)
  58. }
  59. return ret, nil
  60. }
  61. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  62. networks, err := self.GetINetworks()
  63. if err != nil {
  64. return nil, err
  65. }
  66. for i := range networks {
  67. if networks[i].GetGlobalId() == netid {
  68. return networks[i], nil
  69. }
  70. }
  71. return nil, cloudprovider.ErrNotFound
  72. }
  73. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  74. return self.vpc
  75. }
  76. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  77. cluster := SCluster{}
  78. err := self.vpc.region.get("cluster", "", nil, &cluster)
  79. if err != nil {
  80. return nil
  81. }
  82. return &SZone{SCluster: cluster, region: self.vpc.region}
  83. }
  84. func (self *SWire) GetStatus() string {
  85. return api.WIRE_STATUS_AVAILABLE
  86. }