github.com/aiven/aiven-go-client@v1.36.0/vpc_peering_connection.go (about)

     1  package aiven
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  type (
     8  	// VPCPeeringConnectionsHandler is the client that interacts with the VPC
     9  	// Peering Connections API on Aiven.
    10  	VPCPeeringConnectionsHandler struct {
    11  		client *Client
    12  	}
    13  
    14  	// CreateVPCPeeringConnectionRequest holds the parameters to create a new
    15  	// peering connection.
    16  	CreateVPCPeeringConnectionRequest struct {
    17  		PeerCloudAccount     string   `json:"peer_cloud_account"`
    18  		PeerVPC              string   `json:"peer_vpc"`
    19  		PeerRegion           *string  `json:"peer_region,omitempty"`
    20  		PeerAzureAppId       string   `json:"peer_azure_app_id,omitempty"`
    21  		PeerAzureTenantId    string   `json:"peer_azure_tenant_id,omitempty"`
    22  		PeerResourceGroup    string   `json:"peer_resource_group,omitempty"`
    23  		UserPeerNetworkCIDRs []string `json:"user_peer_network_cidrs,omitempty"`
    24  	}
    25  )
    26  
    27  // Create the given VPC on Aiven.
    28  // when CreateVPCPeeringConnectionRequest.PeerRegion == nil the PeerVPC must be in the
    29  // same region as the project VPC (vpcID)
    30  func (h *VPCPeeringConnectionsHandler) Create(
    31  	project string,
    32  	vpcID string,
    33  	req CreateVPCPeeringConnectionRequest,
    34  ) (*VPCPeeringConnection, error) {
    35  	path := buildPath("project", project, "vpcs", vpcID, "peering-connections")
    36  	rsp, err := h.client.doPostRequest(path, req)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var response *VPCPeeringConnection
    42  	if err := json.Unmarshal(rsp, &response); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// if region was not set in the request omit it from the response
    47  	if req.PeerRegion == nil {
    48  		response.PeerRegion = nil
    49  	}
    50  
    51  	return response, nil
    52  }
    53  
    54  // GetVPCPeering Connection from Aiven.
    55  // if peerRegion == nil the peered VPC is assumed to be in the same region as the project VPC (vpcID)
    56  func (h *VPCPeeringConnectionsHandler) GetVPCPeering(
    57  	project string,
    58  	vpcID string,
    59  	peerCloudAccount string,
    60  	peerVPC string,
    61  	peerRegion *string,
    62  ) (*VPCPeeringConnection, error) {
    63  	// There's no API call for getting individual peering connection. Get the VPC
    64  	// info and filter from there
    65  	vpc, err := h.client.VPCs.Get(project, vpcID)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	for _, pc := range vpc.PeeringConnections {
    71  		if (peerRegion == nil || eqStrPointers(pc.PeerRegion, peerRegion)) && pc.PeerCloudAccount == peerCloudAccount && pc.PeerVPC == peerVPC {
    72  			return pc, nil
    73  		}
    74  	}
    75  
    76  	err = Error{Message: "Peering connection not found", Status: 404}
    77  	return nil, err
    78  }
    79  
    80  // GetVPCPeeringWithResourceGroup retrieves a VPC peering connection
    81  func (h *VPCPeeringConnectionsHandler) GetVPCPeeringWithResourceGroup(
    82  	project string,
    83  	vpcID string,
    84  	peerCloudAccount string,
    85  	peerVPC string,
    86  	peerRegion *string,
    87  	peerResourceGroup *string,
    88  ) (*VPCPeeringConnection, error) {
    89  	// There's no API call for getting individual peering connection. Get the VPC
    90  	// info and filter from there
    91  	vpc, err := h.client.VPCs.Get(project, vpcID)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	for _, pc := range vpc.PeeringConnections {
    97  		found := pc.PeerCloudAccount == peerCloudAccount &&
    98  			pc.PeerVPC == peerVPC &&
    99  			// Not given or equal
   100  			(peerRegion == nil || eqStrPointers(pc.PeerRegion, peerRegion)) &&
   101  			(peerResourceGroup == nil || eqStrPointers(pc.PeerResourceGroup, peerResourceGroup))
   102  
   103  		if found {
   104  			return pc, nil
   105  		}
   106  	}
   107  
   108  	err = Error{Message: "Peering connection not found", Status: 404}
   109  	return nil, err
   110  }
   111  
   112  // Get a VPC Peering Connection from Aiven.
   113  func (h *VPCPeeringConnectionsHandler) Get(
   114  	project string,
   115  	vpcID string,
   116  	peerCloudAccount string,
   117  	peerVPC string,
   118  ) (*VPCPeeringConnection, error) {
   119  	return h.GetVPCPeering(project, vpcID, peerCloudAccount, peerVPC, nil)
   120  }
   121  
   122  // DeleteVPCPeering Connection from Aiven.
   123  // If peerRegion == nil the peering VPC must be in the same region as project VPC (vpcID)
   124  func (h *VPCPeeringConnectionsHandler) DeleteVPCPeering(project, vpcID, peerCloudAccount, peerVPC string, peerRegion *string) error {
   125  	pathElements := []string{"project", project, "vpcs", vpcID, "peering-connections", "peer-accounts", peerCloudAccount, "peer-vpcs", peerVPC}
   126  	if peerRegion != nil {
   127  		pathElements = append(pathElements, "peer-regions", *peerRegion)
   128  	}
   129  
   130  	bts, err := h.client.doDeleteRequest(buildPath(pathElements...), nil)
   131  	if err != nil {
   132  		return err
   133  	}
   134  
   135  	return checkAPIResponse(bts, nil)
   136  }
   137  
   138  // DeleteVPCPeeringWithResourceGroup deletes a VPC peering connection
   139  func (h *VPCPeeringConnectionsHandler) DeleteVPCPeeringWithResourceGroup(project, vpcID, peerCloudAccount, peerVPC, peerResourceGroup string, peerRegion *string) error {
   140  	pathElements := []string{"project", project,
   141  		"vpcs", vpcID,
   142  		"peering-connections", "peer-accounts", peerCloudAccount,
   143  		"peer-resource-groups", peerResourceGroup,
   144  		"peer-vpcs", peerVPC,
   145  	}
   146  	if peerRegion != nil {
   147  		pathElements = append(pathElements, "peer-regions", *peerRegion)
   148  	}
   149  
   150  	bts, err := h.client.doDeleteRequest(buildPath(pathElements...), nil)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	return checkAPIResponse(bts, nil)
   156  
   157  }
   158  
   159  // Delete the given VPC Peering Connection from Aiven.
   160  func (h *VPCPeeringConnectionsHandler) Delete(project, vpcID, peerCloudAccount, peerVPC string) error {
   161  	return h.DeleteVPCPeering(project, vpcID, peerCloudAccount, peerVPC, nil)
   162  }
   163  
   164  // List all VPC peering connections for a given VPC.
   165  func (h *VPCPeeringConnectionsHandler) List(project, vpcID string) ([]*VPCPeeringConnection, error) {
   166  	vpc, err := h.client.VPCs.Get(project, vpcID)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	return vpc.PeeringConnections, nil
   172  }
   173  
   174  func eqStrPointers(a, b *string) bool {
   175  	if a != nil && b != nil {
   176  		return *a == *b
   177  	}
   178  	// one or both of them nil
   179  	return a == b
   180  }