
                         >   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d	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  ej.                  ej0                  j2                  ej0                  j4                         G d dej6                               Zy)z?Command to authorize a service agent to manage other resources.    )absolute_import)division)unicode_literals)projects_api)lake)storage_api)storage_util)apis)
exceptions)base)resource_args)utilc                   X    e Zd ZdZddiZed        Z ej                  d      d        Z	y)	AuthorizezAuthorize a service agent to manage resources.

  The service agent for the primary project will be granted an IAM role on a
  secondary project, a Cloud Storage bucket, or a BigQuery dataset.
  EXAMPLESa            To authorize the service agent in project `test-project` to manage
          resources in the project `test-project2`, run:

            $ {command} --project=test-project --project-resource=test-project2

          To authorize the service agent in project `test-project` to manage the
          Cloud Storage bucket `dataplex-storage-bucket`, run:

            $ {command} --project=test-project --storage-bucket-resource=dataplex-storage-bucket

          To authorize the service agent in project `test-project` to manage the
          BigQuery dataset `test-dataset` in project `test-project2`, run:

            $ {command} --project=test-project --bigquery-dataset-resource=test-dataset --secondary-project=test-project2
          c                    t        j                  | d       | j                  ddd      }|j                  dd       |j                  dd	       |j                  d
      }|j                  ddd       |j                  ddd       y )Nz(to grant a role to the service agent in.Tz;The resource on which to grant a role to the service agent.)mutexrequiredhelpz--storage-bucket-resourcezNThe identifier of the Cloud Storage bucket that the service agent will manage.)r   z--project-resourcezLThe identifier of the project whose resources the service agent will manage.z(Fields to identify the BigQuery dataset.z--bigquery-dataset-resourcezJThe identifier of the BigQuery dataset that the service agent will manage.)r   r   z--secondary-projectzDThe identifier of the project where the BigQuery dataset is located.)r   AddProjectArg	add_groupadd_argument)parser
data_groupdataset_groups      'lib/surface/dataplex/lakes/authorize.pyArgszAuthorize.Args<   s    : !!J " J
 #a      ((7 ) M %	   	      z-Status code: {status_code}. {status_message}.c                 0   |j                   j                  j                         }dt        t	        j
                  |j                              z   dz   }|j                  d      rEt        j                         j                  t        j                  |j                        d|z   d      S |j                  d      rt        j                  dd      j!                  |j"                  |j$                  	      }t        j&                  dd      j(                  j+                  |
      }t-        j.                  t        j                  dd      j0                  j2                  ||d       t        j&                  dd      j(                  j5                  t        j                  dd      j7                  |j"                  |j$                  |            S |j                  d      r7t9        j                  t	        j:                  |j<                        d|z   d      S y )Nzservice-z(@gcp-sa-dataplex.iam.gserviceaccount.comstorage_bucket_resourcezserviceAccount:zroles/dataplex.serviceAgentbigquery_dataset_resourcebigqueryv2)	datasetId	projectId)request)r$   r%   datasetproject_resource)CONCEPTSprojectParsestrproject_utilGetProjectNumber
projectsIdIsSpecifiedr   StorageClientAddIamPolicyBindingr	   BucketReferencer    r
   GetMessagesModuleBigqueryDatasetsGetRequestr!   secondary_projectGetClientInstancedatasetsGetr    AddServiceAccountToDatasetPolicyDatasetAccessValueListEntryPatchBigqueryDatasetsPatchRequestr   ParseProjectr(   )selfargsproject_refservice_accountget_dataset_requestr'   s         r   RunzAuthorize.Rune   s    --''--/K 3%%k&<&<=$ 23O 12&&(<<

&
&t'C'C
D
o
-/LN N 34 22
d6666.. 7 0  &&
d$HSS1DS%E 
++

 
 T
2
:
:
O
O
?$AC ##J5>>DD

 
 T
2
O
O66.. P     *+--

#
#D$9$9
:
o
-/LN N ,r   N)
__name__
__module____qualname____doc__detailed_helpstaticmethodr   gcloud_exception CatchHTTPErrorRaiseHTTPExceptionrE    r   r   r   r   !   sT      -& & &P 54457N7Nr   r   N)rI   
__future__r   r   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   googlecloudsdk.api_lib.dataplexr   googlecloudsdk.api_lib.storager   r	   googlecloudsdk.api_lib.utilr
   r   rL   googlecloudsdk.callioper   #googlecloudsdk.command_lib.dataplexr   #googlecloudsdk.command_lib.projectsr   r-   ReleaseTracksReleaseTrackALPHAGACommandr   rN   r   r   <module>r\      s~    F '  ' D 0 6 7 , F ( = D D%%++T->->-A-ABcN cN CcNr   