github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/elb/v2/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 DomainName string `q:"domain_name"` 32 HTTPMethod string `q:"http_method"` 33 URLPath string `q:"url_path"` 34 MonitorPort string `q:"monitor_port"` 35 ExpectedCodes string `q:"expected_codes"` 36 AdminStateUp *bool `q:"admin_state_up"` 37 Status string `q:"status"` 38 Limit int `q:"limit"` 39 PageReverse *bool `q:"page_reverse"` 40 Marker string `q:"marker"` 41 SortKey string `q:"sort_key"` 42 SortDir string `q:"sort_dir"` 43 } 44 45 // ToMonitorListQuery formats a ListOpts into a query string. 46 func (opts ListOpts) ToMonitorListQuery() (string, error) { 47 q, err := golangsdk.BuildQueryString(opts) 48 if err != nil { 49 return "", err 50 } 51 return q.String(), nil 52 } 53 54 // List returns a Pager which allows you to iterate over a collection of 55 // health monitors. It accepts a ListOpts struct, which allows you to filter and sort 56 // the returned collection for greater efficiency. 57 // 58 // Default policy settings return only those health monitors that are owned by the 59 // tenant who submits the request, unless an admin user submits the request. 60 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 61 url := rootURL(c) 62 if opts != nil { 63 query, err := opts.ToMonitorListQuery() 64 if err != nil { 65 return pagination.Pager{Err: err} 66 } 67 url += query 68 } 69 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 70 return MonitorPage{pagination.LinkedPageBase{PageResult: r}} 71 }) 72 } 73 74 // Constants that represent approved monitoring types. 75 const ( 76 TypePING = "PING" 77 TypeTCP = "TCP" 78 TypeHTTP = "HTTP" 79 TypeHTTPS = "HTTPS" 80 ) 81 82 var ( 83 errDelayMustGETimeout = fmt.Errorf("Delay must be greater than or equal to timeout") 84 ) 85 86 // CreateOptsBuilder allows extensions to add additional parameters to the 87 // List request. 88 type CreateOptsBuilder interface { 89 ToMonitorCreateMap() (map[string]interface{}, error) 90 } 91 92 // CreateOpts is the common options struct used in this package's Create 93 // operation. 94 type CreateOpts struct { 95 // The Pool to Monitor. 96 PoolID string `json:"pool_id" required:"true"` 97 98 // Specifies the health check protocol. 99 // The value can be TCP, UDP_CONNECT, or HTTP. 100 Type string `json:"type" required:"true"` 101 102 // The time, in seconds, between sending probes to members. 103 Delay int `json:"delay" required:"true"` 104 105 // Maximum number of seconds for a Monitor to wait for a ping reply 106 // before it times out. The value must be less than the delay value. 107 Timeout int `json:"timeout" required:"true"` 108 109 // Number of permissible ping failures before changing the member's 110 // status to INACTIVE. Must be a number between 1 and 10. 111 MaxRetries int `json:"max_retries" required:"true"` 112 113 // Specifies the domain name of HTTP requests during the health check. 114 // This parameter takes effect only when the value of type is set to HTTP. 115 DomainName string `json:"domain_name,omitempty"` 116 117 // URI path that will be accessed if Monitor type is HTTP or HTTPS. 118 // Required for HTTP(S) types. 119 URLPath string `json:"url_path,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 // Specifies the health check protocol. 216 // The value can be TCP, UDP_CONNECT, or HTTP. 217 Type string `json:"type,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 // The HTTP method used for requests by the Monitor. If this attribute 224 // is not specified, it defaults to "GET". Required for HTTP(S) types. 225 HTTPMethod string `json:"http_method,omitempty"` 226 227 // Expected HTTP codes for a passing HTTP(S) Monitor. You can either specify 228 // a single status like "200", or a range like "200-202". Required for HTTP(S) 229 // types. 230 ExpectedCodes string `json:"expected_codes,omitempty"` 231 232 // The Name of the Monitor. 233 Name string `json:"name,omitempty"` 234 235 // The administrative state of the Monitor. A valid value is true (UP) 236 // or false (DOWN). 237 AdminStateUp *bool `json:"admin_state_up,omitempty"` 238 239 // The Port of the Monitor. 240 MonitorPort int `json:"monitor_port,omitempty"` 241 } 242 243 // ToMonitorUpdateMap builds a request body from UpdateOpts. 244 func (opts UpdateOpts) ToMonitorUpdateMap() (map[string]interface{}, error) { 245 return golangsdk.BuildRequestBody(opts, "healthmonitor") 246 } 247 248 // Update is an operation which modifies the attributes of the specified 249 // Monitor. 250 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 251 b, err := opts.ToMonitorUpdateMap() 252 if err != nil { 253 r.Err = err 254 return 255 } 256 257 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 258 OkCodes: []int{200, 202}, 259 }) 260 return 261 } 262 263 // Delete will permanently delete a particular Monitor based on its unique ID. 264 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 265 _, r.Err = c.Delete(resourceURL(c, id), nil) 266 return 267 }