github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/elb/v3/monitors/requests.go (about) 1 package monitors 2 3 import ( 4 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToMonitorListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering and sorting of paginated collections through 15 // the API. Filtering is achieved by passing in struct field values that map to 16 // the Monitor attributes you want to see returned. SortKey allows you to 17 // sort by a particular Monitor attribute. SortDir sets the direction, and is 18 // either `asc' or `desc'. Marker and Limit are used for pagination. 19 type ListOpts struct { 20 ID string `q:"id"` 21 Name string `q:"name"` 22 TenantID string `q:"tenant_id"` 23 ProjectID string `q:"project_id"` 24 PoolID string `q:"pool_id"` 25 Type string `q:"type"` 26 Delay int `q:"delay"` 27 Timeout int `q:"timeout"` 28 MaxRetries int `q:"max_retries"` 29 HTTPMethod string `q:"http_method"` 30 URLPath string `q:"url_path"` 31 ExpectedCodes string `q:"expected_codes"` 32 AdminStateUp *bool `q:"admin_state_up"` 33 Status string `q:"status"` 34 Limit int `q:"limit"` 35 Marker string `q:"marker"` 36 SortKey string `q:"sort_key"` 37 SortDir string `q:"sort_dir"` 38 } 39 40 // ToMonitorListQuery formats a ListOpts into a query string. 41 func (opts ListOpts) ToMonitorListQuery() (string, error) { 42 q, err := golangsdk.BuildQueryString(opts) 43 if err != nil { 44 return "", err 45 } 46 return q.String(), nil 47 } 48 49 // List returns a Pager which allows you to iterate over a collection of 50 // health monitors. It accepts a ListOpts struct, which allows you to filter and sort 51 // the returned collection for greater efficiency. 52 // 53 // Default policy settings return only those health monitors that are owned by the 54 // tenant who submits the request, unless an admin user submits the request. 55 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 56 url := rootURL(client) 57 if opts != nil { 58 query, err := opts.ToMonitorListQuery() 59 if err != nil { 60 return pagination.Pager{Err: err} 61 } 62 url += query 63 } 64 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 65 return MonitorPage{PageWithInfo: pagination.NewPageWithInfo(r)} 66 }) 67 } 68 69 type Type string 70 71 // Constants that represent approved monitoring types. 72 const ( 73 TypePING Type = "PING" 74 TypeTCP Type = "TCP" 75 TypeHTTP Type = "HTTP" 76 TypeHTTPS Type = "HTTPS" 77 ) 78 79 // CreateOptsBuilder allows extensions to add additional parameters to the 80 // List request. 81 type CreateOptsBuilder interface { 82 ToMonitorCreateMap() (map[string]interface{}, error) 83 } 84 85 // CreateOpts is the common options' struct used in this package's Create 86 // operation. 87 type CreateOpts struct { 88 // The Pool to Monitor. 89 PoolID string `json:"pool_id" required:"true"` 90 91 // Specifies the health check protocol. 92 // 93 // The value can be TCP, UDP_CONNECT, HTTP, HTTPS, or PING. 94 Type Type `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 maximum time required for waiting for a response from the health check, in seconds. 100 // It is recommended that you set the value less than that of parameter delay. 101 Timeout int `json:"timeout" required:"true"` 102 103 // Specifies the number of consecutive health checks when the health check result of a backend server changes 104 // from OFFLINE to ONLINE. The value ranges from 1 to 10. 105 MaxRetries int `json:"max_retries" required:"true"` 106 107 // Specifies the number of consecutive health checks when the health check result of a backend server changes 108 // from ONLINE to OFFLINE. 109 MaxRetriesDown int `json:"max_retries_down,omitempty"` 110 111 // Specifies the HTTP request path for the health check. 112 // The value must start with a slash (/), and the default value is /. This parameter is available only when type is set to HTTP. 113 URLPath string `json:"url_path,omitempty"` 114 115 // Specifies the domain name that HTTP requests are sent to during the health check. 116 // This parameter is available only when type is set to HTTP. 117 DomainName string `json:"domain_name,omitempty"` 118 119 // The HTTP method used for requests by the Monitor. If this attribute 120 // is not specified, it defaults to "GET". 121 HTTPMethod string `json:"http_method,omitempty"` 122 123 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 124 // a single status like "200", or a range like "200-202". 125 ExpectedCodes string `json:"expected_codes,omitempty"` 126 127 // ProjectID is the UUID of the project who owns the Monitor. 128 // Only administrative users can specify a project UUID other than their own. 129 ProjectID string `json:"project_id,omitempty"` 130 131 // The Name of the Monitor. 132 Name string `json:"name,omitempty"` 133 134 // The administrative state of the Monitor. A valid value is true (UP) 135 // or false (DOWN). 136 AdminStateUp *bool `json:"admin_state_up,omitempty"` 137 138 // The Port of the Monitor. 139 MonitorPort int `json:"monitor_port,omitempty"` 140 } 141 142 // ToMonitorCreateMap builds a request body from CreateOpts. 143 func (opts CreateOpts) ToMonitorCreateMap() (map[string]interface{}, error) { 144 b, err := golangsdk.BuildRequestBody(opts, "healthmonitor") 145 if err != nil { 146 return nil, err 147 } 148 149 return b, nil 150 } 151 152 // Create is an operation which provisions a new Health Monitor. There are 153 // different types of Monitor you can provision: PING, TCP or HTTP(S). Below 154 // are examples of how to create each one. 155 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 156 b, err := opts.ToMonitorCreateMap() 157 if err != nil { 158 r.Err = err 159 return 160 } 161 _, r.Err = client.Post(rootURL(client), b, &r.Body, nil) 162 return 163 } 164 165 // Get retrieves a particular Health Monitor based on its unique ID. 166 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 167 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 168 return 169 } 170 171 // UpdateOptsBuilder allows extensions to add additional parameters to the 172 // Update request. 173 type UpdateOptsBuilder interface { 174 ToMonitorUpdateMap() (map[string]interface{}, error) 175 } 176 177 // UpdateOpts is the common options' struct used in this package's Update 178 // operation. 179 type UpdateOpts struct { 180 // The time, in seconds, between sending probes to members. 181 Delay int `json:"delay,omitempty"` 182 183 // Maximum number of seconds for a Monitor to wait for a ping reply 184 // before it times out. The value must be less than the delay value. 185 Timeout int `json:"timeout,omitempty"` 186 187 // Number of permissible ping failures before changing the member's 188 // status to INACTIVE. Must be a number between 1 and 10. 189 MaxRetries int `json:"max_retries,omitempty"` 190 191 MaxRetriesDown int `json:"max_retries_down,omitempty"` 192 193 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 194 // Required for HTTP(S) types. 195 URLPath string `json:"url_path,omitempty"` 196 197 // Domain Name. 198 DomainName string `json:"domain_name,omitempty"` 199 200 // The HTTP method used for requests by the Monitor. If this attribute 201 // is not specified, it defaults to "GET". Required for HTTP(S) types. 202 HTTPMethod string `json:"http_method,omitempty"` 203 204 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 205 // a single status like "200", or a range like "200-202". Required for HTTP(S) 206 // types. 207 ExpectedCodes string `json:"expected_codes,omitempty"` 208 209 // The Name of the Monitor. 210 Name string `json:"name,omitempty"` 211 212 // The administrative state of the Monitor. A valid value is true (UP) 213 // or false (DOWN). 214 AdminStateUp *bool `json:"admin_state_up,omitempty"` 215 216 // The Port of the Monitor. 217 MonitorPort int `json:"monitor_port,omitempty"` 218 219 // The type of probe, which is PING, TCP, HTTP, or HTTPS, that is 220 // sent by the load balancer to verify the member state. 221 Type string `json:"type,omitempty"` 222 } 223 224 // ToMonitorUpdateMap builds a request body from UpdateOpts. 225 func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { 226 return golangsdk.BuildRequestBody(opts, "healthmonitor") 227 } 228 229 // Update is an operation which modifies the attributes of the specified 230 // Monitor. 231 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 232 b, err := opts.ToMonitorUpdateMap() 233 if err != nil { 234 r.Err = err 235 return 236 } 237 238 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 239 OkCodes: []int{200, 202}, 240 }) 241 return 242 } 243 244 // Delete will permanently delete a particular Monitor based on its unique ID. 245 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 246 _, r.Err = client.Delete(resourceURL(client, id), nil) 247 return 248 }