
                             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Zd	Zh d
ZddZ	 ddZ	 ddZd Zd Zd Zd Zd Zy)z1Service Consumer Management API helper functions.    )absolute_import)division)unicode_literals)
exceptions)
list_pager)apiszservices/%s/%sz%s/producerOverrides/%s>   folders/	projects/organizations/Nc                     t        |       t               }|j                  }|j                  t        | |fz        }t        j                  |j                  ||d|d      S )a  List service quota metrics for a consumer.

  Args:
    service: The service to list metrics for.
    consumer: The consumer to list metrics for, e.g. "projects/123".
    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
  )parentpageSizemetrics)limitbatch_size_attribute
batch_sizefield)_ValidateConsumer_GetClientInstanceMESSAGES_MODULE@ServiceconsumermanagementServicesConsumerQuotaMetricsListRequest_SERVICE_CONSUMER_RESOURCEr   YieldFromListservices_consumerQuotaMetrics)serviceconsumer	page_sizer   clientmessagesrequests          *lib/googlecloudsdk/api_lib/services/scm.pyListQuotaMetricsr"      sm      H&##(UU'7H*== V ?'		!	!**%
     c                    t        |       t               }|j                  }t        ||      }	|j	                  t
        | |fz  |j                  |j                  |j                  ||||	      g      |            }
	 |j                  j                  |
      S # t        j                  t        j                  f$ r.}t        j                  |t        j                          Y d}~yd}~ww xY w)a  Update a quota override.

  Args:
    service: The service to update a quota override for.
    consumer: The consumer to update a quota override for, e.g. "projects/123".
    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.
  )metricunitoverrideValue
dimensions)	overrides)inlineSourceforce)r   %v1Beta1ImportProducerOverridesRequestN)r   r   r   _GetDimensionsSServiceconsumermanagementServicesConsumerQuotaMetricsImportProducerOverridesRequestr   %V1Beta1ImportProducerOverridesRequestV1Beta1OverrideInlineSourceV1Beta1QuotaOverrider   ImportProducerOverridesapitools_exceptionsHttpForbiddenErrorHttpNotFoundErrorr   ReraiseError,UpdateQuotaOverridePermissionDeniedException)r   r   r%   r&   r(   valuer+   r   r   dimensions_messager    es               r!   UpdateQuotaOverrideCallr;   >   s   8 H&##(%h
;hh'7H*==,4,,;;//#$)!3	 0 5 <   - 	 i 'D//GGPP

0
0

/
/
1 D	:BBD DDs    B #C,>$C''C,c                 |   t        |       t               }|j                  }t        | |||      }t        ||fz  }	|j                  |	|      }
	 |j                  j                  |
      S # t        j                  t        j                  f$ r.}t        j                  |t        j                         Y d}~yd}~ww xY w)au  Delete a quota override.

  Args:
    service: The service to delete a quota aoverride for.
    consumer: The consumer to delete a quota override for, e.g. "projects/123".
    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.
  )namer+   N)r   r   r   _GetMetricResourceName_LIMIT_OVERRIDE_RESOURCEYServiceconsumermanagementServicesConsumerQuotaMetricsLimitsProducerOverridesDeleteRequest6services_consumerQuotaMetrics_limits_producerOverridesDeleter3   r4   r5   r   r6   ,DeleteQuotaOverridePermissionDeniedException)r   r   r%   r&   override_idr+   r   r   r   r=   r    r:   s               r!   DeleteQuotaOverrideCallrE   u   s    2 H&##(!'8VTB&	!V[$9	9$nn o 'DHHOO 

0
0

/
/
1 D	:BBD DDs   A* *#B;$B66B;c           
          |y | j                   j                  } |t        |j                               D cg c]  }|j	                  |||          c}      S c c}w )N)keyr8   )additionalProperties)r1   DimensionsValuesortedkeysAdditionalProperty)r   r(   dtks       r!   r-   r-      sk    $$44"	 *//+,,a 

AZ]

;,

 
s   Ac                     t        | |      }|D ]B  }|j                  |k(  s|j                  D ]!  }|j                  |k(  s|j                  c c S  D t        j                  d|d|d      )a  Get the metric resource name from metric name and unit.

  Args:
    service: The service to manage an override for.
    consumer: The consumer to manage an override for, e.g. "projects/123".
    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 "z" and unit "z".)r"   r%   consumerQuotaLimitsr&   r=   r   Error)r   r   r%   r&   r   mqs          r!   r>   r>      si     Wh/'axx6$$!66T>- % 
 	 $( 	) )r#   c                 2   t               }|j                  }|j                  |       }	 |j                  j	                  |      S # t
        j                  t
        j                  f$ r.}t        j                  |t        j                         Y d}~yd}~ww xY w)a)  Make API call to get an operation.

  Args:
    name: The name of the 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   -ServiceconsumermanagementOperationsGetRequest
operationsGetr3   r4   r5   r   r6   OperationErrorException)r=   r   r   r    r:   s        r!   GetOperationrY      s     &##(BBBM'C  ))

0
0

/
/
1 CAzAABBCs   A #B($BBc                 n    t         D ]  }| j                  |      s y  t        j                  d| z        )Nzinvalid consumer format "%s".)_VALID_CONSUMER_PREFIX
startswithr   rQ   )r   prefixs     r!   r   r      s6    &f6" ' 	88CDDr#   c                  .    t        j                  dd      S )Nserviceconsumermanagementv1beta1)r   GetClientInstance r#   r!   r   r      s    			 ;Y	GGr#   )NN)F)__doc__
__future__r   r   r   apitools.base.pyr   r3   r   googlecloudsdk.api_lib.servicesgooglecloudsdk.api_lib.utilr   r   r?   r[   r"   r;   rE   r-   r>   rY   r   r   rb   r#   r!   <module>rh      sk    8 &  ' > ' 6 ,- 4 D J #(4Dx #()DX	
)2C.EHr#   