blob: 726176acf7979ab78b28b8c5a8aceb24fe1d48ea [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
4 "errors"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/openstack/db/v1/instances"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11func List(client *gophercloud.ServiceClient) pagination.Pager {
12 pageFn := func(r pagination.PageResult) pagination.Page {
13 return ConfigPage{pagination.SinglePageBase(r)}
14 }
15
16 return pagination.NewPager(client, baseURL(client), pageFn)
17}
18
19type CreateOptsBuilder interface {
20 ToConfigCreateMap() (map[string]interface{}, error)
21}
22
23type DatastoreOpts struct {
24 Type string
25 Version string
26}
27
28func (opts DatastoreOpts) ToMap() (map[string]string, error) {
29 datastore := map[string]string{}
30
31 if opts.Type != "" {
32 datastore["type"] = opts.Type
33 }
34
35 if opts.Version != "" {
36 datastore["version"] = opts.Version
37 }
38
39 return datastore, nil
40}
41
42type CreateOpts struct {
43 Datastore *DatastoreOpts
44 Description string
45 Name string
46 Values map[string]interface{}
47}
48
49func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
50 if opts.Name == "" {
51 return nil, errors.New("Name is a required field")
52 }
53 if len(opts.Values) == 0 {
54 return nil, errors.New("Values must be a populated map")
55 }
56
57 config := map[string]interface{}{
58 "name": opts.Name,
59 "values": opts.Values,
60 }
61
62 if opts.Datastore != nil {
63 ds, err := opts.Datastore.ToMap()
64 if err != nil {
65 return config, err
66 }
67 config["datastore"] = ds
68 }
69
70 if opts.Description != "" {
71 config["description"] = opts.Description
72 }
73
74 return map[string]interface{}{"configuration": config}, nil
75}
76
77func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
78 var res CreateResult
79
80 reqBody, err := opts.ToConfigCreateMap()
81 if err != nil {
82 res.Err = err
83 return res
84 }
85
86 _, res.Err = client.Request("POST", baseURL(client), gophercloud.RequestOpts{
87 OkCodes: []int{201},
88 JSONBody: &reqBody,
89 JSONResponse: &res.Body,
90 })
91
92 return res
93}
94
95func Get(client *gophercloud.ServiceClient, configID string) GetResult {
96 var res GetResult
97
98 _, res.Err = client.Request("GET", resourceURL(client, configID), gophercloud.RequestOpts{
99 OkCodes: []int{200},
100 JSONResponse: &res.Body,
101 })
102
103 return res
104}
105
106type UpdateOptsBuilder interface {
107 ToConfigUpdateMap() (map[string]interface{}, error)
108}
109
110type UpdateOpts struct {
111 Datastore *DatastoreOpts
112 Description string
113 Name string
114 Values map[string]interface{}
115}
116
117func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
118 config := map[string]interface{}{}
119
120 if opts.Name != "" {
121 config["name"] = opts.Name
122 }
123
124 if opts.Description != "" {
125 config["description"] = opts.Description
126 }
127
128 if opts.Datastore != nil {
129 ds, err := opts.Datastore.ToMap()
130 if err != nil {
131 return config, err
132 }
133 config["datastore"] = ds
134 }
135
136 if len(opts.Values) > 0 {
137 config["values"] = opts.Values
138 }
139
140 return map[string]interface{}{"configuration": config}, nil
141}
142
143func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) UpdateResult {
144 var res UpdateResult
145
146 reqBody, err := opts.ToConfigUpdateMap()
147 if err != nil {
148 res.Err = err
149 return res
150 }
151
152 _, res.Err = client.Request("PATCH", resourceURL(client, configID), gophercloud.RequestOpts{
153 OkCodes: []int{200},
154 JSONBody: &reqBody,
155 JSONResponse: &res.Body,
156 })
157
158 return res
159}
160
161func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) ReplaceResult {
162 var res ReplaceResult
163
164 reqBody, err := opts.ToConfigUpdateMap()
165 if err != nil {
166 res.Err = err
167 return res
168 }
169
170 _, res.Err = client.Request("PUT", resourceURL(client, configID), gophercloud.RequestOpts{
171 OkCodes: []int{202},
172 JSONBody: &reqBody,
173 JSONResponse: &res.Body,
174 })
175
176 return res
177}
178
179func Delete(client *gophercloud.ServiceClient, configID string) DeleteResult {
180 var res DeleteResult
181
182 _, res.Err = client.Request("DELETE", resourceURL(client, configID), gophercloud.RequestOpts{
183 OkCodes: []int{202},
184 })
185
186 return res
187}
188
189func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
190 pageFn := func(r pagination.PageResult) pagination.Page {
191 return instances.InstancePage{pagination.LinkedPageBase{PageResult: r}}
192 }
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100193 return pagination.NewPager(client, instancesURL(client, configID), pageFn)
194}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100195
196func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
197 pageFn := func(r pagination.PageResult) pagination.Page {
198 return ParamPage{pagination.SinglePageBase(r)}
199 }
200 return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), pageFn)
201}
202
203func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) ParamResult {
204 var res ParamResult
205
206 _, res.Err = client.Request("GET", getDSParamURL(client, datastoreID, versionID, paramID), gophercloud.RequestOpts{
207 OkCodes: []int{200},
208 JSONResponse: &res.Body,
209 })
210
211 return res
212}
213
214func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
215 pageFn := func(r pagination.PageResult) pagination.Page {
216 return ParamPage{pagination.SinglePageBase(r)}
217 }
218 return pagination.NewPager(client, listGlobalParamsURL(client, versionID), pageFn)
219}
220
221func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) ParamResult {
222 var res ParamResult
223
224 _, res.Err = client.Request("GET", getGlobalParamURL(client, versionID, paramID), gophercloud.RequestOpts{
225 OkCodes: []int{200},
226 JSONResponse: &res.Body,
227 })
228
229 return res
230}