
    !                         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ZddiZej,                   G d dej.                               Zy)z#Command for updating a custom role.    )absolute_import)division)unicode_literals)
exceptions)util)
http_retry)base)flags)iam_util)
console_ioNEXAMPLESaD            To update the role ``ProjectUpdater'' from a YAML file, run:

            $ {command} ProjectUpdater --organization=123 --file=role_file_path

          To update the role ``ProjectUpdater'' with flags, run:

            $ {command} ProjectUpdater --project=myproject --permissions=permission1,permission2
        c                       e Zd ZdZeZed        Zd Z e	j                  ej                  j                  j                        d        Zd Zd Zy)UpdatezJUpdate an IAM custom role.

  This command updates an IAM custom role.

  c                    | j                  d      }|j                  dd       |j                  dd       |j                  dd       |j                  d	d
       |j                  dd       |j                  dd       | j                  dd       t        j                  | d       t        j                  d      j                  |        y )NzThe following flags determine the fields need to be updated. You can update a role by specifying the following flags, or you can update a role from a YAML file by specifying the file flag.z--titlez)The title of the role you want to update.)helpz--descriptionz/The description of the role you want to update.z--stagez)The state of the role you want to update.--permissionszIThe permissions of the role you want to set. Use commas to separate them.z--add-permissionszIThe permissions you want to add to the role. Use commas to separate them.z--remove-permissionszNThe permissions you want to remove from the role. Use commas to separate them.z--filezeThe YAML file you want to use to update a role. Can not be specified with other flags except role-id.update)add_argument_groupadd_argumentr
   AddParentFlagsGetCustomRoleFlagAddToParser)parserupdateds     lib/surface/iam/roles/update.pyArgszUpdate.Args:   s   ''	NOG C  EO  QC  E'  ( '  ( -  . @  A 
*	H%11&9    c                 H   t        j                         \  }}t        j                  |j                  |j
                  |j                        }|j                         }|j                  r1|j                  s<|j                  s0|j                  s$|j                  s|j                  s|j                  rt        j                   dd      t        j"                  |j                  |j                        }|j$                  sd}t'        j(                  |dd       |j*                  s3| j-                  |||j.                  |j
                  |j                         	 |j0                  j3                  |j5                  ||            }t        j6                  |       |S | jA                  |||||      }t        j6                  |       |S # t8        j:                  $ r}t        j<                  |d	      d }~wt8        j>                  $ r}t        j<                  |      d }~ww xY w)
NfileotherszThe specified role does not contain an "etag" field identifying a specific version to replace. Updating a role without an "etag" can overwrite concurrent role changes.zReplace existing roleT)messageprompt_stringcancel_on_no)namerolezStale "etag": Please use the etag from your latest describe response. Or new changes have been made since your latest describe operation. Please retry the whole describe-update process. Or you can leave the etag blank to overwrite concurrent role changes.)error_format)!r   GetClientAndMessagesr   GetRoleNameorganizationprojectr%   Roler   titledescriptionstagepermissionsadd_permissionsremove_permissionsr   ConflictingArgumentsExceptionParseYamlToRoleetagr   PromptContinuequietWarnPermissionsincludedPermissionsorganizations_rolesPatch!IamOrganizationsRolesPatchRequestSetRoleStageIfAlphaapitools_exceptionsHttpConflictErrorHttpException	HttpErrorUpdateWithFlags)	selfargsclientmessages	role_namer%   msgreses	            r   Runz
Update.RunY   s   002FH$$T%6%6diiPI==?Dyy
**((DJJ$:J:J


$"9"966vxHH%%dii?dYY 	!!1	 ZZVXt/G/G!\\4+<+<	>*((..66T 7 +, 	$$S)
 

tYfh
GC  %J !22 /&& -/ 	/ !** *&&q))*s%   AG H!G11H!HH!c           	          | j                  |||||      \  }}|j                  j                  |j                  ||dj	                  |                  S )N,)r$   r%   
updateMask)GetUpdatedRoler9   r:   r;   join)rB   rC   rF   r%   
iam_clientrE   changed_fieldss          r   rA   zUpdate.UpdateWithFlags   sb    ..tY/98ED.))//22#((>2J 	3 	LM Mr   c                    g }|j                   "|j                  d       |j                   |_         |j                  "|j                  d       |j                  |_        |j                  r5|j                  d       t	        j
                  |j                        |_        |j                  .|j                  s|j                  rt        j                  dd      |j                  |j                  d       |j                  j                  d      |_        |j                  sg |_        |j                  s3| j                  |||j                  |j                  |j                          |j"                  j%                  |j'                  |            }|j                  s|j                  r9t)        |j                        }d	}	t)               }
|j                  rI|j                  j                  d      D ]+  }||vs|j+                  |       |
j+                  |       d
}	- |j                  rM|j                  j                  d      D ]/  }||v r|j-                  |       d
}	||
v s|
j-                  |       1 |	r|j                  d       t/        t1        |            |_        |j                  s2| j                  ||t/        |
      |j                  |j                          |j2                  |_        ||fS )z!Gets the updated role from flags.r-   r,   r.   r   z(-add-permissions or --remove-permissionsr8   rL   )r$   FT)r-   appendr,   r.   r   StageTypeFromStringr/   r0   r1   r   r2   splitr8   r6   r7   r*   r)   r9   GetIamOrganizationsRolesGetRequestsetaddremovelistsortedr4   )rB   rC   rF   r%   rP   rE   rQ   origin_roler/   changednewly_added_permissions
permissions               r   rN   zUpdate.GetUpdatedRole   s   N#M*))dzzG$::djzzG$//

;dj#)=)=)-)@)@44
EG G#12!%!1!1!7!7!<d#% ZZZ43K3K!\\4+<+<	>004400i0@BKt66778kg #			..44S9J{*OOJ'#''
3G	 :
 
	 	 1177<J;&z*G22#**:6 = 
34!%f[&9!:dZZZ!"9:DLL!..	0   DIr   c                     t        j                  ||t        j                  ||      |      }|j	                         }t        j
                  |       |j                         }t        j                  |       y )N)r   PermissionsHelperr   GetResourceReferenceGetApiDisabledPermissonsApiDisabledPermissionsWarningGetTestingPermissionsTestingPermissionsWarning)	rB   rP   rE   r/   r*   r)   permissions_helperapi_disabled_permissionstesting_permissionss	            r   r7   zUpdate.WarnPermissions   sl    //
H080M0M4;\1K0;=  2JJL**+CD,BBD&&':;r   N)__name__
__module____qualname____doc__DETAILED_HELPdetailed_helpstaticmethodr   rJ   r   RetryOnHttpStatussixmoveshttp_clientCONFLICTrA   rN   r7    r   r   r   r   0   se      -: :<)V  :		 5 5 > >?M @M3 j	<r   r   )rn   
__future__r   r   r   apitools.base.pyr   r=   googlecloudsdk.api_lib.iamr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   googlecloudsdk.command_lib.iamr
   r   googlecloudsdk.core.consoler   six.moves.http_clientrs   ro   UniverseCompatibleCommandr   rw   r   r   <module>r      sg     * &  ' > + 2 ( . 0 3 2  	 Y<T\\ Y< Y<r   