
                  
       j   d Z ddlmZ ddlmZ ddlm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j/                  dj1                   eej4                  D  cg c]  } | j6                   c}                   ZdadedefdZ d Z!d Z"ddZ#ddZ$yc c} w )z5Returns correct API client instance for user command.    )absolute_import)division)unicode_literalsN)errorsclient)storage_url)log)
propertiesz-Invalid provider. Valid provider prefixes: {}z, bucket_namereturnc                 >   |r| t         j                  j                  k7  ryt        j                         }	 |j                  |      }|j                  dk(  S # t        j                  $ r3}t        |dd      }|dv rt        j                  d||       Y d}~y d}~ww xY w)z7Returns true if the given bucket is a GCS zonal bucket.Fzonestatus_codeN)i  i  i  z.Failed to get storage layout for bucket %s: %s)r	   ProviderPrefixGCSgcs_json_client
JsonClientget_storage_layoutlocationTypecloud_api_errorsCloudApiErrorgetattrr
   debug)providerr   
api_clientstorage_layouter   s         1lib/googlecloudsdk/api_lib/storage/api_factory.py_is_gcs_zonal_bucketr    0   s    	K$>$>$B$BB))+*
22;?N&&&00		'	' 

!]D1Ko%	ii
:K 	

s   A B)(BBBc                    | t         j                  j                  k(  r[|r't        j                  d       ddlm} |j                  S t        j                  j                  j                  j                         sWt        j                  j                  j                  j                         t        j                  j                   j"                  k(  r%t        j                  d       t$        j&                  S t        j                  j                  j(                  j                         rBt        j                  j                  j*                  j                         rt,        j.                  S t0        j2                  S | t         j                  j4                  k(  rt6        j8                  S t;        j<                  t>              )a]  Returns a CloudApi subclass corresponding to the requested provider.

  Args:
    provider (storage_url.ProviderPrefix): Cloud provider prefix.
    is_zonal_bucket (bool): Whether the bucket is a zonal bucket.

  Returns:
    An appropriate CloudApi subclass.

  Raises:
    Error: If provider is not a cloud scheme in storage_url.ProviderPrefix.
  z2Using gRPC Bidi Streaming client for zonal bucket.r   r   z%Using gRPC client with JSON Fallback.) r	   r   r   r
   r   6googlecloudsdk.api_lib.storage.gcs_grpc_bidi_streamingr   GcsGrpcBidiStreamingClientr   VALUESstorageuse_grpc_if_availableGetBoolpreferred_apiGetStoragePreferredApiGRPC_WITH_JSON_FALLBACKvaluegcs_grpc_clientGrpcClientWithJsonFallbackgs_xml_access_key_idgs_xml_secret_access_keygcs_xml_client	XmlClientr   r   S3s3_xml_clientS3XmlClientr   Error _INVALID_PROVIDER_PREFIX_MESSAGE)r   is_zonal_bucketgrpc_bidi_streaming_clients      r   _get_api_classr:   E   s/    ++///	iiDEm'BBB   66>>@!!//335))AAGG	H 
ii78777!!66::<%%>>BBD%%%%%%;--000$$$
,,7
88    c                 >    |r| j                    dS | j                    S )zGReturns the thread local storage key for the given provider and bucket._zonal)r,   )r   r8   s     r   _get_thread_local_storage_keyr>   r   s$    nnV$$NNr;   c                    t        | |      }t        j                  j                  j                  j                         r9t        st        j                         at        t        t        | |      d      }|r|S t        | |      } |       }t        j                  j                  j                  j                         rt        t        t        | |      |       |S )aC  Returns thread local API instance based on provider and bucket(if available).

  Uses thread local storage to make sure only one instance of an API exists
  per thread per provider + bucket type.

  Args:
    provider (storage_url.ProviderPrefix): Cloud provider prefix.
    bucket_name (str): Name of the bucket. If available, the API client will be
      chosen based on the bucket type as well. Currently only supported for GCS.

  Returns:
    CloudApi client object for the arguments.

  Raises:
    Error: If provider is not a cloud scheme in storage_url.ProviderPrefix.
  N)r    r   r$   r%   use_threading_localr'   _cloud_api_thread_local_storage	threadinglocalr   r>   r:   setattr)r   r   r8   r   	api_classs        r   get_apirF   y   s    . );?/22::<*(1(9%'%h@J
 X7){*22::<'%h@ 
r;   c                 F    t        | t        | |            }|j                  S )a  Gets the capabilities of a cloud provider.

  Args:
    provider (storage_url.ProviderPrefix): Cloud provider prefix.
    bucket_name (str): Name of the bucket. If available, the API client will be
      chosen based on the bucket type as well. Currently only supported for GCS.

  Returns:
    The CloudApi.capabilities attribute for the requested provider.

  Raises:
    Error: If provider is not a cloud scheme in storage_url.ProviderPrefix.
  )r:   r    capabilities)r   r   rE   s      r   get_capabilitiesrI      s)     $X{;) 
		r;   )N)%__doc__
__future__r   r   r   rB   googlecloudsdk.api_lib.storager   r   'googlecloudsdk.api_lib.storage.gcs_grpcr   r-   'googlecloudsdk.api_lib.storage.gcs_jsonr   &googlecloudsdk.api_lib.storage.gcs_xmlr1   %googlecloudsdk.api_lib.storage.s3_xmlr4   "googlecloudsdk.command_lib.storager	   googlecloudsdk.corer
   r   formatjoinsortedVALID_CLOUD_SCHEMESr,   r7   rA   strboolr    r:   r>   rF   rI   )schemes   0r   <module>rZ      s    < '  '  E M M K I 5 : # * 4::		{/N/NO/NVFLL/NOP	
 ! #' 
 
 
**9Z/d K Ps   5B0