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 | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 4 | "strconv" |
| 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 | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/utils" |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 10 | ) |
| 11 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 12 | type ListOpts struct { |
| 13 | Status string |
| 14 | Name string |
| 15 | AdminStateUp *bool |
| 16 | TenantID string |
| 17 | Shared *bool |
| 18 | ID string |
| 19 | Page int |
| 20 | PerPage int |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 21 | Limit int |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 22 | SortKey string |
| 23 | SortDir string |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame^] | 26 | type networkOpts struct { |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 27 | AdminStateUp bool |
| 28 | Name string |
| 29 | Shared *bool |
| 30 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 31 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 32 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 33 | func ptrToStr(val *bool) string { |
| 34 | if *val == true { |
| 35 | return "true" |
| 36 | } else if *val == false { |
| 37 | return "false" |
| 38 | } else { |
| 39 | return "" |
| 40 | } |
| 41 | } |
| 42 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 43 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 44 | // Build query parameters |
| 45 | q := make(map[string]string) |
| 46 | if opts.Status != "" { |
| 47 | q["status"] = opts.Status |
| 48 | } |
| 49 | if opts.Name != "" { |
| 50 | q["name"] = opts.Name |
| 51 | } |
| 52 | if opts.AdminStateUp != nil { |
| 53 | q["admin_state_up"] = ptrToStr(opts.AdminStateUp) |
| 54 | } |
| 55 | if opts.TenantID != "" { |
| 56 | q["tenant_id"] = opts.TenantID |
| 57 | } |
| 58 | if opts.Shared != nil { |
| 59 | q["shared"] = ptrToStr(opts.Shared) |
| 60 | } |
| 61 | if opts.ID != "" { |
| 62 | q["id"] = opts.ID |
| 63 | } |
| 64 | if opts.Page != 0 { |
| 65 | q["page"] = strconv.Itoa(opts.Page) |
| 66 | } |
| 67 | if opts.PerPage != 0 { |
| 68 | q["per_page"] = strconv.Itoa(opts.PerPage) |
| 69 | } |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 70 | if opts.Limit != 0 { |
| 71 | q["limit"] = strconv.Itoa(opts.Limit) |
| 72 | } |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 73 | if opts.SortKey != "" { |
| 74 | q["sort_key"] = opts.SortKey |
| 75 | } |
| 76 | if opts.SortDir != "" { |
| 77 | q["sort_dir"] = opts.SortDir |
| 78 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 79 | |
| 80 | u := ListURL(c) + utils.BuildQuery(q) |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 81 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
| 82 | return NetworkPage{pagination.LinkedPageBase(r)} |
| 83 | }) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func Get(c *gophercloud.ServiceClient, id string) (*Network, error) { |
| 87 | var n Network |
| 88 | _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{ |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 89 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 90 | Results: &struct { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 91 | Network *Network `json:"network"` |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 92 | }{&n}, |
| 93 | OkCodes: []int{200}, |
| 94 | }) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | return &n, nil |
| 99 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 100 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame^] | 101 | type CreateOpts networkOpts |
| 102 | |
| 103 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*NetworkCreateResult, error) { |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 104 | // Define structures |
| 105 | type network struct { |
| 106 | AdminStateUp bool `json:"admin_state_up"` |
| 107 | Name string `json:"name"` |
| 108 | Shared *bool `json:"shared,omitempty"` |
| 109 | TenantID *string `json:"tenant_id,omitempty"` |
| 110 | } |
| 111 | type request struct { |
| 112 | Network network `json:"network"` |
| 113 | } |
| 114 | type response struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 115 | Network *NetworkCreateResult `json:"network"` |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Validate |
| 119 | if opts.Name == "" { |
| 120 | return nil, ErrNameRequired |
| 121 | } |
| 122 | |
| 123 | // Populate request body |
| 124 | reqBody := request{Network: network{ |
| 125 | AdminStateUp: opts.AdminStateUp, |
| 126 | Name: opts.Name, |
| 127 | Shared: opts.Shared, |
| 128 | }} |
| 129 | |
| 130 | if opts.TenantID != "" { |
| 131 | reqBody.Network.TenantID = &opts.TenantID |
| 132 | } |
| 133 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 134 | // Send request to API |
| 135 | var res response |
| 136 | _, err := perigee.Request("POST", CreateURL(c), perigee.Options{ |
| 137 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 138 | ReqBody: &reqBody, |
| 139 | Results: &res, |
| 140 | OkCodes: []int{201}, |
| 141 | }) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | return res.Network, nil |
| 147 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 148 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame^] | 149 | type UpdateOpts networkOpts |
| 150 | |
| 151 | func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) (*Network, error) { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 152 | // Define structures |
| 153 | type network struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame^] | 154 | AdminStateUp bool `json:"admin_state_up"` |
| 155 | Name string `json:"name"` |
| 156 | Shared *bool `json:"shared,omitempty"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | type request struct { |
| 160 | Network network `json:"network"` |
| 161 | } |
| 162 | type response struct { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 163 | Network *Network `json:"network"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Populate request body |
| 167 | reqBody := request{Network: network{ |
| 168 | AdminStateUp: opts.AdminStateUp, |
| 169 | Name: opts.Name, |
| 170 | Shared: opts.Shared, |
| 171 | }} |
| 172 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 173 | // Send request to API |
| 174 | var res response |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 175 | _, err := perigee.Request("PUT", GetURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 176 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 177 | ReqBody: &reqBody, |
| 178 | Results: &res, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 179 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 180 | }) |
| 181 | if err != nil { |
| 182 | return nil, err |
| 183 | } |
| 184 | |
| 185 | return res.Network, nil |
| 186 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 187 | |
| 188 | func Delete(c *gophercloud.ServiceClient, networkID string) error { |
| 189 | _, err := perigee.Request("DELETE", DeleteURL(c, networkID), perigee.Options{ |
| 190 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 191 | OkCodes: []int{204}, |
| 192 | }) |
| 193 | return err |
| 194 | } |