github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/lbaas_v2/monitors/requests.go (about)

     1  package monitors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/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  	DomainName    string `q:"domain_name"`
    30  	Timeout       int    `q:"timeout"`
    31  	MaxRetries    int    `q:"max_retries"`
    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 := golangsdk.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 *golangsdk.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  )
    79  
    80  // CreateOptsBuilder allows extensions to add additional parameters to the
    81  // List request.
    82  type CreateOptsBuilder interface {
    83  	ToMonitorCreateMap() (map[string]interface{}, error)
    84  }
    85  
    86  // CreateOpts is the common options struct used in this package's Create
    87  // operation.
    88  type CreateOpts struct {
    89  	// The Pool to Monitor.
    90  	PoolID string `json:"pool_id" required:"true"`
    91  
    92  	// The type of probe, which is PING, TCP, HTTP, or HTTPS, that is
    93  	// sent by the load balancer to verify the member state.
    94  	Type string `json:"type" required:"true"`
    95  
    96  	// The time, in seconds, between sending probes to members.
    97  	Delay int `json:"delay" required:"true"`
    98  
    99  	// Specifies the domain name of the HTTP request during the health check.
   100  	//
   101  	// This parameter is valid when the value of type is set to HTTP.
   102  	DomainName string `json:"domain_name,omitempty"`
   103  
   104  	// Maximum number of seconds for a Monitor to wait for a ping reply
   105  	// before it times out. The value must be less than the delay value.
   106  	Timeout int `json:"timeout" required:"true"`
   107  
   108  	// Number of permissible ping failures before changing the member's
   109  	// status to INACTIVE. Must be a number between 1 and 10.
   110  	MaxRetries int `json:"max_retries" required:"true"`
   111  
   112  	// URI path that will be accessed if Monitor type is HTTP or HTTPS.
   113  	// Required for HTTP(S) types.
   114  	URLPath string `json:"url_path,omitempty"`
   115  
   116  	// The HTTP method used for requests by the Monitor. If this attribute
   117  	// is not specified, it defaults to "GET". Required for HTTP(S) types.
   118  	HTTPMethod string `json:"http_method,omitempty"`
   119  
   120  	// Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify
   121  	// a single status like "200", or a range like "200-202". Required for HTTP(S)
   122  	// types.
   123  	ExpectedCodes string `json:"expected_codes,omitempty"`
   124  
   125  	// TenantID is the UUID of the project who owns the Monitor.
   126  	// Only administrative users can specify a project UUID other than their own.
   127  	TenantID string `json:"tenant_id,omitempty"`
   128  
   129  	// ProjectID is the UUID of the project who owns the Monitor.
   130  	// Only administrative users can specify a project UUID other than their own.
   131  	ProjectID string `json:"project_id,omitempty"`
   132  
   133  	// The Name of the Monitor.
   134  	Name string `json:"name,omitempty"`
   135  
   136  	// The administrative state of the Monitor. A valid value is true (UP)
   137  	// or false (DOWN).
   138  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   139  
   140  	// The Port of the Monitor.
   141  	MonitorPort int `json:"monitor_port,omitempty"`
   142  }
   143  
   144  // ToMonitorCreateMap builds a request body from CreateOpts.
   145  func (opts CreateOpts) ToMonitorCreateMap() (map[string]interface{}, error) {
   146  	b, err := golangsdk.BuildRequestBody(opts, "healthmonitor")
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	switch opts.Type {
   152  	case TypeHTTP, TypeHTTPS:
   153  		switch opts.URLPath {
   154  		case "":
   155  			return nil, fmt.Errorf("URLPath must be provided for HTTP and HTTPS")
   156  		}
   157  		switch opts.ExpectedCodes {
   158  		case "":
   159  			return nil, fmt.Errorf("ExpectedCodes must be provided for HTTP and HTTPS")
   160  		}
   161  	}
   162  
   163  	return b, nil
   164  }
   165  
   166  /*
   167   Create is an operation which provisions a new Health Monitor. There are
   168   different types of Monitor you can provision: PING, TCP or HTTP(S). Below
   169   are examples of how to create each one.
   170  
   171   Here is an example config struct to use when creating a PING or TCP Monitor:
   172  
   173   CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
   174   CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
   175  
   176   Here is an example config struct to use when creating a HTTP(S) Monitor:
   177  
   178   CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
   179   HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"}
   180  */
   181  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   182  	b, err := opts.ToMonitorCreateMap()
   183  	if err != nil {
   184  		r.Err = err
   185  		return
   186  	}
   187  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   188  	return
   189  }
   190  
   191  // Get retrieves a particular Health Monitor based on its unique ID.
   192  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   193  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   194  	return
   195  }
   196  
   197  // UpdateOptsBuilder allows extensions to add additional parameters to the
   198  // Update request.
   199  type UpdateOptsBuilder interface {
   200  	ToMonitorUpdateMap() (map[string]interface{}, error)
   201  }
   202  
   203  // UpdateOpts is the common options struct used in this package's Update
   204  // operation.
   205  type UpdateOpts struct {
   206  	// The time, in seconds, between sending probes to members.
   207  	Delay int `json:"delay,omitempty"`
   208  
   209  	// Specifies the domain name of the HTTP request during the health check.
   210  	//
   211  	// This parameter is valid when the value of type is set to HTTP.
   212  	DomainName string `json:"domain_name,omitempty"`
   213  
   214  	// Maximum number of seconds for a Monitor to wait for a ping reply
   215  	// before it times out. The value must be less than the delay value.
   216  	Timeout int `json:"timeout,omitempty"`
   217  
   218  	// Number of permissible ping failures before changing the member's
   219  	// status to INACTIVE. Must be a number between 1 and 10.
   220  	MaxRetries int `json:"max_retries,omitempty"`
   221  
   222  	// URI path that will be accessed if Monitor type is HTTP or HTTPS.
   223  	// Required for HTTP(S) types.
   224  	URLPath string `json:"url_path,omitempty"`
   225  
   226  	// The HTTP method used for requests by the Monitor. If this attribute
   227  	// is not specified, it defaults to "GET". Required for HTTP(S) types.
   228  	HTTPMethod string `json:"http_method,omitempty"`
   229  
   230  	// Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify
   231  	// a single status like "200", or a range like "200-202". Required for HTTP(S)
   232  	// types.
   233  	ExpectedCodes string `json:"expected_codes,omitempty"`
   234  
   235  	// The Name of the Monitor.
   236  	Name string `json:"name,omitempty"`
   237  
   238  	// The administrative state of the Monitor. A valid value is true (UP)
   239  	// or false (DOWN).
   240  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   241  
   242  	// The Port of the Monitor.
   243  	MonitorPort int `json:"monitor_port,omitempty"`
   244  }
   245  
   246  // ToMonitorUpdateMap builds a request body from UpdateOpts.
   247  func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
   248  	return golangsdk.BuildRequestBody(opts, "healthmonitor")
   249  }
   250  
   251  // Update is an operation which modifies the attributes of the specified
   252  // Monitor.
   253  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   254  	b, err := opts.ToMonitorUpdateMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  
   260  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   261  		OkCodes: []int{200, 202},
   262  	})
   263  	return
   264  }
   265  
   266  // Delete will permanently delete a particular Monitor based on its unique ID.
   267  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   268  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   269  	return
   270  }