o
                        @   s  d Z ddlZddlZddlZddlZddl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dZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&e'h dZ(dZ)d Z*d!Z+d"Z,d#Z-d$Z.d%d&iZ/G d'd( d(ej0Z1d)d* Z2d+d, Z3d-d. Z4d/d0 Z5d1d2 Z6d3d4 Z7d5e8d6e8fd7d8Z9d5e8d6e8fd9d:Z:dd5e8d<e8fd=d>Z;dd5e8d<e8fd?d@Z<dAdB Z=dCdD Z>dEej?fdFdGZ@	HddIdJZAddKdLZB	HddMdNZCdOej?fdPe8dQe8dReDdSeDfdTdUZE	OddPe8dQe8dReDfdVdWZFddPe8dQe8dReDfdXdYZGddPe8d6e8dReDfdZd[ZHd\d] ZI	H	H	Hdd^eJd_eJd`eJfdadbZKdce8dde8fdedfZLdce8dde8fdgdhZM	i			H	Hddjee8 dke8dle8dme8dne8d^eJdoeJfdpdqZN		dd6e8dke8dme8dne8fdrdsZO	i	H			H	H	Hddke8djee8 dle8d`eJdme8dne8d^eJdteJdueJfdvdwZP	i		ddke8d6e8dxe8dme8dne8f
dydzZQd{d| ZRd}d~ ZSdddZTdd ZUdd ZVG dd dZW		ddke8deJdReDdSeDdme8dne8fddZX		ddke8deJdReDdSeDdme8dne8fddZYdd ZZd5e8fddZ[d5e8fddZ\d5e8fddZ]de^e8 fddZ_e1j`fddZadddZb	HdddZc	HdddZddd Zedd Zfdd Zgddej?fddZhddej?fddZidddZjdS )zservices helper functions.    N)List)encoding)
exceptions)
list_pager)services_util)apis_internal)
http_retry)log)
properties)	transportzprojects/%sz
folders/%szorganizations/%szprojects/%s/services/%szfolders/%s/services/%szorganizations/%s/services/%szservices/%sz/groups/dependenciesz/reverseClosurez%s/services/%sz/consumerPolicies/%sz/mcpPolicies/%szmcp_server:urlsz/effectivePolicyz/effectiveMcpPolicyzcategories/googlez%s/consumerOverrides/%s>   folders/organizations/	projects/v1v1beta1v1alphav2alphav2betai  zanthos.googleapis.comz\Warning: Disabling this service will also automatically disable any running Anthos clusters.c                   @   s   e Zd ZdZdZdZdZdS )ContainerTypezReturn the container type.         N)__name__
__module____qualname____doc__PROJECT_SERVICE_RESOURCEFOLDER_SERVICE_RESOURCEORG_SERVICE_RESOURCE r   r   I/tmp/google-cloud-sdk/lib/googlecloudsdk/api_lib/services/serviceusage.pyr   E   s
    r   c                 C   s
   t | S )z?Return the warning message associated with a protected service.)_PROTECTED_SERVICESget)service_namer   r   r    GetProtectedServiceWarningL   s   
r$   c                 C   s   d|  dS )z?Return the error message associated with a MCP enabled service.zrTo enable the MCP endpoint, the service must be enabled first. Do you want to enable the service for the resource ?r   )resource_namer   r   r    GetMcpEnabledErrorQ   s   r'   c              
   C   h   t td}|j}|j| d}z|j|W S  tjtjfy3 } zt	
|t	j W Y d}~dS d}~ww )a  Make API call to get a consumer policy.

  Args:
    policy_name: The name of a consumer policy. Currently supported format
      '{resource_type}/{resource_name}/consumerPolicies/default'. For example,
      'projects/100/consumerPolicies/default'.

  Raises:
    exceptions.GetConsumerPolicyPermissionDeniedException: when getting a
      consumer policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    message.GoogleApiServiceusageV2betaConsumerPolicy: The consumer policy
  versionnameN)_GetClientInstance_V2BETA_VERSIONMESSAGES_MODULE&ServiceusageConsumerPoliciesGetRequestconsumerPoliciesGetapitools_exceptionsHttpForbiddenErrorHttpNotFoundErrorr   ReraiseErrorGetConsumerPolicyExceptionpolicy_nameclientmessagesrequester   r   r    GetConsumerPolicyV2BetaZ   s   
r>   c              
   C   f   t t}|j}|j| d}z|j|W S  tjtjfy2 } zt	
|t	j W Y d}~dS d}~ww )a  Make API call to get a MCP policy.

  Args:
    policy_name: The name of a MCP policy. Currently supported format
      '{resource_type}/{resource_name}/mcpPolicies/default'. For example,
      'projects/100/mcpPolicies/default'.

  Raises:
    exceptions.GetMcpPolicyException: when getting a
      MCP policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The MCP policy
  r+   N)r-   r.   r/   !ServiceusageMcpPoliciesGetRequestmcpPoliciesr2   r3   r4   r5   r   r6   GetMcpPolicyExceptionr8   r   r   r    GetMcpPolicyx      rC   c              
   C   r?   )a0  Make API call to get the content security policy for a given project, folder or organization.

  Args:
    name: The name of a content security policy. Currently supported format
      '{resource_type}/{resource_name}/contentSecurityPolicies/default'. For
      example, 'projects/100/contentSecurityPolicies/default'.

  Raises:
    exceptions.GetContentSecurityPolicyException: when getting a
      content security policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The content security policy.
  r+   N)r-   r.   r/   -ServiceusageContentSecurityPoliciesGetRequestcontentSecurityPoliciesr2   r3   r4   r5   r   r6   !GetContentSecurityPolicyExceptionr,   r:   r;   r<   r=   r   r   r    GetContentSecurityPolicy   rD   rI   c              
   C   sh   t t}|j}|j| |d}z|j|W S  tjtjfy3 } zt	
|t	j W Y d}~dS d}~ww )a{  Make API call to update the content security policy for a given project, folder or organization.

  Args:
    name: The name of a content security policy. Currently supported format
      '{resource_type}/{resource_name}/contentSecurityPolicies/default'. For
      example, 'projects/100/contentSecurityPolicies/default'.
    content_security_policy: The content security policy to update.

  Raises:
    exceptions.UpdateContentSecurityPolicyException: when updating a
      content security policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The content security policy.
  )r,   contentSecurityPolicyN)r-   r.   r/   /ServiceusageContentSecurityPoliciesPatchRequestrF   Patchr3   r4   r5   r   r6   $UpdateContentSecurityPolicyException)r,   content_security_policyr:   r;   r<   r=   r   r   r    UpdateContentSecurityPolicy   s   rO   r,   servicec              
   C   p   t t}|j}|j| |j|dd}z|j|W S  tjtj	fy7 } zt
|t
j W Y d}~dS d}~ww )aP  Make API call to test MCP enabled.

  Args:
    name: Parent resource to test a value against the result of merging MCP
      policies in the resource hierarchy. Format: "projects/{PROJECT_ID}" (e.g.,
      "projects/foo-bar"), "projects/{PROJECT_NUMBER}" (e.g.,
      "projects/12345678"), "folders/{FOLDER_NUMBER}" (e.g., "folders/1234567")
      , "organizations/{ORGANIZATION_NUMBER}" (e.g., "organizations/123456").
    service: Service name to check if the targeted resource can use this service
      for MCP. Current supported value: services/{SERVICE_NAME} (format:
      "services/{service}").

  Raises:
    exceptions.TestMcpEnabledException: when testing value for a
      service and resource.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.State: The state of the service.
  serviceName)r,   testMcpEnabledRequestN)r-   r.   r/   !ServiceusageTestMcpEnabledRequestTestMcpEnabledRequestr   TestMcpEnabledr3   r4   r5   r   r6   TestMcpEnabledExceptionr,   rP   r:   r;   r<   r=   r   r   r    rW      s   
rW   c              
   C   rQ   )a  Make API call to test enabled.

  Args:
    name: Parent resource to test a value against the result of merging consumer
      policies in the resource hierarchy. format-"projects/100", "folders/101"
      or "organizations/102".
    service: Service name to check if the targeted resource can use this
      service. Current supported value: SERVICE (format: "services/{service}").

  Raises:
    exceptions.TestEnabledPermissionDeniedException: when testing value for a
      service and resource.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.State: The state of the service.
  rR   )r,   testEnabledRequestN)r-   r.   r/   ServiceusageTestEnabledRequestTestEnabledRequestr   TestEnabledr3   r4   r5   r   r6   TestEnabledExceptionrY   r   r   r    r]      s   
r]   BASICviewc              
   C      t td}|j}|dkr|jjj}n|jjj}|j| |d}z|j|W S  t	j
t	jfyC } zt|tj W Y d}~dS d}~ww )a)  Make API call to get a effective policy.

  Args:
    name: The name of the effective policy.Currently supported format
      '{resource_type}/{resource_name}/effectivePolicy'. For example,
      'projects/100/effectivePolicy'.
    view: The view of the effective policy to use. The default view is 'BASIC'.

  Raises:
    exceptions.GetEffectivePolicyException: when getting a
      effective policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    message.EffectivePolicy: The effective policy
  r)   r_   r,   r`   N)r-   r.   r/   %ServiceusageGetEffectivePolicyRequestViewValueValuesEnumEFFECTIVE_POLICY_VIEW_BASICEFFECTIVE_POLICY_VIEW_FULLr   GetEffectivePolicyr3   r4   r5   r   r6   GetEffectivePolicyExceptionr,   r`   r:   r;   	view_typer<   r=   r   r   r    GetEffectivePolicyV2Beta  s&   
rk   c              
   C   ra   )aI  Make API call to get a effective MCP policy.

  Args:
    name: The name of the effective MCP policy. Currently supported format
      '{resource_type}/{resource_name}/effectiveMcpPolicy'. For example,
      'projects/100/effectivePolicy'.
    view: The view of the effective MCP policy to use. The default view is
      'BASIC'.

  Raises:
    exceptions.GetEffectiveMcpPolicyException: when getting a
      effective policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    message.EffectiveMcpPolicy: The effective MCP policy
  r)   r_   rb   N)r-   r.   r/   (ServiceusageGetEffectiveMcpPolicyRequestrd   EFFECTIVE_MCP_POLICY_VIEW_BASICEFFECTIVE_MCP_POLICY_VIEW_FULLr   GetEffectiveMcpPolicyr3   r4   r5   r   r6   GetEffectiveMcpPolicyExceptionri   r   r   r    ro   G  s&   
ro   c              
   C   sp   t td}|j}|j| |jjjd}z|j|W S  tj	tj
fy7 } zt|tj W Y d}~dS d}~ww )a  Make API call to get service state for a service .

  Args:
    service: Service. Current supported value:(format:
      "{resource}/{resource_Id}/services/{service}").

  Raises:
    exceptions.GetServiceException: when getting service
      service state for service in the resource.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.GetServicesResponse: Service state of the given resource.
  r)   rb   N)r-   r.   r/   ServiceusageServicesGetRequestrd   SERVICE_STATE_VIEW_FULLservicesr2   r3   r4   r5   r   r6   GetServiceException)rP   r:   r;   r<   r=   r   r   r    GetServiceV2Betaq  s   
ru   c              
   C   sr   t td}|j}|j| ||jjjd}z|j|W S  tj	tj
fy8 } zt|tj W Y d}~dS d}~ww )aO  Make API call to get service state for multiple services .

  Args:
    parent: Parent resource to get service state for. format-"projects/100",
      "folders/101" or "organizations/102".
    services: Services. Current supported value:(format:
      "{resource}/{resource_Id}/services/{service}").

  Raises:
    exceptions.BatchGetServiceException: when getting batch
      service state for services in the resource.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.BatchGetServicesResponse: Service state of the given resource.
  r)   )parentrs   r`   N)r-   r.   r/   #ServiceusageServicesBatchGetRequestrd   rr   rs   BatchGetr3   r4   r5   r   r6   BatchGetServiceException)rv   rs   r:   r;   r<   r=   r   r   r    BatchGetService  s    
rz      c              
   C   s   t td}|j}|jd| |d}ztjt|j||d|ddW S  t	j
t	jfy? } zt|tj W Y d}~dS d}~ww )at  Make API call to list category services .

  Args:
    resource: resource to get list for. format-"projects/100", "folders/101" or
      "organizations/102".
    category: category to get list for. format-"catgeory/<category>".
    page_size: The page size to list.default=200
    limit: The max number of services to display.

  Raises:
    exceptions.ListCategoryServicesException: when listing the
    services the parent category includes.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.ListCategoryServicesResponse: The services the parent category
    includes.
  r)   {}/{}rv   pageSizers   limitbatch_size_attribute
batch_sizefieldN)r-   r.   r/   1ServiceusageCategoriesCategoryServicesListRequestformatr   YieldFromList_Listercategories_categoryServicesr3   r4   r5   r   r6   ListCategoryServicesException)resourcecategory	page_sizer   r:   r;   r<   r=   r   r   r    ListCategoryServices  s*   

r   Fc              
   C   s   t d}|j}|j| |||d}z|j|W S  tjtjfy5 } zt	|tj
 W Y d}~dS d}~w tjyU } ztjd t	|tj W Y d}~dS d}~ww )a  Make API call to update a consumer policy.

  Args:
    consumerpolicy: The consumer policy to update.
    name: The resource name of the policy. Currently supported format
      '{resource_type}/{resource_name}/consumerPolicies/default. For example,
      'projects/100/consumerPolicies/default'.
    force: Disable service with usage within last 30 days or disable recently
      enabled service.
    validateonly: If set, validate the request and preview the result but do not
      actually commit it. The default is false.

  Raises:
    exceptions.UpdateConsumerPolicyPermissionDeniedException: when updating a
      consumer policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Updated consumer policy
  r   )*googleApiServiceusageV2alphaConsumerPolicyr,   forcevalidateOnlyN?Provide the --force flag if you wish to force disable services.)r-   r/   (ServiceusageConsumerPoliciesPatchRequestr1   rL   r3   r4   r5   r   r6   UpdateConsumerPolicyExceptionHttpBadRequestErrorr	   statusPrintErrorconsumerpolicyr,   r   validateonlyr:   r;   r<   r=   r   r   r    UpdateConsumerPolicyV2Alpha  .   r   c              
   C   sl   t t}|j}|j| |||d}z|j|W S  tjtjfy5 } zt	
|t	j W Y d}~dS d}~ww )a  Make API call to update a MCP policy.

  Args:
    policy: The MCP policy to update.
    name: The resource name of the MCP policy. Currently supported format
      '{resource_type}/{resource_name}/mcpPolicies/default. For example,
      'projects/100/mcpPolicies/default'.
    force: Disable service with usage within last 30 days or disable recently
      enabled service.(not supported during MVP.)
    validateonly: If set, validate the request and preview the result but do not
      actually commit it. The default is false.(not supported during MVP.)

  Raises:
    exceptions.class UpdateMcpPolicyException: when getting a
      MCP policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The MCP policy
  )	mcpPolicyr,   r   r   N)r-   r.   r/   #ServiceusageMcpPoliciesPatchRequestrA   rL   r3   r4   r5   r   r6   UpdateMcpPolicyException)policyr,   r   r   r:   r;   r<   r=   r   r   r    UpdateMcpPolicy  s   r   c              
   C   s   t t}|j}|j| |||d}z|j|W S  tjtjfy5 } zt	
|t	j W Y d}~dS d}~w tjyU } ztjd t	
|t	j W Y d}~dS d}~ww )a  Make API call to update a consumer policy.

  Args:
    consumerpolicy: The consumer policy to update.
    name: The resource name of the policy. Currently supported format
      '{resource_type}/{resource_name}/consumerPolicies/default. For example,
      'projects/100/consumerPolicies/default'.
    force: Disable service with usage within last 30 days or disable recently
      enabled service.
    validateonly: If set, validate the request and preview the result but do not
      actually commit it. The default is false.

  Raises:
    exceptions.UpdateConsumerPolicyException: when updating a
      consumer policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Updated consumer policy
  ))googleApiServiceusageV2betaConsumerPolicyr,   r   r   Nr   )r-   r.   r/   r   r1   rL   r3   r4   r5   r   r6   r   r   r	   r   r   r   r   r   r   r    UpdateConsumerPolicyV2Beta2  r   r   2   r   service_groupr   r   c              
   C   s   t t}|j}|j| d | d}ztjt|j||d|dd}g }|D ]}	||	 q$|W S  t	j
t	jt	jfy\ }
 zdt|
v rJg W  Y d}
~
S t|
tj W Y d}
~
dS d}
~
ww )a  Make API call to list group members of a specific service group.

  Args:
    resource: The target resource.
    service_group: Service group which owns a collection of group members, for
      example, 'services/compute.googleapis.com/groups/dependencies'.
    page_size: The page size to list. The default page_size is 50.
    limit: The max number of services to display.

  Raises:
    exceptions.ListGroupMembersPermissionDeniedException: when listing
      group members fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.ListGroupMembersResponse : Group members in the given service group.
  /r}   r~   memberStatesr   SU_GROUP_NOT_FOUNDN)r-   r.   r/   ,ServiceusageServicesGroupsMembersListRequestr   r   r   services_groups_membersappendr3   r   r4   r5   strr   r6   ListGroupMembersException)r   r   r   r   r:   r;   r<   responsemember_statesmember_stater=   r   r   r    ListGroupMembersa  s8   
r   c              
   C   s|   t d}|j}|jd| |d}ztjt|j|d|ddW S  tj	tj
fy= } zt|tj W Y d}~dS d}~ww )ae  Make API call to list descendant services of a specific service group.

  Args:
    resource: The target resource in the format:
      '{resource_type}/{resource_name}'.
    service_group: Service group, for example,
      'services/compute.googleapis.com/groups/dependencies'.
    page_size: The page size to list. The default page_size is 50.

  Raises:
    exceptions.ListDescendantServicesPermissionDeniedException: when listing
      descendant services fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Descendant services in the given service group.
  r   r|   r}   r~   rs   r   r   r   N)r-   r/   7ServiceusageServicesGroupsDescendantServicesListRequestr   r   r   r   "services_groups_descendantServicesr3   r4   r5   r   r6   ListDescendantServicesException)r   r   r   r:   r;   r<   r=   r   r   r    ListDescendantServices  s(   
r   c           
   
   C   s   t t}|j}|jd| |d}ztjt|j|d|dd}g }|D ]}|	|j
 q#|W S  tjtjfyZ }	 zdt|	v rHg W  Y d}	~	S t|	tj W Y d}	~	dS d}	~	ww )a  Make API call to list expanded members of a specific service group.

  Args:
    resource: The target resource in the format:
      '{resource_type}/{resource_name}'.
    service_group: Service group, for example,
      'services/compute.googleapis.com/groups/dependencies'.
    page_size: The page size to list. The default page_size is 50.

  Raises:
    exceptions.ListExpandedMembersException: when listing
      expanded members fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message. ExpandedMember.serviceName : Service names of the expanded members
    of the service group.
  r|   r}   r~   membersr   r   N)r-   r.   r/   4ServiceusageServicesGroupsExpandedMembersListRequestr   r   r   r   services_groups_expandedMembersr   rS   r3   r4   r5   r   r   r6   ListExpandedMembersException)
r   r   r   r:   r;   r<   r   service_namesmemberr=   r   r   r    ListExpandedMembers  s4   
r   c              
   C   s~   t d}|j}|j|  d| d}ztjt|j|d|ddW S  tjtj	fy> } zt
|t
j W Y d}~dS d}~ww )aV  Make API call to list ancestor groups that depend on the service.

  Args:
    resource: The target resource.format : '{resource_type}/{resource_name}'.
    service: The identifier of the service to get ancestor groups of, for
      example, 'services/compute.googleapis.com'.
    page_size: The page size to list.The default page_size is 50.

  Raises:
    exceptions.ListAncestorGroupsPermissionDeniedException: when listing
      ancestor group fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Ancestor groups that depend on the service.
  r   r   r+   r~   groupsr   N)r-   r/   -ServiceusageServicesAncestorGroupsListRequestr   r   r   services_ancestorGroupsr3   r4   r5   r   r6   +ListAncestorGroupsPermissionDeniedException)r   rP   r   r:   r;   r<   r=   r   r   r    ListAncestorGroups  s,   r   c              
   C   s~   t td}|j}|j|j| |jjjgd| jd}z|j	|W S  t
jt
jfy> } zt|tj W Y d}~dS d}~ww )a  Make API call to analyze a consumer policy for dependencies.

  Args:
    proposed_policy: The consumer policy to analyze. type :
      message.GoogleApiServiceusageV2alphaConsumerPolicy

  Raises:
    exceptions.AnalyzeConsumerPolicyException: when analyzing a
      consumer policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    message.
  r)   )proposedPolicyanalysisTypes)analyzeConsumerPolicyRequestr,   N)r-   r.   r/   *ServiceusageConsumerPoliciesAnalyzeRequestAnalyzeConsumerPolicyRequest%AnalysisTypesValueListEntryValuesEnumANALYSIS_TYPE_DEPENDENCYr,   r1   Analyzer3   r4   r5   r   r6   AnalyzeConsumerPolicyException)proposed_policyr:   r;   r<   r=   r   r   r    AnalyzeConsumerPolicy  s&   
	r   validate_onlybypass_dependency_checkr   c              
   C   s  t td}|j}z|j| d d}d|  v r4| d dur4| d D ]}d|v r3|j|j|d d q!|st|}t	
|jt}t|j}	i }
d|	v rz|	d D ](}|d	 d
 D ]}|d |
 vrm|d g|
|d < qY|
|d  |d  qYqQ|
rd}|
D ]}|
| D ]}||d | d 7 }qqt|t||j||dW S  tjtjfy } zt|tj W Y d}~dS d}~ww )aY  Make API call to update a consumer policy.

  Args:
    consumerpolicy: The consumer policy to update.
    validate_only: If True, the action will be validated and result will be
      preview but not exceuted.
    bypass_dependency_check: If True, dependencies check will be bypassed.
    force: If True, the system will bypass usage checks for services that are
      being removed.

  Raises:
    exceptions.UpdateConsumerPolicyException: when updating policy API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r)   r,   r+   enableRulesNrs   rs   analysisanalysisResultwarningsrP   missingDependencyzPolicy cannot be updated as 
z is missing service dependency 
)r   r   )r-   r.   r/   )GoogleApiServiceusageV2betaConsumerPolicykeysr   r   %GoogleApiServiceusageV2betaEnableRuler   r   WaitOperationr,   GetOperationV2Betar   MessageToDictr   r   ConfigErrorr   r3   r4   r5   r6   r   )r   r   r   r   r:   r;   r   enable_ruleopanalysis_reponsemissing_dependenciesr   warningerror_messagerP   
dependencyr=   r   r   r    UpdateConsumerPolicy@  sj   


r   content_security_providerr&   c           	   
   C   s   t td}|j}z<t|}| dsd|  } |j}dd |jD }| |v r/td|  dt	
|}|jj|j| d t||W S  tjtjfyb } zt|tj W Y d}~dS d}~ww )	a  Make API call to add a content security provider.

  Args:
    content_security_provider: The content security provider to add.
    resource_name: The resource name of the content security policy.

  Raises:
    exceptions.AddContentSecurityProviderException: when adding content security
    provider to content security policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r)   	services/c                 S   s   g | ]}|j qS r   r+   ).0pr   r   r    
<listcomp>  s    z.AddContentSecurityProvider.<locals>.<listcomp>The content security provider z already exists.r+   N)r-   r.   r/   rI   
startswithmcpContentSecuritycontentSecurityProvidersr   r   copydeepcopyr   ContentSecurityProviderrO   r3   r4   r5   r6   #AddContentSecurityProviderException)	r   r&   r:   r;   rN   mcp_content_security#existing_content_security_providersupdate_policyr=   r   r   r    AddContentSecurityProvider  s:   





r   c           	   
   C   s   zAt |}| dsd|  } t|}|j}g }d}|jD ]}|j| kr(d}q|| q|s9t	d|  d||_t
||W S  tjtjfy_ } zt|tj W Y d}~dS d}~ww )a  Make API call to remove a content security provider.

  Args:
    content_security_provider: The content security provider to remove.
    resource_name: The resource name of the content security policy.

  Raises:
    exceptions.RemoveContentSecurityProviderException: when removing content
    security
    provider from content security policy fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r   FTr   z does not exist.N)rI   r   r   r   r   r   r,   r   r   r   rO   r3   r4   r5   r6   &RemoveContentSecurityProviderException)	r   r&   rN   r   r   "updated_content_security_providers
is_presentr   r=   r   r   r    RemoveContentSecurityProvider  s>   





r   defaultrs   projectconsumer_policy_namefolderorganizationskip_dependencyc              
   C   s  t td}|j}t| }	|rt| }	|rt| }	|	t|  }
zt|
}t }t }|j	r4t|j	d j
}| D ]}t| |vrE|t|  q6t }|s~| D ] }t|	t| t }|D ]}|dd }|| vrl|| qZqMt|D ]}||vr}|| qr|s|rtdd|  d d| }d| }|rdt|}|d| 7 }|d7 }t||j	rt|D ]}|j	d j
| qn|j	|jt|d	 t||
|d
t|fW S  tjtjfy } zt|tj W Y d}~dS d}~ww )aH  Make API call to enable a specific service.

  Args:
    services: The identifier of the service to enable, for example
      'serviceusage.googleapis.com'.
    project: The project for which to enable the service.
    consumer_policy_name: Name of consumer policy. The default name is
      "default".
    folder: The folder for which to enable the service.
    organization: The organization for which to enable the service.
    validate_only: If True, the action will be validated and result will be
      preview but not exceuted.
    skip_dependency: If True, the dependencies of the service to be enabled will
      not be enabled.

  Raises:
    exceptions.EnableServiceException: when enabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r)   r   r   zThe service(s) ,z7 are already enabled and present in the consumer policyz and their dependencies r   )r   N)r-   r.   r/   _PROJECT_RESOURCE_FOLDER_RESOURCE_ORGANIZATION_RESOURCE_CONSUMER_POLICY_DEFAULTr>   setr   rs   _SERVICE_RESOURCEaddr   _DEPENDENCY_GROUPsplitlistr   r   joinr   r   r   r3   r4   r5   r6   EnableServiceException)rs   r   r   r   r   r   r   r:   r;   r&   r9   r   services_to_enabledexisting_servicesrP   dependent_serviceslist_expanded_membersdependent_servicedependent_service_nameservice_list_strmessagedependent_list_strr=   r   r   r    AddEnableRule  s   





r  c              
   C   s  t td}|j}t| }|rt| }|rt| }|td  }zGt|}|jrO|jd j	D ]}	|	j
t|  kr>td|  d q,|jd j	|jt|  d n|j|j|jt|  dgd t||W S  tjtjfy }
 zt|
tj W Y d}
~
dS d}
~
ww )	a*  Make API call to enable a specific service in mcp policy.

  Args:
    service: The identifier of the service to enable, for example
      'serviceusage.googleapis.com'.
    project: The project for which to enable the service.
    folder: The folder for which to enable the service.
    organization: The organization for which to enable the service.

  Raises:
    exceptions.EnableServiceException: when enabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r)   r   r   The service z is already enabled for MCP.rP   )mcpServicesN)r-   r.   r/   r  r  r  _MCP_POLICY_DEFAULTrC   mcpEnableRulesr  rP   r  r   r   r   
McpServiceMcpEnableRuler   r3   r4   r5   r6   EnableMcpServiceException)rP   r   r   r   r:   r;   r&   r9   r   mcp_servicer=   r   r   r    AddMcpEnableRule  sD   

r"  skip_dependency_checkdisable_dependency_servicesc	              
      sn  t |  }	|r
t| }	|rt| }	|	t|  }
zt|
  fdd|D fdd|D }s;tdd| d t	 }|j
D ]}D ]}t| |jv rX|jt|  qGqCg }|st|}t|jt}t|j}i }d|v r|d D ]0}|d	 d
 D ]'}D ]"}t| |d kr||vrg ||< || |d  ||d  qqqy|s|rt|}td| d t|}t	|}|j
  |j
D ] }t	|}|jD ]}||v r|j| q|jr|j
| qt||
||dW S  tjtjfy } zt|tj  W Y d}~dS d}~w tj!y6 } zt"j#$d t|tj% W Y d}~dS d}~ww )a  Make API call to disable a specific service.

  Args:
    project: The project for which to disable the service.
    services: The list of identifiers of the services to disable, for example
      ['serviceusage.googleapis.com', 'apikeys.googleapis.com'].
    consumer_policy_name: Name of consumer policy. The default name is
      "default".
    force: Disable service with usage within last 30 days or disable recently
      enabled service or disable the service even if there are enabled services
      which depend on it. This also disables the services which depend on the
      service to be disabled.
    folder: The folder for which to disable the service.
    organization: The organization for which to disable the service.
    validate_only: If True, the action will be validated and result will be
      preview but not exceuted.`
    skip_dependency_check: If True, the enabled dependent services of the
      service to be disabled will remian enabled.
    disable_dependency_services: If True, the services which depend on the
      service to be disabled will also be disabled.

  Raises:
    exceptions.EnableServiceException: when disabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  c                    s(   h | ] t  fd djD r qS )c                 3   s    | ]
}t   |jv V  qd S N)r  rs   )r   r   r  r   r    	<genexpr>  s
    
z-RemoveEnableRule.<locals>.<setcomp>.<genexpr>)anyr   )r   )current_policyr  r    	<setcomp>  s    z#RemoveEnableRule.<locals>.<setcomp>c                    s   g | ]}| vr|qS r   r   )r   rP   )services_to_remover   r    r     s    z$RemoveEnableRule.<locals>.<listcomp>zThe services r  z( are not enabled in the consumer policy.r   r   r   r   rP   z@The services are depended on by the following active service(s) z . Please remove the active dependent services or provide the --disable-dependency-services flag to disable them, or --bypass-dependency-service-check to ignore this check.)r   r   Nr   )&r  r  r  r  r>   r   r   r  r   r   r   r  rs   remover   r   r   r,   r   r   r   r   r   jsondumpsr  clearr   r3   r4   r5   r6   r  r   r	   r   r   r   )r   rs   r   r   r   r   r   r#  r$  r&   r9   r   r   rP   	to_remover   r   missing_dependencyr   r   json_stringupdated_consumer_poicyruler#   r=   r   )r(  r*  r    RemoveEnableRule  s   '

	

	





r4  mcp_policy_namec              
   C   s8  t |  }|r
t| }|rt| }|t|  }zMt|}d}t|}	|	j  |jrS|jD ]%}
t|
}|j	D ]}|j
t| krHd}|j	| q7|j	rR|	j| q-|r^td| d t|	|W S  tjtjfy } zt|tj W Y d}~dS d}~w tjy } zt|tj W Y d}~dS d}~ww )aj  Make API call to disable a service for MCP.

  Args:
    project: The project for which to disable the service for MCP.
    service: The service to disable for MCP, for example
      'serviceusage.googleapis.com'.
    mcp_policy_name: Name of MCP policy. The default name is "default".
    folder: The folder for which to disable the service.
    organization: The organization for which to disable the service.

  Raises:
    exceptions.EnableMcpServiceException: when disabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  TFr  z is not enabled for MCP.N)r  r  r  r  rC   r   r   r  r.  r  rP   r  r+  r   r   r   r   r3   r4   r5   r6   r   r   r   )r   rP   r5  r   r   r&   r9   r   already_disabledupdated_mcp_policymcp_enable_ruler3  r!  r=   r   r   r    RemoveMcpEnableRuleV  sJ   





r9  c              
   C   l   t  }|j}|jt| |f d}z|j|W S  tjtjfy5 } zt	
|t	j W Y d}~dS d}~ww )a  Make API call to enable a specific service.

  Args:
    project: The project for which to enable the service.
    service: The identifier of the service to enable, for example
      'serviceusage.googleapis.com'.

  Raises:
    exceptions.EnableServiceException: when enabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r+   N)r-   r/   !ServiceusageServicesEnableRequest_PROJECT_SERVICE_RESOURCErs   Enabler3   r4   r5   r   r6   r  r   rP   r:   r;   r<   r=   r   r   r    EnableApiCall  s   
r?  c              
   C   sr   t  }|j}|j|j|dt|  d}z|j|W S  tjtj	fy8 } zt
|t
j W Y d}~dS d}~ww )as  Make API call to batch enable services.

  Args:
    project: The project for which to enable the services.
    services: Iterable of identifiers of services to enable.

  Raises:
    exceptions.EnableServiceException: when enabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  )
serviceIds)batchEnableServicesRequestrv   N)r-   r/   &ServiceusageServicesBatchEnableRequestBatchEnableServicesRequestr  rs   BatchEnabler3   r4   r5   r   r6   r  )r   rs   r:   r;   r<   r=   r   r   r    BatchEnableApiCall  s    rE  c              
   C   s   t  }|j}|jjj}|r|jjj}|jt| |f |j||dd}z|j	|W S  t
jt
jfyG } zt|tj W Y d}~dS d}~w t
jyg } ztjd t|tj W Y d}~dS d}~ww )aQ  Make API call to disable a specific service.

  Args:
    project: The project for which to enable the service.
    service: The identifier of the service to disable, for example
      'serviceusage.googleapis.com'.
    force: disable the service even if there are enabled services which depend
      on it. This also disables the services which depend on the service to be
      disabled.

  Raises:
    exceptions.EnableServiceException: when disabling API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  )disableDependentServicescheckIfServiceHasUsage)r,   disableServiceRequestNr   )r-   r/   DisableServiceRequest%CheckIfServiceHasUsageValueValuesEnumCHECKSKIP"ServiceusageServicesDisableRequestr<  rs   Disabler3   r4   r5   r   r6   r  r   r	   r   r   r   )r   rP   r   r:   r;   checkr<   r=   r   r   r    DisableApiCall  s0   


rP  c              
   C   r:  )aI  Get a service.

  Args:
    project: The project for which to get the service.
    service: The service to get.

  Raises:
    exceptions.GetServicePermissionDeniedException: when getting service fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The service configuration.
  r+   N)r-   r/   rq   r<  rs   r2   r3   r4   r5   r   r6   #GetServicePermissionDeniedExceptionr>  r   r   r    
GetService  s   
rR  c                 C   s   t  }|j}| j|jjjkS r%  )r-   r/   stateGoogleApiServiceusageV1ServiceStateValueValuesEnumENABLED)rP   r:   r;   r   r   r    IsServiceEnabled  s   rW  c                   @   s(   e Zd Zdd ZeedddZdS )r   c                 C   s
   || _ d S r%  )service_usage)selfrX  r   r   r    __init__  s   
z_Lister.__init__Nc                 C   s   | j j||dS )N)global_params)rX  r   )rY  r<   r[  r   r   r    r      s   z_Lister.Listr%  )r   r   r   rZ  r   RetryOnHttpStatus_TOO_MANY_REQUESTSr   r   r   r   r    r     s    r   enabledc              
   C   s  t |  }|r
t| }|rt| }i }g }z|rs|t }	t|	}
|
jD ] }|jD ]}|dkr/ n|| d|  d||< |d8 }q'q"tdt	|dD ]&}t
||||d  }|jD ]}d|jddd }|jj||< q[qKn!t||dD ]}|j||j< qyt|||dD ]
}|jj||jj< qg }td	d
dg}|D ]}||||| d q|W S  tjtjfy } zt|tj W Y d}~dS d}~ww )a  Make API call to list services.

  Args:
    project: The project for which to list services.
    enabled: List only enabled services.
    page_size: The page size to list.
    limit: The max number of services to display.
    folder: The folder for which to list services.
    organization: The organization for which to list services.

  Raises:
    exceptions.ListServicesException: when listing services
    fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The list of services
  r   r    r      r   N)r   r   ServiceListr,   title)r,   rb  )r  r  r  _EFFECTIVE_POLICYrk   r   rs   r   rangelenrz   r  r,   r  rP   displayName_ListPublicServices_ListSharedServicescollections
namedtupler3   r4   r5   r   r6   ListServicesException)r   r^  r   r   r   r   r&   rs   rv   r9   effectivepolicyrulesvaluer   service_stater#   public_serviceshared_serviceresultservice_inforP   r=   r   r   r    ListServicesV2Beta%  s^   





rt  c              
   C   s  t |  }|r
t| }|rt| }i }g }z|r|t }	t|	}
|
jD ]}|jD ]}|| d|j  d||j< q'q"t	dt
|dD ]<}t||||d  }|jD ]+}|dkr[ n$d|jddd }|jjrz|jjjrz|jjjd ||< |d8 }qSqCnt|t|D ]}|jjd ||j< qg }tdd	d
g}|D ]}||||| d q|W S  tjtjfy } zt|tj W Y d}~dS d}~ww )a7  Make API call to list services.

  Args:
    project: The project for which to list MCP services.
    enabled: List only enabled  MCP services.
    page_size: The page size to list.
    limit: The max number of services to display.
    folder: The folder for which to list MCP services.
    organization: The organization for which to list MCP services.

  Raises:
    exceptions.ListMcpServicesException: when listing MCP services
    fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The list of MCP services
  r   r_  r   r`  r   Nr   ra  r,   mcp_endpoint)r,   ru  )r  r  r  _EFFECTIVE_MCP_POLICYro   r  r  r   rP   rd  re  rz   rs   r  r,   r  	mcpServerurlsrg  _MCP_LIST_FILTERri  rj  r3   r4   r5   r   r6   ListMcpServicesException)r   r^  r   r   r   r   r&   service_to_endpointrv   r9   effectivemcppolicyrm  r!  rn  r   ro  r#   rp  rr  rs  rP   r=   r   r   r    ListMcpServicesV2Betat  sh   





r}  c           	   
   C   s   t  }|j}|rd}nd}|j|t|  d}ztjt|j||d|ddW S  tj	tj
fyC } zt|tj W Y d}~dS d}~ww )a  Make API call to list services.

  Args:
    project: The project for which to list services.
    enabled: List only enabled services.
    page_size: The page size to list.
    limit: The max number of services to display.

  Raises:
    exceptions.ListServicesException: when listing services
    fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The list of services
  zstate:ENABLEDN)filterrv   r~   rs   r   )r-   r/   ServiceusageServicesListRequestr  r   r   r   rs   r3   r4   r5   r   r6   rk  )	r   r^  r   r   r:   r;   service_filterr<   r=   r   r   r    ListServices  s.   r  c              
   C   sd   t  }|j}|j| d}z|j|W S  tjtjfy1 } zt	|tj
 W Y d}~dS d}~ww )a>  Make API call to get an operation using serviceusageV1 api.

  Args:
    name: The name of operation.

  Raises:
    exceptions.OperationErrorException: when the getting operation API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The result of the operation
  r+   Nr-   r/    ServiceusageOperationsGetRequest
operationsr2   r3   r4   r5   r   r6   OperationErrorExceptionrH   r   r   r    GetOperation  s   r  c              
   C   sf   t d}|j}|j| d}z|j|W S  tjtjfy2 } zt	|tj
 W Y d}~dS d}~ww )a  Make API call to get an operation using serviceusageV2alpha api.

  Args:
    name: The name of the operation resource. Format
      'operations/<operation_id>'.

  Raises:
    exceptions.OperationErrorException: when the getting operation API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The message.Operation object with response and error.
  r   r+   Nr  rH   r   r   r    GetOperationV2Alpha  s   r  c              
   C   r(   )a  Make API call to get an operation using serviceusageV2beta api.

  Args:
    name: The name of the operation resource. Format
      'operations/<operation_id>'.

  Raises:
    exceptions.OperationErrorException: when the getting operation API fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The message.Operation object with response and error.
  r)   r+   N)r-   r.   r/   r  r  r2   r3   r4   r5   r   r6   r  rH   r   r   r    r   "  s   
r   enabled_servicesc                 C   s   t td}|j}tt|D ]<}|jd|  d| d}z|j|}W q tj	y.   Y q tj
tjfyJ } zt|tj W Y d}~qd}~ww dS )aM  Generate a service identity for an enabled service.

  Args:
    container: The container to generate a service identity for.
    enabled_services: The services to generate a service identity for.

  Raises:
    exceptions.GenerateServiceIdentityPermissionDeniedException: when
    generating
    service identity fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the
    service.

  Returns:
    A dict with the email and uniqueId of the generated service identity. If
    service does not have a default identity, the response will be an empty
    dictionary.
  r)   r   r   r}   N)r-   _V1BETA1_VERSIONr/   sortedr  2ServiceusageServicesGenerateServiceIdentityRequestrs   GenerateServiceIdentityr3   r   r4   r5   r   r6   0GenerateServiceIdentityPermissionDeniedException)	containerr  r:   r;   rP   r<   _r=   r   r   r    (GenerateServiceIdentityForEnabledService<  s(   
r  c           	   
      s   t td}|j}|tjkrt| |f }n|tjkr t| |f }n|tjkr,t	| |f }nt
d|j|d}z|j|}t|j  fdddD W S  tjtjfyj } zt|tj W Y d}~dS d}~ww )af  Generate a service identity.

  Args:
    container: The container to generate a service identity for.
    service: The service to generate a service identity for.
    container_type: The type of container, default to be project.

  Raises:
    exceptions.GenerateServiceIdentityPermissionDeniedException: when generating
    service identity fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    A dict with the email and uniqueId of the generated service identity. If
    service does not have a default identity, the response will be an empty
    dictionary.
  r)   z!Invalid container type specified.r}   c                    s   i | ]}| v r| | qS r   r   r   kr   r   r    
<dictcomp>  s    z+GenerateServiceIdentity.<locals>.<dictcomp>)emailuniqueIdN)r-   r  r/   r   r   r<  r   _FOLDER_SERVICE_RESOURCEr   _ORG_SERVICE_RESOURCE
ValueErrorr  rs   r  r   r   r   r3   r4   r5   r   r6   r  )	r  rP   container_typer:   r;   rv   r<   r   r=   r   r  r    r  h  s0   



r  c                 C   sD   t |  ttd}|j}|jt| |f d}tj|j||d|ddS )a  List service quota metrics for a consumer.

  Args:
    consumer: The consumer to list metrics for, e.g. "projects/123".
    service: The service to list metrics for.
    page_size: The page size to list.
    limit: The max number of metrics to return.

  Raises:
    exceptions.PermissionDeniedException: when listing metrics fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The list of quota metrics
  r)   r}   r~   metricsr   )	_ValidateConsumerr-   r  r/   3ServiceusageServicesConsumerQuotaMetricsListRequest_CONSUMER_SERVICE_RESOURCEr   r   services_consumerQuotaMetrics)consumerrP   r   r   r:   r;   r<   r   r   r    ListQuotaMetrics  s   

r  c              
   C   s   t |  ttd}|j}t||}	|jt| |f |j|j|j	||||	dgd|dd}
z|j
|
W S  tjtjfyR } zt|tj W Y d}~dS d}~ww )a  Update a quota override.

  Args:
    consumer: The consumer to update a quota override for, e.g. "projects/123".
    service: The service to update a quota override for.
    metric: The quota metric name.
    unit: The unit of quota metric.
    dimensions: The dimensions of the override in dictionary format. It can be
      None.
    value: The override integer value.
    force: Force override update even if the change results in a substantial
      decrease in available quota.

  Raises:
    exceptions.UpdateQuotaOverridePermissionDeniedException: when updating an
    override fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The quota override operation.
  r)   )metricunitoverrideValue
dimensions)	overrides)inlineSourcer   )rv   importConsumerOverridesRequestN)r  r-   r  r/   _GetDimensionsFServiceusageServicesConsumerQuotaMetricsImportConsumerOverridesRequestr  ImportConsumerOverridesRequestOverrideInlineSourceQuotaOverrider  ImportConsumerOverridesr3   r4   r5   r   r6   ,UpdateQuotaOverridePermissionDeniedException)r  rP   r  r  r  rn  r   r:   r;   dimensions_messager<   r=   r   r   r    UpdateQuotaOverrideCall  s:   


r  c              
   C   s   t |  ttd}|j}t| |||}t||f }	|j|	|d}
z|j|
W S  t	j
t	jfyE } zt|tj W Y d}~dS d}~ww )au  Delete a quota override.

  Args:
    consumer: The consumer to delete a quota override for, e.g. "projects/123".
    service: The service to delete a quota aoverride for.
    metric: The quota metric name.
    unit: The unit of quota metric.
    override_id: The override ID.
    force: Force override deletion even if the change results in a substantial
      decrease in available quota.

  Raises:
    exceptions.DeleteQuotaOverridePermissionDeniedException: when deleting an
    override fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    The quota override operation.
  r)   )r,   r   N)r  r-   r  r/   _GetMetricResourceName_LIMIT_OVERRIDE_RESOURCELServiceusageServicesConsumerQuotaMetricsLimitsConsumerOverridesDeleteRequest6services_consumerQuotaMetrics_limits_consumerOverridesDeleter3   r4   r5   r   r6   ,DeleteQuotaOverridePermissionDeniedException)r  rP   r  r  override_idr   r:   r;   rv   r,   r<   r=   r   r   r    DeleteQuotaOverrideCall  s*   
r  c                    s6    d u rd S | j j fddt  D dS )Nc                    s   g | ]}j | | d qS ))keyrn  )AdditionalPropertyr  r  dtr   r    r     s    z"_GetDimensions.<locals>.<listcomp>)additionalProperties)r  DimensionsValuer  r   )r;   r  r   r  r    r    s   
r  c                 C   sT   t | |}|D ]}|j|kr |jD ]}|j|kr|j    S qqtd||f )a  Get the metric resource name from metric name and unit.

  Args:
    consumer: The consumer to manage an override for, e.g. "projects/123".
    service: The service to manage an override for.
    metric: The quota metric name.
    unit: The unit of quota metric.

  Raises:
    exceptions.Error: when the limit with given metric and unit is not found.

  Returns:
    The quota override operation.
  z-limit not found with name "%s" and unit "%s".)r  r  consumerQuotaLimitsr  r,   r   r   )r  rP   r  r  r  mqr   r   r    r  $  s   



r  c                 C   s(   t D ]
}| |r d S qtd|  )Nzinvalid consumer format "%s".)_VALID_CONSUMER_PREFIXr   r   r   )r  prefixr   r   r    r  =  s
   
r  i  r_  c              
   C   sx   t td}|j}|j|d}ztjt|j||d| ddW S  tj	tj
fy; } zt|tj W Y d}~dS d}~ww )a  Make API call to list public services.

  Args:
    page_size: The page size to list. default=1000
    list_filter: The filter to list public services.
    limit: The max number of services to display.

  Raises:
    exceptions.ListPublicServicesException: when listing public services fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.ListPublicServicesResponse: The public services.
  r)   )r~  r~   rs   r   N)r-   r.   r/   r  r   r   r   rs   r3   r4   r5   r   r6   ListPublicServicesException)r   list_filterr   r:   r;   r<   r=   r   r   r    rg  D  s&   
rg  c              
   C   sz   t td}|j}|j| |d}ztjt|j||d|ddW S  tj	tj
fy< } zt|tj W Y d}~dS d}~ww )a  Make API call to list shared services.

  Args:
    parent: The parent for which to list shared services.
    page_size: The page size to list. default=1000
    list_filter: The filter to list shared services.
    limit: The max number of services to display.

  Raises:
    exceptions.ListSharedServicesException: when listing shared services fails.
    apitools_exceptions.HttpError: Another miscellaneous error with the service.

  Returns:
    Message.ListSharedServicesResponse: The shared services.
  r)   )rv   r~  r~   sharedServicesr   N)r-   r.   r/   %ServiceusageSharedServicesListRequestr   r   r   r  r3   r4   r5   r   r6   ListSharedServicesException)rv   r   r  r   r:   r;   r<   r=   r   r   r    rh  h  s*   
rh  c                 C   s:   ddl m} tjjj }|jtj	|d}t
jd| |dS )z(Get a client instance for service usage.r   )
transports)response_encodingenable_resource_quotaserviceusage)http_client)googlecloudsdk.core.credentialsr  r
   VALUESbillingquota_projectIsExplicitlySetGetApitoolsTransportr   ENCODINGr   r-   )r*   r  r  r  r   r   r    r-     s   	r-   )r_   )FF)r   )FFF)r   NNFF)NN)r   FNNFFF)r   NN)F)r   )kr   ri  r   enumr,  systypingr   apitools.base.pyr   r   r3   r   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.corer	   r
   r   r  r  r  r<  r  r  r  r
  _REVERSE_CLOSUREr  r  r  ry  rc  rv  _GOOGLE_CATEGORY_RESOURCEr  	frozensetr  _V1_VERSIONr  _V1ALPHA_VERSION_V2ALPHA_VERSIONr.   r]  r!   Enumr   r$   r'   r>   rC   rI   rO   r   rW   r]   rk   ro   ru   rz   maxsizer   r   r   r   intr   r   r   r   r   boolr   r   r   r  r"  r4  r9  r?  rE  rP  rR  rW  r   rt  r}  r  r  r  r   r  r  r   r  r  r  r  r  r  r  rg  rh  r-   r   r   r   r    <module>   s  	!&#)* #+

/&
2
7
*0)(
Y
:
A
y
D	
 
D
*
T
V'
-

/%
;
,%
)