
    9                     .   d 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 ej                  j                  d	iZej                  j                  fd
Zej                  j                  fdZd Zd Zd Zd Zd Zd Z	 	 ddZ	 	 ddZd Zy)z API lib for Gemini Cloud Assist.    N)apis)waiter)base)cloud_assist)iam_util)	resourcesv1alphac                 T    t        j                  dt        j                  |             S )zReturns the messages module for the given release track.

  Args:
    release_track: The release track to use.

  Returns:
    The messages module for the given release track.
  geminicloudassist)r   GetMessagesModuleVERSION_MAPgetrelease_tracks    6lib/googlecloudsdk/api_lib/gemini_cloud_assist/util.pyr   r      %     
		;??=9
     c                 T    t        j                  dt        j                  |             S )zReturns the client instance for the given release track.

  Args:
    release_track: The release track to use.

  Returns:
    The client instance for the given release track.
  r   )r   GetClientInstancer   r   r   s    r   r   r   ,   r   r   c                     t               }t               }|j                  j                  |j	                  |             S )zReturns the investigation for the given investigation resource.

  Args:
    investigations_resource_name: The name of the investigation resource.

  Returns:
    The investigation for the given investigation resource.
  name)r   r   !projects_locations_investigationsGet:GeminicloudassistProjectsLocationsInvestigationsGetRequestinvestigations_resource_nameclientmessagess      r   GetInvestigationr    :   sD     & (		1	1	5	5II+ J 
 r   c                 2   t               }t               }| j                         j                  d      d   }|j	                  i       }	|dk7  r| j                         |	_        nd}|	j                  j                  j                  |j                  j                  j                  d|j                  d| j                         j                         j                         j                  d      d   |j                  j                  j                  |j                  j                  j                                      |r||	_        |s|s|s|r|j                  d	d
|j                  j                  j$                  |j                  j                  j                         }
|r||
_        |rK|rI|
j(                  j                  |j+                  |j-                         |j-                                      n|r:|
j(                  j                  |j+                  |j-                                      n|r:|
j(                  j                  |j+                  |j-                                      nt.        j.                  j1                  t.        j2                  j4                        t/        j6                  d      z
  }|
j(                  j                  |j+                  |j-                                      |r||
_        |	j                  j                  j                  |j                  j                  j                  |
j:                  |
             |j<                  j?                  |jA                  | j                         j                         ||	            S )a  Creates the investigation for the given investigation resource.

  Args:
    investigation_resource: The investigation resource to create.
    title: The title of the investigation.
    issue: The issue of the investigation.
    start_time: The start time of the investigation.
    end_time: The end time of the investigation.
    relevant_resources: The resources of the investigation.

  Returns:
    The created investigation.
  /)observations-Nuser.projectidtextobservationTypeobserverTypekeyvalueuser.input.logUser Provided Issuer(   titler*   r+   	startTimeendTime)r4   )r5      )minutes)parentinvestigationIdinvestigation)!r   r   RelativeNamesplitInvestigationr   r$   additionalPropertiesappendObservationsValueAdditionalPropertyObservationParentObservationTypeValueValuesEnum!OBSERVATION_TYPE_STRUCTURED_INPUTObserverTypeValueValuesEnumOBSERVER_TYPE_USERr2   OBSERVATION_TYPE_CLOUD_LOGr)   timeIntervalsInterval	isoformatdatetimenowtimezoneutc	timedeltarelevantResourcesr(   r   Create=GeminicloudassistProjectsLocationsInvestigationsCreateRequest)investigation_resourcer2   issue
start_timeend_timerelevant_resourcesr   r   investigation_idr:   observationhalf_an_hour_agos               r   CreateInvestigationr\   L   s'   * & (+88:@@EbI((b(9-/<<>M1188..AA$$)002vx|~uSz" '22QQss#//KK^^ %  B  M
jH(:&&# ,,KKff))EEXX	 ' K kh&&


",,.8J8J8L  
 
&&


j&:&:&<

= 
&&


H$6$6$8

9 "**..







R
() &&


&6&@&@&B

C &8k#33::00CC 	D 	
 
	1	1	8	8LL'..0==?*% M 
 r   c                    d\  }}|rJt        |j                        dk\  r2|j                  d   j                  }|j                  d   j                  }| r/| t        j                  j
                  k7  r| j                         nd}|r/|t        j                  j                  k7  r|j                         nd}t               j                  ||      S )a(  Calculates the time interval for the given start and end times.

  Args:
    start_time: The start time of the investigation.
    end_time: The end time of the investigation.
    observation: The observation to get the existing time interval from.

  Returns:
    The calculated time interval.
  )NN   r   Nr3   )
lenrI   r4   r5   rL   minrK   maxr   rJ   )rV   rW   rZ   start_time_strend_time_strs        r   CalculateTimeIntervalrd      s     ",.,S223q8 ..q1;;N,,Q/77L **... 		   x((,,, 		  
		%	% 
& 
 r   c                 z   t        | j                               }t               }t               }|j	                  di       }	g }
| j                         |	_        |	j                  j                  j                  |j                  j                  j                  d|j                  d| j                         j                         j                         j                  d      d   |j                  j                  j                  |j                  j                   j"                                     |
j                  d       ||
j                  d
       ||	_        ||s|s||j                  dd|j                  j                  j&                  |j                  j                   j"                        }|
j                  d       |
j                  d       |
j                  d       |
j                  d       ||
j                  d       ||_        |s|rq|
j                  d       d	}|j                  j                  D ]  }|j*                  dk(  s|j,                  } n |j.                  j                  t1        |||             ||
j                  d       ||_        |	j                  j                  j                  |j                  j                  j                  |j4                  |             |j6                  j9                  |j;                  | j                         dj=                  |
      |	            S )aI  Updates the investigation for the given investigation resource.

  Args:
    investigation_resource: The investigation resource to create.
    title: The title of the investigation. Pass an empty string to clear.
    issue: The issue of the investigation.
    start_time: The start time of the investigation. Pass datetime.datetime.min
      to clear.
    end_time: The end time of the investigation. Pass datetime.datetime.max to
      clear.
    relevant_resources: The resources of the investigation. Pass an empty array
      to clear.

  Returns:
    The updated investigation.
     )dataVersionr$   r&   r"   r#   r'   r,   zobservations.`user.project`Nr2   r/   r0   r1   z observations.`user.input.log`.idz#observations.`user.input.log`.titlez-observations.`user.input.log`.observationTypez*observations.`user.input.log`.observerTypez"observations.`user.input.log`.textz+observations.`user.input.log`.timeIntervalsz/observations.`user.input.log`.relevantResources,)r   
updateMaskr:   )r    r;   r   r   r=   r   r$   r>   r?   r@   rA   rB   rC   r<   rD   rE   rF   rG   r2   rH   r)   r-   r.   rI   rd   rQ   r(   r   Patch<GeminicloudassistProjectsLocationsInvestigationsPatchRequestjoin)rT   r2   rU   rV   rW   rX   old_investigationr   r   r:   maskrZ   old_observationobss                 r   UpdateInvestigationrq      s   4 ''='J'J'LM& (((QR(H-	$-::<-1188..AA$$)002vx|~uSz" '22QQss#//KK^^ %  B  +++,
KKM 				'&&# ,,KKff))EEXX	 ' K 	KK23KK56KK?@KK<=
kk67kX
kk?@ o"//DD#77&&II/
 E
 &&
 %
kkCD&8k#33::00CC 	D 	
 
	1	1	7	7KK%224XXd^% L 
 r   c                    t               }t               }|j                  j                  |j	                  |             }t        j                  |j                        }t        j                  j                  |j                  d      }t        j                  ||d       t        j                  t        | j!                  d      d               S )zRuns the investigation revision for the given revision name.

  Args:
    revision_name: The name of the investigation revision.

  Returns:
    The response of running the investigation revision.
  r   z/geminicloudassist.projects.locations.operations)
collectionzRunning investigation)messagez/revisions/r   )r   r   +projects_locations_investigations_revisionsRunCGeminicloudassistProjectsLocationsInvestigationsRevisionsRunRequestr   CloudOperationPollerNoResourcesprojects_locations_operationsr   REGISTRYParser   WaitForr   ReformatInvestigationr    r<   )revision_namer   r   	operationpolleroperation_refs         r    RunInvestigationRevisionBlockingr   E  s     & (@@DDRR S )
 11**& $$**nnB + - 	..0GH		+	+}**=9!<=
 r   c                     t               }t               }|j                  j                  |j	                  |             S )zReturns the IAM policy for the given investigation resource.

  Args:
    investigations_resource_name: The name of the investigation resource.

  Returns:
    The IAM policy for the given investigation resource.
  )resource)r   r   r   GetIamPolicyCGeminicloudassistProjectsLocationsInvestigationsGetIamPolicyRequestr   s      r   GetInvestigationIamPolicyr   b  sD     & (		1	1	>	>RR/ S 
 r   c                     t               }t               }t        |       }t        j                  |j
                  |||       |j                  j                  |j                  | |j                  |                  S )a:  Adds an IAM policy binding to the given investigation resource.

  Args:
    investigations_resource_name: The name of the investigation resource.
    member: The member to add to the binding.
    role: The role to add to the binding.

  Returns:
    The updated IAM policy for the given investigation resource.
  policyr   setIamPolicyRequest)
r   r   r   r   AddBindingToIamPolicyBindingr   SetIamPolicyCGeminicloudassistProjectsLocationsInvestigationsSetIamPolicyRequestSetIamPolicyRequestr   memberroler   r   r   s         r    AddInvestigationIamPolicyBindingr   t  sz     & ($%AB&
  !1!1664H		1	1	>	>RR/&::&:I S 
 r   c                     t               }t               }t        |       }t        j                  |||       |j
                  j                  |j                  | |j                  |                  S )aI  Removes an IAM policy binding from the given investigation resource.

  Args:
    investigations_resource_name: The name of the investigation resource.
    member: The member to remove from the binding.
    role: The role to remove from the binding.

  Returns:
    The updated IAM policy for the given investigation resource.
  r   r   )	r   r   r   r   RemoveBindingFromIamPolicyr   r   r   r   r   s         r   #RemoveInvestigationIamPolicyBindingr     sr     & ($%AB&
%%ffd;		1	1	>	>RR/&::&:I S 
 r   c           	         t               }t               }t        j                  ||j                        \  }}|j
                  j                  |j                  | |j                  ||                  }t        j                  | d       |S )a
  Sets the IAM policy for the given investigation resource.

  Args:
    investigations_resource_name: The name of the investigation resource.
    policy_file: The path to the policy file.

  Returns:
    The updated IAM policy for the given investigation resource.
  )r   ri   r   r:   )
r   r   r   ParseYamlOrJsonPolicyFilePolicyr   r   r   r   LogSetIamPolicy)r   policy_filer   r   r   update_maskresults          r   SetInvestigationIamPolicyr     s     & ( ::8??&+ 33@@RR/&:: ;  S & 7I	-r   )allUsersz+roles/geminicloudassist.investigationViewer)__doc__rL   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.callioper   !googlecloudsdk.command_lib.geminir   googlecloudsdk.command_lib.iamr   googlecloudsdk.corer   ReleaseTrackALPHAr   r   r   r    r\   rd   rq   r   r   r   r   r    r   r   <module>r      s     '  , . ( : 3 )   &&	2 %)$5$5$;$;  %)$5$5$;$; $`F!Hod:( 	6: 	66r   