yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/vpc.go (about) 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 15 package google 16 17 import ( 18 "fmt" 19 "time" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 "yunion.io/x/pkg/utils" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 ) 29 30 type SVpc struct { 31 multicloud.SVpc 32 GoogleTags 33 SResourceBase 34 35 region *SRegion 36 37 CreationTimestamp time.Time 38 Network string 39 IpCidrRange string 40 Region string 41 GatewayAddress string 42 Status string 43 AvailableCpuPlatforms []string 44 PrivateIpGoogleAccess bool 45 Fingerprint string 46 Purpose string 47 Kind string 48 } 49 50 func (self *SVpc) GetGlobalVpcId() string { 51 gvpc := &SGlobalNetwork{} 52 err := self.region.GetBySelfId(self.Network, gvpc) 53 if err != nil { 54 return "" 55 } 56 return gvpc.Id 57 } 58 59 func (self *SVpc) Refresh() error { 60 vpc, err := self.region.GetVpc(self.Id) 61 if err != nil { 62 return errors.Wrapf(err, "GetVpc") 63 } 64 return jsonutils.Update(self, vpc) 65 } 66 67 func (self *SRegion) GetVpc(id string) (*SVpc, error) { 68 vpc := &SVpc{region: self} 69 return vpc, self.Get("subnetworks", id, vpc) 70 } 71 72 func (vpc *SVpc) GetStatus() string { 73 return api.VPC_STATUS_AVAILABLE 74 } 75 76 func (vpc *SVpc) Delete() error { 77 return vpc.region.Delete(vpc.SelfLink) 78 } 79 80 func (vpc *SVpc) GetCidrBlock() string { 81 return vpc.IpCidrRange 82 } 83 84 func (vpc *SVpc) IsEmulated() bool { 85 return false 86 } 87 88 func (vpc *SVpc) GetIsDefault() bool { 89 return false 90 } 91 92 func (vpc *SVpc) GetRegion() cloudprovider.ICloudRegion { 93 return vpc.region 94 } 95 96 func (vpc *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { 97 return nil, cloudprovider.ErrNotImplemented 98 } 99 100 func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) { 101 return nil, cloudprovider.ErrNotSupported 102 } 103 104 func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) { 105 firewalls, err := vpc.region.client.GetFirewalls(vpc.Network, 0, "") 106 if err != nil { 107 return nil, errors.Wrap(err, "GetFirewalls") 108 } 109 gvpc := &SGlobalNetwork{client: vpc.region.client} 110 err = vpc.region.GetBySelfId(vpc.Network, gvpc) 111 if err != nil { 112 return nil, errors.Wrapf(err, "GetGlobalNetwork") 113 } 114 isecgroups := []cloudprovider.ICloudSecurityGroup{} 115 allInstance := false 116 tags := []string{} 117 for _, firewall := range firewalls { 118 if len(firewall.TargetServiceAccounts) > 0 { 119 secgroup := &SSecurityGroup{gvpc: gvpc, ServiceAccount: firewall.TargetServiceAccounts[0]} 120 isecgroups = append(isecgroups, secgroup) 121 } else if len(firewall.TargetTags) > 0 && !utils.IsInStringArray(firewall.TargetTags[0], tags) { 122 secgroup := &SSecurityGroup{gvpc: gvpc, Tag: firewall.TargetTags[0]} 123 tags = append(tags, firewall.TargetTags[0]) 124 isecgroups = append(isecgroups, secgroup) 125 } else if !allInstance { 126 secgroup := &SSecurityGroup{gvpc: gvpc} 127 isecgroups = append(isecgroups, secgroup) 128 allInstance = true 129 } 130 } 131 return isecgroups, nil 132 } 133 134 func (vpc *SVpc) getWire() *SWire { 135 return &SWire{vpc: vpc} 136 } 137 138 func (vpc *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) { 139 wire := vpc.getWire() 140 return []cloudprovider.ICloudWire{wire}, nil 141 } 142 143 func (vpc *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) { 144 if id != vpc.getWire().GetGlobalId() { 145 return nil, cloudprovider.ErrNotFound 146 } 147 return &SWire{vpc: vpc}, nil 148 } 149 150 func (self *SRegion) CreateVpc(name string, gvpcId string, cidr string, desc string) (*SVpc, error) { 151 body := map[string]interface{}{ 152 "name": name, 153 "description": desc, 154 "network": gvpcId, 155 "ipCidrRange": cidr, 156 } 157 resource := fmt.Sprintf("regions/%s/subnetworks", self.Name) 158 vpc := &SVpc{region: self} 159 err := self.Insert(resource, jsonutils.Marshal(body), vpc) 160 if err != nil { 161 return nil, err 162 } 163 return vpc, nil 164 } 165 166 func (self *SRegion) GetVpcs() ([]SVpc, error) { 167 vpcs := []SVpc{} 168 resource := fmt.Sprintf("regions/%s/subnetworks", self.Name) 169 return vpcs, self.List(resource, nil, 0, "", &vpcs) 170 }