Skip to content

Commit 1cb51f2

Browse files
committed
aws_ami_launch_permission: support group permissions
1 parent 997ca2c commit 1cb51f2

4 files changed

+235
-33
lines changed

.changelog/20677.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_ami_launch_permission: Add `group` support for making public AMIs
3+
```

aws/resource_aws_ami_launch_permission.go

+69-14
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,32 @@ func resourceAwsAmiLaunchPermission() *schema.Resource {
1919
Importer: &schema.ResourceImporter{
2020
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
2121
idParts := strings.Split(d.Id(), "/")
22-
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
23-
return nil, fmt.Errorf("Unexpected format of ID (%q), expected ACCOUNT-ID/IMAGE-ID", d.Id())
22+
23+
parseError := fmt.Errorf("Unexpected format of ID (%q), expected ACCOUNT-ID/IMAGE-ID or group/GROUP-NAME/ACCOUNT-ID", d.Id())
24+
if len(idParts) == 2 {
25+
// Parsing the ACCOUNT-ID/IMAGE-ID branch
26+
if idParts[0] == "" || idParts[1] == "" {
27+
return nil, parseError
28+
}
29+
accountId := idParts[0]
30+
imageId := idParts[1]
31+
d.Set("account_id", accountId)
32+
d.Set("image_id", imageId)
33+
d.SetId(fmt.Sprintf("%s-account-%s", imageId, accountId))
34+
} else if len(idParts) == 3 && idParts[0] == "group" {
35+
// Parsing the group/GROUP-NAME/ACCOUNT-ID branch
36+
if idParts[1] == "" || idParts[2] == "" {
37+
return nil, parseError
38+
}
39+
groupName := idParts[1]
40+
imageId := idParts[2]
41+
d.Set("group_name", groupName)
42+
d.Set("image_id", imageId)
43+
d.SetId(fmt.Sprintf("%s-group-%s", imageId, groupName))
44+
} else {
45+
return nil, parseError
2446
}
25-
accountId := idParts[0]
26-
imageId := idParts[1]
27-
d.Set("account_id", accountId)
28-
d.Set("image_id", imageId)
29-
d.SetId(fmt.Sprintf("%s-%s", imageId, accountId))
47+
3048
return []*schema.ResourceData{d}, nil
3149
},
3250
},
@@ -39,8 +57,21 @@ func resourceAwsAmiLaunchPermission() *schema.Resource {
3957
},
4058
"account_id": {
4159
Type: schema.TypeString,
42-
Required: true,
60+
Optional: true,
4361
ForceNew: true,
62+
ExactlyOneOf: []string{
63+
"account_id",
64+
"group_name",
65+
},
66+
},
67+
"group_name": {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
ForceNew: true,
71+
ExactlyOneOf: []string{
72+
"account_id",
73+
"group_name",
74+
},
4475
},
4576
},
4677
}
@@ -51,28 +82,42 @@ func resourceAwsAmiLaunchPermissionCreate(d *schema.ResourceData, meta interface
5182

5283
image_id := d.Get("image_id").(string)
5384
account_id := d.Get("account_id").(string)
85+
group_name := d.Get("group_name").(string)
86+
87+
var launch_permission *ec2.LaunchPermission
88+
89+
if account_id != "" {
90+
launch_permission = &ec2.LaunchPermission{UserId: aws.String(account_id)}
91+
} else {
92+
launch_permission = &ec2.LaunchPermission{Group: aws.String(group_name)}
93+
}
5494

5595
_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
5696
ImageId: aws.String(image_id),
5797
Attribute: aws.String(ec2.ImageAttributeNameLaunchPermission),
5898
LaunchPermission: &ec2.LaunchPermissionModifications{
5999
Add: []*ec2.LaunchPermission{
60-
{UserId: aws.String(account_id)},
100+
launch_permission,
61101
},
62102
},
63103
})
64104
if err != nil {
65105
return fmt.Errorf("error creating AMI launch permission: %w", err)
66106
}
67107

68-
d.SetId(fmt.Sprintf("%s-%s", image_id, account_id))
108+
if account_id != "" {
109+
d.SetId(fmt.Sprintf("%s-account-%s", image_id, account_id))
110+
} else {
111+
d.SetId(fmt.Sprintf("%s-group-%s", image_id, group_name))
112+
}
113+
69114
return nil
70115
}
71116

72117
func resourceAwsAmiLaunchPermissionRead(d *schema.ResourceData, meta interface{}) error {
73118
conn := meta.(*AWSClient).ec2conn
74119

75-
exists, err := hasLaunchPermission(conn, d.Get("image_id").(string), d.Get("account_id").(string))
120+
exists, err := hasLaunchPermission(conn, d.Get("image_id").(string), d.Get("account_id").(string), d.Get("group_name").(string))
76121
if err != nil {
77122
return fmt.Errorf("error reading AMI launch permission (%s): %w", d.Id(), err)
78123
}
@@ -94,13 +139,21 @@ func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface
94139

95140
image_id := d.Get("image_id").(string)
96141
account_id := d.Get("account_id").(string)
142+
group_name := d.Get("group_name").(string)
97143

144+
var launch_permission *ec2.LaunchPermission
145+
146+
if account_id != "" {
147+
launch_permission = &ec2.LaunchPermission{UserId: aws.String(account_id)}
148+
} else {
149+
launch_permission = &ec2.LaunchPermission{Group: aws.String(group_name)}
150+
}
98151
_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
99152
ImageId: aws.String(image_id),
100153
Attribute: aws.String(ec2.ImageAttributeNameLaunchPermission),
101154
LaunchPermission: &ec2.LaunchPermissionModifications{
102155
Remove: []*ec2.LaunchPermission{
103-
{UserId: aws.String(account_id)},
156+
launch_permission,
104157
},
105158
},
106159
})
@@ -111,7 +164,7 @@ func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface
111164
return nil
112165
}
113166

114-
func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string) (bool, error) {
167+
func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string, group_name string) (bool, error) {
115168
attrs, err := conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{
116169
ImageId: aws.String(image_id),
117170
Attribute: aws.String(ec2.ImageAttributeNameLaunchPermission),
@@ -127,7 +180,9 @@ func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string) (boo
127180
}
128181

129182
for _, lp := range attrs.LaunchPermissions {
130-
if aws.StringValue(lp.UserId) == account_id {
183+
if account_id != "" && aws.StringValue(lp.UserId) == account_id {
184+
return true, nil
185+
} else if group_name != "" && aws.StringValue(lp.Group) == group_name {
131186
return true, nil
132187
}
133188
}

0 commit comments

Comments
 (0)