wire.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 azure
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SWire struct {
  25. multicloud.SResourceBase
  26. AzureTags
  27. zone *SZone
  28. vpc *SVpc
  29. inetworks []cloudprovider.ICloudNetwork
  30. }
  31. func (self *SWire) GetId() string {
  32. return fmt.Sprintf("%s/%s/%s", self.zone.region.GetGlobalId(), self.zone.region.client.subscriptionId, self.vpc.GetName())
  33. }
  34. func (self *SWire) GetGlobalId() string {
  35. return strings.ToLower(self.GetId())
  36. }
  37. func (self *SWire) GetName() string {
  38. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.vpc.GetName())
  39. }
  40. func (self *SWire) IsEmulated() bool {
  41. return true
  42. }
  43. func (self *SWire) GetStatus() string {
  44. return api.WIRE_STATUS_AVAILABLE
  45. }
  46. func (self *SRegion) CreateNetwork(vpcId, name string, cidr string, desc string) (*SNetwork, error) {
  47. params := map[string]interface{}{
  48. "Name": name,
  49. "Properties": map[string]interface{}{
  50. "AddressPrefix": cidr,
  51. },
  52. }
  53. resource := fmt.Sprintf("%s/subnets/%s", vpcId, name)
  54. network := &SNetwork{}
  55. resp, err := self.put(resource, jsonutils.Marshal(params))
  56. if err != nil {
  57. return nil, errors.Wrapf(err, "put(%s)", resource)
  58. }
  59. err = resp.Unmarshal(network)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "resp.Unmarshal")
  62. }
  63. return network, nil
  64. }
  65. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  66. network, err := self.zone.region.CreateNetwork(self.vpc.ID, opts.Name, opts.Cidr, opts.Desc)
  67. if err != nil {
  68. return nil, err
  69. }
  70. network.wire = self
  71. return network, nil
  72. }
  73. func (self *SWire) GetBandwidth() int {
  74. return 10000
  75. }
  76. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  77. networks, err := self.GetINetworks()
  78. if err != nil {
  79. return nil, err
  80. }
  81. for i := 0; i < len(networks); i += 1 {
  82. if networks[i].GetGlobalId() == strings.ToLower(netid) {
  83. return networks[i], nil
  84. }
  85. }
  86. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", netid)
  87. }
  88. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  89. ret := []cloudprovider.ICloudNetwork{}
  90. networks := self.vpc.GetNetworks()
  91. for i := range networks {
  92. networks[i].wire = self
  93. ret = append(ret, &networks[i])
  94. }
  95. return ret, nil
  96. }
  97. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  98. return self.vpc
  99. }
  100. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  101. return self.zone
  102. }