github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas/monitors/requests.go (about) 1 package monitors 2 3 import ( 4 "fmt" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 // ListOpts allows the filtering and sorting of paginated collections through 11 // the API. Filtering is achieved by passing in struct field values that map to 12 // the floating IP attributes you want to see returned. SortKey allows you to 13 // sort by a particular network attribute. SortDir sets the direction, and is 14 // either `asc' or `desc'. Marker and Limit are used for pagination. 15 type ListOpts struct { 16 ID string `q:"id"` 17 TenantID string `q:"tenant_id"` 18 Type string `q:"type"` 19 Delay int `q:"delay"` 20 Timeout int `q:"timeout"` 21 MaxRetries int `q:"max_retries"` 22 HTTPMethod string `q:"http_method"` 23 URLPath string `q:"url_path"` 24 ExpectedCodes string `q:"expected_codes"` 25 AdminStateUp *bool `q:"admin_state_up"` 26 Status string `q:"status"` 27 Limit int `q:"limit"` 28 Marker string `q:"marker"` 29 SortKey string `q:"sort_key"` 30 SortDir string `q:"sort_dir"` 31 } 32 33 // List returns a Pager which allows you to iterate over a collection of 34 // monitors. It accepts a ListOpts struct, which allows you to filter and sort 35 // the returned collection for greater efficiency. 36 // 37 // Default policy settings return only those monitors that are owned by the 38 // tenant who submits the request, unless an admin user submits the request. 39 func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { 40 q, err := gophercloud.BuildQueryString(&opts) 41 if err != nil { 42 return pagination.Pager{Err: err} 43 } 44 u := rootURL(c) + q.String() 45 return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 46 return MonitorPage{pagination.LinkedPageBase{PageResult: r}} 47 }) 48 } 49 50 // MonitorType is the type for all the types of LB monitors. 51 type MonitorType string 52 53 // Constants that represent approved monitoring types. 54 const ( 55 TypePING MonitorType = "PING" 56 TypeTCP MonitorType = "TCP" 57 TypeHTTP MonitorType = "HTTP" 58 TypeHTTPS MonitorType = "HTTPS" 59 ) 60 61 // CreateOptsBuilder allows extensions to add additional parameters to the 62 // Create request. 63 type CreateOptsBuilder interface { 64 ToLBMonitorCreateMap() (map[string]interface{}, error) 65 } 66 67 // CreateOpts contains all the values needed to create a new health monitor. 68 type CreateOpts struct { 69 // MonitorType is the type of probe, which is PING, TCP, HTTP, or HTTPS, 70 // that is sent by the load balancer to verify the member state. 71 Type MonitorType `json:"type" required:"true"` 72 73 // Delay is the time, in seconds, between sending probes to members. 74 Delay int `json:"delay" required:"true"` 75 76 // Timeout is the maximum number of seconds for a monitor to wait for a ping 77 // reply before it times out. The value must be less than the delay value. 78 Timeout int `json:"timeout" required:"true"` 79 80 // MaxRetries is the number of permissible ping failures before changing the 81 // member's status to INACTIVE. Must be a number between 1 and 10. 82 MaxRetries int `json:"max_retries" required:"true"` 83 84 // URLPath is the URI path that will be accessed if monitor type 85 // is HTTP or HTTPS. Required for HTTP(S) types. 86 URLPath string `json:"url_path,omitempty"` 87 88 // HTTPMethod is the HTTP method used for requests by the monitor. If this 89 // attribute is not specified, it defaults to "GET". Required for HTTP(S) 90 // types. 91 HTTPMethod string `json:"http_method,omitempty"` 92 93 // ExpectedCodes is the expected HTTP codes for a passing HTTP(S) monitor 94 // You can either specify a single status like "200", or a range like 95 // "200-202". Required for HTTP(S) types. 96 ExpectedCodes string `json:"expected_codes,omitempty"` 97 98 // TenantID is only required if the caller has an admin role and wants 99 // to create a pool for another tenant. 100 TenantID string `json:"tenant_id,omitempty"` 101 102 // AdminStateUp denotes whether the monitor is administratively up or down. 103 AdminStateUp *bool `json:"admin_state_up,omitempty"` 104 } 105 106 // ToLBMonitorCreateMap builds a request body from CreateOpts. 107 func (opts CreateOpts) ToLBMonitorCreateMap() (map[string]interface{}, error) { 108 if opts.Type == TypeHTTP || opts.Type == TypeHTTPS { 109 if opts.URLPath == "" { 110 err := gophercloud.ErrMissingInput{} 111 err.Argument = "monitors.CreateOpts.URLPath" 112 return nil, err 113 } 114 if opts.ExpectedCodes == "" { 115 err := gophercloud.ErrMissingInput{} 116 err.Argument = "monitors.CreateOpts.ExpectedCodes" 117 return nil, err 118 } 119 } 120 if opts.Delay < opts.Timeout { 121 err := gophercloud.ErrInvalidInput{} 122 err.Argument = "monitors.CreateOpts.Delay/monitors.CreateOpts.Timeout" 123 err.Info = "Delay must be greater than or equal to timeout" 124 return nil, err 125 } 126 return gophercloud.BuildRequestBody(opts, "health_monitor") 127 } 128 129 // Create is an operation which provisions a new health monitor. There are 130 // different types of monitor you can provision: PING, TCP or HTTP(S). Below 131 // are examples of how to create each one. 132 // 133 // Here is an example config struct to use when creating a PING or TCP monitor: 134 // 135 // CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3} 136 // CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3} 137 // 138 // Here is an example config struct to use when creating a HTTP(S) monitor: 139 // 140 // CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3, 141 // 142 // HttpMethod: "HEAD", ExpectedCodes: "200"} 143 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 144 b, err := opts.ToLBMonitorCreateMap() 145 if err != nil { 146 r.Err = err 147 return 148 } 149 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 150 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 151 return 152 } 153 154 // Get retrieves a particular health monitor based on its unique ID. 155 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 156 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 157 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 158 return 159 } 160 161 // UpdateOptsBuilder allows extensions to add additional parameters to the 162 // Update request. 163 type UpdateOptsBuilder interface { 164 ToLBMonitorUpdateMap() (map[string]interface{}, error) 165 } 166 167 // UpdateOpts contains all the values needed to update an existing monitor. 168 // Attributes not listed here but appear in CreateOpts are immutable and cannot 169 // be updated. 170 type UpdateOpts struct { 171 // Delay is the time, in seconds, between sending probes to members. 172 Delay int `json:"delay,omitempty"` 173 174 // Timeout is the maximum number of seconds for a monitor to wait for a ping 175 // reply before it times out. The value must be less than the delay value. 176 Timeout int `json:"timeout,omitempty"` 177 178 // MaxRetries is the number of permissible ping failures before changing the 179 // member's status to INACTIVE. Must be a number between 1 and 10. 180 MaxRetries int `json:"max_retries,omitempty"` 181 182 // URLPath is the URI path that will be accessed if monitor type 183 // is HTTP or HTTPS. 184 URLPath string `json:"url_path,omitempty"` 185 186 // HTTPMethod is the HTTP method used for requests by the monitor. If this 187 // attribute is not specified, it defaults to "GET". 188 HTTPMethod string `json:"http_method,omitempty"` 189 190 // ExpectedCodes is the expected HTTP codes for a passing HTTP(S) monitor 191 // You can either specify a single status like "200", or a range like 192 // "200-202". 193 ExpectedCodes string `json:"expected_codes,omitempty"` 194 195 // AdminStateUp denotes whether the monitor is administratively up or down. 196 AdminStateUp *bool `json:"admin_state_up,omitempty"` 197 } 198 199 // ToLBMonitorUpdateMap builds a request body from UpdateOpts. 200 func (opts UpdateOpts) ToLBMonitorUpdateMap() (map[string]interface{}, error) { 201 if opts.Delay > 0 && opts.Timeout > 0 && opts.Delay < opts.Timeout { 202 err := gophercloud.ErrInvalidInput{} 203 err.Argument = "monitors.CreateOpts.Delay/monitors.CreateOpts.Timeout" 204 err.Value = fmt.Sprintf("%d/%d", opts.Delay, opts.Timeout) 205 err.Info = "Delay must be greater than or equal to timeout" 206 return nil, err 207 } 208 return gophercloud.BuildRequestBody(opts, "health_monitor") 209 } 210 211 // Update is an operation which modifies the attributes of the specified 212 // monitor. 213 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 214 b, err := opts.ToLBMonitorUpdateMap() 215 if err != nil { 216 r.Err = err 217 return 218 } 219 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 220 OkCodes: []int{200, 202}, 221 }) 222 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 223 return 224 } 225 226 // Delete will permanently delete a particular monitor based on its unique ID. 227 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 228 resp, err := c.Delete(resourceURL(c, id), nil) 229 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 230 return 231 }