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