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