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

     1  package aiven
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type (
     8  	// ConnectionPoolsHandler is the client which interacts with the connection pool endpoints
     9  	// on Aiven.
    10  	ConnectionPoolsHandler struct {
    11  		client *Client
    12  	}
    13  
    14  	// CreateConnectionPoolRequest are the parameters used to create a connection pool entry.
    15  	CreateConnectionPoolRequest struct {
    16  		Database string  `json:"database"`
    17  		PoolMode string  `json:"pool_mode"`
    18  		PoolName string  `json:"pool_name"`
    19  		PoolSize int     `json:"pool_size"`
    20  		Username *string `json:"username,omitempty"`
    21  	}
    22  
    23  	// UpdateConnectionPoolRequest are the parameters used to update a connection pool entry.
    24  	UpdateConnectionPoolRequest struct {
    25  		Database string  `json:"database"`
    26  		PoolMode string  `json:"pool_mode"`
    27  		PoolSize int     `json:"pool_size"`
    28  		Username *string `json:"username"`
    29  	}
    30  )
    31  
    32  // Create new connection pool entry.
    33  func (h *ConnectionPoolsHandler) Create(
    34  	project string,
    35  	serviceName string,
    36  	req CreateConnectionPoolRequest,
    37  ) (*ConnectionPool, error) {
    38  	path := buildPath("project", project, "service", serviceName, "connection_pool")
    39  	_, err := h.client.doPostRequest(path, req)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	// Server doesn't return the connection pool we created, need to fetch it separately.
    45  	return h.Get(project, serviceName, req.PoolName)
    46  }
    47  
    48  // Get a specific connection pool.
    49  func (h *ConnectionPoolsHandler) Get(project, serviceName, poolName string) (*ConnectionPool, error) {
    50  	// There's no API for getting individual connection pool entry. List instead and filter from there
    51  	pools, err := h.List(project, serviceName)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	for _, pool := range pools {
    57  		if pool.PoolName == poolName {
    58  			return pool, nil
    59  		}
    60  	}
    61  
    62  	err = Error{Message: fmt.Sprintf("Connection pool with name %v not found", poolName), Status: 404}
    63  	return nil, err
    64  }
    65  
    66  // List returns all the connection pool entries for a given service.
    67  func (h *ConnectionPoolsHandler) List(project, serviceName string) ([]*ConnectionPool, error) {
    68  	// There's no API for listing connection pool entries. Need to get them from
    69  	// service info instead
    70  	service, err := h.client.Services.Get(project, serviceName)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return service.ConnectionPools, nil
    76  }
    77  
    78  // Update a specific connection pool with the given parameters.
    79  func (h *ConnectionPoolsHandler) Update(
    80  	project string,
    81  	serviceName string,
    82  	poolName string,
    83  	req UpdateConnectionPoolRequest,
    84  ) (*ConnectionPool, error) {
    85  	path := buildPath("project", project, "service", serviceName, "connection_pool", poolName)
    86  	_, err := h.client.doPutRequest(path, req)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	// Server doesn't return the connection pool we updated, need to fetch it separately.
    92  	return h.Get(project, serviceName, poolName)
    93  }
    94  
    95  // Delete removes the specified connection pool entry.
    96  func (h *ConnectionPoolsHandler) Delete(project, serviceName, poolName string) error {
    97  	path := buildPath("project", project, "service", serviceName, "connection_pool", poolName)
    98  	bts, err := h.client.doDeleteRequest(path, nil)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return checkAPIResponse(bts, nil)
   104  }