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