
                         n    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d Zy)zBUtility function for OS Config Troubleshooter to service accounts.    )absolute_import)division)unicode_literals)
exceptions)projects_api)utils)utilc                 Z    dj                  t        j                  |             }d|z   dz   S )N2service-{}@gcp-sa-osconfig.iam.gserviceaccount.coma%  No
No OS Config service account is present and enabled for this instance. To create an OS Config service account for this instance, visit https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#createanewserviceaccount to create a service account of the name zf, grant it the "Cloud OS Config Service Agent" IAM role, then disable and re-enable the OS Config API.)formatr	   GetProjectNumber)
project_idservice_accounts     Plib/googlecloudsdk/command_lib/compute/os_config/troubleshoot/service_account.py_FailEnablementMessager      sA    HOO
J')/2 5D	DG%	%    c                     d}| j                   s|dz  }t        j                  d|      S |dz  }t        j                  d|      S )z8Checks whether a service account exists on the instance.z0> Is a service account present on the instance? zNo
No service account is present on the instance. Visit https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances on how to create a service account for an instance.FYesT)serviceAccountsr   Response)instanceresponse_messages     r   CheckExistencer   )   sR    G		!	!	?
 >>%!122e	.	//r   c                    d}d}d}t        j                  | j                        }	 t        j                  |      }|j                  D ]  }|j                  dk(  s|j                  s nt        t        j                  | j                              }|j                  D ]'  }||v s|dz  }d}t        j                  ||      c c S  dj                  |      }	|d|	z   z  }t        j                  ||      c S  |t!        | j                        z  }t        j                  ||      S # t
        j                  $ r8}|t        j                  |      z  }t        j                  ||      cY d}~S d}~ww xY w)	z=Checks whether there is an enabled OS Config service account.z>> Is the OS Config Service account present for this instance? FNzroles/osconfig.serviceAgentr   Tr   z|Yes
However, the service account name does not contain a matching project number. The service account should be of the name )r	   ParseProjectnamer   GetIamPolicyr   	HttpErrorr   UnknownMessager   bindingsrolemembersstrr   r   r   )
projectr   continue_flag
iam_policyproject_refebindingproject_numbermemberr   s
             r   CheckEnablementr,   8   sj   # -*!!',,/+;**;7J
 $$g||44__T227<<@AooFv%% M>>-1ABB	 &
 OUU > APP 	Q ~~m-=>># %& ,W\\::	'7	881 
		 ;,,Q//>>-)9::;s   D' 'E2:-E-'E2-E2N)__doc__
__future__r   r   r   apitools.base.pyr   +googlecloudsdk.api_lib.cloudresourcemanagerr   9googlecloudsdk.command_lib.compute.os_config.troubleshootr   #googlecloudsdk.command_lib.projectsr	   r   r   r,    r   r   <module>r4      s/    I &  ' ' D K 40#9r   