
                            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  e	j                   e	j"                  j$                  e	j"                  j&                         G d de	j(                               Zy)z/Command to set scopes for an instance resource.    )absolute_import)division)unicode_literals)base_classes)	constants)base)scope)
exceptions)flagsc                   f     e Zd ZdZdddZ fdZed        Zd Zd Z	d	 Z
d
 Zd Zd Zd Z xZS )	SetScopeszCSet scopes and service account for a Compute Engine VM instance.
  z        `{command}` lets you configure service account and scopes for a
        Compute Engine VM instance.

        Note: This command might be deprecated in a future release.
        Use `gcloud compute instances set-service-account` instead.
        z
       To set a service account with the ``cloud-platform'' scope, run:

    $ {command} example-instance --scopes=cloud-platform --zone=us-central1-b --service-account=example-account
       )DESCRIPTIONEXAMPLESc                 F    t        | j                  | 
  |i | d | _        y )N)super	__class____init__	_instance)selfargskwargsr   s      +lib/surface/compute/instances/set_scopes.pyr   zSetScopes.__init__1   s"    	$..$($9&9DN    c                 n    t         j                  j                  |        t        j                  | d       y )NT)r   INSTANCE_ARGAddArgumentAddServiceAccountAndScopeArgs)parsers    r   ArgszSetScopes.Args5   s&    	""6*	''5r   c                     | j                   s^|j                  j                  d |j                  j                  di |j                         f}|j                  |g      }|d   | _         | j                   S )z?Return cached instance if there isn't one fetch referenced one.Get)requestsr    )r   apitools_client	instancesmessagesComputeInstancesGetRequestAsDictMakeRequests)r   instance_refclientrequestinstances        r   _get_instancezSetScopes._get_instance:   sr    >>''115;;; .#**,./g $$wi$8h{dn>>r   c                 h    | j                  ||      }|y|j                  }|r|d   j                  S y)z2Return email of service account instance is using.Nr   )r.   serviceAccountsemail)r   r*   r+   r-   orignal_service_accountss        r   _original_emailzSetScopes._original_emailF   s@    !!,7H'77%a(...r   c                 z    | j                  ||      }|g S |j                  }g }|D ]  }||j                  z  } |S )z Return scopes instance is using.)r.   r0   scopes)r   r*   r+   r-   r2   resultaccountss          r   _original_scopeszSetScopes._original_scopesP   sM    !!,7Hi'77F,f -Mr   c                 p    |j                   ry|j                  r|j                  S | j                  ||      S )z8Return email to set as service account for the instance.N)no_service_accountservice_accountr3   r   r   r*   r+   s       r   _emailzSetScopes._email[   s6    !!!f55r   c                 r    |j                   rg S |j                  |j                  S | j                  ||      S )z&Return scopes to set for the instance.)	no_scopesr5   r8   r<   s       r   _unprocessed_scopeszSetScopes._unprocessed_scopesc   s5    ~~i{{[[  v66r   c                     g }| j                  |||      D ]4  }t        j                  j                  ||g      }|j	                  |       6 |S )aF  Get list of scopes to be assigned to the instance.

    Args:
      args: parsed command  line arguments.
      instance_ref: reference to the instance to which scopes will be assigned.
      client: a compute_holder.client instance

    Returns:
      List of scope urls extracted from args, with scope aliases expanded.
    )r@   r   SCOPESgetextend)r   r   r*   r+   r6   unprocessed_scoper	   s          r   _scopeszSetScopes._scopesk   sX     F!55d6BFL""#47H6IJemmEL Mr   c                    t        j                  | j                               }|j                  }t	        j
                  |       t        j                  j                  ||j                  t        j                  j                  t	        j                  |            }| j                  |||      }| j                  |||      }|r|st        j                   d      |j"                  j%                  |j"                  j'                  ||      |j(                  |j*                  |j-                               }|j/                  |j0                  j2                  d|fg      S )N)default_scopescope_listerz4Can not set scopes when there is no service acoount.)r1   r5   )!instancesSetServiceAccountRequestprojectzoner-   SetServiceAccount)r   ComputeApiHolderReleaseTrackr+   r   "ValidateServiceAccountAndScopeArgsr   ResolveAsResource	resourcescompute_scope	ScopeEnumZONEGetInstanceZoneScopeListerr=   rF   r
   $ScopesWithoutServiceAccountExceptionr&   (ComputeInstancesSetServiceAccountRequest!InstancesSetServiceAccountRequestrK   rL   Namer)   r$   r%   )r   r   compute_holderr+   r*   r1   r5   r,   s           r   RunzSetScopes.Run}   sH   !2243D3D3FGN""F	,,T2%%77n&&#--2255f= 8 ?L
 KKlF3E\\$f5Fe;;
@B B ooFFOO== >  $$""$ G &G !7!7!A!A!4g!?  @ A Ar   )__name__
__module____qualname____doc__detailed_helpr   staticmethodr   r.   r3   r8   r=   r@   rF   r\   __classcell__)r   s   @r   r   r      sV    
- 6 6
	67$Ar   r   N)r`   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer	   rS   ,googlecloudsdk.command_lib.compute.instancesr
   r   ReleaseTracksrO   ALPHABETASilentCommandr   r#   r   r   <module>rm      st    6 &  ' 7 4 ( E C > D%%++T->->-C-CD{A"" {A E{Ar   