@@ -19,14 +19,32 @@ func resourceAwsAmiLaunchPermission() *schema.Resource {
19
19
Importer : & schema.ResourceImporter {
20
20
State : func (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
21
21
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
24
46
}
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
+
30
48
return []* schema.ResourceData {d }, nil
31
49
},
32
50
},
@@ -39,8 +57,21 @@ func resourceAwsAmiLaunchPermission() *schema.Resource {
39
57
},
40
58
"account_id" : {
41
59
Type : schema .TypeString ,
42
- Required : true ,
60
+ Optional : true ,
43
61
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
+ },
44
75
},
45
76
},
46
77
}
@@ -51,28 +82,42 @@ func resourceAwsAmiLaunchPermissionCreate(d *schema.ResourceData, meta interface
51
82
52
83
image_id := d .Get ("image_id" ).(string )
53
84
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
+ }
54
94
55
95
_ , err := conn .ModifyImageAttribute (& ec2.ModifyImageAttributeInput {
56
96
ImageId : aws .String (image_id ),
57
97
Attribute : aws .String (ec2 .ImageAttributeNameLaunchPermission ),
58
98
LaunchPermission : & ec2.LaunchPermissionModifications {
59
99
Add : []* ec2.LaunchPermission {
60
- { UserId : aws . String ( account_id )} ,
100
+ launch_permission ,
61
101
},
62
102
},
63
103
})
64
104
if err != nil {
65
105
return fmt .Errorf ("error creating AMI launch permission: %w" , err )
66
106
}
67
107
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
+
69
114
return nil
70
115
}
71
116
72
117
func resourceAwsAmiLaunchPermissionRead (d * schema.ResourceData , meta interface {}) error {
73
118
conn := meta .(* AWSClient ).ec2conn
74
119
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 ) )
76
121
if err != nil {
77
122
return fmt .Errorf ("error reading AMI launch permission (%s): %w" , d .Id (), err )
78
123
}
@@ -94,13 +139,21 @@ func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface
94
139
95
140
image_id := d .Get ("image_id" ).(string )
96
141
account_id := d .Get ("account_id" ).(string )
142
+ group_name := d .Get ("group_name" ).(string )
97
143
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
+ }
98
151
_ , err := conn .ModifyImageAttribute (& ec2.ModifyImageAttributeInput {
99
152
ImageId : aws .String (image_id ),
100
153
Attribute : aws .String (ec2 .ImageAttributeNameLaunchPermission ),
101
154
LaunchPermission : & ec2.LaunchPermissionModifications {
102
155
Remove : []* ec2.LaunchPermission {
103
- { UserId : aws . String ( account_id )} ,
156
+ launch_permission ,
104
157
},
105
158
},
106
159
})
@@ -111,7 +164,7 @@ func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface
111
164
return nil
112
165
}
113
166
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 ) {
115
168
attrs , err := conn .DescribeImageAttribute (& ec2.DescribeImageAttributeInput {
116
169
ImageId : aws .String (image_id ),
117
170
Attribute : aws .String (ec2 .ImageAttributeNameLaunchPermission ),
@@ -127,7 +180,9 @@ func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string) (boo
127
180
}
128
181
129
182
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 {
131
186
return true , nil
132
187
}
133
188
}
0 commit comments