github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas/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  // ListOpts allows the filtering and sorting of paginated collections through
    11  // the API. Filtering is achieved by passing in struct field values that map to
    12  // the floating IP attributes you want to see returned. SortKey allows you to
    13  // sort by a particular network attribute. SortDir sets the direction, and is
    14  // either `asc' or `desc'. Marker and Limit are used for pagination.
    15  type ListOpts struct {
    16  	ID            string `q:"id"`
    17  	TenantID      string `q:"tenant_id"`
    18  	Type          string `q:"type"`
    19  	Delay         int    `q:"delay"`
    20  	Timeout       int    `q:"timeout"`
    21  	MaxRetries    int    `q:"max_retries"`
    22  	HTTPMethod    string `q:"http_method"`
    23  	URLPath       string `q:"url_path"`
    24  	ExpectedCodes string `q:"expected_codes"`
    25  	AdminStateUp  *bool  `q:"admin_state_up"`
    26  	Status        string `q:"status"`
    27  	Limit         int    `q:"limit"`
    28  	Marker        string `q:"marker"`
    29  	SortKey       string `q:"sort_key"`
    30  	SortDir       string `q:"sort_dir"`
    31  }
    32  
    33  // List returns a Pager which allows you to iterate over a collection of
    34  // monitors. It accepts a ListOpts struct, which allows you to filter and sort
    35  // the returned collection for greater efficiency.
    36  //
    37  // Default policy settings return only those monitors that are owned by the
    38  // tenant who submits the request, unless an admin user submits the request.
    39  func List(c *golangsdk.ServiceClient, opts ListOpts) pagination.Pager {
    40  	q, err := golangsdk.BuildQueryString(&opts)
    41  	if err != nil {
    42  		return pagination.Pager{Err: err}
    43  	}
    44  	u := rootURL(c) + q.String()
    45  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    46  		return MonitorPage{pagination.LinkedPageBase{PageResult: r}}
    47  	})
    48  }
    49  
    50  // MonitorType is the type for all the types of LB monitors.
    51  type MonitorType string
    52  
    53  // Constants that represent approved monitoring types.
    54  const (
    55  	TypePING  MonitorType = "PING"
    56  	TypeTCP   MonitorType = "TCP"
    57  	TypeHTTP  MonitorType = "HTTP"
    58  	TypeHTTPS MonitorType = "HTTPS"
    59  )
    60  
    61  // CreateOptsBuilder allows extensions to add additional parameters to the
    62  // Create request.
    63  type CreateOptsBuilder interface {
    64  	ToLBMonitorCreateMap() (map[string]interface{}, error)
    65  }
    66  
    67  // CreateOpts contains all the values needed to create a new health monitor.
    68  type CreateOpts struct {
    69  	// MonitorType is the type of probe, which is PING, TCP, HTTP, or HTTPS,
    70  	// that is sent by the load balancer to verify the member state.
    71  	Type MonitorType `json:"type" required:"true"`
    72  
    73  	// Delay is the time, in seconds, between sending probes to members.
    74  	Delay int `json:"delay" required:"true"`
    75  
    76  	// Timeout is the maximum number of seconds for a monitor to wait for a ping
    77  	// reply before it times out. The value must be less than the delay value.
    78  	Timeout int `json:"timeout" required:"true"`
    79  
    80  	// MaxRetries is the number of permissible ping failures before changing the
    81  	// member's status to INACTIVE. Must be a number between 1 and 10.
    82  	MaxRetries int `json:"max_retries" required:"true"`
    83  
    84  	// URLPath is the URI path that will be accessed if monitor type
    85  	// is HTTP or HTTPS. Required for HTTP(S) types.
    86  	URLPath string `json:"url_path,omitempty"`
    87  
    88  	// HTTPMethod is the HTTP method used for requests by the monitor. If this
    89  	// attribute is not specified, it defaults to "GET". Required for HTTP(S)
    90  	// types.
    91  	HTTPMethod string `json:"http_method,omitempty"`
    92  
    93  	// ExpectedCodes is the expected HTTP codes for a passing HTTP(S) monitor
    94  	// You can either specify a single status like "200", or a range like
    95  	// "200-202". Required for HTTP(S) types.
    96  	ExpectedCodes string `json:"expected_codes,omitempty"`
    97  
    98  	// TenantID is only required if the caller has an admin role and wants
    99  	// to create a pool for another tenant.
   100  	TenantID string `json:"tenant_id,omitempty"`
   101  
   102  	// AdminStateUp denotes whether the monitor is administratively up or down.
   103  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   104  }
   105  
   106  // ToLBMonitorCreateMap builds a request body from CreateOpts.
   107  func (opts CreateOpts) ToLBMonitorCreateMap() (map[string]interface{}, error) {
   108  	if opts.Type == TypeHTTP || opts.Type == TypeHTTPS {
   109  		if opts.URLPath == "" {
   110  			err := golangsdk.ErrMissingInput{}
   111  			err.Argument = "monitors.CreateOpts.URLPath"
   112  			return nil, err
   113  		}
   114  		if opts.ExpectedCodes == "" {
   115  			err := golangsdk.ErrMissingInput{}
   116  			err.Argument = "monitors.CreateOpts.ExpectedCodes"
   117  			return nil, err
   118  		}
   119  	}
   120  	if opts.Delay < opts.Timeout {
   121  		err := golangsdk.ErrInvalidInput{}
   122  		err.Argument = "monitors.CreateOpts.Delay/monitors.CreateOpts.Timeout"
   123  		err.Info = "Delay must be greater than or equal to timeout"
   124  		return nil, err
   125  	}
   126  	return golangsdk.BuildRequestBody(opts, "health_monitor")
   127  }
   128  
   129  // Create is an operation which provisions a new health monitor. There are
   130  // different types of monitor you can provision: PING, TCP or HTTP(S). Below
   131  // are examples of how to create each one.
   132  //
   133  // Here is an example config struct to use when creating a PING or TCP monitor:
   134  //
   135  // CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
   136  // CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
   137  //
   138  // Here is an example config struct to use when creating a HTTP(S) monitor:
   139  //
   140  // CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
   141  //  HttpMethod: "HEAD", ExpectedCodes: "200"}
   142  //
   143  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   144  	b, err := opts.ToLBMonitorCreateMap()
   145  	if err != nil {
   146  		r.Err = err
   147  		return
   148  	}
   149  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   150  	return
   151  }
   152  
   153  // Get retrieves a particular health monitor based on its unique ID.
   154  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   155  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   156  	return
   157  }
   158  
   159  // UpdateOptsBuilder allows extensions to add additional parameters to the
   160  // Update request.
   161  type UpdateOptsBuilder interface {
   162  	ToLBMonitorUpdateMap() (map[string]interface{}, error)
   163  }
   164  
   165  // UpdateOpts contains all the values needed to update an existing monitor.
   166  // Attributes not listed here but appear in CreateOpts are immutable and cannot
   167  // be updated.
   168  type UpdateOpts struct {
   169  	// Delay is the time, in seconds, between sending probes to members.
   170  	Delay int `json:"delay,omitempty"`
   171  
   172  	// Timeout is the maximum number of seconds for a monitor to wait for a ping
   173  	// reply before it times out. The value must be less than the delay value.
   174  	Timeout int `json:"timeout,omitempty"`
   175  
   176  	// MaxRetries is the number of permissible ping failures before changing the
   177  	// member's status to INACTIVE. Must be a number between 1 and 10.
   178  	MaxRetries int `json:"max_retries,omitempty"`
   179  
   180  	// URLPath is the URI path that will be accessed if monitor type
   181  	// is HTTP or HTTPS.
   182  	URLPath string `json:"url_path,omitempty"`
   183  
   184  	// HTTPMethod is the HTTP method used for requests by the monitor. If this
   185  	// attribute is not specified, it defaults to "GET".
   186  	HTTPMethod string `json:"http_method,omitempty"`
   187  
   188  	// ExpectedCodes is the expected HTTP codes for a passing HTTP(S) monitor
   189  	// You can either specify a single status like "200", or a range like
   190  	// "200-202".
   191  	ExpectedCodes string `json:"expected_codes,omitempty"`
   192  
   193  	// AdminStateUp denotes whether the monitor is administratively up or down.
   194  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   195  }
   196  
   197  // ToLBMonitorUpdateMap builds a request body from UpdateOpts.
   198  func (opts UpdateOpts) ToLBMonitorUpdateMap() (map[string]interface{}, error) {
   199  	if opts.Delay > 0 && opts.Timeout > 0 && opts.Delay < opts.Timeout {
   200  		err := golangsdk.ErrInvalidInput{}
   201  		err.Argument = "monitors.CreateOpts.Delay/monitors.CreateOpts.Timeout"
   202  		err.Value = fmt.Sprintf("%d/%d", opts.Delay, opts.Timeout)
   203  		err.Info = "Delay must be greater than or equal to timeout"
   204  		return nil, err
   205  	}
   206  	return golangsdk.BuildRequestBody(opts, "health_monitor")
   207  }
   208  
   209  // Update is an operation which modifies the attributes of the specified
   210  // monitor.
   211  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   212  	b, err := opts.ToLBMonitorUpdateMap()
   213  	if err != nil {
   214  		r.Err = err
   215  		return
   216  	}
   217  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   218  		OkCodes: []int{200, 202},
   219  	})
   220  	return
   221  }
   222  
   223  // Delete will permanently delete a particular monitor based on its unique ID.
   224  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   225  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   226  	return
   227  }