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