github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/elb/v3/monitors/requests.go (about) 1 package monitors 2 3 import ( 4 "fmt" 5 6 "github.com/chnsz/golangsdk" 7 "github.com/chnsz/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 // Number of permissible ping success before changing the member's 111 // status to ACTIVE. Must be a number between 1 and 10. 112 MaxRetriesDown int `json:"max_retries_down,omitempty"` 113 114 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 115 // Required for HTTP(S) types. 116 URLPath string `json:"url_path,omitempty"` 117 118 // Domain Name. 119 DomainName string `json:"domain_name,omitempty"` 120 121 // The HTTP method used for requests by the Monitor. If this attribute 122 // is not specified, it defaults to "GET". 123 HTTPMethod string `json:"http_method,omitempty"` 124 125 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 126 // a single status like "200", or a range like "200-202". 127 ExpectedCodes string `json:"expected_codes,omitempty"` 128 129 // TenantID is the UUID of the project who owns the Monitor. 130 // Only administrative users can specify a project UUID other than their own. 131 TenantID string `json:"tenant_id,omitempty"` 132 133 // ProjectID is the UUID of the project who owns the Monitor. 134 // Only administrative users can specify a project UUID other than their own. 135 ProjectID string `json:"project_id,omitempty"` 136 137 // The Name of the Monitor. 138 Name string `json:"name,omitempty"` 139 140 // The administrative state of the Monitor. A valid value is true (UP) 141 // or false (DOWN). 142 AdminStateUp *bool `json:"admin_state_up,omitempty"` 143 144 // The Port of the Monitor. 145 MonitorPort int `json:"monitor_port,omitempty"` 146 } 147 148 // ToMonitorCreateMap builds a request body from CreateOpts. 149 func (opts CreateOpts) ToMonitorCreateMap() (map[string]interface{}, error) { 150 if opts.Type == TypeHTTP || opts.Type == TypeHTTPS { 151 if opts.URLPath == "" { 152 return nil, fmt.Errorf("url_path must be provided for HTTP and HTTPS") 153 } 154 } 155 156 b, err := golangsdk.BuildRequestBody(opts, "healthmonitor") 157 if err != nil { 158 return nil, err 159 } 160 161 return b, nil 162 } 163 164 /* 165 Create is an operation which provisions a new Health Monitor. There are 166 different types of Monitor you can provision: PING, TCP or HTTP(S). Below 167 are examples of how to create each one. 168 169 Here is an example config struct to use when creating a PING or TCP Monitor: 170 171 CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3} 172 CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3} 173 174 Here is an example config struct to use when creating a HTTP(S) Monitor: 175 176 CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3, 177 HttpMethod: "HEAD", ExpectedCodes: "200", PoolID: "2c946bfc-1804-43ab-a2ff-58f6a762b505"} 178 */ 179 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 180 b, err := opts.ToMonitorCreateMap() 181 if err != nil { 182 r.Err = err 183 return 184 } 185 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 186 return 187 } 188 189 // Get retrieves a particular Health Monitor based on its unique ID. 190 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 191 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 192 return 193 } 194 195 // UpdateOptsBuilder allows extensions to add additional parameters to the 196 // Update request. 197 type UpdateOptsBuilder interface { 198 ToMonitorUpdateMap() (map[string]interface{}, error) 199 } 200 201 // UpdateOpts is the common options struct used in this package's Update 202 // operation. 203 type UpdateOpts struct { 204 // The time, in seconds, between sending probes to members. 205 Delay int `json:"delay,omitempty"` 206 207 // Maximum number of seconds for a Monitor to wait for a ping reply 208 // before it times out. The value must be less than the delay value. 209 Timeout int `json:"timeout,omitempty"` 210 211 // Number of permissible ping failures before changing the member's 212 // status to INACTIVE. Must be a number between 1 and 10. 213 MaxRetries int `json:"max_retries,omitempty"` 214 215 // Number of permissible ping success before changing the member's 216 // status to ACTIVE. Must be a number between 1 and 10. 217 MaxRetriesDown int `json:"max_retries_down,omitempty"` 218 219 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 220 // Required for HTTP(S) types. 221 URLPath string `json:"url_path,omitempty"` 222 223 // Domain Name. 224 DomainName string `json:"domain_name,omitempty"` 225 226 // The HTTP method used for requests by the Monitor. If this attribute 227 // is not specified, it defaults to "GET". Required for HTTP(S) types. 228 HTTPMethod string `json:"http_method,omitempty"` 229 230 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 231 // a single status like "200", or a range like "200-202". Required for HTTP(S) 232 // types. 233 ExpectedCodes string `json:"expected_codes,omitempty"` 234 235 // The Name of the Monitor. 236 Name string `json:"name,omitempty"` 237 238 // The administrative state of the Monitor. A valid value is true (UP) 239 // or false (DOWN). 240 AdminStateUp *bool `json:"admin_state_up,omitempty"` 241 242 // The Port of the Monitor. 243 MonitorPort int `json:"monitor_port,omitempty"` 244 245 // The type of probe, which is PING, TCP, HTTP, or HTTPS, that is 246 // sent by the load balancer to verify the member state. 247 Type string `json:"type,omitempty"` 248 } 249 250 // ToMonitorUpdateMap builds a request body from UpdateOpts. 251 func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { 252 return golangsdk.BuildRequestBody(opts, "healthmonitor") 253 } 254 255 // Update is an operation which modifies the attributes of the specified 256 // Monitor. 257 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 258 b, err := opts.ToMonitorUpdateMap() 259 if err != nil { 260 r.Err = err 261 return 262 } 263 264 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 265 OkCodes: []int{200, 202}, 266 }) 267 return 268 } 269 270 // Delete will permanently delete a particular Monitor based on its unique ID. 271 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 272 _, r.Err = c.Delete(resourceURL(c, id), nil) 273 return 274 }