github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/monitors/requests.go (about)

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