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