22import logging
33from dataclasses import dataclass
44from enum import Enum
5- from typing import Any , Dict , List , Optional
5+ from typing import Any , Dict , List , Optional , Tuple
66
77import boto3
88import botocore .exceptions
@@ -145,23 +145,30 @@ def _tags(self):
145145 tags .append ({"Key" : name , "Value" : value })
146146 return tags
147147
148- def _share_list_filtered (self , share_conf : List [str ]) -> List [Dict [str , str ]]:
148+ def _share_list_filtered (self , share_conf : List [str ]) -> Tuple [ List [Dict [str , str ]], List [ Dict [ str , str ] ]]:
149149 """
150150 Get a filtered list of share configurations based on the current partition
151151 :param share_conf: the share configuration
152152 :type share_conf: List[str]
153153 :return: a List of share configurations that is usable by modify_image_attribute()
154- :rtype: List[Dict[str, str]]
154+ :rtype: Tuple[ List[Dict[str, str]], List[Dict[str, str] ]]
155155 """
156156 # the current partition
157157 partition_current = boto3 .client ("ec2" ).meta .partition
158158
159159 share_list : List [Dict [str , str ]] = []
160+ volume_list : List [Dict [str , str ]] = []
160161 for share in share_conf :
161- partition , account_id = _split_partition (share )
162+ partition , account_id_or_arn = _split_partition (share )
162163 if partition == partition_current :
163- share_list .append ({"UserId" : account_id })
164- return share_list
164+ if ":organization/o-" in account_id_or_arn :
165+ share_list .append ({"OrganizationArn" : account_id_or_arn })
166+ elif ":ou/o-" in account_id_or_arn :
167+ share_list .append ({"OrganizationalUnitArn" : account_id_or_arn })
168+ else :
169+ share_list .append ({"UserId" : account_id_or_arn })
170+ volume_list .append ({"UserId" : account_id_or_arn })
171+ return share_list , volume_list
165172
166173 def _share (self , share_conf : List [str ], images : Dict [str , _ImageInfo ]):
167174 """
@@ -172,7 +179,7 @@ def _share(self, share_conf: List[str], images: Dict[str, _ImageInfo]):
172179 :param images: a Dict with region names as keys and _ImageInfo objects as values
173180 :type images: Dict[str, _ImageInfo]
174181 """
175- share_list = self ._share_list_filtered (share_conf )
182+ share_list , volume_list = self ._share_list_filtered (share_conf )
176183
177184 if not share_list :
178185 logger .info ("no valid accounts found for sharing in this partition, skipping" )
@@ -188,11 +195,11 @@ def _share(self, share_conf: List[str], images: Dict[str, _ImageInfo]):
188195 )
189196
190197 # modify snapshot permissions
191- if image_info .snapshot_id :
198+ if image_info .snapshot_id and volume_list :
192199 ec2client .modify_snapshot_attribute (
193200 Attribute = "createVolumePermission" ,
194201 SnapshotId = image_info .snapshot_id ,
195- CreateVolumePermission = {"Add" : share_list }, # type: ignore
202+ CreateVolumePermission = {"Add" : volume_list }, # type: ignore
196203 )
197204
198205 logger .info (f"shared images & snapshots with '{ share_conf } '" )
0 commit comments