github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas_v2/loadbalancers/requests.go (about) 1 package loadbalancers 2 3 import ( 4 "fmt" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 // ListOptsBuilder allows extensions to add additional parameters to the 11 // List request. 12 type ListOptsBuilder interface { 13 ToLoadBalancerListQuery() (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 Loadbalancer attributes you want to see returned. SortKey allows you to 19 // sort by a particular attribute. SortDir sets the direction, and is 20 // either `asc' or `desc'. Marker and Limit are used for pagination. 21 type ListOpts struct { 22 Description string `q:"description"` 23 AdminStateUp *bool `q:"admin_state_up"` 24 TenantID string `q:"tenant_id"` 25 ProjectID string `q:"project_id"` 26 ProvisioningStatus string `q:"provisioning_status"` 27 VipAddress string `q:"vip_address"` 28 VipPortID string `q:"vip_port_id"` 29 VipSubnetID string `q:"vip_subnet_id"` 30 ID string `q:"id"` 31 OperatingStatus string `q:"operating_status"` 32 Name string `q:"name"` 33 FlavorID string `q:"flavor_id"` 34 Provider string `q:"provider"` 35 Limit int `q:"limit"` 36 Marker string `q:"marker"` 37 SortKey string `q:"sort_key"` 38 SortDir string `q:"sort_dir"` 39 } 40 41 // ToLoadBalancerListQuery formats a ListOpts into a query string. 42 func (opts ListOpts) ToLoadBalancerListQuery() (string, error) { 43 q, err := gophercloud.BuildQueryString(opts) 44 return q.String(), err 45 } 46 47 // List returns a Pager which allows you to iterate over a collection of 48 // load balancers. It accepts a ListOpts struct, which allows you to filter 49 // and sort the returned collection for greater efficiency. 50 // 51 // Default policy settings return only those load balancers that are owned by 52 // the tenant who submits the request, unless an admin user submits the request. 53 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 54 url := rootURL(c) 55 if opts != nil { 56 query, err := opts.ToLoadBalancerListQuery() 57 if err != nil { 58 return pagination.Pager{Err: err} 59 } 60 url += query 61 } 62 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 63 return LoadBalancerPage{pagination.LinkedPageBase{PageResult: r}} 64 }) 65 } 66 67 // CreateOptsBuilder allows extensions to add additional parameters to the 68 // Create request. 69 type CreateOptsBuilder interface { 70 ToLoadBalancerCreateMap() (map[string]interface{}, error) 71 } 72 73 // CreateOpts is the common options struct used in this package's Create 74 // operation. 75 type CreateOpts struct { 76 // Human-readable name for the Loadbalancer. Does not have to be unique. 77 Name string `json:"name,omitempty"` 78 79 // Human-readable description for the Loadbalancer. 80 Description string `json:"description,omitempty"` 81 82 // The network on which to allocate the Loadbalancer's address. A tenant can 83 // only create Loadbalancers on networks authorized by policy (e.g. networks 84 // that belong to them or networks that are shared). 85 VipSubnetID string `json:"vip_subnet_id" required:"true"` 86 87 // TenantID is the UUID of the project who owns the Loadbalancer. 88 // Only administrative users can specify a project UUID other than their own. 89 TenantID string `json:"tenant_id,omitempty"` 90 91 // ProjectID is the UUID of the project who owns the Loadbalancer. 92 // Only administrative users can specify a project UUID other than their own. 93 ProjectID string `json:"project_id,omitempty"` 94 95 // The IP address of the Loadbalancer. 96 VipAddress string `json:"vip_address,omitempty"` 97 98 // The administrative state of the Loadbalancer. A valid value is true (UP) 99 // or false (DOWN). 100 AdminStateUp *bool `json:"admin_state_up,omitempty"` 101 102 // The UUID of a flavor. 103 FlavorID string `json:"flavor_id,omitempty"` 104 105 // The name of the provider. 106 Provider string `json:"provider,omitempty"` 107 } 108 109 // ToLoadBalancerCreateMap builds a request body from CreateOpts. 110 func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) { 111 return gophercloud.BuildRequestBody(opts, "loadbalancer") 112 } 113 114 // Create is an operation which provisions a new loadbalancer based on the 115 // configuration defined in the CreateOpts struct. Once the request is 116 // validated and progress has started on the provisioning process, a 117 // CreateResult will be returned. 118 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 119 b, err := opts.ToLoadBalancerCreateMap() 120 if err != nil { 121 r.Err = err 122 return 123 } 124 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 125 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 126 return 127 } 128 129 // Get retrieves a particular Loadbalancer based on its unique ID. 130 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 131 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 132 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 133 return 134 } 135 136 // UpdateOptsBuilder allows extensions to add additional parameters to the 137 // Update request. 138 type UpdateOptsBuilder interface { 139 ToLoadBalancerUpdateMap() (map[string]interface{}, error) 140 } 141 142 // UpdateOpts is the common options struct used in this package's Update 143 // operation. 144 type UpdateOpts struct { 145 // Human-readable name for the Loadbalancer. Does not have to be unique. 146 Name *string `json:"name,omitempty"` 147 148 // Human-readable description for the Loadbalancer. 149 Description *string `json:"description,omitempty"` 150 151 // The administrative state of the Loadbalancer. A valid value is true (UP) 152 // or false (DOWN). 153 AdminStateUp *bool `json:"admin_state_up,omitempty"` 154 } 155 156 // ToLoadBalancerUpdateMap builds a request body from UpdateOpts. 157 func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) { 158 return gophercloud.BuildRequestBody(opts, "loadbalancer") 159 } 160 161 // Update is an operation which modifies the attributes of the specified 162 // LoadBalancer. 163 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 164 b, err := opts.ToLoadBalancerUpdateMap() 165 if err != nil { 166 r.Err = err 167 return 168 } 169 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 170 OkCodes: []int{200, 202}, 171 }) 172 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 173 return 174 } 175 176 // Delete will permanently delete a particular LoadBalancer based on its 177 // unique ID. 178 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 179 resp, err := c.Delete(resourceURL(c, id), nil) 180 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 181 return 182 } 183 184 // CascadingDelete is like `Delete`, but will also delete any of the load balancer's 185 // children (listener, monitor, etc). 186 // NOTE: This function will only work with Octavia load balancers; Neutron does not 187 // support this. 188 func CascadingDelete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 189 if c.Type != "load-balancer" { 190 r.Err = fmt.Errorf("error prior to running cascade delete: only Octavia LBs supported") 191 return 192 } 193 u := fmt.Sprintf("%s?cascade=true", resourceURL(c, id)) 194 resp, err := c.Delete(u, nil) 195 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 196 return 197 } 198 199 // GetStatuses will return the status of a particular LoadBalancer. 200 func GetStatuses(c *gophercloud.ServiceClient, id string) (r GetStatusesResult) { 201 resp, err := c.Get(statusRootURL(c, id), &r.Body, nil) 202 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 203 return 204 } 205 206 // GetStats will return the shows the current statistics of a particular LoadBalancer. 207 func GetStats(c *gophercloud.ServiceClient, id string) (r StatsResult) { 208 resp, err := c.Get(statisticsRootURL(c, id), &r.Body, nil) 209 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 210 return 211 }