blob: 9a23cc06aae7c59914040b1f885c51827d87670f [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6)
7
Jamie Hannaford01e14922014-09-11 15:23:49 +02008func APIVersions(c *gophercloud.ServiceClient) (*APIVersionsList, error) {
Jamie Hannaford01e14922014-09-11 15:23:49 +02009 var resp APIVersionsList
Jamie Hannafordf14d4562014-09-11 17:46:18 +020010 _, 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
22func APIInfo(c *gophercloud.ServiceClient, v string) (*APIInfoList, error) {
23 var resp APIInfoList
24 _, err := perigee.Request("GET", APIInfoURL(c, v), perigee.Options{
Jamie Hannaford01e14922014-09-11 15:23:49 +020025 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 Hannaford12bc2472014-09-15 12:14:31 +020035
36func 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 Hannafordd01a3c72014-09-15 12:51:00 +020051
Jamie Hannaford79475052014-09-15 17:08:06 +020052func Get(c *gophercloud.ServiceClient, id string) (*NetworkResult, error) {
53 var n NetworkResult
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020054 _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{
55 MoreHeaders: c.Provider.AuthenticatedHeaders(),
56 Results: &struct {
Jamie Hannaford79475052014-09-15 17:08:06 +020057 Network *NetworkResult `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020058 }{&n},
59 OkCodes: []int{200},
60 })
61 if err != nil {
62 return nil, err
63 }
64 return &n, nil
65}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020066
67type NetworkOpts struct {
68 AdminStateUp bool
69 Name string
70 Shared *bool
71 TenantID string
72}
73
Jamie Hannaford79475052014-09-15 17:08:06 +020074func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020075 // 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 Hannaford79475052014-09-15 17:08:06 +020086 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020087 }
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 Hannafordd2d9f562014-09-15 15:35:07 +0200105 // 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 Hannaford79475052014-09-15 17:08:06 +0200119
120func 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}