vpc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 google
  15. import (
  16. "fmt"
  17. "time"
  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 SVpc struct {
  25. multicloud.SVpc
  26. GoogleTags
  27. SResourceBase
  28. region *SRegion
  29. CreationTimestamp time.Time
  30. Network string
  31. IpCidrRange string
  32. Region string
  33. GatewayAddress string
  34. Status string
  35. AvailableCpuPlatforms []string
  36. PrivateIpGoogleAccess bool
  37. Fingerprint string
  38. Purpose string
  39. Kind string
  40. }
  41. func (self *SVpc) GetGlobalVpcId() string {
  42. gvpc := &SGlobalNetwork{}
  43. err := self.region.GetBySelfId(self.Network, gvpc)
  44. if err != nil {
  45. return ""
  46. }
  47. return gvpc.Id
  48. }
  49. func (self *SVpc) Refresh() error {
  50. vpc, err := self.region.GetVpc(self.Id)
  51. if err != nil {
  52. return errors.Wrapf(err, "GetVpc")
  53. }
  54. return jsonutils.Update(self, vpc)
  55. }
  56. func (self *SRegion) GetVpc(id string) (*SVpc, error) {
  57. vpc := &SVpc{region: self}
  58. return vpc, self.Get("subnetworks", id, vpc)
  59. }
  60. func (vpc *SVpc) GetStatus() string {
  61. return api.VPC_STATUS_AVAILABLE
  62. }
  63. func (vpc *SVpc) Delete() error {
  64. return vpc.region.Delete(vpc.SelfLink)
  65. }
  66. func (vpc *SVpc) GetCidrBlock() string {
  67. return vpc.IpCidrRange
  68. }
  69. func (vpc *SVpc) IsEmulated() bool {
  70. return false
  71. }
  72. func (vpc *SVpc) GetIsDefault() bool {
  73. return false
  74. }
  75. func (vpc *SVpc) GetRegion() cloudprovider.ICloudRegion {
  76. return vpc.region
  77. }
  78. func (vpc *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  79. return nil, cloudprovider.ErrNotImplemented
  80. }
  81. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  82. return nil, cloudprovider.ErrNotSupported
  83. }
  84. func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  85. return []cloudprovider.ICloudSecurityGroup{}, nil
  86. }
  87. func (vpc *SVpc) getWire() *SWire {
  88. return &SWire{vpc: vpc}
  89. }
  90. func (vpc *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  91. wire := vpc.getWire()
  92. return []cloudprovider.ICloudWire{wire}, nil
  93. }
  94. func (vpc *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) {
  95. if id != vpc.getWire().GetGlobalId() {
  96. return nil, cloudprovider.ErrNotFound
  97. }
  98. return &SWire{vpc: vpc}, nil
  99. }
  100. func (self *SRegion) CreateVpc(name string, gvpcId string, cidr string, desc string) (*SVpc, error) {
  101. body := map[string]interface{}{
  102. "name": normalizeString(name),
  103. "description": desc,
  104. "network": gvpcId,
  105. "ipCidrRange": cidr,
  106. }
  107. resource := fmt.Sprintf("regions/%s/subnetworks", self.Name)
  108. vpc := &SVpc{region: self}
  109. err := self.Insert(resource, jsonutils.Marshal(body), vpc)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return vpc, nil
  114. }
  115. func (self *SRegion) GetVpcs() ([]SVpc, error) {
  116. vpcs := []SVpc{}
  117. resource := fmt.Sprintf("regions/%s/subnetworks", self.Name)
  118. return vpcs, self.List(resource, nil, 0, "", &vpcs)
  119. }