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

     1  package aiven
     2  
     3  import "fmt"
     4  
     5  type (
     6  	// StaticIPsHandler aiven go-client handler for static ips
     7  	StaticIPsHandler struct {
     8  		client *Client
     9  	}
    10  
    11  	// CreateStaticIPRequest Aiven API request
    12  	// POST https://api.aiven.io/v1/project/<project>/static-ips
    13  	CreateStaticIPRequest struct {
    14  		CloudName string `json:"cloud_name"`
    15  	}
    16  
    17  	// CreateStaticIPResponse Aiven API response
    18  	// POST https://api.aiven.io/v1/project/<project>/static-ips
    19  	CreateStaticIPResponse struct {
    20  		APIResponse
    21  
    22  		StaticIP
    23  	}
    24  
    25  	// DeleteStaticIPRequest Aiven API request
    26  	// DELETE https://api.aiven.io/v1/project/<project>/static-ips/<static_ip_address_id>
    27  	DeleteStaticIPRequest struct {
    28  		StaticIPAddressID string `json:"static_ip_address_id"`
    29  	}
    30  
    31  	// ListStaticIPResponse Aiven API response
    32  	// GET https://api.aiven.io/v1/project/<project>/static-ips
    33  	ListStaticIPResponse struct {
    34  		APIResponse
    35  
    36  		StaticIPs []StaticIP `json:"static_ips"`
    37  	}
    38  
    39  	// AssociateStaticIPRequest Aiven API request
    40  	// POST https://api.aiven.io/v1/project/<project>/static-ips/<static-ip>/association
    41  	AssociateStaticIPRequest struct {
    42  		ServiceName string `json:"service_name"`
    43  	}
    44  
    45  	// StaticIP shared fields by API responses
    46  	StaticIP struct {
    47  		CloudName         string `json:"cloud_name"`
    48  		IPAddress         string `json:"ip_address"`
    49  		ServiceName       string `json:"service_name"`
    50  		State             string `json:"state"`
    51  		StaticIPAddressID string `json:"static_ip_address_id"`
    52  	}
    53  )
    54  
    55  // Create creates a static ip
    56  func (h *StaticIPsHandler) Create(project string, req CreateStaticIPRequest) (*CreateStaticIPResponse, error) {
    57  	path := buildPath("project", project, "static-ips")
    58  	bts, err := h.client.doPostRequest(path, req)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	var r CreateStaticIPResponse
    64  	errR := checkAPIResponse(bts, &r)
    65  
    66  	return &r, errR
    67  }
    68  
    69  // Delete deletes a static ip
    70  func (h *StaticIPsHandler) Delete(project string, req DeleteStaticIPRequest) error {
    71  	path := buildPath("project", project, "static-ips", req.StaticIPAddressID)
    72  	bts, err := h.client.doDeleteRequest(path, nil)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	return checkAPIResponse(bts, nil)
    78  }
    79  
    80  // Get retrieves a Static IP
    81  // NOTE: API does not support GET /v1/project/{project}/static-ips/{static-ip-id},
    82  // need to fetch all, filter by ID and fake a 404 if nothing is found
    83  func (h *StaticIPsHandler) Get(project, staticIPID string) (*StaticIP, error) {
    84  	staticIPs, err := h.List(project)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	var targetStaticIP StaticIP
    90  
    91  	for _, staticIP := range staticIPs.StaticIPs {
    92  		if staticIP.StaticIPAddressID == staticIPID {
    93  			targetStaticIP = staticIP
    94  		}
    95  	}
    96  
    97  	if targetStaticIP.StaticIPAddressID == "" {
    98  		return nil, Error{
    99  			Message: fmt.Sprintf("static ip not found by id:%s", staticIPID),
   100  			Status:  404,
   101  		}
   102  	}
   103  	return &targetStaticIP, nil
   104  }
   105  
   106  // List lists all static ips
   107  func (h *StaticIPsHandler) List(project string) (*ListStaticIPResponse, error) {
   108  	path := buildPath("project", project, "static-ips")
   109  	bts, err := h.client.doGetRequest(path, nil)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	var r ListStaticIPResponse
   115  	errR := checkAPIResponse(bts, &r)
   116  
   117  	return &r, errR
   118  }
   119  
   120  func (h *StaticIPsHandler) Associate(project, staticIPID string, req AssociateStaticIPRequest) error {
   121  	path := buildPath("project", project, "static-ips", staticIPID, "association")
   122  	rsp, err := h.client.doPostRequest(path, req)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	return checkAPIResponse(rsp, nil)
   128  }
   129  
   130  func (h *StaticIPsHandler) Dissociate(project, staticIPID string) error {
   131  	path := buildPath("project", project, "static-ips", staticIPID, "association")
   132  	rsp, err := h.client.doDeleteRequest(path, nil)
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	return checkAPIResponse(rsp, nil)
   138  }