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 ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | ) |
| 7 | |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 8 | func APIVersions(c *gophercloud.ServiceClient) (*APIVersionsList, error) { |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 9 | var resp APIVersionsList |
Jamie Hannaford | f14d456 | 2014-09-11 17:46:18 +0200 | [diff] [blame] | 10 | _, err := perigee.Request("GET", APIVersionsURL(c), perigee.Options{ |
| 11 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 12 | Results: &resp, |
| 13 | OkCodes: []int{200}, |
| 14 | }) |
| 15 | if err != nil { |
| 16 | return nil, err |
| 17 | } |
| 18 | |
| 19 | return &resp, nil |
| 20 | } |
| 21 | |
| 22 | func APIInfo(c *gophercloud.ServiceClient, v string) (*APIInfoList, error) { |
| 23 | var resp APIInfoList |
| 24 | _, err := perigee.Request("GET", APIInfoURL(c, v), perigee.Options{ |
Jamie Hannaford | 01e1492 | 2014-09-11 15:23:49 +0200 | [diff] [blame] | 25 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 26 | Results: &resp, |
| 27 | OkCodes: []int{200}, |
| 28 | }) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | return &resp, nil |
| 34 | } |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 35 | |
| 36 | func GetExtension(c *gophercloud.ServiceClient, name string) (*Extension, error) { |
| 37 | var ext Extension |
| 38 | _, err := perigee.Request("GET", ExtensionURL(c, name), perigee.Options{ |
| 39 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 40 | Results: &struct { |
| 41 | Extension *Extension `json:"extension"` |
| 42 | }{&ext}, |
| 43 | OkCodes: []int{200}, |
| 44 | }) |
| 45 | |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return &ext, nil |
| 50 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 51 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame^] | 52 | func Get(c *gophercloud.ServiceClient, id string) (*NetworkResult, error) { |
| 53 | var n NetworkResult |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 54 | _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{ |
| 55 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 56 | Results: &struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame^] | 57 | Network *NetworkResult `json:"network"` |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 58 | }{&n}, |
| 59 | OkCodes: []int{200}, |
| 60 | }) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return &n, nil |
| 65 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 66 | |
| 67 | type NetworkOpts struct { |
| 68 | AdminStateUp bool |
| 69 | Name string |
| 70 | Shared *bool |
| 71 | TenantID string |
| 72 | } |
| 73 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame^] | 74 | func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) { |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 75 | // Define structures |
| 76 | type network struct { |
| 77 | AdminStateUp bool `json:"admin_state_up"` |
| 78 | Name string `json:"name"` |
| 79 | Shared *bool `json:"shared,omitempty"` |
| 80 | TenantID *string `json:"tenant_id,omitempty"` |
| 81 | } |
| 82 | type request struct { |
| 83 | Network network `json:"network"` |
| 84 | } |
| 85 | type response struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame^] | 86 | Network *NetworkCreateResult `json:"network"` |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Validate |
| 90 | if opts.Name == "" { |
| 91 | return nil, ErrNameRequired |
| 92 | } |
| 93 | |
| 94 | // Populate request body |
| 95 | reqBody := request{Network: network{ |
| 96 | AdminStateUp: opts.AdminStateUp, |
| 97 | Name: opts.Name, |
| 98 | Shared: opts.Shared, |
| 99 | }} |
| 100 | |
| 101 | if opts.TenantID != "" { |
| 102 | reqBody.Network.TenantID = &opts.TenantID |
| 103 | } |
| 104 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 105 | // Send request to API |
| 106 | var res response |
| 107 | _, err := perigee.Request("POST", CreateURL(c), perigee.Options{ |
| 108 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 109 | ReqBody: &reqBody, |
| 110 | Results: &res, |
| 111 | OkCodes: []int{201}, |
| 112 | }) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | return res.Network, nil |
| 118 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame^] | 119 | |
| 120 | func Update(c *gophercloud.ServiceClient, networkID string, opts NetworkOpts) (*NetworkResult, error) { |
| 121 | // Define structures |
| 122 | type network struct { |
| 123 | AdminStateUp bool `json:"admin_state_up"` |
| 124 | Name string `json:"name"` |
| 125 | Shared *bool `json:"shared,omitempty"` |
| 126 | TenantID *string `json:"tenant_id,omitempty"` |
| 127 | } |
| 128 | |
| 129 | type request struct { |
| 130 | Network network `json:"network"` |
| 131 | } |
| 132 | type response struct { |
| 133 | Network *NetworkResult `json:"network"` |
| 134 | } |
| 135 | |
| 136 | // Populate request body |
| 137 | reqBody := request{Network: network{ |
| 138 | AdminStateUp: opts.AdminStateUp, |
| 139 | Name: opts.Name, |
| 140 | Shared: opts.Shared, |
| 141 | }} |
| 142 | |
| 143 | if opts.TenantID != "" { |
| 144 | reqBody.Network.TenantID = &opts.TenantID |
| 145 | } |
| 146 | |
| 147 | // Send request to API |
| 148 | var res response |
| 149 | _, err := perigee.Request("PUT", NetworkURL(c, networkID), perigee.Options{ |
| 150 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 151 | ReqBody: &reqBody, |
| 152 | Results: &res, |
| 153 | OkCodes: []int{200}, |
| 154 | }) |
| 155 | if err != nil { |
| 156 | return nil, err |
| 157 | } |
| 158 | |
| 159 | return res.Network, nil |
| 160 | } |