github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/monitors/requests.go (about)

     1  package monitors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToMonitorListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the API. Filtering is achieved by passing in struct field values that map to
    18  // the Monitor attributes you want to see returned. SortKey allows you to
    19  // sort by a particular Monitor attribute. SortDir sets the direction, and is
    20  // either `asc' or `desc'. Marker and Limit are used for pagination.
    21  type ListOpts struct {
    22  	ID             string `q:"id"`
    23  	Name           string `q:"name"`
    24  	TenantID       string `q:"tenant_id"`
    25  	ProjectID      string `q:"project_id"`
    26  	PoolID         string `q:"pool_id"`
    27  	Type           string `q:"type"`
    28  	Delay          int    `q:"delay"`
    29  	Timeout        int    `q:"timeout"`
    30  	MaxRetries     int    `q:"max_retries"`
    31  	MaxRetriesDown int    `q:"max_retries_down"`
    32  	HTTPMethod     string `q:"http_method"`
    33  	URLPath        string `q:"url_path"`
    34  	ExpectedCodes  string `q:"expected_codes"`
    35  	AdminStateUp   *bool  `q:"admin_state_up"`
    36  	Status         string `q:"status"`
    37  	Limit          int    `q:"limit"`
    38  	Marker         string `q:"marker"`
    39  	SortKey        string `q:"sort_key"`
    40  	SortDir        string `q:"sort_dir"`
    41  }
    42  
    43  // ToMonitorListQuery formats a ListOpts into a query string.
    44  func (opts ListOpts) ToMonitorListQuery() (string, error) {
    45  	q, err := gophercloud.BuildQueryString(opts)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  	return q.String(), nil
    50  }
    51  
    52  // List returns a Pager which allows you to iterate over a collection of
    53  // health monitors. It accepts a ListOpts struct, which allows you to filter and sort
    54  // the returned collection for greater efficiency.
    55  //
    56  // Default policy settings return only those health monitors that are owned by the
    57  // tenant who submits the request, unless an admin user submits the request.
    58  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    59  	url := rootURL(c)
    60  	if opts != nil {
    61  		query, err := opts.ToMonitorListQuery()
    62  		if err != nil {
    63  			return pagination.Pager{Err: err}
    64  		}
    65  		url += query
    66  	}
    67  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    68  		return MonitorPage{pagination.LinkedPageBase{PageResult: r}}
    69  	})
    70  }
    71  
    72  // Constants that represent approved monitoring types.
    73  const (
    74  	TypePING       = "PING"
    75  	TypeTCP        = "TCP"
    76  	TypeHTTP       = "HTTP"
    77  	TypeHTTPS      = "HTTPS"
    78  	TypeTLSHELLO   = "TLS-HELLO"
    79  	TypeUDPConnect = "UDP-CONNECT"
    80  	TypeSCTP       = "SCTP"
    81  )
    82  
    83  var (
    84  	errDelayMustGETimeout = fmt.Errorf("Delay must be greater than or equal to timeout")
    85  )
    86  
    87  // CreateOptsBuilder allows extensions to add additional parameters to the
    88  // List request.
    89  type CreateOptsBuilder interface {
    90  	ToMonitorCreateMap() (map[string]interface{}, error)
    91  }
    92  
    93  // CreateOpts is the common options struct used in this package's Create
    94  // operation.
    95  type CreateOpts struct {
    96  	// The Pool to Monitor.
    97  	PoolID string `json:"pool_id,omitempty"`
    98  
    99  	// The type of probe, which is PING, TCP, HTTP, or HTTPS, that is
   100  	// sent by the load balancer to verify the member state.
   101  	Type string `json:"type" required:"true"`
   102  
   103  	// The time, in seconds, between sending probes to members.
   104  	Delay int `json:"delay" required:"true"`
   105  
   106  	// Maximum number of seconds for a Monitor to wait for a ping reply
   107  	// before it times out. The value must be less than the delay value.
   108  	Timeout int `json:"timeout" required:"true"`
   109  
   110  	// Number of permissible ping failures before changing the member's
   111  	// status to INACTIVE. Must be a number between 1 and 10.
   112  	MaxRetries int `json:"max_retries" required:"true"`
   113  
   114  	// Number of permissible ping failures befor changing the member's
   115  	// status to ERROR. Must be a number between 1 and 10.
   116  	MaxRetriesDown int `json:"max_retries_down,omitempty"`
   117  
   118  	// URI path that will be accessed if Monitor type is HTTP or HTTPS.
   119  	URLPath string `json:"url_path,omitempty"`
   120  
   121  	// The HTTP method used for requests by the Monitor. If this attribute
   122  	// is not specified, it defaults to "GET". Required for HTTP(S) types.
   123  	HTTPMethod string `json:"http_method,omitempty"`
   124  
   125  	// Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify
   126  	// a single status like "200", a range like "200-202", or a combination like
   127  	// "200-202, 401".
   128  	ExpectedCodes string `json:"expected_codes,omitempty"`
   129  
   130  	// TenantID is the UUID of the project who owns the Monitor.
   131  	// Only administrative users can specify a project UUID other than their own.
   132  	TenantID string `json:"tenant_id,omitempty"`
   133  
   134  	// ProjectID is the UUID of the project who owns the Monitor.
   135  	// Only administrative users can specify a project UUID other than their own.
   136  	ProjectID string `json:"project_id,omitempty"`
   137  
   138  	// The Name of the Monitor.
   139  	Name string `json:"name,omitempty"`
   140  
   141  	// The administrative state of the Monitor. A valid value is true (UP)
   142  	// or false (DOWN).
   143  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   144  }
   145  
   146  // ToMonitorCreateMap builds a request body from CreateOpts.
   147  func (opts CreateOpts) ToMonitorCreateMap() (map[string]interface{}, error) {
   148  	return gophercloud.BuildRequestBody(opts, "healthmonitor")
   149  }
   150  
   151  /*
   152  Create is an operation which provisions a new Health Monitor. There are
   153  different types of Monitor you can provision: PING, TCP or HTTP(S). Below
   154  are examples of how to create each one.
   155  
   156  Here is an example config struct to use when creating a PING or TCP Monitor:
   157  
   158  CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
   159  CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
   160  
   161  Here is an example config struct to use when creating a HTTP(S) Monitor:
   162  
   163  CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
   164  HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"}
   165  */
   166  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   167  	b, err := opts.ToMonitorCreateMap()
   168  	if err != nil {
   169  		r.Err = err
   170  		return
   171  	}
   172  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   173  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   174  	return
   175  }
   176  
   177  // Get retrieves a particular Health Monitor based on its unique ID.
   178  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   179  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   180  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   181  	return
   182  }
   183  
   184  // UpdateOptsBuilder allows extensions to add additional parameters to the
   185  // Update request.
   186  type UpdateOptsBuilder interface {
   187  	ToMonitorUpdateMap() (map[string]interface{}, error)
   188  }
   189  
   190  // UpdateOpts is the common options struct used in this package's Update
   191  // operation.
   192  type UpdateOpts struct {
   193  	// The time, in seconds, between sending probes to members.
   194  	Delay int `json:"delay,omitempty"`
   195  
   196  	// Maximum number of seconds for a Monitor to wait for a ping reply
   197  	// before it times out. The value must be less than the delay value.
   198  	Timeout int `json:"timeout,omitempty"`
   199  
   200  	// Number of permissible ping failures before changing the member's
   201  	// status to INACTIVE. Must be a number between 1 and 10.
   202  	MaxRetries int `json:"max_retries,omitempty"`
   203  
   204  	// Number of permissible ping failures befor changing the member's
   205  	// status to ERROR. Must be a number between 1 and 10.
   206  	MaxRetriesDown int `json:"max_retries_down,omitempty"`
   207  
   208  	// URI path that will be accessed if Monitor type is HTTP or HTTPS.
   209  	// Required for HTTP(S) types.
   210  	URLPath string `json:"url_path,omitempty"`
   211  
   212  	// The HTTP method used for requests by the Monitor. If this attribute
   213  	// is not specified, it defaults to "GET". Required for HTTP(S) types.
   214  	HTTPMethod string `json:"http_method,omitempty"`
   215  
   216  	// Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify
   217  	// a single status like "200", or a range like "200-202". Required for HTTP(S)
   218  	// types.
   219  	ExpectedCodes string `json:"expected_codes,omitempty"`
   220  
   221  	// The Name of the Monitor.
   222  	Name *string `json:"name,omitempty"`
   223  
   224  	// The administrative state of the Monitor. A valid value is true (UP)
   225  	// or false (DOWN).
   226  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   227  }
   228  
   229  // ToMonitorUpdateMap builds a request body from UpdateOpts.
   230  func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
   231  	return gophercloud.BuildRequestBody(opts, "healthmonitor")
   232  }
   233  
   234  // Update is an operation which modifies the attributes of the specified
   235  // Monitor.
   236  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   237  	b, err := opts.ToMonitorUpdateMap()
   238  	if err != nil {
   239  		r.Err = err
   240  		return
   241  	}
   242  
   243  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   244  		OkCodes: []int{200, 202},
   245  	})
   246  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   247  	return
   248  }
   249  
   250  // Delete will permanently delete a particular Monitor based on its unique ID.
   251  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   252  	resp, err := c.Delete(resourceURL(c, id), nil)
   253  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   254  	return
   255  }