yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 21 api "yunion.io/x/cloudmux/pkg/apis/compute" 22 "yunion.io/x/cloudmux/pkg/cloudprovider" 23 "yunion.io/x/cloudmux/pkg/multicloud" 24 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 25 ) 26 27 type RequestVpcInfo struct { 28 VpcID string `json:"vpc_id"` 29 TenantID string `json:"tenant_id"` 30 } 31 type AcceptVpcInfo struct { 32 VpcID string `json:"vpc_id"` 33 TenantID string `json:"tenant_id"` 34 } 35 type SVpcPeering struct { 36 multicloud.SResourceBase 37 huawei.HuaweiTags 38 vpc *SVpc 39 40 RequestVpcInfo RequestVpcInfo `json:"request_vpc_info"` 41 AcceptVpcInfo AcceptVpcInfo `json:"accept_vpc_info"` 42 Name string `json:"name"` 43 ID string `json:"id"` 44 Status string `json:"status"` 45 } 46 47 func (self *SRegion) GetVpcPeerings(vpcId string) ([]SVpcPeering, error) { 48 querys := make(map[string]string) 49 querys["vpc_id"] = vpcId 50 vpcPeerings := make([]SVpcPeering, 0) 51 err := doListAllWithMarker(self.ecsClient.VpcPeerings.List, querys, &vpcPeerings) 52 if err != nil { 53 return nil, errors.Wrapf(err, "oListAllWithMarker(self.ecsClient.VpcPeerings.List, %s, &vpcPeerings)", jsonutils.Marshal(querys).String()) 54 } 55 return vpcPeerings, nil 56 } 57 58 func (self *SRegion) GetVpcPeering(vpcPeeringId string) (*SVpcPeering, error) { 59 if len(vpcPeeringId) == 0 { 60 return nil, cloudprovider.ErrNotFound 61 } 62 vpcPeering := SVpcPeering{} 63 err := DoGet(self.ecsClient.VpcPeerings.Get, vpcPeeringId, nil, &vpcPeering) 64 if err != nil { 65 return nil, errors.Wrapf(err, "DoGet(self.ecsClient.VpcPeerings.Get, %s, nil, &vpcPeering)", vpcPeeringId) 66 } 67 return &vpcPeering, nil 68 } 69 70 func (self *SRegion) CreateVpcPeering(vpcId string, opts *cloudprovider.VpcPeeringConnectionCreateOptions) (*SVpcPeering, error) { 71 params := jsonutils.NewDict() 72 vpcPeeringObj := jsonutils.NewDict() 73 requestVpcObj := jsonutils.NewDict() 74 acceptVpcObj := jsonutils.NewDict() 75 vpcPeeringObj.Set("name", jsonutils.NewString(opts.Name)) 76 requestVpcObj.Set("vpc_id", jsonutils.NewString(vpcId)) 77 requestVpcObj.Set("tenant_id", jsonutils.NewString(self.client.projectId)) 78 vpcPeeringObj.Set("request_vpc_info", requestVpcObj) 79 acceptVpcObj.Set("vpc_id", jsonutils.NewString(opts.PeerVpcId)) 80 acceptVpcObj.Set("tenant_id", jsonutils.NewString(opts.PeerAccountId)) 81 vpcPeeringObj.Set("accept_vpc_info", acceptVpcObj) 82 params.Set("peering", vpcPeeringObj) 83 ret := SVpcPeering{} 84 err := DoCreate(self.ecsClient.VpcPeerings.Create, params, &ret) 85 if err != nil { 86 return nil, errors.Wrapf(err, "DoCreate(self.ecsClient.VpcPeerings.Create, %s, &ret)", jsonutils.Marshal(params).String()) 87 } 88 return &ret, nil 89 } 90 91 func (self *SRegion) AcceptVpcPeering(vpcPeeringId string) error { 92 err := DoUpdateWithSpec(self.ecsClient.VpcPeerings.UpdateInContextWithSpec, vpcPeeringId, "accept", nil) 93 if err != nil { 94 return errors.Wrapf(err, "DoUpdateWithSpec(self.ecsClient.VpcPeerings.UpdateInContextWithSpec, %s, accept, nil)", vpcPeeringId) 95 } 96 return nil 97 } 98 99 func (self *SRegion) DeleteVpcPeering(vpcPeeringId string) error { 100 err := DoDelete(self.ecsClient.VpcPeerings.Delete, vpcPeeringId, nil, nil) 101 if err != nil { 102 return errors.Wrapf(err, "DoDelete(self.ecsClient.VpcPeerings.Delete,%s,nil)", vpcPeeringId) 103 } 104 return nil 105 } 106 107 func (self *SVpcPeering) GetId() string { 108 return self.ID 109 } 110 111 func (self *SVpcPeering) GetName() string { 112 return self.Name 113 } 114 115 func (self *SVpcPeering) GetGlobalId() string { 116 return self.GetId() 117 } 118 119 func (self *SVpcPeering) GetStatus() string { 120 switch self.Status { 121 case "PENDING_ACCEPTANCE": 122 return api.VPC_PEERING_CONNECTION_STATUS_PENDING_ACCEPT 123 case "ACTIVE": 124 return api.VPC_PEERING_CONNECTION_STATUS_ACTIVE 125 default: 126 return api.VPC_PEERING_CONNECTION_STATUS_UNKNOWN 127 } 128 } 129 130 func (self *SVpcPeering) Refresh() error { 131 peer, err := self.vpc.region.GetVpcPeering(self.ID) 132 if err != nil { 133 return errors.Wrapf(err, "self.region.GetVpcPeering(%s)", self.ID) 134 } 135 return jsonutils.Update(self, peer) 136 } 137 138 func (self *SVpcPeering) GetVpcId() string { 139 return self.RequestVpcInfo.VpcID 140 } 141 142 func (self *SVpcPeering) GetPeerVpcId() string { 143 return self.AcceptVpcInfo.VpcID 144 } 145 146 func (self *SVpcPeering) GetPeerAccountId() string { 147 return self.AcceptVpcInfo.TenantID 148 } 149 150 func (self *SVpcPeering) GetEnabled() bool { 151 return true 152 } 153 154 func (self *SVpcPeering) Delete() error { 155 err := self.vpc.region.DeleteVpcPeering(self.ID) 156 if err != nil { 157 return errors.Wrapf(err, "self.region.DeleteVpcPeering(%s)", self.ID) 158 } 159 return nil 160 }