
    4                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	l
mZ dd
l
mZ ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ d Zd Zd Zd Zy)z+Utilities for storage surface IAM commands.    )absolute_import)division)unicode_literals)	cloud_api)gcs_iam_util)iam_util)errors)plurality_checkable_iterator)storage_url)wildcard_iterator)task)task_executor)task_graph_executor)task_status)	task_utilc                 X   t        j                  |       st        j                  |       S t        j                  | t
        j                  j                        }t        j                  |      }|j                         rt        j                  d      t        |      d   j                  S )zEGets cloud resource, allowing wildcards that match only one resource.)fields_scopez2get-iam-policy must match a single cloud resource.r   )r   contains_wildcardr   storage_url_from_stringget_wildcard_iteratorr   FieldsScopeSHORTr
   PluralityCheckableIterator	is_pluralr	   InvalidUrlErrorlist)
url_stringresource_iterator%plurality_checkable_resource_iterators      :lib/googlecloudsdk/command_lib/storage/iam_command_util.pyget_single_matching_urlr!   #   s    		,	,Z	8..z::'==y44::< #==>OP ( +446

 
 <> >	3	4Q	7	C	CC    c           	         t        j                  |       }|j                         sQdt        j                  t        |      j                         j                  t        j                  j                        fS t        j                  j                         }t        j                  |d|t!        j"                  t         j$                  j&                  d      |      }|dfS )a#  Executes single or multiple set-IAM tasks with different handling.

  Args:
    iterator (iter[set_iam_policy_task._SetIamPolicyTask]): Contains set IAM
      task(s) to execute.
    continue_on_error (bool): If multiple tasks in iterator, determines whether
      to continue executing after an error.

  Returns:
    int: Status code. For multiple tasks, the task executor will return if
      any of the tasks failed.
    object|None: If executing a single task, the newly set IAM policy. This
      is useful for outputting to the terminal.
  r   TN)increment_typemanifest_path)parallelizabletask_status_queueprogress_manager_argscontinue_on_error)r
   r   r   r   "get_first_matching_message_payloadnextexecutemessagesr   TopicSET_IAM_POLICYr   multiprocessing_contextQueuer   execute_tasksr   ProgressManagerArgsIncrementTypeINTEGER)iteratorr)   !plurality_checkable_task_iteratorr'   	exit_codes        r    execute_set_iam_task_iteratorr9   4   s      #==hG $ 
+	4	4	6 i::./779BB

!!# # # *AAGGI))')';;$22::$P)+) 
Dr"   c                    t        j                  |       }t        j                  |_        t        j
                  |j                  j                  |j                  || j                  | j                  |        |||      j                         }t        j                  |j                  t        j                   j"                        S )a  Extracts new binding from args and applies to existing policy.

  Args:
    args (argparse Args): Contains flags user ran command with.
    url (CloudUrl): URL of target resource, already validated for type.
    messages (object): Must contain IAM data types needed to create new policy.
    policy (object): Existing IAM policy on target to update.
    task_type (set_iam_policy_task._SetIamPolicyTask): The task instance to use
      to execute the iam binding change.

  Returns:
    object: The updated IAM policy set in the cloud.
  )r   ValidateAndExtractConditionr   IAM_POLICY_VERSIONversion"AddBindingToIamPolicyWithConditionPolicyBindingsValueListEntryExprmemberroler,   r   r*   r-   r   r.   r/   )argsurlr-   policy	task_type	conditiontask_outputs          r    add_iam_binding_to_resourcerJ   X   s     2248)22&.
--oo,,hmmV
kk499i) #v&..0+		5	5k6J6J6:jj6O6O
Q Qr"   c                 b   t        j                  |       }t        j                  |_        t        j
                  || j                  | j                  || j                          |||      j                         }t        j                  |j                  t        j                  j                        S )a  Extracts binding from args and removes it from existing policy.

  Args:
    args (argparse Args): Contains flags user ran command with.
    url (CloudUrl): URL of target resource, already validated for type.
    policy (object): Existing IAM policy on target to update.
    task_type (set_iam_policy_task._SetIamPolicyTask): The task instance to use
      to execute the iam binding change.

  Returns:
    object: The updated IAM policy set in the cloud.
  )r   r;   r   r<   r=   'RemoveBindingFromIamPolicyWithConditionrB   rC   allr,   r   r*   r-   r   r.   r/   )rD   rE   rF   rG   rH   rI   s         r     remove_iam_binding_from_resourcerN   r   s     2248)22&.
2264;;3799i3788= #v&..0+		5	5k6J6J6:jj6O6O
Q Qr"   N)__doc__
__future__r   r   r   googlecloudsdk.api_lib.storager   r   googlecloudsdk.command_lib.iamr   "googlecloudsdk.command_lib.storager	   r
   r   r   (googlecloudsdk.command_lib.storage.tasksr   r   r   r   r   r!   r9   rJ   rN    r"   r    <module>rV      sO    2 &  ' 4 7 3 5 K : @ 9 B H @ >D"!HQ4Qr"   