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