
                         f    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
  G d d	e      Zy
)z+API Utilities for gcloud diagnose commands.    )absolute_import)division)unicode_literals)metadata_utils)apis)iam_utilc                   D    e Zd ZdZddZd Zd Zd Zd ZddZ	d	 Z
d
 Zy)DiagnoseClientz4Client for calling apis needed by diagnose commands.Nc                     |xs t        j                  dd      | _        |xs t        j                  dd      | _        |xs t        j                  dd      | _        y)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__zDiagnoseClient.__init__   sX     	A$00DA 	!HT%;%;E4%HDA$00DA 	    c                     | j                   j                  }| j                   j                  j                  |j	                  t        j                  |      |j                  |                  }|j                  S )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!   zDiagnoseClient.SignBlob%   sv     //H88AA::44_E$44) 5 + 	; 	,-H r   c                     | j                   j                  }| j                   j                  j                  |j	                  t        j                  |                  }|j                  S )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   ListServiceAccountsz"DiagnoseClient.ListServiceAccounts;   sb     //H88==6666w? 	7 	ABH r   c                     | j                   j                  }| j                   j                  j                  |j	                  t        j                  |      |j                  |                  }|j                  S )a  Creates 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   r-   CreateServiceAccountRequestemail)r   r/   
account_idr(   r)   s        r   CreateServiceAccountz#DiagnoseClient.CreateServiceAccountL   su     //H88??8866w?(0(L(L$ )M )& 	9 	'(H
 >>r   c                     | j                   j                  }| j                   j                  j                  |j	                  ||            }|j
                  D ]  }|c S  y)aD  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.
    )prefixr/   N)r   r   bucketsr+   StorageBucketsListRequestitems)r   r/   r;   r(   r)   buckets         r   
FindBucketzDiagnoseClient.FindBucket^   s_     ##33H##++00**&'*JLH ..m ! r   c                    | j                   j                  }|j                  j                         }|j                  j                  j	                         }|j                  j                  j                  j                         |_        ||j                  _        |j                  j                  j                  j                  d      |_	        |j                  j                  |       |j                  |      S )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(   rD   lifecycle_rules        r   CreateBucketWithLifecyclez(DiagnoseClient.CreateBucketWithLifecycleq   s     ##33H..0I__33FFHN&&99HHJ #/N &&99EE 	F 	  NN.)??Y?//r   c                     | j                   j                  }| j                   j                  j                  |j	                  ||             y)a  Inserts 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.
    )r?   r/   N)r   r   r<   InsertStorageBucketsInsertRequest)r   
project_idr?   r(   s       r   InsertBucketzDiagnoseClient.InsertBucket   sC     ##33H  '',,FJ,OQr   c           
         | j                   j                  }| j                   j                  j                   |j                  di |j                               }|j                  }||i}| j                   j                  j                  |j                  |j                  t        j                  |||      ||j                               y)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)instancerX   r/   zoneN )r   r   	instancesGetComputeInstancesGetRequestAsDictrX   SetMetadata"ComputeInstancesSetMetadataRequestr   r   ConstructMetadataMessager[   )	r   r/   instance_refkeyvalr(   rZ   rY   new_metadatas	            r   UpdateMetadatazDiagnoseClient.UpdateMetadata   s     ##33H##--11+++Dl.A.A.CDFH )):L""..33]]#<<%"35 "" 	4 	$%r   )NNN)
   )__name__
__module____qualname____doc__r   r!   r0   r9   r@   rQ   rV   rh   r\   r   r   r
   r
      s/    <C,"$&0,Q%r   r
   N)rm   
__future__r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.iamr   objectr
   r\   r   r   <module>rs      s*    2 &  ' 9 , 3Q%V Q%r   