
    '                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrS rS r	S	 r
 " S
 S\5      r " S S\5      rS rg)a  Module for user service account mapping API support.

Typical usage (update command):

  # When defining arguments
  user_sa_mapping_util.AddUpdateUserSaMappingFlags(parser)

  # When running the command
  user_sa_mapping_diff = user_sa_mapping_util.Diff.FromUpdateArgs(args)
  if user_sa_mapping_diff.HasUpdates():
    orig_resource = Get(...)  # to prevent unnecessary Get calls
    user_sa_mapping_update = user_sa_mapping_diff.Apply(user_sa_mapping_cls,
    orig_resource.user_sa_mapping)
    if user_sa_mapping_update.needs_update:
      new_resource.user_sa_mapping = user_sa_mapping_update.user_sa_mapping
      field_mask.append('user_sa_mapping')
  Update(..., new_resource)

  # Or alternatively, when running the command
  user_sa_mapping_update = user_sa_mapping_util.ProcessUpdateArgsLazy(
    args, user_sa_mapping_cls, lambda: Get(...).user_sa_mapping)
  if user_sa_mapping_update.needs_update:
    new_resource.user_sa_mapping = user_sa_mapping_update.user_sa_mapping
    field_mask.append('user_sa_mapping')
  Update(..., new_resource)
    )absolute_import)division)unicode_literals)arg_parsersNc                 2   U R                  SS9nUR                  5       nUR                  SS[        R                  " 5       [        R                  SS9  UR                  SS[        R
                  " 5       [        R                  S	S9  UR                  S
SS9  g)zAdds update command user service account mapping flags to an argparse parser.

Args:
  parser: The argparse parser to add the flags to.
T)mutexz--add-user-mappingsz	KEY=VALUEz                List of user-to-service-account mappings to add to current mappings.
                If a mapping exists, its value is modified; otherwise, the new
                mapping is added.
            )metavartypeactionhelpz--remove-user-mappingsKEYz                List of user-to-service-account mappings to remove from the
                current mappings. If a mapping does not exist, it is ignored.
        z--identity-config-filea                  Path to a YAML (or JSON) file that contains the configuration for [Secure Multi-Tenancy](/dataproc/docs/concepts/iam/sa-multi-tenancy)
                on the cluster. The path can be a Cloud Storage URL (example: 'gs://path/to/file')
                or a local filesystem path. The mappings provided in the file will overwrite existing mappings.

                The YAML file is formatted as follows:

                ```
                  # Mapping header (first line) required.
                  user_service_account_mapping:
                    bob@company.com: service-account-bob@project.iam.gserviceaccount.com
                    alice@company.com: service-account-alice@project.iam.gserviceaccount.com
                ```
            )r   N)	add_groupadd_argumentr   ArgDictUpdateActionArgList)parsersecure_multi_tenancy_group!add_and_remove_user_mapping_groups      Elib/googlecloudsdk/command_lib/dataproc/utils/user_sa_mapping_util.pyAddUpdateUserSaMappingFlagsr   1   s      &//d/;&@&J&J&L##00 %% 1 
 $00 %% 1 	 )) *     c                     U R                   $ )zReturns the add user mapping dict from the parsed args.

Args:
  args: The parsed args.

Returns:
  The add user mapping dict from the parsed args.
)add_user_mappingsargss    r   GetAddUserSaMappingDictFromArgsr   a   s     
		r   c                     U R                   $ )zReturns the remove user mapping list from the parsed args.

Args:
  args: The parsed args.

Returns:
  The remove user mapping list from the parsed args.
)remove_user_mappingsr   s    r   "GetRemoveUserSaMappingListFromArgsr    m   s     
	"	""r   c                   4    \ rS rSrSrS r\S 5       rS rSr	g)UpdateResulty   a~  Result type for Diff application.

Attributes:
  needs_update: bool, whether the diff resulted in any changes to the existing
    user service account mapping proto.
  _user_sa_mapping: UserServiceAccountMappingValue, the new populated
    UserServiceAccountMappingValue object. If needs_update is False, this is
    identical to the original UserServiceAccountMappingValue object.
c                     Xl         X l        g N)needs_update_user_sa_mapping)selfr&   user_sa_mappings      r   __init__UpdateResult.__init__   s    $+r   c                 R    U R                   (       d  [        S5      eU R                  $ )z_Returns the new user service account mapping.

Raises:
  ValueError: if needs_update is False.
zjIf no update is needed (self.needs_update == False), checking user service account mapping is unnecessary.)r&   
ValueErrorr'   r(   s    r   r)   UpdateResult.user_sa_mapping   s.     B     r   c                 <     U R                   $ ! [         a     gf = f)aH  Returns the new user service account mapping if an update is needed or None otherwise.

NOTE: If this function returns None, make sure not to include the user
service account mapping field in the field mask of the update command.
Otherwise, it's possible to inadvertently clear the user service account
mapping on the resource.
N)r)   r-   r.   s    r   	GetOrNoneUpdateResult.GetOrNone   s%    !!! s    
)r'   r&   N)
__name__
__module____qualname____firstlineno____doc__r*   propertyr)   r1   __static_attributes__ r   r   r"   r"   y   s%    , ! !r   r"   c                   Z    \ rS rSrSrSS jrS rS rS rS r	S	 r
SS
 jr\S 5       rSrg)Diff   z/Class for diffing user service account mapping.Nc                     Xl         X l        g)zInitialize a Diff.

Args:
  add_user_mapping: {str: str}, any user-to-service-account mapping to be
    added
  remove_user_mapping: List[str], any user-to-service-account mappings to be
    removed

Returns:
  Diff.
N_add_user_mapping_remove_user_mapping)r(   add_user_mappingremove_user_mappings      r   r*   Diff.__init__   s     . 3r   c                 \    UR                  5       nUR                  U R                  5        U$ r%   )copyupdater@   )r(   new_user_sa_mappings     r   _AddUserSaMappingDiff._AddUserSaMapping   s+    -224t556r   c                 p    UR                  5       nU R                   H  nUR                  US 5        M     U$ r%   )rF   rA   pop)r(   rH   keys      r   _RemoveUserSaMappingDiff._RemoveUserSaMapping   s7    -224((c4( )r   c                     U(       d  0 $ UR                    Vs0 s H  o"R                  UR                  _M     sn$ s  snf r%   )additionalPropertiesrM   value)r(   r)   ls      r   _GetExistingUserSaMappingDict"Diff._GetExistingUserSaMappingDict   s4    i$3$H$HI$HqEE177N$HIIIs    ;c                     U" [        [        R                  " U5      5       VVs/ s H  u  p4UR                  X4S9PM     snnS9$ s  snnf )a  Converts a dictionary representing a user service account mapping into an instance of a specified class.

Args:
  user_sa_mapping_cls: The class to instantiate, which should have an
    `AdditionalProperty` inner class and an `additionalProperties`
    attribute.
  user_sa_mapping: A dictionary where keys are user identifiers and values
    are service account identifiers.

Returns:
  An instance of `user_sa_mapping_cls` populated with the key-value pairs
  from `user_sa_mapping`.
)rM   rR   )rQ   )sortedsix	iteritemsAdditionalProperty)r(   user_sa_mapping_clsr)   rM   rR   s        r   _PackageUserSaMappingDiff._PackageUserSaMapping   sQ      %S]]?%CD
D
  22s2HD
 
s   A
c                 D    [        U R                  U R                  /5      $ )zaReturns true iff the diff is non-empty which means user service account mapping has been updated.)anyr@   rA   r.   s    r   
HasUpdatesDiff.HasUpdates   s    &&(A(ABCCr   c                 
   U R                  U5      nUR                  5       nU R                  (       a  U R                  U5      nU R                  (       a  U R                  U5      nXC:g  n[        UU R                  X5      5      $ )a  Apply this Diff to the existing user service account mapping.

Args:
  user_sa_mapping_cls: type, the UserServiceAccountMappingValue class for
    the resource.
  existing_user_sa_mapping: UserServiceAccountMappingValue, the existing
    UserServiceAccountMappingValue object for the original resource (or
    None, which is treated the same as empty user service account mapping)

Returns:
  UpdateResult, the result of applying the diff.
)rT   rF   r@   rI   rA   rN   r"   r\   )r(   r[   existing_user_sa_mappingexisting_user_sa_mapping_dictnew_user_sa_mapping_dictr&   s         r   Apply
Diff.Apply   s     %)$F$F %!  =AAC!%!7!7
""   !%!:!:
"" ,LL""	
 r   c                 <    U " UR                   UR                  5      $ r%   )r   r   )clsr   s     r   FromUpdateArgsDiff.FromUpdateArgs  s    t%%t'@'@AAr   r?   )NNr%   )r3   r4   r5   r6   r7   r*   rI   rN   rT   r\   r`   rf   classmethodrj   r9   r:   r   r   r<   r<      sA    74
J
,D!F B Br   r<   c                     [         R                  U 5      nUR                  5       (       a  U" 5       OSnUR                  X5      $ )a  Returns the result of applying the diff constructed from args.

Lazily fetches the original user service account mapping value if needed.

Args:
  args: argparse.Namespace, the parsed arguments with add_user_mapping and
    remove_user_mapping
  user_sa_mapping_cls: type, the UserSaMappingValue class for the new user
    service account mapping.
  orig_user_sa_mapping_thunk: callable, a thunk which will return the original
    user_service_account_mapping object when evaluated.

Returns:
  UpdateResult: the result of applying the diff.
N)r<   rj   r`   rf   )r   r[   orig_user_sa_mapping_thunkdifforig_user_sa_mappings        r   ProcessUpdateArgsLazyrq     s@    $ 
		T	"$&*oo&7&7 "T  
'	>>r   )r7   
__future__r   r   r   googlecloudsdk.callioper   rX   r   r   r    objectr"   r<   rq   r:   r   r   <module>ru      sN   6 '  ' / 
-`	 	#(6 (VaB6 aBH?r   