
    *                         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	 	 	 ddZd Zd Zd Zy)zHCommands for interacting with Immersive Stream for XR service instances.    )absolute_import)division)unicode_literals)api_util)
exceptions)log)
propertiesc                 &    dj                  | |      S )Nzprojects/{}/locations/{})format)projectlocations     ;lib/googlecloudsdk/api_lib/immersive_stream/xr/instances.pyProjectLocationr      s    	#	*	*7H	==    c                 \   |<|j                   j                  }|D ci c]  }|j                  |j                   c}ni |r7t	        fd|D              r t
        j                  j                  d       y|}nx|r>t	        fd|D              r t
        j                  j                  d       yd |D        }n8|r6t	        fd|D              r t
        j                  j                  d       y|}t        j                  |       }|j                  j                         }	D ]  }
|
d	   }t        |
d
         }|
j                  dd      }|j                  |||      }|r&t        |
d         |_        t        |
d         |_        |	j                  j#                  |j                  j                  j%                  ||              |	j                  D ]S  }|r6|j                  j&                  dk(  rj)                  |j                  d       ;|j                  |j                  <   U |j                  j                         }t+        j-                               D ]E  \  }}|j                  j#                  |j                  j                  j%                  ||             G |S c c}w )a>  Generates the target location configs.

  Args:
    release_track: ALPHA or GA release track
    add_region_configs: List of region config dicts of the form: [{'region':
      region1, 'capacity': capacity1, 'enable_autoscaling': enable_autoscaling1,
      'autoscaling_buffer': autoscaling_buffer1, 'autoscaling_min_capacity':
      autoscaling_min_capacity1}] that specifies the regions to add to the
      service instance
    update_region_configs: List of region config dicts of the form: [{'region':
      region1, 'capacity': capacity1}] that specifies the regions to update to
      the service instance
    remove_regions: List of regions to remove
    current_instance: instance object - current state of the service instance
      before update

  Returns:
    A LocationConfigsValue, with entries sorted by location
  Nc              3   ,   K   | ]  }|d    v   ywregionN .0region_configlocation_configss     r   	<genexpr>z0GenerateTargetLocationConfigs.<locals>.<genexpr>D   s$      /M 	h#33/   zOnly new regions can be added.c              3   &   K   | ]  }|v 
 y w)Nr   )r   r   r   s     r   r   z0GenerateTargetLocationConfigs.<locals>.<genexpr>M   s     
Gf6))s   z%Only existing regions can be removed.c              3   (   K   | ]
  }|d dd  yw)r   F)r   capacityenable_autoscalingNr   )r   r   s     r   r   z0GenerateTargetLocationConfigs.<locals>.<genexpr>R   s      $F qF$s   c              3   ,   K   | ]  }|d    v  ywr   r   r   s     r   r   z0GenerateTargetLocationConfigs.<locals>.<genexpr>X   s$      2M 	h'772r   z%Only existing regions can be updated.r   r   r   F)r   r   enableAutoscalingautoscaling_bufferautoscaling_min_capacity)keyvaluer   )locationConfigsadditionalPropertiesr$   r%   anyr   statusPrintr   GetMessagesStreamInstanceLocationConfigsValueintgetLocationConfigautoscalingBufferautoscalingMinCapacityappendAdditionalPropertyr   popsorteditems)release_trackadd_region_configsupdate_region_configsremove_regionscurrent_instanceadditonal_propertieslocation_configregion_configs_diffmessageslocation_configs_diffr   r   r   r   target_location_configsr$   r   s                   @r   GenerateTargetLocationConfigsrC      s   6 !+;;PP  43O 	_2223
 
 /  
jj78,

G
GG	jj>?$
 
 2  
jj>? 0!!-0("11FFH*m8$F=,-H&**+?G--, . O
 *-
,
-+o' 03
2
30o, ..5544GGo 	H 	
# +0 /CCo///88A=?..5 />.C.C**+ D %33HHJ %%5%;%;%=>c?007744GG? 	H 	
 ? 
! gs   J)c                     t        j                  |       }t        j                  |       }|j                  |      }|j	                  |j                  |            S )zGet resource details of an Immersive Stream for XR service instance.

  Args:
    release_track: ALPHA or GA release track
    instance_relative_name: string - canonical resource name of the instance

  Returns:
    A service instance resource object.
  )name)r   	GetClientr+   'ProjectsLocationsStreamInstancesServiceGet0StreamProjectsLocationsStreamInstancesGetRequest)r8   instance_relative_nameclientr@   services        r   rH   rH      s\     m,&!!-0(::6B'	??% @ 
 r   Nc	                    t        j                  |       }	t        j                  |       }
|
j                  |      }|
j	                  ||||||      }|r|
j                  |      }||_        |	j                  |	      }|j                  |
j                  t        t        j                  j                  j                  j                         |      ||            S )an  Create a new Immersive Stream for XR service instance.

  Args:
    release_track: ALPHA or GA release track
    instance_name: string - name of the service instance
    content: string - resource path of the content resource that is served by
      the instance
    location: string - location where the resource will be created
    version: string - content build version tag
    target_location_configs: A LocationConfigsValue proto message represents the
      target location configs to achieve
    fallback_url: string - A url to redirect users to when the instance is
      unable to provide the streaming experience
    mode: string - The rendering mode supported by the service instance
    gpu_class: string - The class of GPU used by this instance

  Returns:
    An Operation object which can be used to check on the progress of the
    service instance creation.
  contentVersionTag)contentcontentBuildVersionrE   r&   modegpuClassfallbackUri)parentstreamInstancestreamInstanceId)r   rF   r+   BuildVersionr,   StreamConfigstreamConfigrG   Create3StreamProjectsLocationsStreamInstancesCreateRequestr   r	   VALUEScorer   rH   )r8   instance_namerP   r   versionrB   fallback_urlrR   	gpu_classrK   r@   build_versioninstancestream_configrL   s                  r   r\   r\      s    > m,&!!-0(''''B-$$'- % ( ))l)CM)H::6B'	BB $$,,002H "( C 
 r   c                 D   |r|j                   st        j                  d      t        j                  |       }t        j
                  |       }|j                  |      }|j                  |      }|j                  |j                  |j                         |d            S )a  Updates the location configs for a service instance.

  Args:
    release_track: ALPHA or GA release track
    instance_ref: resource object - service instance to be updated
    target_location_configs: A LocationConfigsValue proto message represents the
      target location configs to achieve

  Returns:
    An Operation object which can be used to check on the progress of the
    service instance update.
  z(Target location configs must be provided)r&   r   rE   rW   
updateMask)r'   r   Errorr   rF   r+   r,   rG   Patch2StreamProjectsLocationsStreamInstancesPatchRequestRelativeName)r8   instance_refrB   rK   r@   re   rL   s          r   UpdateLocationConfigsro      s     "$99


E
FFm,&!!-0($$5L$M(::6B'	AA((*!' B 
 r   c                 "   t        j                  |       }t        j                  |       }|j                  |      }|j	                  |      }|j                  |      }|j                  |j                  |j                         |d            S )ab  Update content build version of an Immersive Stream for XR service instance.

  Args:
    release_track: ALPHA or GA release track
    instance_ref: resource object - service instance to be updated
    version: content build version tag

  Returns:
    An Operation object which can be used to check on the progress of the
    service instance update.
  rN   )rQ   content_build_versionrh   )	r   rF   r+   rY   r,   rG   rk   rl   rm   )r8   rn   ra   rK   r@   rd   re   rL   s           r   UpdateContentBuildVersionrr     s     m,&!!-0(''''B-$$$G(::6B'	AA((*!, B 
 r   c                 ,   t        j                  |       }t        j                  |       }|j                  |      }|j	                  |      }|j                         }||_        |j                  |j                  |j                         |d            S )a  Update fallback url of an Immersive Stream for XR service instance.

  Args:
    release_track: ALPHA or GA release track
    instance_ref: resource object - service instance to be updated
    fallback_url: string - fallback url to redirect users to when the instance
      is not available

  Returns:
    An Operation object which can be used to check on the progress of the
    service instance update.
  rT   rf   rh   )
r   rF   r+   rG   rZ   r,   r[   rk   rl   rm   )r8   rn   rb   rK   r@   rL   rf   re   s           r   UpdateFallbackUrlrt     s     m,&!!-0(::6B'''L'A-$$&('(	AA((*!$ B 
 r   )NNN)__doc__
__future__r   r   r   *googlecloudsdk.api_lib.immersive_stream.xrr   googlecloudsdk.corer   r   r	   r   rC   rH   r\   ro   rr   rt   r   r   r   <module>ry      sR    O &  ' ? * # *>p!f8 	8vD6r   