
                         f    S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
   " S S	\5      rg
)z+API Utilities for gcloud diagnose commands.    )absolute_import)division)unicode_literals)metadata_utils)apis)iam_utilc                   P    \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rS rSrg)DiagnoseClient   z4Client for calling apis needed by diagnose commands.Nc                     U=(       d    [         R                  " SS5      U l        U=(       d    [         R                  " SS5      U l        U=(       d    [         R                  " SS5      U l        g)z?Initializes DiagnoseClient with the common api clients it uses.computev1iamstorageN)r   GetClientInstance_compute_client_iam_client_storage_client)selfcompute_client
iam_clientstorage_clients       =lib/googlecloudsdk/api_lib/compute/diagnose/diagnose_utils.py__init__DiagnoseClient.__init__   sX     	A$00DA 	!HT%;%;E4%HDA$00DA 	    c           	          U R                   R                  nU R                   R                  R                  UR	                  [
        R                  " U5      UR                  US9S95      nUR                  $ )a<  Signs a string with the private key of the provided service account.

Args:
  service_account: The string email of a service account that has
    permissions to sign a blob.
  bytes_to_sign: The byte-string to sign.

Returns:
  A byte-string signature of the provided blob, signed by the provided
  service account.
)bytesToSign)namesignBlobRequest)	r   MESSAGES_MODULEprojects_serviceAccountsSignBlob)IamProjectsServiceAccountsSignBlobRequestr   EmailToAccountResourceNameSignBlobRequest	signature)r   service_accountbytes_to_signmessagesresponses        r   r#   DiagnoseClient.SignBlob%   sv     //H88AA::44_E$44) 5 + 	; 	,-H r   c                     U R                   R                  nU R                   R                  R                  UR	                  [
        R                  " U5      S95      nUR                  $ )zLists all service accounts within a particular project.

Args:
  project: The project string to search for the service account in.

Returns:
  A list of service account message objects.
)r   )r   r!   r"   List%IamProjectsServiceAccountsListRequestr   ProjectToProjectResourceNameaccounts)r   projectr*   r+   s       r   ListServiceAccounts"DiagnoseClient.ListServiceAccounts;   sb     //H88==6666w? 	7 	ABH r   c           	          U R                   R                  nU R                   R                  R                  UR	                  [
        R                  " U5      UR                  US9S95      nUR                  $ )zCreates a service account within the provided project.

Args:
  project: The project string to create a service account within.
  account_id: The string id to create the service account with.

Returns:
  A string email of the service account.
)	accountId)r   createServiceAccountRequest)	r   r!   r"   Create'IamProjectsServiceAccountsCreateRequestr   r0   CreateServiceAccountRequestemail)r   r2   
account_idr*   r+   s        r   CreateServiceAccount#DiagnoseClient.CreateServiceAccountL   su     //H88??8866w?(0(L(L$ )M )& 	9 	'(H
 >>r   c                     U R                   R                  nU R                   R                  R                  UR	                  X!S95      nUR
                   H  nUs  $    g)a,  Gets the first bucket the project has access to with a matching prefix.

Args:
  project: The id string of the project the bucket is associated with.
  prefix: The string literal prefix of the bucket being searched for.

Returns:
  The first bucket message object found matching the prefix, or none.
)prefixr2   N)r   r!   bucketsr.   StorageBucketsListRequestitems)r   r2   r@   r*   r+   buckets         r   
FindBucketDiagnoseClient.FindBucket^   s]     ##33H##++00**&*JLH ..m ! r   c                    U R                   R                  nUR                  R                  5       nUR                  R                  R	                  5       nUR                  R                  R                  R                  5       Ul        XR                  l        UR                  R                  R                  R                  SS9Ul	        UR                  R                  U5        UR                  US9$ )a'  Creates a bucket object that deletes its contents after a number of days.

Args:
  days_to_live: The number of days to wait before deleting an item within
    this bucket. Count starts when the item is created.

Returns:
  A bucket message object that has not yet been created in Cloud Storage.
Delete)type)	lifecycle)r   r!   BucketLifecycleValueRuleValueListEntryConditionValue	conditionageActionValueactionruleappend)r   days_to_liver*   rJ   lifecycle_rules        r   CreateBucketWithLifecycle(DiagnoseClient.CreateBucketWithLifecycleq   s     ##33H..0I__33FFHN&&99HHJ #/ &&99EE 	F 	  NN.)??Y?//r   c                     U R                   R                  nU R                   R                  R                  UR	                  X!S95        g)zInserts the bucket object as a GCS bucket associated with the project.

Args:
  project_id: The project string to save the bucket to.
  bucket: The bucket message object to insert.

Raises:
  HttpError: with status_code 409 if the bucket already exists.
)rD   r2   N)r   r!   rA   InsertStorageBucketsInsertRequest)r   
project_idrD   r*   s       r   InsertBucketDiagnoseClient.InsertBucket   sA     ##33H  '',,F,OQr   c                    U R                   R                  nU R                   R                  R                  UR                  " S0 UR                  5       D65      nUR                  nX40nU R                   R                  R                  UR                  UR                  [        R                  " UUUS9UUR                  S95        g)a  Writes a key value pair to the metadata server.

Args:
  project: The project string the instance is in.
  instance_ref: The instance the metadata server relates to.
  key: The string key to enter the data in.
  val: The string value to be written at the key.
)metadataexisting_metadata)instancer`   r2   zoneN )r   r!   	instancesGetComputeInstancesGetRequestAsDictr`   SetMetadata"ComputeInstancesSetMetadataRequestr   r   ConstructMetadataMessagerc   )	r   r2   instance_refkeyvalr*   rb   ra   new_metadatas	            r   UpdateMetadataDiagnoseClient.UpdateMetadata   s     ##33H##--11++Dl.A.A.CDFH )):L""..33]]#<<%"35 "" 	4 	$%r   )r   r   r   )NNN)
   )__name__
__module____qualname____firstlineno____doc__r   r#   r3   r=   rE   rW   r]   rp   __static_attributes__rd   r   r   r
   r
      s/    <C,"$&0,Q%r   r
   N)rw   
__future__r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.iamr   objectr
   rd   r   r   <module>r~      s*    2 &  ' 9 , 3Q%V Q%r   