github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/monitors/requests.go (about)

     1  package monitors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/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 := golangsdk.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 *golangsdk.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".
   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".
   120  	ExpectedCodes string `json:"expected_codes,omitempty"`
   121  
   122  	// TenantID is the UUID of the project who owns the Monitor.
   123  	// Only administrative users can specify a project UUID other than their own.
   124  	TenantID string `json:"tenant_id,omitempty"`
   125  
   126  	// ProjectID is the UUID of the project who owns the Monitor.
   127  	// Only administrative users can specify a project UUID other than their own.
   128  	ProjectID string `json:"project_id,omitempty"`
   129  
   130  	// The Name of the Monitor.
   131  	Name string `json:"name,omitempty"`
   132  
   133  	// The administrative state of the Monitor. A valid value is true (UP)
   134  	// or false (DOWN).
   135  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   136  
   137  	// The Port of the Monitor.
   138  	MonitorPort int `json:"monitor_port,omitempty"`
   139  }
   140  
   141  // ToMonitorCreateMap builds a request body from CreateOpts.
   142  func (opts CreateOpts) ToMonitorCreateMap() (map[string]interface{}, error) {
   143  	if opts.Type == TypeHTTP || opts.Type == TypeHTTPS {
   144  		if opts.URLPath == "" {
   145  			return nil, fmt.Errorf("url_path must be provided for HTTP and HTTPS")
   146  		}
   147  	}
   148  
   149  	b, err := golangsdk.BuildRequestBody(opts, "healthmonitor")
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	return b, nil
   155  }
   156  
   157  /*
   158   Create is an operation which provisions a new Health Monitor. There are
   159   different types of Monitor you can provision: PING, TCP or HTTP(S). Below
   160   are examples of how to create each one.
   161  
   162   Here is an example config struct to use when creating a PING or TCP Monitor:
   163  
   164   CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
   165   CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
   166  
   167   Here is an example config struct to use when creating a HTTP(S) Monitor:
   168  
   169   CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
   170   HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"}
   171  */
   172  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   173  	b, err := opts.ToMonitorCreateMap()
   174  	if err != nil {
   175  		r.Err = err
   176  		return
   177  	}
   178  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   179  	return
   180  }
   181  
   182  // Get retrieves a particular Health Monitor based on its unique ID.
   183  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   184  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   185  	return
   186  }
   187  
   188  // UpdateOptsBuilder allows extensions to add additional parameters to the
   189  // Update request.
   190  type UpdateOptsBuilder interface {
   191  	ToMonitorUpdateMap() (map[string]interface{}, error)
   192  }
   193  
   194  // UpdateOpts is the common options struct used in this package's Update
   195  // operation.
   196  type UpdateOpts struct {
   197  	// The time, in seconds, between sending probes to members.
   198  	Delay int `json:"delay,omitempty"`
   199  
   200  	// Maximum number of seconds for a Monitor to wait for a ping reply
   201  	// before it times out. The value must be less than the delay value.
   202  	Timeout int `json:"timeout,omitempty"`
   203  
   204  	// Number of permissible ping failures before changing the member's
   205  	// status to INACTIVE. Must be a number between 1 and 10.
   206  	MaxRetries int `json:"max_retries,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  	// The Port of the Monitor.
   229  	MonitorPort int `json:"monitor_port,omitempty"`
   230  }
   231  
   232  // ToMonitorUpdateMap builds a request body from UpdateOpts.
   233  func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
   234  	return golangsdk.BuildRequestBody(opts, "healthmonitor")
   235  }
   236  
   237  // Update is an operation which modifies the attributes of the specified
   238  // Monitor.
   239  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   240  	b, err := opts.ToMonitorUpdateMap()
   241  	if err != nil {
   242  		r.Err = err
   243  		return
   244  	}
   245  
   246  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   247  		OkCodes: []int{200, 202},
   248  	})
   249  	return
   250  }
   251  
   252  // Delete will permanently delete a particular Monitor based on its unique ID.
   253  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   254  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   255  	return
   256  }