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

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  // HealthCheck represents a health check response from the Fastly API.
    11  type HealthCheck struct {
    12  	ServiceID      string `mapstructure:"service_id"`
    13  	ServiceVersion int    `mapstructure:"version"`
    14  
    15  	Name             string     `mapstructure:"name"`
    16  	Comment          string     `mapstructure:"comment"`
    17  	Method           string     `mapstructure:"method"`
    18  	Headers          []string   `mapstructure:"headers"`
    19  	Host             string     `mapstructure:"host"`
    20  	Path             string     `mapstructure:"path"`
    21  	HTTPVersion      string     `mapstructure:"http_version"`
    22  	Timeout          uint       `mapstructure:"timeout"`
    23  	CheckInterval    uint       `mapstructure:"check_interval"`
    24  	ExpectedResponse uint       `mapstructure:"expected_response"`
    25  	Window           uint       `mapstructure:"window"`
    26  	Threshold        uint       `mapstructure:"threshold"`
    27  	Initial          uint       `mapstructure:"initial"`
    28  	CreatedAt        *time.Time `mapstructure:"created_at"`
    29  	UpdatedAt        *time.Time `mapstructure:"updated_at"`
    30  	DeletedAt        *time.Time `mapstructure:"deleted_at"`
    31  }
    32  
    33  // healthChecksByName is a sortable list of health checks.
    34  type healthChecksByName []*HealthCheck
    35  
    36  // Len, Swap, and Less implement the sortable interface.
    37  func (s healthChecksByName) Len() int      { return len(s) }
    38  func (s healthChecksByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    39  func (s healthChecksByName) Less(i, j int) bool {
    40  	return s[i].Name < s[j].Name
    41  }
    42  
    43  // ListHealthChecksInput is used as input to the ListHealthChecks function.
    44  type ListHealthChecksInput struct {
    45  	// ServiceID is the ID of the service (required).
    46  	ServiceID string
    47  
    48  	// ServiceVersion is the specific configuration version (required).
    49  	ServiceVersion int
    50  }
    51  
    52  // ListHealthChecks returns the list of health checks for the configuration
    53  // version.
    54  func (c *Client) ListHealthChecks(i *ListHealthChecksInput) ([]*HealthCheck, error) {
    55  	if i.ServiceID == "" {
    56  		return nil, ErrMissingServiceID
    57  	}
    58  
    59  	if i.ServiceVersion == 0 {
    60  		return nil, ErrMissingServiceVersion
    61  	}
    62  
    63  	path := fmt.Sprintf("/service/%s/version/%d/healthcheck", i.ServiceID, i.ServiceVersion)
    64  	resp, err := c.Get(path, nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	var hcs []*HealthCheck
    70  	if err := decodeBodyMap(resp.Body, &hcs); err != nil {
    71  		return nil, err
    72  	}
    73  	sort.Stable(healthChecksByName(hcs))
    74  	return hcs, nil
    75  }
    76  
    77  // CreateHealthCheckInput is used as input to the CreateHealthCheck function.
    78  type CreateHealthCheckInput struct {
    79  	// ServiceID is the ID of the service (required).
    80  	ServiceID string
    81  
    82  	// ServiceVersion is the specific configuration version (required).
    83  	ServiceVersion int
    84  
    85  	Name             string   `url:"name,omitempty"`
    86  	Comment          string   `url:"comment,omitempty"`
    87  	Method           string   `url:"method,omitempty"`
    88  	Headers          []string `url:"headers,omitempty"`
    89  	Host             string   `url:"host,omitempty"`
    90  	Path             string   `url:"path,omitempty"`
    91  	HTTPVersion      string   `url:"http_version,omitempty"`
    92  	Timeout          *uint    `url:"timeout,omitempty"`
    93  	CheckInterval    *uint    `url:"check_interval,omitempty"`
    94  	ExpectedResponse *uint    `url:"expected_response,omitempty"`
    95  	Window           *uint    `url:"window,omitempty"`
    96  	Threshold        *uint    `url:"threshold,omitempty"`
    97  	Initial          *uint    `url:"initial,omitempty"`
    98  }
    99  
   100  // CreateHealthCheck creates a new Fastly health check.
   101  func (c *Client) CreateHealthCheck(i *CreateHealthCheckInput) (*HealthCheck, error) {
   102  	if i.ServiceID == "" {
   103  		return nil, ErrMissingServiceID
   104  	}
   105  
   106  	if i.ServiceVersion == 0 {
   107  		return nil, ErrMissingServiceVersion
   108  	}
   109  
   110  	ro := new(RequestOptions)
   111  	ro.HealthCheckHeaders = true
   112  
   113  	path := fmt.Sprintf("/service/%s/version/%d/healthcheck", i.ServiceID, i.ServiceVersion)
   114  	resp, err := c.PostForm(path, i, ro)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	defer resp.Body.Close()
   119  
   120  	var h *HealthCheck
   121  	if err := decodeBodyMap(resp.Body, &h); err != nil {
   122  		return nil, err
   123  	}
   124  	return h, nil
   125  }
   126  
   127  // GetHealthCheckInput is used as input to the GetHealthCheck function.
   128  type GetHealthCheckInput struct {
   129  	// ServiceID is the ID of the service (required).
   130  	ServiceID string
   131  
   132  	// ServiceVersion is the specific configuration version (required).
   133  	ServiceVersion int
   134  
   135  	// Name is the name of the health check to fetch.
   136  	Name string
   137  }
   138  
   139  // GetHealthCheck gets the health check configuration with the given parameters.
   140  func (c *Client) GetHealthCheck(i *GetHealthCheckInput) (*HealthCheck, error) {
   141  	if i.ServiceID == "" {
   142  		return nil, ErrMissingServiceID
   143  	}
   144  
   145  	if i.ServiceVersion == 0 {
   146  		return nil, ErrMissingServiceVersion
   147  	}
   148  
   149  	if i.Name == "" {
   150  		return nil, ErrMissingName
   151  	}
   152  
   153  	path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   154  	resp, err := c.Get(path, nil)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	defer resp.Body.Close()
   159  
   160  	var h *HealthCheck
   161  	if err := decodeBodyMap(resp.Body, &h); err != nil {
   162  		return nil, err
   163  	}
   164  	return h, nil
   165  }
   166  
   167  // UpdateHealthCheckInput is used as input to the UpdateHealthCheck function.
   168  type UpdateHealthCheckInput struct {
   169  	// ServiceID is the ID of the service (required).
   170  	ServiceID string
   171  
   172  	// ServiceVersion is the specific configuration version (required).
   173  	ServiceVersion int
   174  
   175  	// Name is the name of the health check to update.
   176  	Name string
   177  
   178  	NewName          *string   `url:"name,omitempty"`
   179  	Comment          *string   `url:"comment,omitempty"`
   180  	Method           *string   `url:"method,omitempty"`
   181  	Headers          *[]string `url:"headers,omitempty"`
   182  	Host             *string   `url:"host,omitempty"`
   183  	Path             *string   `url:"path,omitempty"`
   184  	HTTPVersion      *string   `url:"http_version,omitempty"`
   185  	Timeout          *uint     `url:"timeout,omitempty"`
   186  	CheckInterval    *uint     `url:"check_interval,omitempty"`
   187  	ExpectedResponse *uint     `url:"expected_response,omitempty"`
   188  	Window           *uint     `url:"window,omitempty"`
   189  	Threshold        *uint     `url:"threshold,omitempty"`
   190  	Initial          *uint     `url:"initial,omitempty"`
   191  }
   192  
   193  // UpdateHealthCheck updates a specific health check.
   194  func (c *Client) UpdateHealthCheck(i *UpdateHealthCheckInput) (*HealthCheck, error) {
   195  	if i.ServiceID == "" {
   196  		return nil, ErrMissingServiceID
   197  	}
   198  
   199  	if i.ServiceVersion == 0 {
   200  		return nil, ErrMissingServiceVersion
   201  	}
   202  
   203  	if i.Name == "" {
   204  		return nil, ErrMissingName
   205  	}
   206  
   207  	ro := new(RequestOptions)
   208  	ro.HealthCheckHeaders = true
   209  
   210  	path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   211  	resp, err := c.PutForm(path, i, ro)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  	defer resp.Body.Close()
   216  
   217  	var h *HealthCheck
   218  	if err := decodeBodyMap(resp.Body, &h); err != nil {
   219  		return nil, err
   220  	}
   221  	return h, nil
   222  }
   223  
   224  // DeleteHealthCheckInput is the input parameter to DeleteHealthCheck.
   225  type DeleteHealthCheckInput struct {
   226  	// ServiceID is the ID of the service (required).
   227  	ServiceID string
   228  
   229  	// ServiceVersion is the specific configuration version (required).
   230  	ServiceVersion int
   231  
   232  	// Name is the name of the health check to delete (required).
   233  	Name string
   234  }
   235  
   236  // DeleteHealthCheck deletes the given health check.
   237  func (c *Client) DeleteHealthCheck(i *DeleteHealthCheckInput) error {
   238  	if i.ServiceID == "" {
   239  		return ErrMissingServiceID
   240  	}
   241  
   242  	if i.ServiceVersion == 0 {
   243  		return ErrMissingServiceVersion
   244  	}
   245  
   246  	if i.Name == "" {
   247  		return ErrMissingName
   248  	}
   249  
   250  	path := fmt.Sprintf("/service/%s/version/%d/healthcheck/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   251  	resp, err := c.Delete(path, nil)
   252  	if err != nil {
   253  		return err
   254  	}
   255  	defer resp.Body.Close()
   256  
   257  	var r *statusResp
   258  	if err := decodeBodyMap(resp.Body, &r); err != nil {
   259  		return err
   260  	}
   261  	if !r.Ok() {
   262  		return ErrNotOK
   263  	}
   264  	return nil
   265  }