vpc.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "net/url"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type AddressSpace struct {
  24. AddressPrefixes []string `json:"addressPrefixes,omitempty"`
  25. }
  26. type VirtualNetworkPropertiesFormat struct {
  27. ProvisioningState string
  28. Status string
  29. VirtualNetworkSiteName string
  30. AddressSpace AddressSpace `json:"addressSpace,omitempty"`
  31. Subnets []SNetwork `json:"subnets,omitempty"`
  32. }
  33. type SVpc struct {
  34. multicloud.SVpc
  35. AzureTags
  36. region *SRegion
  37. ID string
  38. Name string
  39. Etag string
  40. Type string
  41. Location string
  42. Properties VirtualNetworkPropertiesFormat `json:"properties,omitempty"`
  43. }
  44. func (self *SVpc) GetTags() (map[string]string, error) {
  45. return self.Tags, nil
  46. }
  47. func (self *SVpc) GetId() string {
  48. return self.ID
  49. }
  50. func (self *SVpc) GetName() string {
  51. return self.Name
  52. }
  53. func (self *SVpc) GetGlobalId() string {
  54. return strings.ToLower(self.ID)
  55. }
  56. func (self *SVpc) IsEmulated() bool {
  57. return false
  58. }
  59. func (self *SVpc) GetIsDefault() bool {
  60. return true
  61. }
  62. func (self *SVpc) GetCidrBlock() string {
  63. if len(self.Properties.AddressSpace.AddressPrefixes) > 0 {
  64. return self.Properties.AddressSpace.AddressPrefixes[0]
  65. }
  66. return ""
  67. }
  68. func (self *SVpc) Delete() error {
  69. return self.region.DeleteVpc(self.ID)
  70. }
  71. func (self *SRegion) DeleteVpc(vpcId string) error {
  72. return self.del(vpcId)
  73. }
  74. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  75. return []cloudprovider.ICloudSecurityGroup{}, nil
  76. }
  77. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  78. rts := []cloudprovider.ICloudRouteTable{}
  79. return rts, nil
  80. }
  81. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  82. return nil, cloudprovider.ErrNotSupported
  83. }
  84. func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  85. wire := self.getWire()
  86. if wire.GetGlobalId() != wireId {
  87. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", wireId)
  88. }
  89. return wire, nil
  90. }
  91. func (self *SVpc) getWire() *SWire {
  92. zone := self.region.getZone()
  93. return &SWire{zone: zone, vpc: self}
  94. }
  95. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  96. return []cloudprovider.ICloudWire{self.getWire()}, nil
  97. }
  98. func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
  99. return self.region
  100. }
  101. func (self *SVpc) GetStatus() string {
  102. if strings.ToLower(self.Properties.ProvisioningState) == "succeeded" {
  103. return "available"
  104. }
  105. return "disabled"
  106. }
  107. func (region *SRegion) GetVpc(vpcId string) (*SVpc, error) {
  108. vpc := SVpc{region: region}
  109. return &vpc, region.get(vpcId, url.Values{}, &vpc)
  110. }
  111. func (self *SVpc) Refresh() error {
  112. vpc, err := self.region.GetVpc(self.ID)
  113. if err != nil {
  114. return err
  115. }
  116. return jsonutils.Update(self, vpc)
  117. }
  118. func (self *SVpc) GetNetworks() []SNetwork {
  119. return self.Properties.Subnets
  120. }
  121. func (self *SRegion) GetNetwork(networkId string) (*SNetwork, error) {
  122. network := SNetwork{}
  123. return &network, self.get(networkId, url.Values{}, &network)
  124. }