forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_ssm_parameter.go
127 lines (102 loc) · 2.98 KB
/
resource_aws_ssm_parameter.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
package aws
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsSsmParameter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmParameterPut,
Read: resourceAwsSsmParameterRead,
Update: resourceAwsSsmParameterPut,
Delete: resourceAwsSsmParameterDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSsmParameterType,
},
"value": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"key_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"overwrite": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceAwsSsmParameterRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] Reading SSM Parameter: %s", d.Id())
paramInput := &ssm.GetParametersInput{
Names: []*string{
aws.String(d.Get("name").(string)),
},
WithDecryption: aws.Bool(true),
}
resp, err := ssmconn.GetParameters(paramInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error describing SSM parameter: {{err}}", err)
}
if len(resp.Parameters) == 0 {
log.Printf("[WARN] SSM Param %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
param := resp.Parameters[0]
d.Set("name", param.Name)
d.Set("type", param.Type)
d.Set("value", param.Value)
return nil
}
func resourceAwsSsmParameterDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[INFO] Deleting SSM Parameter: %s", d.Id())
paramInput := &ssm.DeleteParameterInput{
Name: aws.String(d.Get("name").(string)),
}
_, err := ssmconn.DeleteParameter(paramInput)
if err != nil {
return err
}
d.SetId("")
return nil
}
func resourceAwsSsmParameterPut(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[INFO] Creating SSM Parameter: %s", d.Get("name").(string))
paramInput := &ssm.PutParameterInput{
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
Value: aws.String(d.Get("value").(string)),
Overwrite: aws.Bool(d.Get("overwrite").(bool)),
}
if keyID, ok := d.GetOk("key_id"); ok {
log.Printf("[DEBUG] Setting key_id for SSM Parameter %s: %s", d.Get("name").(string), keyID.(string))
paramInput.SetKeyId(keyID.(string))
}
log.Printf("[DEBUG] Waiting for SSM Parameter %q to be updated", d.Get("name").(string))
_, err := ssmconn.PutParameter(paramInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SSM parameter: {{err}}", err)
}
d.SetId(d.Get("name").(string))
return resourceAwsSsmParameterRead(d, meta)
}