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