github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/elb/v3/monitors/requests.go (about) 1 package monitors 2 3 import ( 4 "fmt" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/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 HTTPMethod string `q:"http_method"` 32 URLPath string `q:"url_path"` 33 ExpectedCodes string `q:"expected_codes"` 34 AdminStateUp *bool `q:"admin_state_up"` 35 Status string `q:"status"` 36 Limit int `q:"limit"` 37 Marker string `q:"marker"` 38 SortKey string `q:"sort_key"` 39 SortDir string `q:"sort_dir"` 40 } 41 42 // ToMonitorListQuery formats a ListOpts into a query string. 43 func (opts ListOpts) ToMonitorListQuery() (string, error) { 44 q, err := golangsdk.BuildQueryString(opts) 45 if err != nil { 46 return "", err 47 } 48 return q.String(), nil 49 } 50 51 // List returns a Pager which allows you to iterate over a collection of 52 // health monitors. It accepts a ListOpts struct, which allows you to filter and sort 53 // the returned collection for greater efficiency. 54 // 55 // Default policy settings return only those health monitors that are owned by the 56 // tenant who submits the request, unless an admin user submits the request. 57 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 58 url := rootURL(c) 59 if opts != nil { 60 query, err := opts.ToMonitorListQuery() 61 if err != nil { 62 return pagination.Pager{Err: err} 63 } 64 url += query 65 } 66 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 67 return MonitorPage{pagination.LinkedPageBase{PageResult: r}} 68 }) 69 } 70 71 // Constants that represent approved monitoring types. 72 const ( 73 TypePING = "PING" 74 TypeTCP = "TCP" 75 TypeHTTP = "HTTP" 76 TypeHTTPS = "HTTPS" 77 ) 78 79 var ( 80 errDelayMustGETimeout = fmt.Errorf("Delay must be greater than or equal to timeout") 81 ) 82 83 // CreateOptsBuilder allows extensions to add additional parameters to the 84 // List request. 85 type CreateOptsBuilder interface { 86 ToMonitorCreateMap() (map[string]interface{}, error) 87 } 88 89 // CreateOpts is the common options struct used in this package's Create 90 // operation. 91 type CreateOpts struct { 92 // The Pool to Monitor. 93 PoolID string `json:"pool_id" required:"true"` 94 95 // The type of probe, which is PING, TCP, HTTP, or HTTPS, that is 96 // sent by the load balancer to verify the member state. 97 Type string `json:"type" required:"true"` 98 99 // The time, in seconds, between sending probes to members. 100 Delay int `json:"delay" required:"true"` 101 102 // Maximum number of seconds for a Monitor to wait for a ping reply 103 // before it times out. The value must be less than the delay value. 104 Timeout int `json:"timeout" required:"true"` 105 106 // Number of permissible ping failures before changing the member's 107 // status to INACTIVE. Must be a number between 1 and 10. 108 MaxRetries int `json:"max_retries" required:"true"` 109 110 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 111 // Required for HTTP(S) types. 112 URLPath string `json:"url_path,omitempty"` 113 114 // Domain Name. 115 DomainName string `json:"domain_name,omitempty"` 116 117 // The HTTP method used for requests by the Monitor. If this attribute 118 // is not specified, it defaults to "GET". 119 HTTPMethod string `json:"http_method,omitempty"` 120 121 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 122 // a single status like "200", or a range like "200-202". 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 if opts.Type == TypeHTTP || opts.Type == TypeHTTPS { 147 if opts.URLPath == "" { 148 return nil, fmt.Errorf("url_path must be provided for HTTP and HTTPS") 149 } 150 } 151 152 b, err := golangsdk.BuildRequestBody(opts, "healthmonitor") 153 if err != nil { 154 return nil, err 155 } 156 157 return b, nil 158 } 159 160 /* 161 Create is an operation which provisions a new Health Monitor. There are 162 different types of Monitor you can provision: PING, TCP or HTTP(S). Below 163 are examples of how to create each one. 164 165 Here is an example config struct to use when creating a PING or TCP Monitor: 166 167 CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3} 168 CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3} 169 170 Here is an example config struct to use when creating a HTTP(S) Monitor: 171 172 CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3, 173 HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"} 174 */ 175 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 176 b, err := opts.ToMonitorCreateMap() 177 if err != nil { 178 r.Err = err 179 return 180 } 181 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 182 return 183 } 184 185 // Get retrieves a particular Health Monitor based on its unique ID. 186 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 187 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 188 return 189 } 190 191 // UpdateOptsBuilder allows extensions to add additional parameters to the 192 // Update request. 193 type UpdateOptsBuilder interface { 194 ToMonitorUpdateMap() (map[string]interface{}, error) 195 } 196 197 // UpdateOpts is the common options struct used in this package's Update 198 // operation. 199 type UpdateOpts struct { 200 // The time, in seconds, between sending probes to members. 201 Delay int `json:"delay,omitempty"` 202 203 // Maximum number of seconds for a Monitor to wait for a ping reply 204 // before it times out. The value must be less than the delay value. 205 Timeout int `json:"timeout,omitempty"` 206 207 // Number of permissible ping failures before changing the member's 208 // status to INACTIVE. Must be a number between 1 and 10. 209 MaxRetries int `json:"max_retries,omitempty"` 210 211 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 212 // Required for HTTP(S) types. 213 URLPath string `json:"url_path,omitempty"` 214 215 // Domain Name. 216 DomainName string `json:"domain_name,omitempty"` 217 218 // The HTTP method used for requests by the Monitor. If this attribute 219 // is not specified, it defaults to "GET". Required for HTTP(S) types. 220 HTTPMethod string `json:"http_method,omitempty"` 221 222 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 223 // a single status like "200", or a range like "200-202". Required for HTTP(S) 224 // types. 225 ExpectedCodes string `json:"expected_codes,omitempty"` 226 227 // The Name of the Monitor. 228 Name string `json:"name,omitempty"` 229 230 // The administrative state of the Monitor. A valid value is true (UP) 231 // or false (DOWN). 232 AdminStateUp *bool `json:"admin_state_up,omitempty"` 233 234 // The Port of the Monitor. 235 MonitorPort int `json:"monitor_port,omitempty"` 236 } 237 238 // ToMonitorUpdateMap builds a request body from UpdateOpts. 239 func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { 240 return golangsdk.BuildRequestBody(opts, "healthmonitor") 241 } 242 243 // Update is an operation which modifies the attributes of the specified 244 // Monitor. 245 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 246 b, err := opts.ToMonitorUpdateMap() 247 if err != nil { 248 r.Err = err 249 return 250 } 251 252 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 253 OkCodes: []int{200, 202}, 254 }) 255 return 256 } 257 258 // Delete will permanently delete a particular Monitor based on its unique ID. 259 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 260 _, r.Err = c.Delete(resourceURL(c, id), nil) 261 return 262 }