
    /                     X   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                   j"                  dej                   j$                  dej                   j&                  diZd Z G d dej,                        Z G d de      Z G d de      Zy)zoslogin client functions.    )absolute_import)division)unicode_literals)Optional)apis)	apis_util)base)
exceptions)
propertiesv1alphav1betav1c                 .    t        j                  d|       S )Noslogin)r   GetClientInstance)versions    ,lib/googlecloudsdk/api_lib/oslogin/client.py
_GetClientr   "   s    				7	33    c                       e Zd ZdZy)OsloginExceptionzDOsloginException is for non-code-bug errors in oslogin client utils.N__name__
__module____qualname____doc__ r   r   r   r   &   s    Lr   r   c                       e Zd ZdZy)OsloginKeyNotFoundErrorzFOsloginKeyNotFoundError is raised when requested SSH key is not found.Nr   r   r   r   r   r   *   s    Nr   r   c                       e Zd ZdZd Zd Zd Z	 	 ddZddZ	 	 	 dde	e
   fd	Zd
 Zd Z	 ddZd Z	 	 	 ddZ	 dde	e
   fdZy)OsloginClientz%Class for working with oslogin users.c                 H   t         |   | _        t        j                  j                  j
                  j                         | _        	 t        | j                        | _        | j                  j                  | _
        y # t        j                  $ r d | _        d | _
        Y y w xY wN)VERSION_MAPr   r   VALUEScoreprojectGetr   clientMESSAGES_MODULEmessagesr   UnknownVersionError)selfrelease_tracks     r   __init__zOsloginClient.__init__1   sy    }-DL$$))11557DLt||,dkkk11dm(( dkdms   5A= =!B! B!c                 "    | j                         S r#   )__bool__r-   s    r   __nonzero__zOsloginClient.__nonzero__;   s    ==?r   c                 ,    t        | j                        S r#   )boolr)   r2   s    r   r1   zOsloginClient.__bool__>   s    r   Nc                 <   | j                   j                  }| j                  dk(  r |dj                  |      ||      }n6|r|j                  j
                  }nd} |dj                  |      |||      }| j                  j                  j                  |      }|S )a[  Return the OS Login profile for a user.

    The login profile includes some information about the user, a list of
    Posix accounts with things like home directory location, and public SSH
    keys for logging into instances.

    Args:
      user: str, The email address of the OS Login user.
      project: str, The project ID associated with the desired profile.
      system_id: str, If supplied, only return profiles associated with the
        given system ID.
      include_security_keys: bool, If true, include security key information.

    Returns:
      The login profile for the user.
    r   	users/{0})name	projectIdsystemIdN)r8   r9   r:   view)	r+   "OsloginUsersGetLoginProfileRequestr   formatViewValueValuesEnumSECURITY_KEYr)   usersGetLoginProfile)	r-   userr'   	system_idinclude_security_keysprofile_requestmessager;   ress	            r   rA   zOsloginClient.GetLoginProfileA   s    & mmFFO||t!!$'g 
22??!!$'	g ++


+
+G
4CJr   c                 h   |ra|j                         }| j                  j                  j                  |      }| j                  j                  |j	                         |      }n*| j                  j                  |j	                               }| j
                  j                  j                  |       y)zDelete the posix accounts for an account in the current project.

    Args:
      project_ref: The oslogin.users.projects resource.
      operating_system: str, 'linux' or 'windows' (case insensitive).
    Returns:
      None
    )r8   operatingSystemTyper8   N)upperr+   !OsloginUsersProjectsDeleteRequest"OperatingSystemTypeValueValuesEnumRelativeNamer)   users_projectsDelete)r-   project_refoperating_systemos_value
os_messagerF   s         r   DeletePosixAccountsz!OsloginClient.DeletePosixAccountsj   s     !'')hMMCC77A ??'')( @ *g ??'') @ +g 	KK%%g.r   regionc                    | j                   j                  }| j                   j                  ||      }|r|gng }| j                  dk(  r& |dj	                  |      | j
                  ||      }	nA|r|j                  j                  }
nd}
 |dj	                  |      | j
                  |||
      }	| j                  j                  j                  |	      }|S )a  Upload an SSH public key to the user's login profile.

    Args:
      user: str, The email address of the OS Login user.
      public_key: str, An SSH public key.
      expiration_time: int, microseconds since epoch.
      include_security_keys: bool, If true, return security key information.
      region: str, The region to which to make sure the key is imported.

    Returns:
      The login profile for the user.
    keyexpirationTimeUsecr   r7   )parentr9   sshPublicKeyregionsN)r[   r9   r\   r]   r;   )r+   %OsloginUsersImportSshPublicKeyRequestSshPublicKeyr   r=   r'   r>   r?   r)   r@   ImportSshPublicKey)r-   rB   
public_keyexpiration_timerD   rV   import_requestpublic_key_messager]   rF   r;   rG   s               r   r`   z OsloginClient.ImportSshPublicKey   s    ( ]]HHN33* 4 , !vhbG||t##D)LL)	g 
11>>##D)LL)g ++


.
.w
7CJr   c                     | j                   j                  dj                  ||            }| j                  j                  j                  |       y)zDelete an SSH public key from the user's login profile.

    Args:
      user: str, The email address of the OS Login user.
      fingerprint: str, The fingerprint of the SSH key to delete.
    Returns:
      None
    users/{0}/sshPublicKeys/{1}rJ   N)r+   &OsloginUsersSshPublicKeysDeleteRequestr=   r)   users_sshPublicKeysrP   )r-   rB   fingerprintrF   s       r   DeleteSshPublicKeyz OsloginClient.DeleteSshPublicKey   sG     mmBB*11$D C FGKK##**73r   c                     | j                   j                  dj                  ||            }| j                  j                  j                  |      }|S )zGet an SSH public key from the user's login profile.

    Args:
      user: str, The email address of the OS Login user.
      fingerprint: str, The fingerprint of the SSH key to delete.
    Returns:
      The requested SSH public key.
    rf   rJ   )r+   #OsloginUsersSshPublicKeysGetRequestr=   r)   rh   r(   )r-   rB   ri   rF   rG   s        r   GetSshPublicKeyzOsloginClient.GetSshPublicKey   sN     mm??*11$D @ FG
++
)
)
-
-g
6CJr   c                     | j                   j                  ||      }| j                   j                  dj                  ||      ||      }| j                  j
                  j                  |      }|S )a  Update an existing SSH public key in a user's login profile.

    Args:
      user: str, The email address of the OS Login user.
      fingerprint: str, The fingerprint of the SSH key to update.
      public_key: str, An SSH public key.
      update_mask: str, A mask to control which fields get updated.
      expiration_time: int, microseconds since epoch.

    Returns:
      The login profile for the user.
    rX   rf   )r8   r\   
updateMask)r+   r_   %OsloginUsersSshPublicKeysPatchRequestr=   r)   rh   Patch)	r-   rB   ri   ra   update_maskrb   rd   rF   rG   s	            r   UpdateSshPublicKeyz OsloginClient.UpdateSshPublicKey   sv     33? 4  mmAA*11$D' B G
 ++
)
)
/
/
8CJr   c                     | j                   j                  |      }| j                   j                  dj                  |||      |      }| j                  j
                  j                  |      S )aQ  Sign an SSH public key for a given user.

    Args:
      user: str, The email address of the OS Login user.
      public_key: str, An SSH public key.
      project_id: str, The project ID associated with the VM.
      region: str, The region where the signed SSH public key may be used.

    Returns:
      A signed SSH public key.
    )r\   z$users/{0}/projects/{1}/locations/{2})r[   signSshPublicKeyRequest)r+   SignSshPublicKeyRequest0OsloginUsersProjectsZonesSignSshPublicKeyRequestr=   r)   users_projects_zonesSignSshPublicKey)r-   rB   ra   
project_idrV   rd   rF   s          r   ry   zOsloginClient.SignSshPublicKey   st     >> ?  mmLL5<<*f
 !3	 M G ;;++<<WEEr   c                    |r|s|s|st        d      ||||d}ddj                  ||      i}| j                  dk(  r= | j                  j                  di |}	 | j                  j
                  di |d|	i}
n< | j                  j                  di |}	 | j                  j
                  di |d|	i}
| j                  j                  j                  |
      S )	a  Sign an SSH public key scoped to a given instance.

    Args:
      public_key: str, An SSH public key.
      project_id: str, The project ID associated with the VM.
      region: str, The region where the signed SSH public key may be used.
      service_account: str, The service account associated with the VM.
      compute_instance: str, The Compute instance to sign the SSH public key
        for. Only one of compute_instance or app_engine_instance should be
        specified. The format for this field is
        'projects/{project}/zones/{zone}/instances/{instance_id}'.
      app_engine_instance: str, The App Engine instance to sign the SSH public
        key for. Only one of compute_instance or app_engine_instance should be
        specified. The format for this field is
        'services/{service}/versions/{version}/instances/{instance}'.

    Raises:
      ValueError: If both or neither compute_instance and
        app_engine_instance are specified.

    Returns:
      A signed SSH public key.
    zIExactly one of compute_instance or app_engine_instance must be specified.)r\   serviceAccountcomputeInstanceappEngineInstancer[   zprojects/{0}/locations/{1}r   DgoogleCloudOsloginControlplaneRegionalV1alphaSignSshPublicKeyRequestCgoogleCloudOsloginControlplaneRegionalV1betaSignSshPublicKeyRequestr   )

ValueErrorr=   r   r+   DGoogleCloudOsloginControlplaneRegionalV1alphaSignSshPublicKeyRequest/OsloginProjectsLocationsSignSshPublicKeyRequestCGoogleCloudOsloginControlplaneRegionalV1betaSignSshPublicKeyRequestr)   projects_locationsry   )r-   ra   rz   rV   service_accountcompute_instanceapp_engine_instancerequest_kwargsmessage_kwargsrequestrF   s              r   SignSshPublicKeyForInstancez)OsloginClient.SignSshPublicKeyForInstance   s   @ 	0%8 
 #)+0	N 	.55j&IN ||y bbb g NMM OVg
 baa g NMM NUg ;;))::7CCr   c                     |r|gng }| j                   j                  |      }dj                  ||      }| j                   j                  ||      }| j                  j
                  j                  |      }|S )a+  Provision a POSIX account for a given user.

    ProvisionPosixAccount is a regional read if the user has the correct
    POSIX account. Otherwise, it is a global write that waits on replication
    to the provided region before returning success.

    Args:
      user: str, The email address of the OS Login user.
      project_id: str, The project ID associated with the VM.
      region: str, The regions to wait to be written to before returning a
        response.

    Returns:
      The user's provisioned POSIX account for the given project.
    )r]   zusers/{0}/projects/{1})r8   provisionPosixAccountRequest)r+   ProvisionPosixAccountRequestr=   0OsloginUsersProjectsProvisionPosixAccountRequestr)   rO   ProvisionPosixAccount)	r-   rB   rz   rV   r]   provision_bodyr8   provision_requestrG   s	            r   r   z#OsloginClient.ProvisionPosixAccountA  s~    $ !vhbG]]???PN#**4<D 	FFN 	G 	
  ++
$
$
:
:;L
MCJr   )NNFr#   )NFN) NN)r   r   r   r   r/   r3   r1   rA   rU   r   strr`   rj   rm   rs   ry   r   r   r   r   r   r!   r!   .   s    - ;?,1'R/4 !"2 sm2h4 *.2F8 @DF 7;&.smr   r!   N)r   
__future__r   r   r   typingr   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.callioper	   googlecloudsdk.corer
   core_exceptionsr   ReleaseTrackALPHABETAGAr$   r   Errorr   r   objectr!   r   r   r   <module>r      s      &  '  , 1 ( = *  &&	  %%x  ##T+
4M,, MO. OqF qr   