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 | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 26 | type NetworkOpts struct { |
| 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 | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 101 | func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) { |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 102 | // Define structures |
| 103 | type network struct { |
| 104 | AdminStateUp bool `json:"admin_state_up"` |
| 105 | Name string `json:"name"` |
| 106 | Shared *bool `json:"shared,omitempty"` |
| 107 | TenantID *string `json:"tenant_id,omitempty"` |
| 108 | } |
| 109 | type request struct { |
| 110 | Network network `json:"network"` |
| 111 | } |
| 112 | type response struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 113 | Network *NetworkCreateResult `json:"network"` |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Validate |
| 117 | if opts.Name == "" { |
| 118 | return nil, ErrNameRequired |
| 119 | } |
| 120 | |
| 121 | // Populate request body |
| 122 | reqBody := request{Network: network{ |
| 123 | AdminStateUp: opts.AdminStateUp, |
| 124 | Name: opts.Name, |
| 125 | Shared: opts.Shared, |
| 126 | }} |
| 127 | |
| 128 | if opts.TenantID != "" { |
| 129 | reqBody.Network.TenantID = &opts.TenantID |
| 130 | } |
| 131 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 132 | // Send request to API |
| 133 | var res response |
| 134 | _, err := perigee.Request("POST", CreateURL(c), perigee.Options{ |
| 135 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 136 | ReqBody: &reqBody, |
| 137 | Results: &res, |
| 138 | OkCodes: []int{201}, |
| 139 | }) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | |
| 144 | return res.Network, nil |
| 145 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 146 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 147 | func Update(c *gophercloud.ServiceClient, networkID string, opts NetworkOpts) (*Network, error) { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 148 | // Define structures |
| 149 | type network struct { |
| 150 | AdminStateUp bool `json:"admin_state_up"` |
| 151 | Name string `json:"name"` |
| 152 | Shared *bool `json:"shared,omitempty"` |
| 153 | TenantID *string `json:"tenant_id,omitempty"` |
| 154 | } |
| 155 | |
| 156 | type request struct { |
| 157 | Network network `json:"network"` |
| 158 | } |
| 159 | type response struct { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 160 | Network *Network `json:"network"` |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Populate request body |
| 164 | reqBody := request{Network: network{ |
| 165 | AdminStateUp: opts.AdminStateUp, |
| 166 | Name: opts.Name, |
| 167 | Shared: opts.Shared, |
| 168 | }} |
| 169 | |
| 170 | if opts.TenantID != "" { |
| 171 | reqBody.Network.TenantID = &opts.TenantID |
| 172 | } |
| 173 | |
| 174 | // Send request to API |
| 175 | var res response |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 176 | _, err := perigee.Request("PUT", GetURL(c, networkID), perigee.Options{ |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 177 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 178 | ReqBody: &reqBody, |
| 179 | Results: &res, |
Jamie Hannaford | f84171d | 2014-09-18 14:00:01 +0200 | [diff] [blame] | 180 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 181 | }) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | |
| 186 | return res.Network, nil |
| 187 | } |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 188 | |
| 189 | func Delete(c *gophercloud.ServiceClient, networkID string) error { |
| 190 | _, err := perigee.Request("DELETE", DeleteURL(c, networkID), perigee.Options{ |
| 191 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 192 | OkCodes: []int{204}, |
| 193 | }) |
| 194 | return err |
| 195 | } |