forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_ssm_activation.go
173 lines (141 loc) · 4.28 KB
/
resource_aws_ssm_activation.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsSsmActivation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmActivationCreate,
Read: resourceAwsSsmActivationRead,
Delete: resourceAwsSsmActivationDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"expired": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"iam_role": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"registration_limit": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"registration_count": {
Type: schema.TypeInt,
Computed: true,
},
"activation_code": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsSsmActivationCreate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] SSM activation create: %s", d.Id())
activationInput := &ssm.CreateActivationInput{
IamRole: aws.String(d.Get("name").(string)),
}
if _, ok := d.GetOk("name"); ok {
activationInput.DefaultInstanceName = aws.String(d.Get("name").(string))
}
if _, ok := d.GetOk("description"); ok {
activationInput.Description = aws.String(d.Get("description").(string))
}
if _, ok := d.GetOk("expiration_date"); ok {
activationInput.ExpirationDate = aws.Time(d.Get("expiration_date").(time.Time))
}
if _, ok := d.GetOk("iam_role"); ok {
activationInput.IamRole = aws.String(d.Get("iam_role").(string))
}
if _, ok := d.GetOk("registration_limit"); ok {
activationInput.RegistrationLimit = aws.Int64(int64(d.Get("registration_limit").(int)))
}
// Retry to allow iam_role to be created and policy attachment to take place
var resp *ssm.CreateActivationOutput
err := resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
resp, err = ssmconn.CreateActivation(activationInput)
if err != nil {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
})
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SSM activation: {{err}}", err)
}
if resp.ActivationId == nil {
return fmt.Errorf("[ERROR] ActivationId was nil")
}
d.SetId(*resp.ActivationId)
d.Set("activation_code", resp.ActivationCode)
return resourceAwsSsmActivationRead(d, meta)
}
func resourceAwsSsmActivationRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] Reading SSM Activation: %s", d.Id())
params := &ssm.DescribeActivationsInput{
Filters: []*ssm.DescribeActivationsFilter{
{
FilterKey: aws.String("ActivationIds"),
FilterValues: []*string{
aws.String(d.Id()),
},
},
},
MaxResults: aws.Int64(1),
}
resp, err := ssmconn.DescribeActivations(params)
if err != nil {
return errwrap.Wrapf("[ERROR] Error reading SSM activation: {{err}}", err)
}
if resp.ActivationList == nil || len(resp.ActivationList) == 0 {
return fmt.Errorf("[ERROR] ActivationList was nil or empty")
}
activation := resp.ActivationList[0] // Only 1 result as MaxResults is 1 above
d.Set("name", activation.DefaultInstanceName)
d.Set("description", activation.Description)
d.Set("expiration_date", activation.ExpirationDate)
d.Set("expired", activation.Expired)
d.Set("iam_role", activation.IamRole)
d.Set("registration_limit", activation.RegistrationLimit)
d.Set("registration_count", activation.RegistrationsCount)
return nil
}
func resourceAwsSsmActivationDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] Deleting SSM Activation: %s", d.Id())
params := &ssm.DeleteActivationInput{
ActivationId: aws.String(d.Id()),
}
_, err := ssmconn.DeleteActivation(params)
if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting SSM activation: {{err}}", err)
}
return nil
}