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