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 | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 6 | |
| 7 | "github.com/racker/perigee" |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 8 | ) |
| 9 | |
Jamie Hannaford | 32979b6 | 2014-10-09 10:32:39 +0200 | [diff] [blame] | 10 | // AdminState gives users a solid type to work with for create and update |
| 11 | // operations. It is recommended that users use the `Up` and `Down` enums. |
| 12 | type AdminState *bool |
| 13 | |
| 14 | // Convenience vars for AdminStateUp values. |
| 15 | var ( |
| 16 | iTrue = true |
| 17 | iFalse = false |
| 18 | |
| 19 | Up AdminState = &iTrue |
| 20 | Down AdminState = &iFalse |
| 21 | ) |
| 22 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 23 | type networkOpts struct { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 24 | AdminStateUp *bool |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 25 | Name string |
| 26 | Shared *bool |
| 27 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 28 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 29 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 30 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 31 | // List request. |
| 32 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 33 | ToNetworkListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 36 | // ListOpts allows the filtering and sorting of paginated collections through |
| 37 | // the API. Filtering is achieved by passing in struct field values that map to |
| 38 | // the network attributes you want to see returned. SortKey allows you to sort |
| 39 | // by a particular network attribute. SortDir sets the direction, and is either |
| 40 | // `asc' or `desc'. Marker and Limit are used for pagination. |
| 41 | type ListOpts struct { |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 42 | Status string `q:"status"` |
| 43 | Name string `q:"name"` |
| 44 | AdminStateUp *bool `q:"admin_state_up"` |
| 45 | TenantID string `q:"tenant_id"` |
| 46 | Shared *bool `q:"shared"` |
| 47 | ID string `q:"id"` |
| 48 | Marker string `q:"marker"` |
| 49 | Limit int `q:"limit"` |
| 50 | SortKey string `q:"sort_key"` |
| 51 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 54 | // ToNetworkListQuery formats a ListOpts into a query string. |
| 55 | func (opts ListOpts) ToNetworkListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 56 | q, err := gophercloud.BuildQueryString(opts) |
| 57 | if err != nil { |
| 58 | return "", err |
| 59 | } |
| 60 | return q.String(), nil |
| 61 | } |
| 62 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 63 | // List returns a Pager which allows you to iterate over a collection of |
| 64 | // networks. It accepts a ListOpts struct, which allows you to filter and sort |
| 65 | // the returned collection for greater efficiency. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 66 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 67 | url := listURL(c) |
| 68 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 69 | query, err := opts.ToNetworkListQuery() |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 70 | if err != nil { |
| 71 | return pagination.Pager{Err: err} |
| 72 | } |
| 73 | url += query |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 74 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 75 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame^] | 76 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 77 | return NetworkPage{pagination.LinkedPageBase{PageResult: 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 | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 84 | _, res.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(), |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 86 | Results: &res.Body, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 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 | 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 | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 92 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 93 | // to be used in the main Create operation in this package. Since many |
| 94 | // extensions decorate or modify the common logic, it is useful for them to |
| 95 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 96 | type CreateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 97 | ToNetworkCreateMap() (map[string]interface{}, error) |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 100 | // CreateOpts is the common options struct used in this package's Create |
| 101 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 102 | type CreateOpts networkOpts |
| 103 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 104 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 105 | func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { |
| 106 | n := make(map[string]interface{}) |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 107 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 108 | if opts.AdminStateUp != nil { |
| 109 | n["admin_state_up"] = &opts.AdminStateUp |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 110 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 111 | if opts.Name != "" { |
| 112 | n["name"] = opts.Name |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 113 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 114 | if opts.Shared != nil { |
| 115 | n["shared"] = &opts.Shared |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 116 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 117 | if opts.TenantID != "" { |
| 118 | n["tenant_id"] = opts.TenantID |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 121 | return map[string]interface{}{"network": n}, nil |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 122 | } |
| 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 | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 131 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 132 | var res CreateResult |
| 133 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 134 | reqBody, err := opts.ToNetworkCreateMap() |
| 135 | if err != nil { |
| 136 | res.Err = err |
| 137 | return res |
| 138 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 139 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 140 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 141 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 142 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 143 | ReqBody: &reqBody, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 144 | Results: &res.Body, |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 145 | OkCodes: []int{201}, |
| 146 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 147 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 148 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 149 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 150 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 151 | // to be used in the main Update operation in this package. Since many |
| 152 | // extensions decorate or modify the common logic, it is useful for them to |
| 153 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 154 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 155 | ToNetworkUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 158 | // UpdateOpts is the common options struct used in this package's Update |
| 159 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 160 | type UpdateOpts networkOpts |
| 161 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 162 | // ToNetworkUpdateMap casts a UpdateOpts struct to a map. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 163 | func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) { |
| 164 | n := make(map[string]interface{}) |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 165 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 166 | if opts.AdminStateUp != nil { |
| 167 | n["admin_state_up"] = &opts.AdminStateUp |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 168 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 169 | if opts.Name != "" { |
| 170 | n["name"] = opts.Name |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 171 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 172 | if opts.Shared != nil { |
| 173 | n["shared"] = &opts.Shared |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 176 | return map[string]interface{}{"network": n}, nil |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 179 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 180 | // values provided. For more information, see the Create function. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 181 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 182 | var res UpdateResult |
| 183 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 184 | reqBody, err := opts.ToNetworkUpdateMap() |
| 185 | if err != nil { |
| 186 | res.Err = err |
| 187 | return res |
| 188 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 189 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 190 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 191 | _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 192 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 193 | ReqBody: &reqBody, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 194 | Results: &res.Body, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 195 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 196 | }) |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 197 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 198 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 199 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 200 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 201 | // Delete accepts a unique ID and deletes the network associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 202 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 203 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 204 | _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 205 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 206 | OkCodes: []int{204}, |
| 207 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 208 | return res |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 209 | } |