Jamie Hannaford | a7f671a | 2014-09-11 10:25:08 +0200 | [diff] [blame] | 1 | package networks |
| 2 | |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 3 | import ( |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 4 | "fmt" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 5 | "strconv" |
| 6 | |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 7 | "github.com/racker/perigee" |
| 8 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/openstack/utils" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 13 | type networkOpts struct { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 14 | AdminStateUp *bool |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 15 | Name string |
| 16 | Shared *bool |
| 17 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 18 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 19 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 20 | // ListOpts allows the filtering and sorting of paginated collections through |
| 21 | // the API. Filtering is achieved by passing in struct field values that map to |
| 22 | // the network attributes you want to see returned. SortKey allows you to sort |
| 23 | // by a particular network attribute. SortDir sets the direction, and is either |
| 24 | // `asc' or `desc'. Marker and Limit are used for pagination. |
| 25 | type ListOpts struct { |
| 26 | Status string |
| 27 | Name string |
| 28 | AdminStateUp *bool |
| 29 | TenantID string |
| 30 | Shared *bool |
| 31 | ID string |
| 32 | Marker string |
| 33 | Limit int |
| 34 | SortKey string |
| 35 | SortDir string |
| 36 | } |
| 37 | |
| 38 | // List returns a Pager which allows you to iterate over a collection of |
| 39 | // networks. It accepts a ListOpts struct, which allows you to filter and sort |
| 40 | // the returned collection for greater efficiency. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 41 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 42 | // Build query parameters |
| 43 | q := make(map[string]string) |
| 44 | if opts.Status != "" { |
| 45 | q["status"] = opts.Status |
| 46 | } |
| 47 | if opts.Name != "" { |
| 48 | q["name"] = opts.Name |
| 49 | } |
| 50 | if opts.AdminStateUp != nil { |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 51 | q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 52 | } |
| 53 | if opts.TenantID != "" { |
| 54 | q["tenant_id"] = opts.TenantID |
| 55 | } |
| 56 | if opts.Shared != nil { |
Jamie Hannaford | 6abf928 | 2014-09-24 10:54:13 +0200 | [diff] [blame] | 57 | q["shared"] = strconv.FormatBool(*opts.Shared) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 58 | } |
| 59 | if opts.ID != "" { |
| 60 | q["id"] = opts.ID |
| 61 | } |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 62 | if opts.Marker != "" { |
| 63 | q["marker"] = opts.Marker |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 64 | } |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 65 | if opts.Limit != 0 { |
| 66 | q["limit"] = strconv.Itoa(opts.Limit) |
| 67 | } |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 68 | if opts.SortKey != "" { |
| 69 | q["sort_key"] = opts.SortKey |
| 70 | } |
| 71 | if opts.SortDir != "" { |
| 72 | q["sort_dir"] = opts.SortDir |
| 73 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 74 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 75 | u := listURL(c) + utils.BuildQuery(q) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 76 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 77 | return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 78 | }) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 81 | // Get retrieves a specific network based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 82 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 83 | var res GetResult |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 84 | _, err := perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 85 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 86 | Results: &res.Resp, |
| 87 | OkCodes: []int{200}, |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 88 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 89 | res.Err = err |
| 90 | return res |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 91 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 92 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 93 | type CreateOptsInt interface { |
| 94 | ToMap() map[string]map[string]interface{} |
| 95 | IsCreateOpts() bool |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 98 | type CreateOpts networkOpts |
| 99 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 100 | func (o CreateOpts) ToMap() map[string]map[string]interface{} { |
| 101 | inner := make(map[string]interface{}) |
| 102 | |
| 103 | if o.AdminStateUp != nil { |
| 104 | inner["admin_state_up"] = &o.AdminStateUp |
| 105 | } |
| 106 | if o.Name != "" { |
| 107 | inner["name"] = o.Name |
| 108 | } |
| 109 | if o.Shared != nil { |
| 110 | inner["shared"] = &o.Shared |
| 111 | } |
| 112 | if o.TenantID != "" { |
| 113 | inner["tenant_id"] = o.TenantID |
| 114 | } |
| 115 | |
| 116 | outer := make(map[string]map[string]interface{}) |
| 117 | outer["network"] = inner |
| 118 | |
| 119 | return outer |
| 120 | } |
| 121 | |
| 122 | func (o CreateOpts) IsCreateOpts() bool { return true } |
| 123 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 124 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 125 | // provided. This operation does not actually require a request body, i.e. the |
| 126 | // CreateOpts struct argument can be empty. |
| 127 | // |
| 128 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 129 | // network. An admin user, however, has the option of specifying another tenant |
| 130 | // ID in the CreateOpts struct. |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 131 | func Create(c *gophercloud.ServiceClient, opts CreateOptsInt) CreateResult { |
| 132 | var res CreateResult |
| 133 | |
| 134 | if opts.IsCreateOpts() != true { |
| 135 | res.Err = fmt.Errorf("Must provide valid create opts") |
| 136 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 137 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 138 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 139 | reqBody := opts.ToMap() |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 140 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 141 | // Send request to API |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 142 | _, err := perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 143 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 144 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 145 | Results: &res.Resp, |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 146 | OkCodes: []int{201}, |
| 147 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 148 | res.Err = err |
| 149 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 150 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 151 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 152 | type UpdateOptsInt interface { |
| 153 | ToMap() map[string]map[string]interface{} |
| 154 | IsUpdateOpts() bool |
| 155 | } |
| 156 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 157 | type UpdateOpts networkOpts |
| 158 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 159 | func (o UpdateOpts) ToMap() map[string]map[string]interface{} { |
| 160 | inner := make(map[string]interface{}) |
| 161 | |
| 162 | if o.AdminStateUp != nil { |
| 163 | inner["admin_state_up"] = &o.AdminStateUp |
| 164 | } |
| 165 | if o.Name != "" { |
| 166 | inner["name"] = o.Name |
| 167 | } |
| 168 | if o.Shared != nil { |
| 169 | inner["shared"] = &o.Shared |
| 170 | } |
| 171 | |
| 172 | outer := make(map[string]map[string]interface{}) |
| 173 | outer["network"] = inner |
| 174 | |
| 175 | return outer |
| 176 | } |
| 177 | |
| 178 | func (o UpdateOpts) IsUpdateOpts() bool { return true } |
| 179 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 180 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 181 | // values provided. For more information, see the Create function. |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 182 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsInt) UpdateResult { |
| 183 | var res UpdateResult |
| 184 | |
| 185 | if opts.IsUpdateOpts() != true { |
| 186 | res.Err = fmt.Errorf("Must provide valid update opts") |
| 187 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame^] | 190 | reqBody := opts.ToMap() |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 191 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 192 | // Send request to API |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 193 | _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 194 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 195 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 196 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 197 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 198 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 199 | res.Err = err |
| 200 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 201 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 202 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 203 | // Delete accepts a unique ID and deletes the network associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 204 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 205 | var res DeleteResult |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 206 | _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 207 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 208 | OkCodes: []int{204}, |
| 209 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 210 | res.Err = err |
| 211 | return res |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 212 | } |