blob: 98ce25f10fa0165bffd2278797108a72497b99a4 [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannafordb0d267b2015-02-19 11:59:53 +01009// Config represents a configuration group API resource.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010010type Config struct {
11 Created string
12 Updated string
13 DatastoreName string `mapstructure:"datastore_name"`
14 DatastoreVersionID string `mapstructure:"datastore_version_id"`
15 DatastoreVersionName string `mapstructure:"datastore_version_name"`
16 Description string
17 ID string
18 Name string
19 Values map[string]interface{}
20}
21
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010022// ConfigPage contains a page of Config resources in a paginated collection.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010023type ConfigPage struct {
24 pagination.SinglePageBase
25}
26
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010027// IsEmpty indicates whether a ConfigPage is empty.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010028func (r ConfigPage) IsEmpty() (bool, error) {
29 is, err := ExtractConfigs(r)
30 if err != nil {
31 return true, err
32 }
33 return len(is) == 0, nil
34}
35
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010036// ExtractConfigs will retrieve a slice of Config structs from a page.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010037func ExtractConfigs(page pagination.Page) ([]Config, error) {
38 casted := page.(ConfigPage).Body
39
40 var resp struct {
41 Configs []Config `mapstructure:"configurations" json:"configurations"`
42 }
43
44 err := mapstructure.Decode(casted, &resp)
45 return resp.Configs, err
46}
47
48type commonResult struct {
49 gophercloud.Result
50}
51
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010052// Extract will retrieve a Config resource from an operation result.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010053func (r commonResult) Extract() (*Config, error) {
54 if r.Err != nil {
55 return nil, r.Err
56 }
57
58 var response struct {
59 Config Config `mapstructure:"configuration"`
60 }
61
62 err := mapstructure.Decode(r.Body, &response)
63 return &response.Config, err
64}
65
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010066// GetResult represents the result of a Get operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010067type GetResult struct {
68 commonResult
69}
70
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010071// CreateResult represents the result of a Create operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010072type CreateResult struct {
73 commonResult
74}
75
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010076// UpdateResult represents the result of an Update operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010077type UpdateResult struct {
78 gophercloud.ErrResult
79}
80
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010081// ReplaceResult represents the result of a Replace operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010082type ReplaceResult struct {
83 gophercloud.ErrResult
84}
85
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010086// DeleteResult represents the result of a Delete operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010087type DeleteResult struct {
88 gophercloud.ErrResult
89}
Jamie Hannaford23867bb2015-02-17 15:56:48 +010090
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010091// Param represents a configuration parameter API resource.
Jamie Hannaford23867bb2015-02-17 15:56:48 +010092type Param struct {
93 Max int
94 Min int
95 Name string
96 RestartRequired bool `mapstructure:"restart_required" json:"restart_required"`
97 Type string
98}
99
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100100// ParamPage contains a page of Param resources in a paginated collection.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100101type ParamPage struct {
102 pagination.SinglePageBase
103}
104
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100105// IsEmpty indicates whether a ParamPage is empty.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100106func (r ParamPage) IsEmpty() (bool, error) {
107 is, err := ExtractParams(r)
108 if err != nil {
109 return true, err
110 }
111 return len(is) == 0, nil
112}
113
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100114// ExtractParams will retrieve a slice of Param structs from a page.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100115func ExtractParams(page pagination.Page) ([]Param, error) {
116 casted := page.(ParamPage).Body
117
118 var resp struct {
119 Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"`
120 }
121
122 err := mapstructure.Decode(casted, &resp)
123 return resp.Params, err
124}
125
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100126// ParamResult represents the result of an operation which retrieves details
127// about a particular configuration param.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100128type ParamResult struct {
129 gophercloud.Result
130}
131
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100132// Extract will retrieve a param from an operation result.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100133func (r ParamResult) Extract() (*Param, error) {
134 if r.Err != nil {
135 return nil, r.Err
136 }
137
138 var param Param
139
140 err := mapstructure.Decode(r.Body, &param)
141 return &param, err
142}