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