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