Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 4 | "encoding/json" |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame^] | 5 | "fmt" |
| 6 | "reflect" |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 7 | "time" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
| 10 | "github.com/rackspace/gophercloud" |
| 11 | "github.com/rackspace/gophercloud/pagination" |
| 12 | ) |
| 13 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 14 | // CreatedStack represents the object extracted from a Create operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 15 | type CreatedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 16 | ID string `mapstructure:"id"` |
| 17 | Links []gophercloud.Link `mapstructure:"links"` |
| 18 | } |
| 19 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 20 | // CreateResult represents the result of a Create operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 21 | type CreateResult struct { |
| 22 | gophercloud.Result |
| 23 | } |
| 24 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 25 | // Extract returns a pointer to a CreatedStack object and is called after a |
| 26 | // Create operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 27 | func (r CreateResult) Extract() (*CreatedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 28 | if r.Err != nil { |
| 29 | return nil, r.Err |
| 30 | } |
| 31 | |
| 32 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 33 | Stack *CreatedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | return res.Stack, nil |
| 41 | } |
| 42 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 43 | // AdoptResult represents the result of an Adopt operation. AdoptResult has the |
| 44 | // same form as CreateResult. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 45 | type AdoptResult struct { |
Jon Perritt | 9741dd9 | 2015-02-04 12:05:47 -0700 | [diff] [blame] | 46 | CreateResult |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 50 | type StackPage struct { |
| 51 | pagination.SinglePageBase |
| 52 | } |
| 53 | |
| 54 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 55 | func (r StackPage) IsEmpty() (bool, error) { |
| 56 | stacks, err := ExtractStacks(r) |
| 57 | if err != nil { |
| 58 | return true, err |
| 59 | } |
| 60 | return len(stacks) == 0, nil |
| 61 | } |
| 62 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 63 | // ListedStack represents an element in the slice extracted from a List operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 64 | type ListedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 65 | CreationTime time.Time `mapstructure:"-"` |
| 66 | Description string `mapstructure:"description"` |
| 67 | ID string `mapstructure:"id"` |
| 68 | Links []gophercloud.Link `mapstructure:"links"` |
| 69 | Name string `mapstructure:"stack_name"` |
| 70 | Status string `mapstructure:"stack_status"` |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 71 | StatusReason string `mapstructure:"stack_status_reason"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 72 | UpdatedTime time.Time `mapstructure:"-"` |
| 73 | } |
| 74 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 75 | // ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 76 | // over a stacks.List call. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 77 | func ExtractStacks(page pagination.Page) ([]ListedStack, error) { |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame^] | 78 | casted := page.(StackPage).Body |
| 79 | |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 80 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 81 | Stacks []ListedStack `mapstructure:"stacks"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | err := mapstructure.Decode(page.(StackPage).Body, &res) |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame^] | 89 | var rawStacks []interface{} |
| 90 | switch casted.(type) { |
| 91 | case map[string]interface{}: |
| 92 | rawStacks = casted.(map[string]interface{})["stacks"].([]interface{}) |
| 93 | case map[string][]interface{}: |
| 94 | rawStacks = casted.(map[string][]interface{})["stacks"] |
| 95 | default: |
| 96 | return res.Stacks, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
| 97 | } |
| 98 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 99 | for i := range rawStacks { |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 100 | thisStack := (rawStacks[i]).(map[string]interface{}) |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 101 | |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 102 | if t, ok := thisStack["creation_time"].(string); ok && t != "" { |
| 103 | creationTime, err := time.Parse(time.RFC3339, t) |
| 104 | if err != nil { |
| 105 | return res.Stacks, err |
| 106 | } |
| 107 | res.Stacks[i].CreationTime = creationTime |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 108 | } |
Jon Perritt | 9cd3d38 | 2015-02-04 15:49:41 -0700 | [diff] [blame] | 109 | |
| 110 | if t, ok := thisStack["updated_time"].(string); ok && t != "" { |
| 111 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 112 | if err != nil { |
| 113 | return res.Stacks, err |
| 114 | } |
| 115 | res.Stacks[i].UpdatedTime = updatedTime |
| 116 | } |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return res.Stacks, nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 122 | // RetrievedStack represents the object extracted from a Get operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 123 | type RetrievedStack struct { |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 124 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 125 | CreationTime time.Time `mapstructure:"-"` |
| 126 | Description string `mapstructure:"description"` |
| 127 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 128 | ID string `mapstructure:"id"` |
| 129 | Links []gophercloud.Link `mapstructure:"links"` |
| 130 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 131 | Outputs []map[string]interface{} `mapstructure:"outputs"` |
| 132 | Parameters map[string]string `mapstructure:"parameters"` |
| 133 | Name string `mapstructure:"stack_name"` |
| 134 | Status string `mapstructure:"stack_status"` |
| 135 | StatusReason string `mapstructure:"stack_status_reason"` |
| 136 | TemplateDescription string `mapstructure:"template_description"` |
| 137 | Timeout int `mapstructure:"timeout_mins"` |
| 138 | UpdatedTime time.Time `mapstructure:"-"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 141 | // GetResult represents the result of a Get operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 142 | type GetResult struct { |
| 143 | gophercloud.Result |
| 144 | } |
| 145 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 146 | // Extract returns a pointer to a RetrievedStack object and is called after a |
| 147 | // Get operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 148 | func (r GetResult) Extract() (*RetrievedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 149 | if r.Err != nil { |
| 150 | return nil, r.Err |
| 151 | } |
| 152 | |
| 153 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 154 | Stack *RetrievedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | config := &mapstructure.DecoderConfig{ |
| 158 | Result: &res, |
| 159 | WeaklyTypedInput: true, |
| 160 | } |
| 161 | decoder, err := mapstructure.NewDecoder(config) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | |
| 166 | if err := decoder.Decode(r.Body); err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | |
| 170 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 171 | |
| 172 | if date, ok := b["creation_time"]; ok && date != nil { |
| 173 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | res.Stack.CreationTime = t |
| 178 | } |
| 179 | |
| 180 | if date, ok := b["updated_time"]; ok && date != nil { |
| 181 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | res.Stack.UpdatedTime = t |
| 186 | } |
| 187 | |
| 188 | return res.Stack, err |
| 189 | } |
| 190 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 191 | // UpdateResult represents the result of a Update operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 192 | type UpdateResult struct { |
| 193 | gophercloud.ErrResult |
| 194 | } |
| 195 | |
Jon Perritt | 7726e49 | 2015-02-04 17:54:28 -0700 | [diff] [blame] | 196 | // DeleteResult represents the result of a Delete operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 197 | type DeleteResult struct { |
| 198 | gophercloud.ErrResult |
| 199 | } |
| 200 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 201 | // PreviewedStack represents the result of a Preview operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 202 | type PreviewedStack struct { |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 203 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 204 | CreationTime time.Time `mapstructure:"-"` |
| 205 | Description string `mapstructure:"description"` |
| 206 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 207 | ID string `mapstructure:"id"` |
| 208 | Links []gophercloud.Link `mapstructure:"links"` |
| 209 | Name string `mapstructure:"stack_name"` |
| 210 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 211 | Parameters map[string]string `mapstructure:"parameters"` |
| 212 | Resources []map[string]interface{} `mapstructure:"resources"` |
| 213 | Status string `mapstructure:"stack_status"` |
| 214 | StatusReason string `mapstructure:"stack_status_reason"` |
| 215 | TemplateDescription string `mapstructure:"template_description"` |
| 216 | Timeout int `mapstructure:"timeout_mins"` |
| 217 | UpdatedTime time.Time `mapstructure:"-"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 220 | // PreviewResult represents the result of a Preview operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 221 | type PreviewResult struct { |
| 222 | gophercloud.Result |
| 223 | } |
| 224 | |
Jon Perritt | 37f9774 | 2015-02-04 18:55:05 -0700 | [diff] [blame] | 225 | // Extract returns a pointer to a PreviewedStack object and is called after a |
| 226 | // Preview operation. |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 227 | func (r PreviewResult) Extract() (*PreviewedStack, error) { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 228 | if r.Err != nil { |
| 229 | return nil, r.Err |
| 230 | } |
| 231 | |
| 232 | var res struct { |
Jon Perritt | 22325f4 | 2015-01-29 14:48:18 -0700 | [diff] [blame] | 233 | Stack *PreviewedStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | config := &mapstructure.DecoderConfig{ |
| 237 | Result: &res, |
| 238 | WeaklyTypedInput: true, |
| 239 | } |
| 240 | decoder, err := mapstructure.NewDecoder(config) |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | |
| 245 | if err := decoder.Decode(r.Body); err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 250 | |
| 251 | if date, ok := b["creation_time"]; ok && date != nil { |
| 252 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 253 | if err != nil { |
| 254 | return nil, err |
| 255 | } |
| 256 | res.Stack.CreationTime = t |
| 257 | } |
| 258 | |
| 259 | if date, ok := b["updated_time"]; ok && date != nil { |
| 260 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 261 | if err != nil { |
| 262 | return nil, err |
| 263 | } |
| 264 | res.Stack.UpdatedTime = t |
| 265 | } |
| 266 | |
| 267 | return res.Stack, err |
| 268 | } |
| 269 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 270 | // AbandonedStack represents the result of an Abandon operation. |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame] | 271 | type AbandonedStack struct { |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 272 | Status string `mapstructure:"status"` |
| 273 | Name string `mapstructure:"name"` |
Jon Perritt | 6ec27cf | 2015-02-09 12:51:41 -0700 | [diff] [blame] | 274 | Template map[string]interface{} `mapstructure:"template"` |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 275 | Action string `mapstructure:"action"` |
| 276 | ID string `mapstructure:"id"` |
| 277 | Resources map[string]interface{} `mapstructure:"resources"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 280 | // AbandonResult represents the result of an Abandon operation. |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 281 | type AbandonResult struct { |
| 282 | gophercloud.Result |
| 283 | } |
Jon Perritt | 9209df4 | 2015-02-05 12:55:33 -0700 | [diff] [blame] | 284 | |
| 285 | // Extract returns a pointer to an AbandonedStack object and is called after an |
| 286 | // Abandon operation. |
| 287 | func (r AbandonResult) Extract() (*AbandonedStack, error) { |
| 288 | if r.Err != nil { |
| 289 | return nil, r.Err |
| 290 | } |
| 291 | |
| 292 | var res AbandonedStack |
| 293 | |
| 294 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 295 | return nil, err |
| 296 | } |
| 297 | |
| 298 | return &res, nil |
| 299 | } |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 300 | |
Jon Perritt | db0d26a | 2015-02-09 13:06:16 -0700 | [diff] [blame] | 301 | // String converts an AbandonResult to a string. This is useful to when passing |
| 302 | // the result of an Abandon operation to an AdoptOpts AdoptStackData field. |
Jon Perritt | 3d381d5 | 2015-02-09 13:04:48 -0700 | [diff] [blame] | 303 | func (r AbandonResult) String() (string, error) { |
| 304 | out, err := json.Marshal(r) |
| 305 | if err != nil { |
| 306 | return "", err |
| 307 | } |
| 308 | return string(out), nil |
| 309 | } |