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