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 | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 9 | type networkOpts struct { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 10 | AdminStateUp *bool |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 11 | Name string |
| 12 | Shared *bool |
| 13 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 14 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 15 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 16 | // ListOpts allows the filtering and sorting of paginated collections through |
| 17 | // the API. Filtering is achieved by passing in struct field values that map to |
| 18 | // the network attributes you want to see returned. SortKey allows you to sort |
| 19 | // by a particular network attribute. SortDir sets the direction, and is either |
| 20 | // `asc' or `desc'. Marker and Limit are used for pagination. |
| 21 | type ListOpts struct { |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 22 | Status string `q:"status"` |
| 23 | Name string `q:"name"` |
| 24 | AdminStateUp *bool `q:"admin_state_up"` |
| 25 | TenantID string `q:"tenant_id"` |
| 26 | Shared *bool `q:"shared"` |
| 27 | ID string `q:"id"` |
| 28 | Marker string `q:"marker"` |
| 29 | Limit int `q:"limit"` |
| 30 | SortKey string `q:"sort_key"` |
| 31 | SortDir string `q:"sort_dir"` |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // List returns a Pager which allows you to iterate over a collection of |
| 35 | // networks. It accepts a ListOpts struct, which allows you to filter and sort |
| 36 | // the returned collection for greater efficiency. |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 37 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 38 | // Build query parameters |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 39 | q, err := gophercloud.BuildQueryString(&opts) |
| 40 | if err != nil { |
| 41 | return pagination.Pager{Err: err} |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 42 | } |
Jamie Hannaford | 92523e3 | 2014-10-02 11:08:36 +0200 | [diff] [blame] | 43 | u := listURL(c) + q.String() |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 44 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 45 | return NetworkPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 46 | }) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 49 | // Get retrieves a specific network based on its unique ID. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 50 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
| 51 | var res GetResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 52 | _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 53 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 54 | Results: &res.Resp, |
| 55 | OkCodes: []int{200}, |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 56 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 57 | return res |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 58 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 59 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 60 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 61 | // to be used in the main Create operation in this package. Since many |
| 62 | // extensions decorate or modify the common logic, it is useful for them to |
| 63 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 64 | type CreateOptsBuilder interface { |
| 65 | ToNetworkCreateMap() map[string]map[string]interface{} |
Jamie Hannaford | 9823bb6 | 2014-09-26 17:06:36 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 68 | // CreateOpts is the common options struct used in this package's Create |
| 69 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 70 | type CreateOpts networkOpts |
| 71 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 72 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 73 | func (o CreateOpts) ToNetworkCreateMap() map[string]map[string]interface{} { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 74 | inner := make(map[string]interface{}) |
| 75 | |
| 76 | if o.AdminStateUp != nil { |
| 77 | inner["admin_state_up"] = &o.AdminStateUp |
| 78 | } |
| 79 | if o.Name != "" { |
| 80 | inner["name"] = o.Name |
| 81 | } |
| 82 | if o.Shared != nil { |
| 83 | inner["shared"] = &o.Shared |
| 84 | } |
| 85 | if o.TenantID != "" { |
| 86 | inner["tenant_id"] = o.TenantID |
| 87 | } |
| 88 | |
| 89 | outer := make(map[string]map[string]interface{}) |
| 90 | outer["network"] = inner |
| 91 | |
| 92 | return outer |
| 93 | } |
| 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 | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 102 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 103 | var res CreateResult |
| 104 | |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 105 | reqBody := opts.ToNetworkCreateMap() |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 106 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 107 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 108 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 109 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 110 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 111 | Results: &res.Resp, |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 112 | OkCodes: []int{201}, |
| 113 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 114 | return res |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 115 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 116 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 117 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 118 | // to be used in the main Update operation in this package. Since many |
| 119 | // extensions decorate or modify the common logic, it is useful for them to |
| 120 | // satisfy a basic interface in order for them to be used. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 121 | type UpdateOptsBuilder interface { |
| 122 | ToNetworkUpdateMap() map[string]map[string]interface{} |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 125 | // UpdateOpts is the common options struct used in this package's Update |
| 126 | // operation. |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 127 | type UpdateOpts networkOpts |
| 128 | |
Jamie Hannaford | 35c91a6 | 2014-10-06 15:50:08 +0200 | [diff] [blame] | 129 | // ToNetworkUpdateMap casts a UpdateOpts struct to a map. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 130 | func (o UpdateOpts) ToNetworkUpdateMap() map[string]map[string]interface{} { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 131 | inner := make(map[string]interface{}) |
| 132 | |
| 133 | if o.AdminStateUp != nil { |
| 134 | inner["admin_state_up"] = &o.AdminStateUp |
| 135 | } |
| 136 | if o.Name != "" { |
| 137 | inner["name"] = o.Name |
| 138 | } |
| 139 | if o.Shared != nil { |
| 140 | inner["shared"] = &o.Shared |
| 141 | } |
| 142 | |
| 143 | outer := make(map[string]map[string]interface{}) |
| 144 | outer["network"] = inner |
| 145 | |
| 146 | return outer |
| 147 | } |
| 148 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 149 | // Update accepts a UpdateOpts struct and updates an existing network using the |
| 150 | // values provided. For more information, see the Create function. |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 151 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | 7db63f2 | 2014-09-29 11:18:45 +0200 | [diff] [blame] | 152 | var res UpdateResult |
| 153 | |
Jamie Hannaford | e3bb3f6 | 2014-10-06 09:40:27 +0200 | [diff] [blame] | 154 | reqBody := opts.ToNetworkUpdateMap() |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 155 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 156 | // Send request to API |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 157 | _, res.Err = perigee.Request("PUT", getURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 158 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 159 | ReqBody: &reqBody, |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 160 | Results: &res.Resp, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 161 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 162 | }) |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 163 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 164 | return res |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 165 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 166 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 167 | // Delete accepts a unique ID and deletes the network associated with it. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 168 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 169 | var res DeleteResult |
Jamie Hannaford | 6f57e9e | 2014-10-02 10:27:28 +0200 | [diff] [blame] | 170 | _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 171 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 172 | OkCodes: []int{204}, |
| 173 | }) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 174 | return res |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 175 | } |