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 | |
| 11 | type CreateStack struct { |
| 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 { |
| 26 | Stack *CreateStack `json:"stack"` |
| 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 | |
| 40 | // StackPage is a pagination.Pager that is returned from a call to the List function. |
| 41 | type StackPage struct { |
| 42 | pagination.SinglePageBase |
| 43 | } |
| 44 | |
| 45 | // IsEmpty returns true if a ListResult contains no Stacks. |
| 46 | func (r StackPage) IsEmpty() (bool, error) { |
| 47 | stacks, err := ExtractStacks(r) |
| 48 | if err != nil { |
| 49 | return true, err |
| 50 | } |
| 51 | return len(stacks) == 0, nil |
| 52 | } |
| 53 | |
| 54 | type ListStack struct { |
| 55 | CreationTime time.Time `mapstructure:"-"` |
| 56 | Description string `mapstructure:"description"` |
| 57 | ID string `mapstructure:"id"` |
| 58 | Links []gophercloud.Link `mapstructure:"links"` |
| 59 | Name string `mapstructure:"stack_name"` |
| 60 | Status string `mapstructure:"stack_status"` |
| 61 | StausReason string `mapstructure:"stack_status_reason"` |
| 62 | UpdatedTime time.Time `mapstructure:"-"` |
| 63 | } |
| 64 | |
| 65 | // ExtractStacks extracts and returns a slice of Stacks. It is used while iterating |
| 66 | // over a stacks.List call. |
| 67 | func ExtractStacks(page pagination.Page) ([]ListStack, error) { |
| 68 | var res struct { |
| 69 | Stacks []ListStack `json:"stacks"` |
| 70 | } |
| 71 | |
| 72 | err := mapstructure.Decode(page.(StackPage).Body, &res) |
| 73 | return res.Stacks, err |
| 74 | } |
| 75 | |
| 76 | type GetStack struct { |
| 77 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 78 | CreationTime time.Time `mapstructure:"-"` |
| 79 | Description string `mapstructure:"description"` |
| 80 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 81 | ID string `mapstructure:"id"` |
| 82 | Links []gophercloud.Link `mapstructure:"links"` |
| 83 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 84 | Outputs []map[string]string `mapstructure:"outputs"` |
| 85 | Parameters map[string]string `mapstructure:"parameters"` |
| 86 | Name string `mapstructure:"stack_name"` |
| 87 | Status string `mapstructure:"stack_status"` |
| 88 | StausReason string `mapstructure:"stack_status_reason"` |
| 89 | TemplateDescription string `mapstructure:"template_description"` |
| 90 | Timeout int `mapstructure:"timeout_mins"` |
| 91 | UpdatedTime time.Time `mapstructure:"-"` |
| 92 | } |
| 93 | |
| 94 | type GetResult struct { |
| 95 | gophercloud.Result |
| 96 | } |
| 97 | |
| 98 | func (r GetResult) Extract() (*GetStack, error) { |
| 99 | if r.Err != nil { |
| 100 | return nil, r.Err |
| 101 | } |
| 102 | |
| 103 | var res struct { |
| 104 | Stack *GetStack `json:"stack"` |
| 105 | } |
| 106 | |
| 107 | config := &mapstructure.DecoderConfig{ |
| 108 | Result: &res, |
| 109 | WeaklyTypedInput: true, |
| 110 | } |
| 111 | decoder, err := mapstructure.NewDecoder(config) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | if err := decoder.Decode(r.Body); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | |
| 120 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 121 | |
| 122 | if date, ok := b["creation_time"]; ok && date != nil { |
| 123 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | res.Stack.CreationTime = t |
| 128 | } |
| 129 | |
| 130 | if date, ok := b["updated_time"]; ok && date != nil { |
| 131 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | res.Stack.UpdatedTime = t |
| 136 | } |
| 137 | |
| 138 | return res.Stack, err |
| 139 | } |
| 140 | |
| 141 | type UpdateResult struct { |
| 142 | gophercloud.ErrResult |
| 143 | } |
| 144 | |
| 145 | type DeleteResult struct { |
| 146 | gophercloud.ErrResult |
| 147 | } |
| 148 | |
| 149 | type PreviewStack struct { |
| 150 | Capabilities []interface{} `mapstructure:"capabilities"` |
| 151 | CreationTime time.Time `mapstructure:"-"` |
| 152 | Description string `mapstructure:"description"` |
| 153 | DisableRollback bool `mapstructure:"disable_rollback"` |
| 154 | ID string `mapstructure:"id"` |
| 155 | Links []gophercloud.Link `mapstructure:"links"` |
| 156 | Name string `mapstructure:"stack_name"` |
| 157 | NotificationTopics []interface{} `mapstructure:"notification_topics"` |
| 158 | Parameters map[string]string `mapstructure:"parameters"` |
| 159 | Resources []map[string]string `mapstructure:"resources"` |
| 160 | Status string `mapstructure:"stack_status"` |
| 161 | StausReason string `mapstructure:"stack_status_reason"` |
| 162 | TemplateDescription string `mapstructure:"template_description"` |
| 163 | Timeout int `mapstructure:"timeout_mins"` |
| 164 | UpdatedTime time.Time `mapstructure:"-"` |
| 165 | } |
| 166 | |
| 167 | type PreviewResult struct { |
| 168 | gophercloud.Result |
| 169 | } |
| 170 | |
| 171 | func (r PreviewResult) Extract() (*PreviewStack, error) { |
| 172 | if r.Err != nil { |
| 173 | return nil, r.Err |
| 174 | } |
| 175 | |
| 176 | var res struct { |
| 177 | Stack *PreviewStack `json:"stack"` |
| 178 | } |
| 179 | |
| 180 | config := &mapstructure.DecoderConfig{ |
| 181 | Result: &res, |
| 182 | WeaklyTypedInput: true, |
| 183 | } |
| 184 | decoder, err := mapstructure.NewDecoder(config) |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | |
| 189 | if err := decoder.Decode(r.Body); err != nil { |
| 190 | return nil, err |
| 191 | } |
| 192 | |
| 193 | b := r.Body.(map[string]interface{})["stack"].(map[string]interface{}) |
| 194 | |
| 195 | if date, ok := b["creation_time"]; ok && date != nil { |
| 196 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 197 | if err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | res.Stack.CreationTime = t |
| 201 | } |
| 202 | |
| 203 | if date, ok := b["updated_time"]; ok && date != nil { |
| 204 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | res.Stack.UpdatedTime = t |
| 209 | } |
| 210 | |
| 211 | return res.Stack, err |
| 212 | } |
| 213 | |
| 214 | type AbandonStack struct { |
| 215 | } |
| 216 | |
| 217 | type AbandonResult struct { |
| 218 | gophercloud.Result |
| 219 | } |