yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/vpc_peering.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 hcs 16 17 import ( 18 "fmt" 19 "net/url" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 28 ) 29 30 type RequestVpcInfo struct { 31 VpcId string `json:"vpc_id"` 32 TenantId string `json:"tenant_id"` 33 } 34 35 type AcceptVpcInfo struct { 36 VpcId string `json:"vpc_id"` 37 TenantId string `json:"tenant_id"` 38 } 39 40 type SVpcPeering struct { 41 multicloud.SResourceBase 42 huawei.HuaweiTags 43 vpc *SVpc 44 45 RequestVpcInfo RequestVpcInfo `json:"request_vpc_info"` 46 AcceptVpcInfo AcceptVpcInfo `json:"accept_vpc_info"` 47 Name string `json:"name"` 48 Id string `json:"id"` 49 Status string `json:"status"` 50 } 51 52 func (self *SRegion) GetVpcPeerings(vpcId string) ([]SVpcPeering, error) { 53 params := url.Values{} 54 if len(vpcId) > 0 { 55 params.Set("vpc_id", vpcId) 56 } 57 ret := []SVpcPeering{} 58 return ret, self.list("vpc", "v2.0", "vpc/peerings", params, &ret) 59 } 60 61 func (self *SRegion) GetVpcPeering(id string) (*SVpcPeering, error) { 62 ret := SVpcPeering{} 63 resource := fmt.Sprintf("vpc/peerings/%s", id) 64 return &ret, self.get("vpc", "v2.0", resource, &ret) 65 } 66 67 func (self *SRegion) CreateVpcPeering(vpcId string, opts *cloudprovider.VpcPeeringConnectionCreateOptions) (*SVpcPeering, error) { 68 params := map[string]interface{}{ 69 "peering": map[string]interface{}{ 70 "name": opts.Name, 71 "request_vpc_info": map[string]interface{}{ 72 "vpc_id": vpcId, 73 "tenant_id": self.client.projectId, 74 }, 75 "accept_vpc_info": map[string]interface{}{ 76 "vpc_id": opts.PeerVpcId, 77 "tenant_id": opts.PeerAccountId, 78 }, 79 }, 80 } 81 ret := &SVpcPeering{} 82 return ret, self.create("vpc", "v2.0", "vpc/peerings", params, ret) 83 } 84 85 func (self *SRegion) AcceptVpcPeering(id string) error { 86 res := fmt.Sprintf("vpc/peerings/%s/accept", id) 87 return self.update("vpc", "v2.0", res, nil) 88 } 89 90 func (self *SRegion) DeleteVpcPeering(id string) error { 91 res := fmt.Sprintf("vpc/peerings/%s", id) 92 return self.delete("vpc", "v2.0", res) 93 } 94 95 func (self *SVpcPeering) GetId() string { 96 return self.Id 97 } 98 99 func (self *SVpcPeering) GetName() string { 100 return self.Name 101 } 102 103 func (self *SVpcPeering) GetGlobalId() string { 104 return self.GetId() 105 } 106 107 func (self *SVpcPeering) GetStatus() string { 108 switch self.Status { 109 case "PENDING_ACCEPTANCE": 110 return api.VPC_PEERING_CONNECTION_STATUS_PENDING_ACCEPT 111 case "ACTIVE": 112 return api.VPC_PEERING_CONNECTION_STATUS_ACTIVE 113 default: 114 return api.VPC_PEERING_CONNECTION_STATUS_UNKNOWN 115 } 116 } 117 118 func (self *SVpcPeering) Refresh() error { 119 peer, err := self.vpc.region.GetVpcPeering(self.Id) 120 if err != nil { 121 return errors.Wrapf(err, "self.region.GetVpcPeering(%s)", self.Id) 122 } 123 return jsonutils.Update(self, peer) 124 } 125 126 func (self *SVpcPeering) GetVpcId() string { 127 return self.RequestVpcInfo.VpcId 128 } 129 130 func (self *SVpcPeering) GetPeerVpcId() string { 131 return self.AcceptVpcInfo.VpcId 132 } 133 134 func (self *SVpcPeering) GetPeerAccountId() string { 135 return self.AcceptVpcInfo.TenantId 136 } 137 138 func (self *SVpcPeering) GetEnabled() bool { 139 return true 140 } 141 142 func (self *SVpcPeering) Delete() error { 143 return self.vpc.region.DeleteVpcPeering(self.Id) 144 }