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 | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 92 | // CreateOpts represents the attributes used when creating a new network. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 93 | type CreateOpts networkOpts |
| 94 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 95 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 96 | // provided. This operation does not actually require a request body, i.e. the |
| 97 | // CreateOpts struct argument can be empty. |
| 98 | // |
| 99 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 100 | // network. An admin user, however, has the option of specifying another tenant |
| 101 | // ID in the CreateOpts struct. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 102 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 103 | // Define structures |
| 104 | type network struct { |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 105 | AdminStateUp bool `json:"admin_state_up,omitempty"` |
| 106 | Name string `json:"name,omitempty"` |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 107 | Shared *bool `json:"shared,omitempty"` |
| 108 | TenantID *string `json:"tenant_id,omitempty"` |
| 109 | } |
| 110 | type request struct { |
| 111 | Network network `json:"network"` |
| 112 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 113 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 114 | // Populate request body |
| 115 | reqBody := request{Network: network{ |
| 116 | AdminStateUp: opts.AdminStateUp, |
| 117 | Name: opts.Name, |
| 118 | Shared: opts.Shared, |
| 119 | }} |
| 120 | |
| 121 | if opts.TenantID != "" { |
| 122 | reqBody.Network.TenantID = &opts.TenantID |
| 123 | } |
| 124 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 125 | // Send request to API |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 126 | var res CreateResult |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 127 | _, err := perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 128 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 129 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 130 | Results: &res.Resp, |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 131 | OkCodes: []int{201}, |
| 132 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 133 | res.Err = err |
| 134 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 135 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 136 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 137 | // UpdateOpts represents the attributes used when updating an existing network. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 138 | type UpdateOpts networkOpts |
| 139 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 140 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 141 | // values provided. For more information, see the Create function. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 142 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 143 | // Define structures |
| 144 | type network struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 145 | AdminStateUp bool `json:"admin_state_up"` |
| 146 | Name string `json:"name"` |
| 147 | Shared *bool `json:"shared,omitempty"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | type request struct { |
| 151 | Network network `json:"network"` |
| 152 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 153 | |
| 154 | // Populate request body |
| 155 | reqBody := request{Network: network{ |
| 156 | AdminStateUp: opts.AdminStateUp, |
| 157 | Name: opts.Name, |
| 158 | Shared: opts.Shared, |
| 159 | }} |
| 160 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 161 | // Send request to API |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 162 | var res UpdateResult |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 163 | _, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 164 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 165 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 166 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 167 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 168 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 169 | res.Err = err |
| 170 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 171 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 172 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 173 | // Delete accepts a unique ID and deletes the network associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 174 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 175 | var res DeleteResult |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 176 | _, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 177 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 178 | OkCodes: []int{204}, |
| 179 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 180 | res.Err = err |
| 181 | return res |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 182 | } |