blob: 06b0fe75eb0c5e6a27db975a115cc88deeee4606 [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 Hannaford1ce30f22014-09-16 11:23:34 +02008type NetworkOpts struct {
9 AdminStateUp bool
10 Name string
11 Shared *bool
12 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020013}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020014
Jamie Hannaford79475052014-09-15 17:08:06 +020015func Get(c *gophercloud.ServiceClient, id string) (*NetworkResult, error) {
16 var n NetworkResult
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020017 _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{
18 MoreHeaders: c.Provider.AuthenticatedHeaders(),
19 Results: &struct {
Jamie Hannaford79475052014-09-15 17:08:06 +020020 Network *NetworkResult `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020021 }{&n},
22 OkCodes: []int{200},
23 })
24 if err != nil {
25 return nil, err
26 }
27 return &n, nil
28}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020029
Jamie Hannaford79475052014-09-15 17:08:06 +020030func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020031 // 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 Hannaford79475052014-09-15 17:08:06 +020042 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020043 }
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 Hannafordd2d9f562014-09-15 15:35:07 +020061 // 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 Hannaford79475052014-09-15 17:08:06 +020075
76func 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}