@@ -107,47 +107,50 @@ def get_env_var_value(env_var):
107107 return value
108108
109109def get_synapse_owner_id (tags ):
110- '''Find the value of the principal ARN among the resource tags. The principal
111- ARN tag is applied by AWS and it's value should be in the following format
112- 'arn:aws:sts::111111111:assumed-role/ServiceCatalogEndusers/1234567'
113- '''
114- principal_arn_tag = 'aws:servicecatalog:provisioningPrincipalArn'
115- for tag in tags :
116- if tag .get ('Key' ) == principal_arn_tag :
117- principal_arn_value = tag .get ('Value' )
118- synapse_owner_id = principal_arn_value .split ('/' )[- 1 ]
119- return synapse_owner_id
120- else :
121- raise ValueError (f'Expected to find { principal_arn_tag } in { tags } ' )
122-
123- def get_synapse_owner_id (tags ):
124- '''Find the value of the principal ARN among the resource tags. The principal
125- ARN tag is applied by AWS and it's value should be in the following format
126- 'arn:aws:sts::111111111:assumed-role/ServiceCatalogEndusers/378505'
110+ '''Find the synapse owner ID from a group of tags. Look for the id from
111+ 'synapse:ownerId' tag first, if not found then look for the
112+ 'aws:servicecatalog:provisioningPrincipalArn' tag (IT-4483).
113+ The principal ARN tag is applied by AWS on an initial service catalog
114+ product deployment, it's value is in the following format
115+ 'arn:aws:sts::111111111:assumed-role/ServiceCatalogEndusers/378505'
127116 :param tags: resource tags can take two forms
128117 * A list of dictionary of key/value pairs
129- i.e. tags: [{'Key':'string ', 'Value':'string '}]
130- * A dictionary of key pairs
131- i.e. tags:{'string ':'string '}
118+ i.e. tags = [{'Key':'key1 ', 'Value':'value1'}, {'Key':'key2', 'Value':'value2 '}]
119+ * A dictionary of key value pairs
120+ i.e. tags = {'key1 ':'value1', 'key2':'value2 '}
132121 returns: the synapse user id (i.e. 378505)
133122 '''
123+ synapse_owner_id_tag = 'synapse:ownerId'
134124 principal_arn_tag = 'aws:servicecatalog:provisioningPrincipalArn'
135- synapse_owner_id = None
136125
137- if isinstance (tags , list ): # tags:[{'Key':'string', 'Value':'string'}]
138- for tag in tags :
139- if tag .get ('Key' ) == principal_arn_tag :
140- principal_arn_value = tag .get ('Value' )
126+ # Case 1: list of dicts with "Key"/"Value"
127+ # tags = [{'Key':'key1', 'Value':'value1'}, {'Key':'key2', 'Value':'value2'}]
128+ if isinstance (tags , list ):
129+ for item in tags : # Look for synapse:ownerId first
130+ if item .get ("Key" ) == synapse_owner_id_tag :
131+ synapse_owner_id = item .get ('Value' )
132+ return synapse_owner_id
133+ for item in tags : # Fallback to aws:servicecatalog:provisioningPrincipalArn
134+ if item .get ("Key" ) == principal_arn_tag :
135+ principal_arn_value = item .get ('Value' )
141136 synapse_owner_id = principal_arn_value .split ('/' )[- 1 ]
142- if isinstance (tags , dict ): # tags:{'string':'string'}
143- if principal_arn_tag in tags :
144- principal_arn_value = tags .get (principal_arn_tag )
145- synapse_owner_id = principal_arn_value .split ('/' )[- 1 ]
146-
147- if synapse_owner_id is None :
148- raise ValueError (f'{ principal_arn_tag } not found in tags: { tags } ' )
137+ return synapse_owner_id
138+ return None
139+
140+ # Case 2: dictionary of key-value pairs
141+ # tags = {'key1':'value1', 'key2':'value2'}
142+ elif isinstance (tags , dict ):
143+ if synapse_owner_id_tag in tags : # Look for synapse:ownerId first
144+ synapse_owner_id = tags [synapse_owner_id_tag ]
145+ return synapse_owner_id
146+ elif principal_arn_tag in tags : # Fallback to aws:servicecatalog:provisioningPrincipalArn
147+ principal_arn_value = tags [principal_arn_tag ]
148+ synapse_owner_id = principal_arn_value .split ('/' )[- 1 ]
149+ return synapse_owner_id
150+ return None
149151
150- return synapse_owner_id
152+ else :
153+ raise TypeError ("Input must be either a dict or a list of {'Key':..., 'Value':...} dicts." )
151154
152155def get_synapse_user_profile (synapse_id ):
153156 '''Get synapse user profile data'''
0 commit comments