Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame^] | 1 | package stacktemplates |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | // Get retreives data for the given stack template. |
| 11 | func Get(c *gophercloud.ServiceClient, stackName, stackID string) GetResult { |
| 12 | var res GetResult |
| 13 | _, res.Err = perigee.Request("GET", getURL(c, stackName, stackID), perigee.Options{ |
| 14 | MoreHeaders: c.AuthenticatedHeaders(), |
| 15 | Results: &res.Body, |
| 16 | OkCodes: []int{200}, |
| 17 | }) |
| 18 | return res |
| 19 | } |
| 20 | |
| 21 | // ValidateOptsBuilder describes struct types that can be accepted by the Validate call. |
| 22 | // The ValidateOpts struct in this package does. |
| 23 | type ValidateOptsBuilder interface { |
| 24 | ToStackTemplateValidateMap() (map[string]interface{}, error) |
| 25 | } |
| 26 | |
| 27 | // ValidateOpts specifies the template validation parameters. |
| 28 | type ValidateOpts struct { |
| 29 | Template map[string]interface{} |
| 30 | TemplateURL string |
| 31 | } |
| 32 | |
| 33 | // ToStackTemplateValidateMap assembles a request body based on the contents of a ValidateOpts. |
| 34 | func (opts ValidateOpts) ToStackTemplateValidateMap() (map[string]interface{}, error) { |
| 35 | vo := make(map[string]interface{}) |
| 36 | if opts.Template != nil { |
| 37 | vo["template"] = opts.Template |
| 38 | return vo, nil |
| 39 | } |
| 40 | if opts.TemplateURL != "" { |
| 41 | vo["template_url"] = opts.TemplateURL |
| 42 | return vo, nil |
| 43 | } |
| 44 | return vo, fmt.Errorf("One of Template or TemplateURL is required.") |
| 45 | } |
| 46 | |
| 47 | // Validate validates the given stack template. |
| 48 | func Validate(c *gophercloud.ServiceClient, opts ValidateOptsBuilder) ValidateResult { |
| 49 | var res ValidateResult |
| 50 | |
| 51 | reqBody, err := opts.ToStackTemplateValidateMap() |
| 52 | if err != nil { |
| 53 | res.Err = err |
| 54 | return res |
| 55 | } |
| 56 | |
| 57 | _, res.Err = perigee.Request("POST", validateURL(c), perigee.Options{ |
| 58 | MoreHeaders: c.AuthenticatedHeaders(), |
| 59 | ReqBody: reqBody, |
| 60 | Results: &res.Body, |
| 61 | OkCodes: []int{200}, |
| 62 | }) |
| 63 | return res |
| 64 | } |