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 | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame^] | 8 | type NetworkOpts struct { |
| 9 | AdminStateUp bool |
| 10 | Name string |
| 11 | Shared *bool |
| 12 | TenantID string |
Jamie Hannaford | 12bc247 | 2014-09-15 12:14:31 +0200 | [diff] [blame] | 13 | } |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 14 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 15 | func Get(c *gophercloud.ServiceClient, id string) (*NetworkResult, error) { |
| 16 | var n NetworkResult |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 17 | _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{ |
| 18 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 19 | Results: &struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 20 | Network *NetworkResult `json:"network"` |
Jamie Hannaford | d01a3c7 | 2014-09-15 12:51:00 +0200 | [diff] [blame] | 21 | }{&n}, |
| 22 | OkCodes: []int{200}, |
| 23 | }) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | return &n, nil |
| 28 | } |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 29 | |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 30 | func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) { |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 31 | // Define structures |
| 32 | type network struct { |
| 33 | AdminStateUp bool `json:"admin_state_up"` |
| 34 | Name string `json:"name"` |
| 35 | Shared *bool `json:"shared,omitempty"` |
| 36 | TenantID *string `json:"tenant_id,omitempty"` |
| 37 | } |
| 38 | type request struct { |
| 39 | Network network `json:"network"` |
| 40 | } |
| 41 | type response struct { |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 42 | Network *NetworkCreateResult `json:"network"` |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Validate |
| 46 | if opts.Name == "" { |
| 47 | return nil, ErrNameRequired |
| 48 | } |
| 49 | |
| 50 | // Populate request body |
| 51 | reqBody := request{Network: network{ |
| 52 | AdminStateUp: opts.AdminStateUp, |
| 53 | Name: opts.Name, |
| 54 | Shared: opts.Shared, |
| 55 | }} |
| 56 | |
| 57 | if opts.TenantID != "" { |
| 58 | reqBody.Network.TenantID = &opts.TenantID |
| 59 | } |
| 60 | |
Jamie Hannaford | d2d9f56 | 2014-09-15 15:35:07 +0200 | [diff] [blame] | 61 | // Send request to API |
| 62 | var res response |
| 63 | _, err := perigee.Request("POST", CreateURL(c), perigee.Options{ |
| 64 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 65 | ReqBody: &reqBody, |
| 66 | Results: &res, |
| 67 | OkCodes: []int{201}, |
| 68 | }) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | return res.Network, nil |
| 74 | } |
Jamie Hannaford | 7947505 | 2014-09-15 17:08:06 +0200 | [diff] [blame] | 75 | |
| 76 | func Update(c *gophercloud.ServiceClient, networkID string, opts NetworkOpts) (*NetworkResult, error) { |
| 77 | // Define structures |
| 78 | type network struct { |
| 79 | AdminStateUp bool `json:"admin_state_up"` |
| 80 | Name string `json:"name"` |
| 81 | Shared *bool `json:"shared,omitempty"` |
| 82 | TenantID *string `json:"tenant_id,omitempty"` |
| 83 | } |
| 84 | |
| 85 | type request struct { |
| 86 | Network network `json:"network"` |
| 87 | } |
| 88 | type response struct { |
| 89 | Network *NetworkResult `json:"network"` |
| 90 | } |
| 91 | |
| 92 | // Populate request body |
| 93 | reqBody := request{Network: network{ |
| 94 | AdminStateUp: opts.AdminStateUp, |
| 95 | Name: opts.Name, |
| 96 | Shared: opts.Shared, |
| 97 | }} |
| 98 | |
| 99 | if opts.TenantID != "" { |
| 100 | reqBody.Network.TenantID = &opts.TenantID |
| 101 | } |
| 102 | |
| 103 | // Send request to API |
| 104 | var res response |
| 105 | _, err := perigee.Request("PUT", NetworkURL(c, networkID), perigee.Options{ |
| 106 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 107 | ReqBody: &reqBody, |
| 108 | Results: &res, |
| 109 | OkCodes: []int{200}, |
| 110 | }) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | |
| 115 | return res.Network, nil |
| 116 | } |