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

     1  package healthcheck
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	// "github.com/opentelekomcloud/gophertelekomcloud/pagination"
     6  )
     7  
     8  // Constants that represent approved monitoring types.
     9  const (
    10  	TypePING  = "PING"
    11  	TypeTCP   = "TCP"
    12  	TypeHTTP  = "HTTP"
    13  	TypeHTTPS = "HTTPS"
    14  )
    15  
    16  // CreateOptsBuilder is the interface options structs have to satisfy in order
    17  // to be used in the main Create operation in this package. Since many
    18  // extensions decorate or modify the common logic, it is useful for them to
    19  // satisfy a basic interface in order for them to be used.
    20  type CreateOptsBuilder interface {
    21  	ToHealthCreateMap() (map[string]interface{}, error)
    22  }
    23  
    24  // CreateOpts is the common options struct used in this package's Create
    25  // operation.
    26  type CreateOpts struct {
    27  	// Required.  Specifies the ID of the listener to which the health check task belongs.
    28  	ListenerID string `json:"listener_id" required:"true"`
    29  	// Optional. Specifies the protocol used for the health check. The value can be HTTP or TCP (case-insensitive).
    30  	HealthcheckProtocol string `json:"healthcheck_protocol,omitempty"`
    31  	// Optional. Specifies the URI for health check. This parameter is valid when healthcheck_ protocol is HTTP.
    32  	// The value is a string of 1 to 80 characters that must start with a slash (/) and can only contain letters, digits,
    33  	// and special characters, such as -/.%?#&.
    34  	HealthcheckUri string `json:"healthcheck_uri,omitempty"`
    35  	// Optional. Specifies the port used for the health check.  The value ranges from 1 to 65535.
    36  	HealthcheckConnectPort int `json:"healthcheck_connect_port,omitempty"`
    37  	// Optional. MSpecifies the threshold at which the health check result is success, that is, the number of consecutive successful
    38  	// health checks when the health check result of the backend server changes from fail to success.
    39  	// The value ranges from 1 to 10.
    40  	HealthyThreshold int `json:"healthy_threshold,omitempty"`
    41  	// Optional. Specifies the threshold at which the health check result is fail, that is, the number of consecutive
    42  	// failed health checks when the health check result of the backend server changes from success to fail.
    43  	// The value ranges from 1 to 10.
    44  	UnhealthyThreshold int `json:"unhealthy_threshold,omitempty"`
    45  	// Optional. Specifies the maximum timeout duration (s) for the health check.
    46  	// The value ranges from 1 to 50.
    47  	HealthcheckTimeout int `json:"healthcheck_timeout,omitempty"`
    48  	// Optional. Specifies the maximum interval (s) for health check.
    49  	// The value ranges from 1 to 5.
    50  	HealthcheckInterval int `json:"healthcheck_interval,omitempty"`
    51  }
    52  
    53  // ToHealthCreateMap casts a CreateOpts struct to a map.
    54  func (opts CreateOpts) ToHealthCreateMap() (map[string]interface{}, error) {
    55  	b, err := golangsdk.BuildRequestBody(opts, "")
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return b, nil
    61  }
    62  
    63  /*
    64   Create is an operation which provisions a new Health Monitor. There are
    65   different types of Monitor you can provision: PING, TCP or HTTP(S). Below
    66   are examples of how to create each one.
    67  
    68   Here is an example config struct to use when creating a PING or TCP Monitor:
    69  
    70   CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
    71   CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
    72  
    73   Here is an example config struct to use when creating a HTTP(S) Monitor:
    74  
    75   CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
    76   HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"}
    77  */
    78  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    79  	b, err := opts.ToHealthCreateMap()
    80  	if err != nil {
    81  		r.Err = err
    82  		return
    83  	}
    84  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
    85  		OkCodes: []int{200},
    86  	})
    87  	return
    88  }
    89  
    90  // Get retrieves a particular Health Monitor based on its unique ID.
    91  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    92  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    93  	return
    94  }
    95  
    96  // UpdateOptsBuilder is the interface options structs have to satisfy in order
    97  // to be used in the main Update operation in this package. Since many
    98  // extensions decorate or modify the common logic, it is useful for them to
    99  // satisfy a basic interface in order for them to be used.
   100  type UpdateOptsBuilder interface {
   101  	ToHealthUpdateMap() (map[string]interface{}, error)
   102  }
   103  
   104  // UpdateOpts is the common options struct used in this package's Update
   105  // operation.
   106  type UpdateOpts struct {
   107  	// Optional. Specifies the protocol used for the health check. The value can be HTTP or TCP (case-insensitive).
   108  	HealthcheckProtocol string `json:"healthcheck_protocol,omitempty"`
   109  	// Optional. Specifies the URI for health check. This parameter is valid when healthcheck_ protocol is HTTP.
   110  	// The value is a string of 1 to 80 characters that must start with a slash (/) and can only contain letters, digits,
   111  	// and special characters, such as -/.%?#&.
   112  	HealthcheckUri string `json:"healthcheck_uri,omitempty"`
   113  	// Optional. Specifies the port used for the health check.  The value ranges from 1 to 65535.
   114  	HealthcheckConnectPort int `json:"healthcheck_connect_port,omitempty"`
   115  	// Optional. MSpecifies the threshold at which the health check result is success, that is, the number of consecutive successful
   116  	// health checks when the health check result of the backend server changes from fail to success.
   117  	// The value ranges from 1 to 10.
   118  	HealthyThreshold int `json:"healthy_threshold,omitempty"`
   119  	// Optional. Specifies the threshold at which the health check result is fail, that is, the number of consecutive
   120  	// failed health checks when the health check result of the backend server changes from success to fail.
   121  	// The value ranges from 1 to 10.
   122  	UnhealthyThreshold int `json:"unhealthy_threshold,omitempty"`
   123  	// Optional. Specifies the maximum timeout duration (s) for the health check.
   124  	// The value ranges from 1 to 50.
   125  	HealthcheckTimeout int `json:"healthcheck_timeout,omitempty"`
   126  	// Optional. Specifies the maximum interval (s) for health check.
   127  	// The value ranges from 1 to 5.
   128  	HealthcheckInterval int `json:"healthcheck_interval,omitempty"`
   129  }
   130  
   131  // ToHealthpdateMap casts a UpdateOpts struct to a map.
   132  func (opts UpdateOpts) ToHealthUpdateMap() (map[string]interface{}, error) {
   133  	return golangsdk.BuildRequestBody(opts, "")
   134  }
   135  
   136  // Update is an operation which modifies the attributes of the specified Monitor.
   137  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   138  	b, err := opts.ToHealthUpdateMap()
   139  	if err != nil {
   140  		r.Err = err
   141  		return
   142  	}
   143  
   144  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   145  		OkCodes: []int{200, 202},
   146  	})
   147  	return
   148  }
   149  
   150  // Delete will permanently delete a particular Health based on its unique ID.
   151  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   152  	_, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{
   153  		OkCodes: []int{204},
   154  	})
   155  	return
   156  }