github.com/fastly/go-fastly/v6@v6.8.0/fastly/pool.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  const (
    11  	// PoolTypeRandom is a pool that does random direction.
    12  	PoolTypeRandom PoolType = "random"
    13  
    14  	// PoolTypeHash is a pool that does hash direction.
    15  	PoolTypeHash PoolType = "hash"
    16  
    17  	// PoolTypeClient ins a pool that does client direction.
    18  	PoolTypeClient PoolType = "client"
    19  )
    20  
    21  // PoolType is a type of pool.
    22  type PoolType string
    23  
    24  // PPoolType returns pointer to PoolType.
    25  func PPoolType(t PoolType) *PoolType {
    26  	pt := PoolType(t)
    27  	return &pt
    28  }
    29  
    30  // Pool represents a pool response from the Fastly API.
    31  type Pool struct {
    32  	ServiceID      string `mapstructure:"service_id"`
    33  	ServiceVersion int    `mapstructure:"version"`
    34  
    35  	ID               string     `mapstructure:"id"`
    36  	Name             string     `mapstructure:"name"`
    37  	Comment          string     `mapstructure:"comment"`
    38  	Shield           string     `mapstructure:"shield"`
    39  	RequestCondition string     `mapstructure:"request_condition"`
    40  	MaxConnDefault   uint       `mapstructure:"max_conn_default"`
    41  	ConnectTimeout   uint       `mapstructure:"connect_timeout"`
    42  	FirstByteTimeout uint       `mapstructure:"first_byte_timeout"`
    43  	Quorum           uint       `mapstructure:"quorum"`
    44  	UseTLS           bool       `mapstructure:"use_tls"`
    45  	TLSCACert        string     `mapstructure:"tls_ca_cert"`
    46  	TLSCiphers       string     `mapstructure:"tls_ciphers"`
    47  	TLSClientKey     string     `mapstructure:"tls_client_key"`
    48  	TLSClientCert    string     `mapstructure:"tls_client_cert"`
    49  	TLSSNIHostname   string     `mapstructure:"tls_sni_hostname"`
    50  	TLSCheckCert     bool       `mapstructure:"tls_check_cert"`
    51  	TLSCertHostname  string     `mapstructure:"tls_cert_hostname"`
    52  	MinTLSVersion    string     `mapstructure:"min_tls_version"`
    53  	MaxTLSVersion    string     `mapstructure:"max_tls_version"`
    54  	Healthcheck      string     `mapstructure:"healthcheck"`
    55  	Type             PoolType   `mapstructure:"type"`
    56  	OverrideHost     string     `mapstructure:"override_host"`
    57  	CreatedAt        *time.Time `mapstructure:"created_at"`
    58  	DeletedAt        *time.Time `mapstructure:"deleted_at"`
    59  	UpdatedAt        *time.Time `mapstructure:"updated_at"`
    60  }
    61  
    62  // poolsByName is a sortable list of pools.
    63  type poolsByName []*Pool
    64  
    65  // Len, Swap, and Less implement the sortable interface.
    66  func (s poolsByName) Len() int      { return len(s) }
    67  func (s poolsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    68  func (s poolsByName) Less(i, j int) bool {
    69  	return s[i].Name < s[j].Name
    70  }
    71  
    72  // ListPoolsInput is used as input to the ListPools function.
    73  type ListPoolsInput struct {
    74  	// ServiceID is the ID of the service (required).
    75  	ServiceID string
    76  
    77  	// ServiceVersion is the specific configuration version (required).
    78  	ServiceVersion int
    79  }
    80  
    81  // ListPools lists all pools for a particular service and version.
    82  func (c *Client) ListPools(i *ListPoolsInput) ([]*Pool, error) {
    83  	if i.ServiceID == "" {
    84  		return nil, ErrMissingServiceID
    85  	}
    86  
    87  	if i.ServiceVersion == 0 {
    88  		return nil, ErrMissingServiceVersion
    89  	}
    90  
    91  	path := fmt.Sprintf("/service/%s/version/%d/pool", i.ServiceID, i.ServiceVersion)
    92  	resp, err := c.Get(path, nil)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	defer resp.Body.Close()
    97  
    98  	var ps []*Pool
    99  	if err := decodeBodyMap(resp.Body, &ps); err != nil {
   100  		return nil, err
   101  	}
   102  	sort.Stable(poolsByName(ps))
   103  	return ps, nil
   104  }
   105  
   106  // CreatePoolInput is used as input to the CreatePool function.
   107  type CreatePoolInput struct {
   108  	// ServiceID is the ID of the service (required).
   109  	ServiceID string
   110  
   111  	// ServiceVersion is the specific configuration version (required).
   112  	ServiceVersion int
   113  
   114  	// Name is the name of the pool to create (required).
   115  	Name string `url:"name"`
   116  
   117  	// Optional fields.
   118  	Comment          string      `url:"comment,omitempty"`
   119  	Shield           string      `url:"shield,omitempty"`
   120  	RequestCondition string      `url:"request_condition,omitempty"`
   121  	MaxConnDefault   uint        `url:"max_conn_default,omitempty"`
   122  	ConnectTimeout   uint        `url:"connect_timeout,omitempty"`
   123  	FirstByteTimeout uint        `url:"first_byte_timeout,omitempty"`
   124  	Quorum           uint        `url:"quorum,omitempty"`
   125  	UseTLS           Compatibool `url:"use_tls,omitempty"`
   126  	TLSCACert        string      `url:"tls_ca_cert,omitempty"`
   127  	TLSCiphers       string      `url:"tls_ciphers,omitempty"`
   128  	TLSClientKey     string      `url:"tls_client_key,omitempty"`
   129  	TLSClientCert    string      `url:"tls_client_cert,omitempty"`
   130  	TLSSNIHostname   string      `url:"tls_sni_hostname,omitempty"`
   131  	TLSCheckCert     Compatibool `url:"tls_check_cert,omitempty"`
   132  	TLSCertHostname  string      `url:"tls_cert_hostname,omitempty"`
   133  	MinTLSVersion    string      `url:"min_tls_version,omitempty"`
   134  	MaxTLSVersion    string      `url:"max_tls_version,omitempty"`
   135  	Healthcheck      string      `url:"healthcheck,omitempty"`
   136  	Type             PoolType    `url:"type,omitempty"`
   137  	OverrideHost     string      `url:"override_host,omitempty"`
   138  }
   139  
   140  // CreatePool creates a pool for a particular service and version.
   141  func (c *Client) CreatePool(i *CreatePoolInput) (*Pool, error) {
   142  	if i.ServiceID == "" {
   143  		return nil, ErrMissingServiceID
   144  	}
   145  
   146  	if i.ServiceVersion == 0 {
   147  		return nil, ErrMissingServiceVersion
   148  	}
   149  
   150  	if i.Name == "" {
   151  		return nil, ErrMissingName
   152  	}
   153  
   154  	path := fmt.Sprintf("/service/%s/version/%d/pool", i.ServiceID, i.ServiceVersion)
   155  	resp, err := c.PostForm(path, i, nil)
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  	defer resp.Body.Close()
   160  
   161  	var p *Pool
   162  	if err := decodeBodyMap(resp.Body, &p); err != nil {
   163  		return nil, err
   164  	}
   165  	return p, nil
   166  }
   167  
   168  // GetPoolInput is used as input to the GetPool function.
   169  type GetPoolInput struct {
   170  	// ServiceID is the ID of the service (required).
   171  	ServiceID string
   172  
   173  	// ServiceVersion is the specific configuration version (required).
   174  	ServiceVersion int
   175  
   176  	// Name is the name of the pool of interest (required).
   177  	Name string
   178  }
   179  
   180  // GetPool gets a single pool for a particular service and version.
   181  func (c *Client) GetPool(i *GetPoolInput) (*Pool, error) {
   182  	if i.ServiceID == "" {
   183  		return nil, ErrMissingServiceID
   184  	}
   185  
   186  	if i.ServiceVersion == 0 {
   187  		return nil, ErrMissingServiceVersion
   188  	}
   189  
   190  	if i.Name == "" {
   191  		return nil, ErrMissingName
   192  	}
   193  
   194  	path := fmt.Sprintf("/service/%s/version/%d/pool/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   195  	resp, err := c.Get(path, nil)
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	defer resp.Body.Close()
   200  
   201  	var p *Pool
   202  	if err := decodeBodyMap(resp.Body, &p); err != nil {
   203  		return nil, err
   204  	}
   205  	return p, nil
   206  }
   207  
   208  // UpdatePoolInput is used as input to the UpdatePool function.
   209  type UpdatePoolInput struct {
   210  	// ServiceID is the ID of the service (required).
   211  	ServiceID string
   212  
   213  	// ServiceVersion is the specific configuration version (required).
   214  	ServiceVersion int
   215  
   216  	// Name is the name of the pool to update (required).
   217  	Name string
   218  
   219  	// Optional fields.
   220  	NewName          *string      `url:"name,omitempty"`
   221  	Comment          *string      `url:"comment,omitempty"`
   222  	Shield           *string      `url:"shield,omitempty"`
   223  	RequestCondition *string      `url:"request_condition,omitempty"`
   224  	MaxConnDefault   *uint        `url:"max_conn_default,omitempty"`
   225  	ConnectTimeout   *uint        `url:"connect_timeout,omitempty"`
   226  	FirstByteTimeout *uint        `url:"first_byte_timeout,omitempty"`
   227  	Quorum           *uint        `url:"quorum,omitempty"`
   228  	UseTLS           *Compatibool `url:"use_tls,omitempty"`
   229  	TLSCACert        *string      `url:"tls_ca_cert,omitempty"`
   230  	TLSCiphers       *string      `url:"tls_ciphers,omitempty"`
   231  	TLSClientKey     *string      `url:"tls_client_key,omitempty"`
   232  	TLSClientCert    *string      `url:"tls_client_cert,omitempty"`
   233  	TLSSNIHostname   *string      `url:"tls_sni_hostname,omitempty"`
   234  	TLSCheckCert     *Compatibool `url:"tls_check_cert,omitempty"`
   235  	TLSCertHostname  *string      `url:"tls_cert_hostname,omitempty"`
   236  	MinTLSVersion    *string      `url:"min_tls_version,omitempty"`
   237  	MaxTLSVersion    *string      `url:"max_tls_version,omitempty"`
   238  	Healthcheck      *string      `url:"healthcheck,omitempty"`
   239  	Type             *PoolType    `url:"type,omitempty"`
   240  	OverrideHost     *string      `url:"override_host,omitempty"`
   241  }
   242  
   243  // UpdatePool updates a specufic pool for a particular service and version.
   244  func (c *Client) UpdatePool(i *UpdatePoolInput) (*Pool, error) {
   245  	if i.ServiceID == "" {
   246  		return nil, ErrMissingServiceID
   247  	}
   248  
   249  	if i.ServiceVersion == 0 {
   250  		return nil, ErrMissingServiceVersion
   251  	}
   252  
   253  	if i.Name == "" {
   254  		return nil, ErrMissingName
   255  	}
   256  
   257  	path := fmt.Sprintf("/service/%s/version/%d/pool/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   258  	resp, err := c.PutForm(path, i, nil)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  	defer resp.Body.Close()
   263  
   264  	var p *Pool
   265  	if err := decodeBodyMap(resp.Body, &p); err != nil {
   266  		return nil, err
   267  	}
   268  	return p, nil
   269  }
   270  
   271  // DeletePoolInput is used as input to the DeletePool function.
   272  type DeletePoolInput struct {
   273  	// ServiceID is the ID of the service (required).
   274  	ServiceID string
   275  
   276  	// ServiceVersion is the specific configuration version (required).
   277  	ServiceVersion int
   278  
   279  	// Name is the name of the pool to delete (required).
   280  	Name string
   281  }
   282  
   283  // DeletePool deletes a specific pool for a particular service and version.
   284  func (c *Client) DeletePool(i *DeletePoolInput) error {
   285  	if i.ServiceID == "" {
   286  		return ErrMissingServiceID
   287  	}
   288  
   289  	if i.ServiceVersion == 0 {
   290  		return ErrMissingServiceVersion
   291  	}
   292  
   293  	if i.Name == "" {
   294  		return ErrMissingName
   295  	}
   296  
   297  	path := fmt.Sprintf("/service/%s/version/%d/pool/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   298  	resp, err := c.Delete(path, nil)
   299  	if err != nil {
   300  		return err
   301  	}
   302  	defer resp.Body.Close()
   303  
   304  	var r *statusResp
   305  	if err := decodeBodyMap(resp.Body, &r); err != nil {
   306  		return err
   307  	}
   308  	if !r.Ok() {
   309  		return ErrNotOK
   310  	}
   311  	return nil
   312  }