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 ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | 32979b6 | 2014-10-09 10:32:39 +0200 | [diff] [blame] | 9 | // AdminState gives users a solid type to work with for create and update |
| 10 | // operations. It is recommended that users use the `Up` and `Down` enums. |
| 11 | type AdminState *bool |
| 12 | |
| 13 | // Convenience vars for AdminStateUp values. |
| 14 | var ( |
| 15 | iTrue = true |
| 16 | iFalse = false |
| 17 | |
| 18 | Up AdminState = &iTrue |
| 19 | Down AdminState = &iFalse |
| 20 | ) |
| 21 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 22 | type networkOpts struct { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 23 | AdminStateUp *bool |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 24 | Name string |
| 25 | Shared *bool |
| 26 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 27 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 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 { |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 35 | Status string `q:"status"` |
| 36 | Name string `q:"name"` |
| 37 | AdminStateUp *bool `q:"admin_state_up"` |
| 38 | TenantID string `q:"tenant_id"` |
| 39 | Shared *bool `q:"shared"` |
| 40 | ID string `q:"id"` |
| 41 | Marker string `q:"marker"` |
| 42 | Limit int `q:"limit"` |
| 43 | SortKey string `q:"sort_key"` |
| 44 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 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 |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 52 | q, err := gophercloud.BuildQueryString(&opts) |
| 53 | if err != nil { |
| 54 | return pagination.Pager{Err: err} |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 55 | } |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 56 | u := listURL(c) + q.String() |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 57 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 58 | return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 59 | }) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 62 | // Get retrieves a specific network based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 63 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 64 | var res GetResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 65 | _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 66 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 67 | Results: &res.Resp, |
| 68 | OkCodes: []int{200}, |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 69 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 70 | return res |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 71 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 72 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 73 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 74 | // to be used in the main Create operation in this package. Since many |
| 75 | // extensions decorate or modify the common logic, it is useful for them to |
| 76 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 77 | type CreateOptsBuilder interface { |
| 78 | ToNetworkCreateMap() map[string]map[string]interface{} |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 81 | // CreateOpts is the common options struct used in this package's Create |
| 82 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 83 | type CreateOpts networkOpts |
| 84 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 85 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 86 | func (o CreateOpts) ToNetworkCreateMap() map[string]map[string]interface{} { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 87 | inner := make(map[string]interface{}) |
| 88 | |
| 89 | if o.AdminStateUp != nil { |
| 90 | inner["admin_state_up"] = &o.AdminStateUp |
| 91 | } |
| 92 | if o.Name != "" { |
| 93 | inner["name"] = o.Name |
| 94 | } |
| 95 | if o.Shared != nil { |
| 96 | inner["shared"] = &o.Shared |
| 97 | } |
| 98 | if o.TenantID != "" { |
| 99 | inner["tenant_id"] = o.TenantID |
| 100 | } |
| 101 | |
| 102 | outer := make(map[string]map[string]interface{}) |
| 103 | outer["network"] = inner |
| 104 | |
| 105 | return outer |
| 106 | } |
| 107 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 108 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 109 | // provided. This operation does not actually require a request body, i.e. the |
| 110 | // CreateOpts struct argument can be empty. |
| 111 | // |
| 112 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 113 | // network. An admin user, however, has the option of specifying another tenant |
| 114 | // ID in the CreateOpts struct. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 115 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 116 | var res CreateResult |
| 117 | |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 118 | reqBody := opts.ToNetworkCreateMap() |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 119 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 120 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 121 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 122 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 123 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 124 | Results: &res.Resp, |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 125 | OkCodes: []int{201}, |
| 126 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 127 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 128 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 129 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 130 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 131 | // to be used in the main Update operation in this package. Since many |
| 132 | // extensions decorate or modify the common logic, it is useful for them to |
| 133 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 134 | type UpdateOptsBuilder interface { |
| 135 | ToNetworkUpdateMap() map[string]map[string]interface{} |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 138 | // UpdateOpts is the common options struct used in this package's Update |
| 139 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 140 | type UpdateOpts networkOpts |
| 141 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 142 | // ToNetworkUpdateMap casts a UpdateOpts struct to a map. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 143 | func (o UpdateOpts) ToNetworkUpdateMap() map[string]map[string]interface{} { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 144 | inner := make(map[string]interface{}) |
| 145 | |
| 146 | if o.AdminStateUp != nil { |
| 147 | inner["admin_state_up"] = &o.AdminStateUp |
| 148 | } |
| 149 | if o.Name != "" { |
| 150 | inner["name"] = o.Name |
| 151 | } |
| 152 | if o.Shared != nil { |
| 153 | inner["shared"] = &o.Shared |
| 154 | } |
| 155 | |
| 156 | outer := make(map[string]map[string]interface{}) |
| 157 | outer["network"] = inner |
| 158 | |
| 159 | return outer |
| 160 | } |
| 161 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 162 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 163 | // values provided. For more information, see the Create function. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 164 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 165 | var res UpdateResult |
| 166 | |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 167 | reqBody := opts.ToNetworkUpdateMap() |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 168 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 169 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 170 | _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 171 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 172 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 173 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 174 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 175 | }) |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 176 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 177 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 178 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 179 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 180 | // Delete accepts a unique ID and deletes the network associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 181 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 182 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 183 | _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 184 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 185 | OkCodes: []int{204}, |
| 186 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 187 | return res |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 188 | } |