|
| 1 | +package loadbalancers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/rackspace/gophercloud" |
| 7 | + "github.com/rackspace/gophercloud/pagination" |
| 8 | +) |
| 9 | + |
| 10 | +// AdminState gives users a solid type to work with for create and update |
| 11 | +// operations. It is recommended that users use the `Up` and `Down` enums. |
| 12 | +type AdminState *bool |
| 13 | + |
| 14 | +// Convenience vars for AdminStateUp values. |
| 15 | +var ( |
| 16 | + iTrue = true |
| 17 | + iFalse = false |
| 18 | + |
| 19 | + Up AdminState = &iTrue |
| 20 | + Down AdminState = &iFalse |
| 21 | +) |
| 22 | + |
| 23 | +// ListOpts allows the filtering and sorting of paginated collections through |
| 24 | +// the API. Filtering is achieved by passing in struct field values that map to |
| 25 | +// the Loadbalancer attributes you want to see returned. SortKey allows you to |
| 26 | +// sort by a particular attribute. SortDir sets the direction, and is |
| 27 | +// either `asc' or `desc'. Marker and Limit are used for pagination. |
| 28 | +type ListOpts struct { |
| 29 | + Description string `q:"description"` |
| 30 | + AdminStateUp *bool `q:"admin_state_up"` |
| 31 | + TenantID string `q:"tenant_id"` |
| 32 | + ProvisioningStatus string `q:"provisioning_status"` |
| 33 | + VipAddress string `q:"vip_address"` |
| 34 | + VipSubnetID string `q:"vip_subnet_id"` |
| 35 | + ID string `q:"id"` |
| 36 | + OperatingStatus string `q:"operating_status"` |
| 37 | + Name string `q:"name"` |
| 38 | + Flavor string `q:"flavor"` |
| 39 | + Provider string `q:"provider"` |
| 40 | + Limit int `q:"limit"` |
| 41 | + Marker string `q:"marker"` |
| 42 | + SortKey string `q:"sort_key"` |
| 43 | + SortDir string `q:"sort_dir"` |
| 44 | +} |
| 45 | + |
| 46 | +// List returns a Pager which allows you to iterate over a collection of |
| 47 | +// routers. It accepts a ListOpts struct, which allows you to filter and sort |
| 48 | +// the returned collection for greater efficiency. |
| 49 | +// |
| 50 | +// Default policy settings return only those routers that are owned by the |
| 51 | +// tenant who submits the request, unless an admin user submits the request. |
| 52 | +func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 53 | + q, err := gophercloud.BuildQueryString(&opts) |
| 54 | + if err != nil { |
| 55 | + return pagination.Pager{Err: err} |
| 56 | + } |
| 57 | + u := rootURL(c) + q.String() |
| 58 | + return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { |
| 59 | + return LoadbalancerPage{pagination.LinkedPageBase{PageResult: r}} |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +var ( |
| 64 | + errVipSubnetIDRequried = fmt.Errorf("VipSubnetID is required") |
| 65 | +) |
| 66 | + |
| 67 | +// CreateOpts contains all the values needed to create a new Loadbalancer. |
| 68 | +type CreateOpts struct { |
| 69 | + // Optional. Human-readable name for the Loadbalancer. Does not have to be unique. |
| 70 | + Name string |
| 71 | + |
| 72 | + // Optional. Human-readable description for the Loadbalancer. |
| 73 | + Description string |
| 74 | + |
| 75 | + // Required. The network on which to allocate the Loadbalancer's address. A tenant can |
| 76 | + // only create Loadbalancers on networks authorized by policy (e.g. networks that |
| 77 | + // belong to them or networks that are shared). |
| 78 | + VipSubnetID string |
| 79 | + |
| 80 | + // Required for admins. The UUID of the tenant who owns the Loadbalancer. |
| 81 | + // Only administrative users can specify a tenant UUID other than their own. |
| 82 | + TenantID string |
| 83 | + |
| 84 | + // Optional. The IP address of the Loadbalancer. |
| 85 | + VipAddress string |
| 86 | + |
| 87 | + // Optional. The administrative state of the Loadbalancer. A valid value is true (UP) |
| 88 | + // or false (DOWN). |
| 89 | + AdminStateUp *bool |
| 90 | + |
| 91 | + // Optional. The UUID of a flavor. |
| 92 | + Flavor string |
| 93 | + |
| 94 | + // Optional. The name of the provider. |
| 95 | + Provider string |
| 96 | +} |
| 97 | + |
| 98 | +// Create is an operation which provisions a new loadbalancer based on the |
| 99 | +// configuration defined in the CreateOpts struct. Once the request is |
| 100 | +// validated and progress has started on the provisioning process, a |
| 101 | +// CreateResult will be returned. |
| 102 | +// |
| 103 | +// Users with an admin role can create loadbalancers on behalf of other tenants by |
| 104 | +// specifying a TenantID attribute different than their own. |
| 105 | +func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 106 | + var res CreateResult |
| 107 | + |
| 108 | + // Validate required opts |
| 109 | + if opts.VipSubnetID == "" { |
| 110 | + res.Err = errVipSubnetIDRequried |
| 111 | + return res |
| 112 | + } |
| 113 | + |
| 114 | + type loadbalancer struct { |
| 115 | + Name *string `json:"name,omitempty"` |
| 116 | + Description *string `json:"description,omitempty"` |
| 117 | + VipSubnetID string `json:"vip_subnet_id"` |
| 118 | + TenantID *string `json:"tenant_id,omitempty"` |
| 119 | + VipAddress *string `json:"vip_address,omitempty"` |
| 120 | + AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 121 | + Flavor *string `json:"flavor,omitempty"` |
| 122 | + Provider *string `json:"provider,omitempty"` |
| 123 | + } |
| 124 | + |
| 125 | + type request struct { |
| 126 | + Loadbalancer loadbalancer `json:"loadbalancer"` |
| 127 | + } |
| 128 | + |
| 129 | + reqBody := request{Loadbalancer: loadbalancer{ |
| 130 | + Name: gophercloud.MaybeString(opts.Name), |
| 131 | + Description: gophercloud.MaybeString(opts.Description), |
| 132 | + VipSubnetID: opts.VipSubnetID, |
| 133 | + TenantID: gophercloud.MaybeString(opts.TenantID), |
| 134 | + VipAddress: gophercloud.MaybeString(opts.VipAddress), |
| 135 | + AdminStateUp: opts.AdminStateUp, |
| 136 | + Flavor: gophercloud.MaybeString(opts.Flavor), |
| 137 | + Provider: gophercloud.MaybeString(opts.Provider), |
| 138 | + }} |
| 139 | + |
| 140 | + _, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil) |
| 141 | + return res |
| 142 | +} |
| 143 | + |
| 144 | +// Get retrieves a particular virtual IP based on its unique ID. |
| 145 | +func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 146 | + var res GetResult |
| 147 | + _, res.Err = c.Get(resourceURL(c, id), &res.Body, nil) |
| 148 | + return res |
| 149 | +} |
| 150 | + |
| 151 | +// UpdateOpts contains all the values needed to update an existing virtual |
| 152 | +// Loadbalancer. Attributes not listed here but appear in CreateOpts are |
| 153 | +// immutable and cannot be updated. |
| 154 | +type UpdateOpts struct { |
| 155 | + // Optional. Human-readable name for the Loadbalancer. Does not have to be unique. |
| 156 | + Name string |
| 157 | + |
| 158 | + // Optional. Human-readable description for the Loadbalancer. |
| 159 | + Description string |
| 160 | + |
| 161 | + // Optional. The administrative state of the Loadbalancer. A valid value is true (UP) |
| 162 | + // or false (DOWN). |
| 163 | + AdminStateUp *bool |
| 164 | +} |
| 165 | + |
| 166 | +// Update is an operation which modifies the attributes of the specified Loadbalancer. |
| 167 | +func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
| 168 | + type loadbalancer struct { |
| 169 | + Name *string `json:"name,omitempty"` |
| 170 | + Description *string `json:"description,omitempty"` |
| 171 | + AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 172 | + } |
| 173 | + |
| 174 | + type request struct { |
| 175 | + Loadbalancer loadbalancer `json:"loadbalancer"` |
| 176 | + } |
| 177 | + |
| 178 | + reqBody := request{Loadbalancer: loadbalancer{ |
| 179 | + Name: gophercloud.MaybeString(opts.Name), |
| 180 | + Description: gophercloud.MaybeString(opts.Description), |
| 181 | + AdminStateUp: opts.AdminStateUp, |
| 182 | + }} |
| 183 | + |
| 184 | + var res UpdateResult |
| 185 | + _, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 186 | + OkCodes: []int{200, 202}, |
| 187 | + }) |
| 188 | + |
| 189 | + return res |
| 190 | +} |
| 191 | + |
| 192 | +// Delete will permanently delete a particular Loadbalancer based on its unique ID. |
| 193 | +func Delete(c *gophercloud.ServiceClient, id string) DeleteResult { |
| 194 | + var res DeleteResult |
| 195 | + _, res.Err = c.Delete(resourceURL(c, id), nil) |
| 196 | + return res |
| 197 | +} |
0 commit comments