blob: 37a77ade76f4808c74f7c5e8055ef16d62d1d47d [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
Jamie Hannaforde65ad952015-11-16 14:05:11 +01004 "time"
5
Jamie Hannaforded7f4532015-02-17 14:56:30 +01006 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010011// Config represents a configuration group API resource.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010012type Config struct {
Jamie Hannaforde65ad952015-11-16 14:05:11 +010013 Created time.Time `mapstructure:"-"`
14 Updated time.Time `mapstructure:"-"`
15 DatastoreName string `mapstructure:"datastore_name"`
16 DatastoreVersionID string `mapstructure:"datastore_version_id"`
17 DatastoreVersionName string `mapstructure:"datastore_version_name"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010018 Description string
19 ID string
20 Name string
21 Values map[string]interface{}
22}
23
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010024// ConfigPage contains a page of Config resources in a paginated collection.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010025type ConfigPage struct {
26 pagination.SinglePageBase
27}
28
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010029// IsEmpty indicates whether a ConfigPage is empty.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010030func (r ConfigPage) IsEmpty() (bool, error) {
31 is, err := ExtractConfigs(r)
32 if err != nil {
33 return true, err
34 }
35 return len(is) == 0, nil
36}
37
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010038// ExtractConfigs will retrieve a slice of Config structs from a page.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010039func ExtractConfigs(page pagination.Page) ([]Config, error) {
40 casted := page.(ConfigPage).Body
41
42 var resp struct {
43 Configs []Config `mapstructure:"configurations" json:"configurations"`
44 }
45
46 err := mapstructure.Decode(casted, &resp)
Jamie Hannaforde65ad952015-11-16 14:05:11 +010047
48 var vals []interface{}
49 switch (casted).(type) {
50 case interface{}:
51 vals = casted.(map[string]interface{})["configurations"].([]interface{})
52 }
53
54 for i, v := range vals {
55 val := v.(map[string]interface{})
56
57 if t, ok := val["created"].(string); ok && t != "" {
58 creationTime, err := time.Parse(time.RFC3339, t)
59 if err != nil {
60 return resp.Configs, err
61 }
62 resp.Configs[i].Created = creationTime
63 }
64
65 if t, ok := val["updated"].(string); ok && t != "" {
66 updatedTime, err := time.Parse(time.RFC3339, t)
67 if err != nil {
68 return resp.Configs, err
69 }
70 resp.Configs[i].Updated = updatedTime
71 }
72 }
73
Jamie Hannaforded7f4532015-02-17 14:56:30 +010074 return resp.Configs, err
75}
76
77type commonResult struct {
78 gophercloud.Result
79}
80
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010081// Extract will retrieve a Config resource from an operation result.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010082func (r commonResult) Extract() (*Config, error) {
83 if r.Err != nil {
84 return nil, r.Err
85 }
86
87 var response struct {
88 Config Config `mapstructure:"configuration"`
89 }
90
91 err := mapstructure.Decode(r.Body, &response)
Jamie Hannaforde65ad952015-11-16 14:05:11 +010092 val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{})
93
94 if t, ok := val["created"].(string); ok && t != "" {
95 creationTime, err := time.Parse(time.RFC3339, t)
96 if err != nil {
97 return &response.Config, err
98 }
99 response.Config.Created = creationTime
100 }
101
102 if t, ok := val["updated"].(string); ok && t != "" {
103 updatedTime, err := time.Parse(time.RFC3339, t)
104 if err != nil {
105 return &response.Config, err
106 }
107 response.Config.Updated = updatedTime
108 }
109
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100110 return &response.Config, err
111}
112
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100113// GetResult represents the result of a Get operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100114type GetResult struct {
115 commonResult
116}
117
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100118// CreateResult represents the result of a Create operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100119type CreateResult struct {
120 commonResult
121}
122
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100123// UpdateResult represents the result of an Update operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100124type UpdateResult struct {
125 gophercloud.ErrResult
126}
127
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100128// ReplaceResult represents the result of a Replace operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100129type ReplaceResult struct {
130 gophercloud.ErrResult
131}
132
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100133// DeleteResult represents the result of a Delete operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100134type DeleteResult struct {
135 gophercloud.ErrResult
136}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100137
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100138// Param represents a configuration parameter API resource.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100139type Param struct {
140 Max int
141 Min int
142 Name string
143 RestartRequired bool `mapstructure:"restart_required" json:"restart_required"`
144 Type string
145}
146
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100147// ParamPage contains a page of Param resources in a paginated collection.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100148type ParamPage struct {
149 pagination.SinglePageBase
150}
151
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100152// IsEmpty indicates whether a ParamPage is empty.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100153func (r ParamPage) IsEmpty() (bool, error) {
154 is, err := ExtractParams(r)
155 if err != nil {
156 return true, err
157 }
158 return len(is) == 0, nil
159}
160
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100161// ExtractParams will retrieve a slice of Param structs from a page.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100162func ExtractParams(page pagination.Page) ([]Param, error) {
163 casted := page.(ParamPage).Body
164
165 var resp struct {
166 Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"`
167 }
168
169 err := mapstructure.Decode(casted, &resp)
170 return resp.Params, err
171}
172
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100173// ParamResult represents the result of an operation which retrieves details
174// about a particular configuration param.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100175type ParamResult struct {
176 gophercloud.Result
177}
178
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100179// Extract will retrieve a param from an operation result.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100180func (r ParamResult) Extract() (*Param, error) {
181 if r.Err != nil {
182 return nil, r.Err
183 }
184
185 var param Param
186
187 err := mapstructure.Decode(r.Body, &param)
188 return &param, err
189}