
    0                         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  G d de      Zy)zApphub Applications API.    )absolute_import)division)unicode_literals)
list_pager)consts)utils)waiter)base)iam_utilc                       e Zd ZdZej
                  j                  fdZd Z	 	 ddZ	d Z
ej
                  j                  fdZd Zd	 Zd
 Zd Zd Zd Zy)ApplicationsClientz&Client for Applications in apphub API.c                    t        j                  |      | _        t        j                  |      | _        | j                  j
                  | _        | j                  j                  | _        t        j                  | j                  | j                  j                        | _        t        j                  | j                  j                        | _        t        j                  | j                  | j                  j                        | _        y )N)api_lib_utilsGetClientInstanceclientGetMessagesModulemessagesprojects_locations_applications_app_clientprojects_locations_lookup_clientr	   CloudOperationPollerprojects_locations_operations_pollerCloudOperationPollerNoResources_delete_poller_lookup_poller)selfrelease_tracks     8lib/googlecloudsdk/api_lib/apphub/applications/client.py__init__zApplicationsClient.__init__    s    11-@DK!33MBDM{{BBD++88D..11DL !@@11D !5511D    c                 p    | j                   j                  |      }| j                  j                  |      S )zDescribe an application in the Project/location.

    Args:
      app_id: str, the application id.

    Returns:
      Described service project Resource.
    name)r   -ApphubProjectsLocationsApplicationsGetRequestr   Get)r   app_iddescribe_reqs      r    DescribezApplicationsClient.Describe1   s:     ==NN O L --r"   Nc                     | j                   j                  |      }t        j                  | j                  |d||d      S )a  List applications in the Projects/Location.

    Args:
      parent: str, projects/{projectId}/locations/{location}
      limit: int or None, the total number of results to return. Default value
        is None
      page_size: int, the number of entries in each batch (affects requests
        made, but not the yielded results). Default value is 100.

    Returns:
      Generator of matching service projects.
    )parentapplicationspageSize)field
batch_sizelimitbatch_size_attribute)r   .ApphubProjectsLocationsApplicationsListRequestr   YieldFromListr   )r   r,   r1   	page_sizelist_reqs        r    ListzApplicationsClient.List?   sN    $ }}KK L H ##' r"   c           	         | j                   j                  ||| j                   j                  | j                   j                  j                  |            |      }| j                   j	                  |||      }	| j
                  j                  |	      }
|r|
S t        j                  | j                  |
t        j                  j                  t        j                  j                        }|S )a  Creates an application in the Project/location.

    Args:
      app_id: str, Application ID
      scope_type: str, Scope of the Application
      display_name: str, Human-friendly display name
      description: str, Description of the Application
      attributes: Attributes, Attributes of the Application
      async_flag: Boolean value for async operation. If true the operation will
        be async
      parent: parent for project resource

    Returns:
      Application or Operation based on async flag value.
    type)descriptiondisplayNamescope
attributes)applicationapplicationIdr,   )r   ApplicationScopeTypeValueValuesEnum0ApphubProjectsLocationsApplicationsCreateRequestr   Creater   WaitForOperationr   api_lib_constsCreateApplicationWAIT_FOR_ADD_MESSAGEADD_TIMELIMIT_SEC)r   r(   
scope_typedisplay_namer;   r>   
async_flagr,   r?   
create_req	operationcreate_responses               r    rE   zApplicationsClient.Create]   s    2 --++ mm!!$$88D " 
  , K OOvf P J   ''
3I#44((==((::	O r"   c                 f   d}t        j                  |      j                         }| j                  j	                  |      }|j
                  t        j                  |      j                  t        j                  |      j                  j                  |j
                              |_        t        j                  |t        j                  j                        }|j                  rt        j                  |      j                  t        j                  |      j                  j                  |j                              |_        t        j                  |t        j                  j                         }|j"                  xs g D ]  }t        j                  |      j%                         }|j'                  dd      |_        |j'                  dd      r|j'                  dd      |_        |t,        j.                  j0                  k(  rK|j'                  dd      r9t        j                  |      j3                  |j'                  d            |_        |j6                  j9                  |       t        j                  |t        j                  j:                        } |j<                  xs g D ]  }	t        j                  |      j%                         }
|	j'                  dd      |
_        |	j'                  dd      r|	j'                  dd      |
_        |t,        j.                  j0                  k(  rK|	j'                  dd      r9t        j                  |      j3                  |	j'                  d            |
_        |j>                  j9                  |
       t        j                  |t        j                  j@                        } |jB                  xs g D ]  }t        j                  |      j%                         }|j'                  dd      |_        |j'                  d      r|j'                  d      |_        |t,        j.                  j0                  k(  rJ|j'                  d      r9t        j                  |      j3                  |j'                  d            |_        |jD                  j9                  |       t        j                  |t        j                  jF                        } |jH                  ?|jH                  |_        t        j                  |t        j                  jJ                        }|jL                  ?|jL                  |_&        t        j                  |t        j                  jN                        }| j                  jQ                  ||jS                         |	      }| jT                  jW                  |      }|jX                  r|S t        jZ                  | j\                  |t        j                  j^                  t        j                  j`                        }|S )
a  Updates an Apphub application.

    Args:
      args: args, Arguments provided by the client
      app_ref: Application reference
      release_track: release_track provided by the client

    Returns:
      Application or Operation based on async flag value.
     )r>   Nr9   emailzdisplay-namezchannel-uri)uri)r?   r%   
updateMask)1r   r   
Attributesr   rA   environment_typeEnvironmentrC   environmentAddToUpdateMaskrG   UpdateApplication"UPDATE_MASK_ENVIRONMENT_FIELD_NAMEcriticality_typeCriticalitycriticality"UPDATE_MASK_CRITICALITY_FIELD_NAMEbusiness_ownersContactInfogetrS   r<   r
   ReleaseTrackALPHAChannelchannelbusinessOwnersappend&UPDATE_MASK_BUSINESS_OWNERS_FIELD_NAMEdeveloper_ownersdeveloperOwners'UPDATE_MASK_DEVELOPER_OWNERS_FIELD_NAMEoperator_ownersoperatorOwners&UPDATE_MASK_OPERATOR_OWNERS_FIELD_NAMErL   #UPDATE_MASK_DISPLAY_NAME_FIELD_NAMEr;   "UPDATE_MASK_DESCRIPTION_FIELD_NAME/ApphubProjectsLocationsApplicationsPatchRequestRelativeNamer   Patchasync_rF   r   WAIT_FOR_UPDATE_MESSAGErJ   )r   argsapp_refr   update_maskr>   r?   b_ownerbusiness_ownerd_ownerdeveloper_ownero_owneroperator_owner	patch_reqrO   patch_responses                   r    UpdatezApplicationsClient.Update   s    K00?JJLJ--++z+BK(,>>
 ..K++D,A,AB 	 	  "11


*
*
M
Mk
 ,>>
 ..K++D,A,AB 	 	  "11


*
*
M
Mk
 ''-2-$66
  %[[$7n	^T	*%,[[%F"	$++11	1;;}d+#0#B#B$GM2G3 
  &&~6!11


*
*
Q
Qk .$ ((.B.%77
  &kk'48o	^T	*&-kk.$&G#	$++11	1;;}d+$1$C$C%GM2G3 
!   ''8!11


*
*
R
Rk /$ ''-2-$66
  %[[$7n	^	$%,[[%@"	$++11	1;;}%#0#B#B$GM2G3 
  &&~6!11


*
*
Q
Qk .$ $ $ 1 1k!11


*
*
N
Nk
 # $ 0 0k!11


*
*
M
Mk
 MM!!# N I   &&y1I{{"33((@@((::	N r"   c                     | j                   j                  |      }| j                  j                  |      }|r|S t	        j
                  | j                  |t        j                  j                  t        j                  j                        }|S )a0  Delete an application in the Project/location.

    Args:
      app_id: str, The name for the application being deleted
      async_flag: Boolean value for async operation. If true the operation will
        be async

    Returns:
      Empty Response Message or Operation based on async flag value.
    r$   )r   0ApphubProjectsLocationsApplicationsDeleteRequestr   Deleter   rF   r   rG   DeleteApplicationWAIT_FOR_DELETE_MESSAGEREMOVE_TIMELIMIT_SEC)r   r(   rM   
remove_reqrO   delete_responses         r    r   zApplicationsClient.Delete  s     OO P J   ''
3I#44((@@((==	O r"   c                 p    | j                   j                  |      }| j                  j                  |      S )zFetch the IAM Policy attached to the sepcified application.

    Args:
      app_id: str, the application id.

    Returns:
      The application's IAM Policy.
    )resource)r   6ApphubProjectsLocationsApplicationsGetIamPolicyRequestr   GetIamPolicy)r   r(   get_reqs      r    r   zApplicationsClient.GetIamPolicy/  s=     	LL 	M 	
 
 ((11r"   c                 z    t        j                  || j                  j                        }| j	                  ||      S )a  Sets an application's IamPolicy to the one provided.

    If 'policy_file' has no etag specified, this will BLINDLY OVERWRITE the IAM
    policy!

    Args:
        app_id: str, the application id..
        policy_file: a policy file.

    Returns:
        The IAM Policy.
    )r   ParsePolicyFiler   Policy_SetIamPolicyHelper)r   r(   policy_filepolicys       r    SetIamPolicyzApplicationsClient.SetIamPolicy@  s3     %%k4==3G3GHF##FF33r"   c                     | j                   j                  || j                   j                  |            }| j                  j	                  |      S )N)r   )r   setIamPolicyRequest)r   6ApphubProjectsLocationsApplicationsSetIamPolicyRequestSetIamPolicyRequestr   r   )r   r(   r   set_reqs       r    r   z&ApplicationsClient._SetIamPolicyHelperP  sV    LL $ A A !B !  	M 	
  ((11r"   c                     | j                  |      }t        j                  | j                  j                  |||       | j                  ||      S )a$  Does an atomic Read-Modify-Write, adding the member to the role.

    Args:
        app_id: str, the application id.
        member: str, the principal to add the binding for.
        role: predefined role, the role name to assign to the principal.

    Returns:
        The IAM Policy.

    )r   r   AddBindingToIamPolicyr   Bindingr   r   r(   memberroler   s        r    AddIamPolicyBindingz&ApplicationsClient.AddIamPolicyBindingY  sE     v&F""4==#8#8&&$O##FF33r"   c                 v    | j                  |      }t        j                  |||       | j                  ||      S )a&  Does an atomic Read-Modify-Write, removing the member to the role.

    Args:
        app_id: str, the application id.
        member: str, the principal to add the binding for.
        role: predefined role, the role name to assign to the principal.

    Returns:
        The IAM Policy.

    )r   r   RemoveBindingFromIamPolicyr   r   s        r    RemoveIamPolicyBindingz)ApplicationsClient.RemoveIamPolicyBindingi  s9     v&F''=##FF33r"   )Nd   )__name__
__module____qualname____doc__r
   rd   re   r!   r*   r7   rE   r   r   r   r   r   r   r    r"   r    r   r      sg    .#'#4#4#:#: "." 	<1f 150A0A0G0G AF82"4 24 4r"   r   N)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.apphubr   rG   r   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   googlecloudsdk.command_lib.iamr   objectr   r   r"   r    <module>r      s3     &  ' ' B @ . ( 3Z4 Z4r"   