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

     1  package aiven
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  )
     7  
     8  type (
     9  	// VPCPeeringConnection holds parameters associated with a VPC peering connection
    10  	VPCPeeringConnection struct {
    11  		CreateTime               *string                 `json:"create_time"`
    12  		PeerCloudAccount         string                  `json:"peer_cloud_account"`
    13  		PeerVPC                  string                  `json:"peer_vpc"`
    14  		PeerRegion               *string                 `json:"peer_region"`
    15  		State                    string                  `json:"state"`
    16  		UpdateTime               string                  `json:"update_time"`
    17  		StateInfo                *map[string]interface{} `json:"state_info"`
    18  		PeerAzureAppId           string                  `json:"peer_azure_app_id,omitempty"`
    19  		PeerAzureTenantId        string                  `json:"peer_azure_tenant_id,omitempty"`
    20  		UserPeerNetworkCIDRs     []string                `json:"user_peer_network_cidrs,omitempty"`
    21  		VPCPeeringConnectionType string                  `json:"vpc_peering_connection_type,omitempty"`
    22  		PeerResourceGroup        *string                 `json:"peer_resource_group,omitempty"`
    23  	}
    24  
    25  	// VPC holds parameters associated with a Virtual Private Cloud
    26  	VPC struct {
    27  		CloudName          string                  `json:"cloud_name"`
    28  		CreateTime         *string                 `json:"create_time"`
    29  		NetworkCIDR        string                  `json:"network_cidr"`
    30  		ProjectVPCID       string                  `json:"project_vpc_id"`
    31  		State              string                  `json:"state"`
    32  		UpdateTime         string                  `json:"update_time"`
    33  		PeeringConnections []*VPCPeeringConnection `json:"peering_connections"`
    34  	}
    35  
    36  	// VPCsHandler is the client that interacts with the VPCs API on Aiven.
    37  	VPCsHandler struct {
    38  		client *Client
    39  	}
    40  
    41  	// CreateVPCRequest holds the parameters to create a new VPC.
    42  	CreateVPCRequest struct {
    43  		CloudName          string                  `json:"cloud_name"`
    44  		NetworkCIDR        string                  `json:"network_cidr"`
    45  		PeeringConnections []*VPCPeeringConnection `json:"peering_connections"`
    46  	}
    47  
    48  	// VPCListResponse represents the response from Aiven for listing VPCs.
    49  	VPCListResponse struct {
    50  		APIResponse
    51  		VPCs []*VPC `json:"vpcs"`
    52  	}
    53  )
    54  
    55  // Create the given VPC on Aiven.
    56  func (h *VPCsHandler) Create(project string, req CreateVPCRequest) (*VPC, error) {
    57  	path := buildPath("project", project, "vpcs")
    58  	if req.PeeringConnections == nil {
    59  		req.PeeringConnections = []*VPCPeeringConnection{}
    60  	}
    61  	rsp, err := h.client.doPostRequest(path, req)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return parseVPCResponse(rsp)
    67  }
    68  
    69  // Get a specific VPC from Aiven.
    70  func (h *VPCsHandler) Get(project, vpcID string) (*VPC, error) {
    71  	path := buildPath("project", project, "vpcs", vpcID)
    72  	rsp, err := h.client.doGetRequest(path, nil)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return parseVPCResponse(rsp)
    78  }
    79  
    80  // Delete the given VPC from Aiven.
    81  func (h *VPCsHandler) Delete(project, vpcID string) error {
    82  	path := buildPath("project", project, "vpcs", vpcID)
    83  	bts, err := h.client.doDeleteRequest(path, nil)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	return checkAPIResponse(bts, nil)
    89  }
    90  
    91  // List all VPCs for a given project.
    92  func (h *VPCsHandler) List(project string) ([]*VPC, error) {
    93  	path := buildPath("project", project, "vpcs")
    94  	rsp, err := h.client.doGetRequest(path, nil)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	var response *VPCListResponse
   100  	if err := json.Unmarshal(rsp, &response); err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	if len(response.Errors) != 0 {
   105  		return nil, errors.New(response.Message)
   106  	}
   107  
   108  	return response.VPCs, nil
   109  }
   110  
   111  func parseVPCResponse(rsp []byte) (*VPC, error) {
   112  	var response *VPC
   113  	if err := json.Unmarshal(rsp, &response); err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return response, nil
   118  }