Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 11 | type CreatedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 12 | ID string `mapstructure:"id"` |
| 13 | Links []gophercloud.Link `mapstructure:"links"` |
| 14 | } |
| 15 | |
| 16 | type CreateResult struct { |
| 17 | gophercloud.Result |
| 18 | } |
| 19 | |
| 20 | func (r CreateResult) Extract() (*CreateStack, error) { |
| 21 | if r.Err != nil { |
| 22 | return nil, r.Err |
| 23 | } |
| 24 | |
| 25 | var res struct { |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 26 | Stack *CreateStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | return res.Stack, nil |
| 34 | } |
| 35 | |
| 36 | type AdoptResult struct { |
| 37 | gophercloud.Result |
| 38 | } |
| 39 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 40 | |
| 41 | |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 42 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 43 | type StackPage struct { |
| 44 | pagination.SinglePageBase |
| 45 | } |
| 46 | |
| 47 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 48 | func (r StackPage) IsEmpty() (bool, error) { |
| 49 | stacks, err := ExtractStacks(r) |
| 50 | if err != nil { |
| 51 | return true, err |
| 52 | } |
| 53 | return len(stacks) == 0, nil |
| 54 | } |
| 55 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 56 | type ListedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 57 | CreationTime time.Time `mapstructure:"-"` |
| 58 | Description string `mapstructure:"description"` |
| 59 | ID string `mapstructure:"id"` |
| 60 | Links []gophercloud.Link `mapstructure:"links"` |
| 61 | Name string `mapstructure:"stack_name"` |
| 62 | Status string `mapstructure:"stack_status"` |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 63 | StatusReason string `mapstructure:"stack_status_reason"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 64 | UpdatedTime time.Time `mapstructure:"-"` |
| 65 | } |
| 66 | |
| 67 | // ExtractStacks extracts and returns a slice of Stacks. It is used while iterating |
| 68 | // over a stacks.List call. |
| 69 | func ExtractStacks(page pagination.Page) ([]ListStack, error) { |
| 70 | var res struct { |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 71 | Stacks []ListStack `mapstructure:"stacks"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | err := mapstructure.Decode(page.(StackPage).Body, &res) |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | rawStacks := (((page.(StackPage).Body).(map[string]interface{}))["stacks"]).([]interface{}) |
| 80 | for i := range rawStacks { |
| 81 | creationTime, err := time.Parse(time.RFC3339, ((rawStacks[i]).(map[string]interface{}))["creation_time"].(string)) |
| 82 | if err != nil { |
| 83 | return res.Stacks, err |
| 84 | } |
| 85 | res.Stacks[i].CreationTime = creationTime |
| 86 | |
| 87 | updatedTime, err := time.Parse(time.RFC3339, ((rawStacks[i]).(map[string]interface{}))["updated_time"].(string)) |
| 88 | if err != nil { |
| 89 | return res.Stacks, err |
| 90 | } |
| 91 | res.Stacks[i].UpdatedTime = updatedTime |
| 92 | } |
| 93 | |
| 94 | return res.Stacks, nil |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 97 | type RetrievedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 98 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 99 | CreationTime time.Time `mapstructure:"-"` |
| 100 | Description string `mapstructure:"description"` |
| 101 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 102 | ID string `mapstructure:"id"` |
| 103 | Links []gophercloud.Link `mapstructure:"links"` |
| 104 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 105 | Outputs []map[string]string `mapstructure:"outputs"` |
| 106 | Parameters map[string]string `mapstructure:"parameters"` |
| 107 | Name string `mapstructure:"stack_name"` |
| 108 | Status string `mapstructure:"stack_status"` |
| 109 | StausReason string `mapstructure:"stack_status_reason"` |
| 110 | TemplateDescription string `mapstructure:"template_description"` |
| 111 | Timeout int `mapstructure:"timeout_mins"` |
| 112 | UpdatedTime time.Time `mapstructure:"-"` |
| 113 | } |
| 114 | |
| 115 | type GetResult struct { |
| 116 | gophercloud.Result |
| 117 | } |
| 118 | |
| 119 | func (r GetResult) Extract() (*GetStack, error) { |
| 120 | if r.Err != nil { |
| 121 | return nil, r.Err |
| 122 | } |
| 123 | |
| 124 | var res struct { |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 125 | Stack *GetStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | config := &mapstructure.DecoderConfig{ |
| 129 | Result: &res, |
| 130 | WeaklyTypedInput: true, |
| 131 | } |
| 132 | decoder, err := mapstructure.NewDecoder(config) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | |
| 137 | if err := decoder.Decode(r.Body); err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | |
| 141 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 142 | |
| 143 | if date, ok := b["creation_time"]; ok && date != nil { |
| 144 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | res.Stack.CreationTime = t |
| 149 | } |
| 150 | |
| 151 | if date, ok := b["updated_time"]; ok && date != nil { |
| 152 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | res.Stack.UpdatedTime = t |
| 157 | } |
| 158 | |
| 159 | return res.Stack, err |
| 160 | } |
| 161 | |
| 162 | type UpdateResult struct { |
| 163 | gophercloud.ErrResult |
| 164 | } |
| 165 | |
| 166 | type DeleteResult struct { |
| 167 | gophercloud.ErrResult |
| 168 | } |
| 169 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 170 | type PreviewedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 171 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 172 | CreationTime time.Time `mapstructure:"-"` |
| 173 | Description string `mapstructure:"description"` |
| 174 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 175 | ID string `mapstructure:"id"` |
| 176 | Links []gophercloud.Link `mapstructure:"links"` |
| 177 | Name string `mapstructure:"stack_name"` |
| 178 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 179 | Parameters map[string]string `mapstructure:"parameters"` |
| 180 | Resources []map[string]string `mapstructure:"resources"` |
| 181 | Status string `mapstructure:"stack_status"` |
| 182 | StausReason string `mapstructure:"stack_status_reason"` |
| 183 | TemplateDescription string `mapstructure:"template_description"` |
| 184 | Timeout int `mapstructure:"timeout_mins"` |
| 185 | UpdatedTime time.Time `mapstructure:"-"` |
| 186 | } |
| 187 | |
| 188 | type PreviewResult struct { |
| 189 | gophercloud.Result |
| 190 | } |
| 191 | |
| 192 | func (r PreviewResult) Extract() (*PreviewStack, error) { |
| 193 | if r.Err != nil { |
| 194 | return nil, r.Err |
| 195 | } |
| 196 | |
| 197 | var res struct { |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 198 | Stack *PreviewStack `mapstructure:"stack"` |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | config := &mapstructure.DecoderConfig{ |
| 202 | Result: &res, |
| 203 | WeaklyTypedInput: true, |
| 204 | } |
| 205 | decoder, err := mapstructure.NewDecoder(config) |
| 206 | if err != nil { |
| 207 | return nil, err |
| 208 | } |
| 209 | |
| 210 | if err := decoder.Decode(r.Body); err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | |
| 214 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 215 | |
| 216 | if date, ok := b["creation_time"]; ok && date != nil { |
| 217 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | res.Stack.CreationTime = t |
| 222 | } |
| 223 | |
| 224 | if date, ok := b["updated_time"]; ok && date != nil { |
| 225 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 226 | if err != nil { |
| 227 | return nil, err |
| 228 | } |
| 229 | res.Stack.UpdatedTime = t |
| 230 | } |
| 231 | |
| 232 | return res.Stack, err |
| 233 | } |
| 234 | |
Jon Perritt | 01972e2 | 2015-01-28 10:30:45 -0700 | [diff] [blame^] | 235 | type AbandonedStack struct { |
Jon Perritt | 35e27e4 | 2014-12-05 11:10:46 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | type AbandonResult struct { |
| 239 | gophercloud.Result |
| 240 | } |