-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
resource_aws_workspaces_ip_group.go
173 lines (146 loc) · 4.41 KB
/
resource_aws_workspaces_ip_group.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"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/workspaces"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"log"
)
func resourceAwsWorkspacesIpGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWorkspacesIpGroupCreate,
Read: resourceAwsWorkspacesIpGroupRead,
Update: resourceAwsWorkspacesIpGroupUpdate,
Delete: resourceAwsWorkspacesIpGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"rules": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"tags": tagsSchema(),
},
}
}
func resourceAwsWorkspacesIpGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).workspacesconn
rules := d.Get("rules").(*schema.Set).List()
tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().WorkspacesTags()
resp, err := conn.CreateIpGroup(&workspaces.CreateIpGroupInput{
GroupName: aws.String(d.Get("name").(string)),
GroupDesc: aws.String(d.Get("description").(string)),
UserRules: expandIpGroupRules(rules),
Tags: tags,
})
if err != nil {
return err
}
d.SetId(*resp.GroupId)
return resourceAwsWorkspacesIpGroupRead(d, meta)
}
func resourceAwsWorkspacesIpGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).workspacesconn
resp, err := conn.DescribeIpGroups(&workspaces.DescribeIpGroupsInput{
GroupIds: []*string{aws.String(d.Id())},
})
if err != nil {
if len(resp.Result) == 0 {
log.Printf("[WARN] Workspaces Ip Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("name", resp.Result[0].GroupName)
d.Set("description", resp.Result[0].GroupDesc)
d.Set("rules", flattenIpGroupRules(resp.Result[0].UserRules))
tags, err := keyvaluetags.WorkspacesListTags(conn, d.Id())
if err != nil {
return fmt.Errorf("error listing tags for Workspaces IP Group (%q): %s", d.Id(), err)
}
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
return nil
}
func resourceAwsWorkspacesIpGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).workspacesconn
if d.HasChange("rules") {
rules := d.Get("rules").(*schema.Set).List()
log.Printf("[INFO] Updating Workspaces IP Group Rules")
_, err := conn.UpdateRulesOfIpGroup(&workspaces.UpdateRulesOfIpGroupInput{
GroupId: aws.String(d.Id()),
UserRules: expandIpGroupRules(rules),
})
if err != nil {
return err
}
}
if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.WorkspacesUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceAwsWorkspacesIpGroupRead(d, meta)
}
func resourceAwsWorkspacesIpGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).workspacesconn
log.Printf("[INFO] Deleting Workspaces IP Group")
_, err := conn.DeleteIpGroup(&workspaces.DeleteIpGroupInput{
GroupId: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Error Deleting Workspaces IP Group: %s", err)
}
return nil
}
func expandIpGroupRules(rules []interface{}) []*workspaces.IpRuleItem {
var result []*workspaces.IpRuleItem
for _, rule := range rules {
r := rule.(map[string]interface{})
result = append(result, &workspaces.IpRuleItem{
IpRule: aws.String(r["source"].(string)),
RuleDesc: aws.String(r["description"].(string)),
})
}
return result
}
func flattenIpGroupRules(rules []*workspaces.IpRuleItem) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(rules))
for _, rule := range rules {
r := map[string]interface{}{
"source": *rule.IpRule,
}
if rule.RuleDesc != nil {
r["description"] = *rule.RuleDesc
}
result = append(result, r)
}
return result
}