This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
/
create_stack.go
108 lines (92 loc) · 2.39 KB
/
create_stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cloudformation
import (
"net/url"
"strconv"
)
type BaseParameters struct {
Capabilities []string
Parameters []*StackParameter
StackName string
StackPolicyBody string
StackPolicyURL string
TemplateBody string
TemplateURL string
}
type Values map[string]string
func (values Values) Encode() string {
ret := url.Values{}
for k, v := range values {
if v != "" {
ret.Add(k, v)
}
}
return ret.Encode()
}
func (v Values) updateCapabilities(capabilities []string) {
for i, c := range capabilities {
v["Capabilities.member."+strconv.Itoa(i+1)] = c
}
}
func (v Values) updateParameters(parameters []*StackParameter) {
for i, p := range parameters {
v["Parameters.member."+strconv.Itoa(i+1)+".ParameterKey"] = p.ParameterKey
v["Parameters.member."+strconv.Itoa(i+1)+".ParameterValue"] = p.ParameterValue
}
}
func (c *BaseParameters) values() Values {
v := Values{
"StackPolicyBody": c.StackPolicyBody,
"StackPolicyURL": c.StackPolicyURL,
"TemplateBody": c.TemplateBody,
"TemplateURL": c.TemplateURL,
"StackName": c.StackName,
}
v.updateCapabilities(c.Capabilities)
v.updateParameters(c.Parameters)
return v
}
type CreateStackParameters struct {
BaseParameters
DisableRollback bool
NotificationARNs []string
OnFailure string
Tags []*Tag
TimeoutInMinutes int
}
func (c *CreateStackParameters) values() Values {
v := c.BaseParameters.values()
v["OnFailure"] = c.OnFailure
if c.DisableRollback {
v["DisableRollback"] = "true"
}
if c.TimeoutInMinutes > 0 {
v["TimeoutInMinutes"] = strconv.Itoa(c.TimeoutInMinutes)
}
for i, arn := range c.NotificationARNs {
v["NoNotificationARNs.member."+strconv.Itoa(i+1)] = arn
}
for i, tag := range c.Tags {
v["Tags.member."+strconv.Itoa(i+1)+".Key"],_ = tag.Key.(string)
v["Tags.member."+strconv.Itoa(i+1)+".Value"],_ = tag.Value.(string)
}
return v
}
type StackParameter struct {
ParameterKey string
ParameterValue string
}
type CreateStackResponse struct {
CreateStackResult *CreateStackResult `xml:"CreateStackResult"`
}
type CreateStackResult struct {
StackId string `xml:"StackId"`
}
func (client *Client) CreateStack(params CreateStackParameters) (stackId string, e error) {
r := &CreateStackResponse{}
v := params.values()
e = client.loadCloudFormationResource("CreateStack", v, r)
if e != nil {
return "", e
}
return r.CreateStackResult.StackId, nil
}