blob: 73b8a1f8de9df8d634ba3933ae9d1d3ea3e6aebd [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannaford4721abc2014-09-16 16:29:04 +02004 "strconv"
5
Jamie Hannaford01e14922014-09-11 15:23:49 +02006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02008 "github.com/rackspace/gophercloud/openstack/utils"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +020010)
11
Jamie Hannaford4721abc2014-09-16 16:29:04 +020012type ListOpts struct {
13 Status string
14 Name string
15 AdminStateUp *bool
16 TenantID string
17 Shared *bool
18 ID string
19 Page int
20 PerPage int
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020021 Limit int
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020022 SortKey string
23 SortDir string
Jamie Hannaford4721abc2014-09-16 16:29:04 +020024}
25
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020026type NetworkOpts struct {
27 AdminStateUp bool
28 Name string
29 Shared *bool
30 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020031}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020032
Jamie Hannaford4721abc2014-09-16 16:29:04 +020033func ptrToStr(val *bool) string {
34 if *val == true {
35 return "true"
36 } else if *val == false {
37 return "false"
38 } else {
39 return ""
40 }
41}
42
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020043func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020044 // Build query parameters
45 q := make(map[string]string)
46 if opts.Status != "" {
47 q["status"] = opts.Status
48 }
49 if opts.Name != "" {
50 q["name"] = opts.Name
51 }
52 if opts.AdminStateUp != nil {
53 q["admin_state_up"] = ptrToStr(opts.AdminStateUp)
54 }
55 if opts.TenantID != "" {
56 q["tenant_id"] = opts.TenantID
57 }
58 if opts.Shared != nil {
59 q["shared"] = ptrToStr(opts.Shared)
60 }
61 if opts.ID != "" {
62 q["id"] = opts.ID
63 }
64 if opts.Page != 0 {
65 q["page"] = strconv.Itoa(opts.Page)
66 }
67 if opts.PerPage != 0 {
68 q["per_page"] = strconv.Itoa(opts.PerPage)
69 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020070 if opts.Limit != 0 {
71 q["limit"] = strconv.Itoa(opts.Limit)
72 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020073 if opts.SortKey != "" {
74 q["sort_key"] = opts.SortKey
75 }
76 if opts.SortDir != "" {
77 q["sort_dir"] = opts.SortDir
78 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020079
80 u := ListURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020081 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
82 return NetworkPage{pagination.LinkedPageBase(r)}
83 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020084}
85
86func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
87 var n Network
88 _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020089 MoreHeaders: c.Provider.AuthenticatedHeaders(),
90 Results: &struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020091 Network *Network `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020092 }{&n},
93 OkCodes: []int{200},
94 })
95 if err != nil {
96 return nil, err
97 }
98 return &n, nil
99}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200100
Jamie Hannaford79475052014-09-15 17:08:06 +0200101func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200102 // Define structures
103 type network struct {
104 AdminStateUp bool `json:"admin_state_up"`
105 Name string `json:"name"`
106 Shared *bool `json:"shared,omitempty"`
107 TenantID *string `json:"tenant_id,omitempty"`
108 }
109 type request struct {
110 Network network `json:"network"`
111 }
112 type response struct {
Jamie Hannaford79475052014-09-15 17:08:06 +0200113 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200114 }
115
116 // Validate
117 if opts.Name == "" {
118 return nil, ErrNameRequired
119 }
120
121 // Populate request body
122 reqBody := request{Network: network{
123 AdminStateUp: opts.AdminStateUp,
124 Name: opts.Name,
125 Shared: opts.Shared,
126 }}
127
128 if opts.TenantID != "" {
129 reqBody.Network.TenantID = &opts.TenantID
130 }
131
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200132 // Send request to API
133 var res response
134 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
135 MoreHeaders: c.Provider.AuthenticatedHeaders(),
136 ReqBody: &reqBody,
137 Results: &res,
138 OkCodes: []int{201},
139 })
140 if err != nil {
141 return nil, err
142 }
143
144 return res.Network, nil
145}
Jamie Hannaford79475052014-09-15 17:08:06 +0200146
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200147func Update(c *gophercloud.ServiceClient, networkID string, opts NetworkOpts) (*Network, error) {
Jamie Hannaford79475052014-09-15 17:08:06 +0200148 // Define structures
149 type network struct {
150 AdminStateUp bool `json:"admin_state_up"`
151 Name string `json:"name"`
152 Shared *bool `json:"shared,omitempty"`
153 TenantID *string `json:"tenant_id,omitempty"`
154 }
155
156 type request struct {
157 Network network `json:"network"`
158 }
159 type response struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200160 Network *Network `json:"network"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200161 }
162
163 // Populate request body
164 reqBody := request{Network: network{
165 AdminStateUp: opts.AdminStateUp,
166 Name: opts.Name,
167 Shared: opts.Shared,
168 }}
169
170 if opts.TenantID != "" {
171 reqBody.Network.TenantID = &opts.TenantID
172 }
173
174 // Send request to API
175 var res response
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200176 _, err := perigee.Request("PUT", GetURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200177 MoreHeaders: c.Provider.AuthenticatedHeaders(),
178 ReqBody: &reqBody,
179 Results: &res,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200180 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200181 })
182 if err != nil {
183 return nil, err
184 }
185
186 return res.Network, nil
187}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200188
189func Delete(c *gophercloud.ServiceClient, networkID string) error {
190 _, err := perigee.Request("DELETE", DeleteURL(c, networkID), perigee.Options{
191 MoreHeaders: c.Provider.AuthenticatedHeaders(),
192 OkCodes: []int{204},
193 })
194 return err
195}