blob: 5f8aba9524dfc186436ec453d37b53e6eedfdb3d [file] [log] [blame]
Jon Perrittf799b942015-02-09 11:23:28 -07001package stacktemplates
2
3import (
4 "fmt"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8)
9
10// Get retreives data for the given stack template.
11func 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.
23type ValidateOptsBuilder interface {
24 ToStackTemplateValidateMap() (map[string]interface{}, error)
25}
26
27// ValidateOpts specifies the template validation parameters.
28type 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.
34func (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.
48func 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}