HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/cloud/clouds/__pycache__/gce.cpython-310.pyc
o

�N�g? �@s�dZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
ZddlZddl
ZddlZddlTddlmZddlmZddlmZdZz(ddlZddlmZmZdd	lmZdd
lmZdd	lmZ dd
l!mZ"dZ#Wne$y�e�%�ZdZ#Ynwe�&e'�Z(d
Z)dZ*dZ+ee,e-��Z,ee.e-��Z.ee/e-��Z/ee0e-��Z0ee1e-��Z1ee2e-��Z2e�3d�Z4dd�Z5dd�Z6dd�Z7dd�Z8d�dd�Z9dd�Z:dd�Z;dd �Z<d!d"�Z=d#d$�Z>d%d&�Z?d�d'd(�Z@d�d)d*�ZAd�d+d,�ZBd-d.�ZCd/d0�ZDd1d2�ZEd3d4�ZFd5d6�ZGd7d8�ZHd9d:�ZId;d<�ZJd=d>�ZKd?d@�ZLdAdB�ZMdCdD�ZNdEdF�ZOdGdH�ZPd�dIdJ�ZQd�dKdL�ZRd�dMdN�ZSd�dOdP�ZTd�dQdR�ZUd�dSdT�ZVd�dUdV�ZWd�dWdX�ZXd�dYdZ�ZYd�d[d\�ZZd�d]d^�Z[d�d_d`�Z\d�dadb�Z]d�dcdd�Z^d�dedf�Z_d�dgdh�Z`d�didj�Zad�dkdl�Zbd�dmdn�Zcd�dodp�Zdd�dqdr�Zed�dsdt�Zfd�dudv�Zgd�dwdx�Zhd�dydz�Zid�d{d|�Zjd�d}d~�Zkd�dd��Zld�d�d��Zmd�d�d��Znd�d�d��Zod�d�d��Z/d�d�d��Zpd�d��Zqd�d�d��Zrd�d�d��Zsd�d�d��ZtdS)�a�
Copyright 2013 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Google Compute Engine Module
============================

The Google Compute Engine module. This module interfaces with Google Compute
Engine (GCE). To authenticate to GCE, you will need to create a Service Account.
To set up Service Account Authentication, follow the :ref:`gce_setup` instructions.

Example Provider Configuration
------------------------------

.. code-block:: yaml

    my-gce-config:
      # The Google Cloud Platform Project ID
      project: "my-project-id"
      # The Service Account client ID
      service_account_email_address: 1234567890@developer.gserviceaccount.com
      # The location of the private key (PEM format)
      service_account_private_key: /home/erjohnso/PRIVKEY.pem
      driver: gce
      # Specify whether to use public or private IP for deploy script.
      # Valid options are:
      #     private_ips - The salt-master is also hosted with GCE
      #     public_ips - The salt-master is hosted outside of GCE
      ssh_interface: public_ips

:maintainer: Eric Johnson <erjohnso@google.com>
:maintainer: Russell Tolle <russ.tolle@gmail.com>
:depends: libcloud >= 1.0.0
�N)�literal_eval)�*)�SaltCloudSystemExit)�namespaced_function)�Version)�ResourceInUseError�ResourceNotFoundError)�
get_driver)�ProviderTF�gcez
salt-cloudz0.2.0z'^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$cCs�tsdSttj�td�krdSt�durdSt�durdStd��D]$\}}d|vr,q#|d}tj	�
|d�}|rGtjj
�||�durGdSq#tS)zI
    Set up the libcloud functions and check for GCE configurations.
    )Fz apache-libcloud is not installedz2.5.0)Fz9The salt-cloud GCE driver requires apache-libcloud>=2.5.0FZ	providersr�service_account_private_key)�HAS_LIBCLOUDr�libcloud�__version__�get_configured_provider�get_dependencies�__opts__�items�os�path�
expanduser�salt�utilsZcloudZcheck_key_path_and_mode�__virtualname__)�provider�detailsZ
parameters�pathname�r�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/cloud/clouds/gce.py�__virtual__hs&

��rcCs"zt��WStytYSw�N)Z__active_provider_name__�value�AttributeErrorrrrr�_get_active_provider_name�s

�r#cCst�tt�pdd�S)z/
    Return the first configured instance.
    r)�project�service_account_email_addressr)�configZis_provider_configuredrr#rrrrr�s
�rcCs,trtjdtd�t�d�t�tdti�S)z*
    Warn if dependencies aren't met.
    z!Failure when importing LibCloud: )�exc_infozFNote: The libcloud dependency is called 'apache-libcloud' on PyPi/pip.r)�LIBCLOUD_IMPORT_ERROR�log�errorr&Zcheck_driver_dependenciesrr
rrrrr�s�rcCs|std��ttj�|d�S)z,
    Return a load-balancer conn object
    z*Missing gce_driver for get_lb_conn method.�Z
gce_driver)r�
get_driver_lb�Provider_lb�GCEr+rrr�get_lb_conn�sr/cCsbttj�}t�}t�d|t�}t�d|t�}t�d|t�}||||d�}|j�t	�dt
���|S)z5
    Return a conn object for the passed VM data
    r$r%r)r$�/)r	r
r.rr&�get_cloud_config_valuer�
connectionZuser_agent_append�_UA_PRODUCT�_UA_VERSION)�driverrr$�emailZprivate_keyrrrr�get_conn�s
��r7cCsi}|�|j�|S)zG
    Convert the libcloud object into something more serializable.
    ��update�__dict__)�item�retrrr�_expand_item�sr=cCs�i}|�|j�z|dd=Wn	tyYnw|dd}i|dd<|dd�|j�d|vr6|d=d|ddvrE|ddd=|S)zL
    Convert the libcloud Node object into something more serializable.
    �extraZ	boot_disk�zoner5)r9r:�	Exception)�noder<r?rrr�_expand_node�s�rBcCs@i}|�|j�|dd}i|dd<|dd�|j�|S)zN
    Convert the libcloud Volume object into something more serializable.
    r>r?r8)�diskr<r?rrr�_expand_disk�srDcCs$i}|�|j�|jj|dd<|S)zR
    Convert the libcloud GCEAddress object into something more serializable.
    r>r?)r9r:�region�name)�addyr<rrr�_expand_address�srHc
Cs�i}|�|j�|dd}g|dd<|D]
}|dd�t|��q|dd}|ddj}|ddj}i|dd<|dd�|j�|j|ddd<|j|ddd<|dd}|ddj}|ddj}|ddj}|ddjj	}	i|dd<|dd�|j�t|�|ddd<g|ddd<|D]}
|ddd�t
|
��q�g|ddd<|D]}|ddd�|j�q�g|dddd<|	D]}|dddd�|j�q�|S)zU
    Convert the libcloud load-balancer object into something more serializable.
    r>�healthchecksZforwarding_rule�
targetpoolrE�nodes�zones)r9r:�appendr=rJrErFrIrKrLrB)
�lbr<�hcr;Zfwr�tpZregrKrErL�nZhci�zrrr�_expand_balancer�s>rScCs:|dkrtd��t�}t|�|��}td|t�t�|S)z4
    Show the details of the existing instance.
    �actionz<The show_instance action must be called with -a or --action.zcloud.cache_node)rr7rB�ex_get_node�	__utils__r#r)�vm_name�call�connrArrr�
show_instances�rZcCsf|st�}|�d�}g}|D]"}|jd}i|jd<|jd�|j�i}|�|j�|�|�q|S)z
    Return a dict of available instances sizes (a.k.a machine types) and
    convert them to something more serializable.
    �allr?)r7Z
list_sizesr>r9r:rM)rYZ	raw_sizes�sizes�sizer?Zmtyperrr�avail_sizes-s


r^cCs�|st�}g}d}|D]
}|�|�|��q|�|���i}|D]}i||j<t|�D]}|�d�r4q,t||�||j|<q,q!|S)a�
    Return a dict of all available VM images on the cloud provider with
    relevant data.

    Note that for GCE, there are custom images within the project, but the
    generic images are in other projects.  This returns a dict of images in
    the project plus images in well-known public projects that provide supported
    images, as listed on this page:
    https://cloud.google.com/compute/docs/operating-systems/

    If image names overlap, the image in the current project is used.
    )	zcentos-cloudzcoreos-cloudzdebian-cloudzgoogle-containerszopensuse-cloudz
rhel-cloudz
suse-cloudzubuntu-os-cloudz
windows-cloud�_)r7�extendZlist_imagesrF�dir�
startswith�getattr)rYZ
all_imagesZpublic_image_projectsr$r<�img�attrrrr�avail_images@s 


�rfcC�tjd|tddd�}|�|�S)z_
    The get_image for GCE allows partial name matching and returns a
    libcloud object.
    �imagezdebian-7F��default�
search_global)r&r1rZex_get_image)rY�vm_rdrrr�__get_imageps
�
rmcCst�d|t�}|�|�S)z5
    Need to override libcloud to find the zone.
    �location)r&r1r�ex_get_zone)rYrlrnrrr�__get_location{s
rpcCs&tjd|tddd�}|�|t||��S)zP
    Need to override libcloud to find the machine type in the proper zone.
    r]z
n1-standard-1Fri)r&r1rZex_get_sizerp)rYrlr]rrr�
__get_size�s
�rqcC�Ntjd|tddd�}zt|�}Wntyd}Ynw|r#t|t�s%d}|S)z 
    Get configured labels.
    �	ex_labels�{}FriN)r&r1rrr@�
isinstance�dict)rl�l�labelsrrr�__get_labels��
��rycCrr)z
    Get configured tags.
    �tagsz[]FriN)r&r1rrr@ru�list)rl�tr{rrr�
__get_tags�rzr~cCs�tjd|tddd�}zt|�}Wntyd}Ynw|r#t|t�s/dd|dd	�gi}|S|d|d<g}|��D]\}}|�||d	��q;d|i}|S)
z?
    Get configured metadata and add 'salt-cloud-profile'.
    �metadatartFriNr�salt-cloud-profile�profile)�keyr!)	r&r1rrr@rurvrrM)rl�mdrr�k�vrrr�__get_metadata�s"
���r�cCsTt|�dks|ddur|jd}t�d|�n|jd}t�d|�|r'|S|jS)zR
    Return public IP, private IP, or hostname for the libcloud 'node' object
    �private_ips�external_ipNrzSalt node data. Private_ip: %szSalt node data. Public_ip: %s)�__get_ssh_interfacer�r)�info�
public_ipsrF)rArl�
ip_addressrrr�
__get_host�s

r�cCrg)zA
    Return a GCE libcloud network object with matching name
    �networkrjFri)r&r1r�ex_get_network)rYrlr�rrr�
__get_network�s
�
r�cCstjd|tdd�}|S)z$
    Get configured subnetwork.
    Z
subnetworkF�rk�r&r1r)rl�
ex_subnetworkrrr�__get_subnetwork�s�r�cCs.t||�}d�|j�d�dd��}|�|�S)zA
    Return a GCE libcloud region object with matching name.
    �-N�)rp�joinrF�splitZ
ex_get_region)rYrlrnrErrr�__get_region�s

r�cCstjd|tddd�S)zj
    Return the ssh_interface type to connect to. Either 'public_ips' (default)
    or 'private_ips'.
    Z
ssh_interfacer�Frir��rlrrrr��s
�r�cCsPz	|�||�}W|Sty'||d�}t|d�}|�|d|d�}Y|Sw)zn
    Reuse or create a static IP address.
    Returns a native GCEAddress construct to use with libcloud.
    �rFrE�functionrFrE)�ex_get_addressr�create_address)rYrFrErGZaddr_kwargsZnew_addyrrr�__create_orget_addresss�

�r�cCs�i}g}|�d�}|D]I}|�d�}|d��dvr$td|d�d���t|�dks2|d��dkr9g||d<q|d|vrI|dg||d<q||d�|d�q|D]}d	|i}||rg|||d
<|�|�qWt�d|�|S)zQ
    Convert firewall rule allowed user-string to specified REST API format.
    �,�:r)�tcpZudp�icmpzUnsupported protocol z. Must be tcp, udp, or icmp.�r�Z
IPProtocol�portsz$firewall allowed protocols/ports: %s)r��lowerr�lenrMr)�debug)�allowZseen_protosZ
allow_dictZ	protocols�p�pairsr��drrr�_parse_allows*


�r�cCs:tjd|tt�d�d�}tjd|ttj�d�d�}||fS)z)
    Get configured SSH credentials.
    Zssh_usernameZUSER�rjZssh_keyfilez~/.ssh/google_compute_engine)r&r1rr�getenvrr)rl�ssh_user�ssh_keyrrr�__get_ssh_credentials5s�
�r�c	Cs�|dkrtd��|rd|vrt�d�dS|�dd�}|�dd	�}|d	ur0|dkr0t�d
�dS|d}|�dd	�}t�}tdd
dd||||d�tdtdd�|�||||�}tdd
dd||||d�tdtdd�t|�S)a;
    .. versionchanged:: 2017.7.0

    Create a GCE network. Must specify name and cidr.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_network gce name=mynet cidr=10.10.10.0/24 mode=legacy description=optional
        salt-cloud -f create_network gce name=mynet description=optional
    r�zAThe create_network function must be called with -f or --function.rFz1A name must be specified when creating a network.F�mode�legacy�cidrNzFA network CIDR range must be specified when creating a legacy network.�description�cloud.fire_event�eventzcreating networkzsalt/cloud/net/creating)rFr�r�r��sock_dir�	transport��argsr�r�zcreated networkzsalt/cloud/net/created)	rr)r*�getr7rVrZex_create_networkr=)�kwargsrXr�r�rF�descrYr�rrr�create_networkEsF
�
��	�r�c
C��|dkrtd��|rd|vrt�d�dS|d}t�}tdddd	d|itd
tdd�z
|�|�|��}WntyU}ztjd
||t	j
d�WYd}~dSd}~wwtddddd|itd
tdd�|S)z�
    Permanently delete a network.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_network gce name=mynet
    r�zAThe delete_network function must be called with -f or --function.rFz1A name must be specified when deleting a network.Fr�r�zdeleting networkzsalt/cloud/net/deletingr�r�r�z*Nework %s was not found. Exception was: %s�Zexc_info_on_loglevelNzdeleted networkzsalt/cloud/net/deleted)rr)r*r7rVrZex_destroy_networkr�r�logging�DEBUG�r�rXrFrY�result�excrrr�delete_network}�L
�
�	���	�r�cC�B|dkrtd��|rd|vrt�d�dSt�}t|�|d��S)z�
    Show the details of an existing network.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_network gce name=mynet
    r�z?The show_network function must be called with -f or --function.rF�Must specify name of network.F)rr)r*r7r=r��r�rXrYrrr�show_network��
�
r�c	
Cs|dkrtd��|rd|vrt�d�dSd|vr t�d�dSd|vr+t�d	�dSd
|vr6t�d�dS|d}|d}|d}|d
}|�dd
�}t�}tdddd|||||d�tdtdd�|�|||||�}tdddd|||||d�tdtdd�t	|�S)a
    .. versionadded:: 2017.7.0

    Create a GCE Subnetwork. Must specify name, cidr, network, and region.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_subnetwork gce name=mysubnet network=mynet1 region=us-west1 cidr=10.0.0.0/24 description=optional
    r�zDThe create_subnetwork function must be called with -f or --function.rF�Must specify name of subnet.Fr�z4Must specify name of network to create subnet under.r�z>A network CIDR range must be specified when creating a subnet.rEz6A region must be specified when creating a subnetwork.r�Nr�r�zcreate subnetworkzsalt/cloud/subnet/creating)rFr�r�rEr�r�r�r�zcreated subnetworkzsalt/cloud/subnet/created)
rr)r*Zerrrorr�r7rVrZex_create_subnetworkr=)	r�rXrFr�r�rEr�rYZsubnetrrr�create_subnetwork�sd�



����r�c
Cs�|dkrtd��|rd|vrt�d�dSd|vr t�d�dS|d}|d}t�}tdd	d
d||d�td
tdd�z|�||�}Wntyc}ztjd||tj	d�WYd}~dSd}~wwtdd	dd||d�td
tdd�|S)z�
    .. versionadded:: 2017.7.0

    Delete a GCE Subnetwork. Must specify name and region.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_subnetwork gce name=mysubnet network=mynet1 region=us-west1
    r�z@The delete_subnet function must be called with -f or --function.rFr�FrE�Must specify region of subnet.r�r�zdeleting subnetworkzsalt/cloud/subnet/deletingr�r�r�r�z.Subnetwork %s was not found. Exception was: %sr�Nzdeleted subnetworkzsalt/cloud/subnet/deleted)
rr)r*r7rVrZex_destroy_subnetworkrr�r�)r�rXrFrErYr�r�rrr�delete_subnetworksT�

�	���	�r�cCsf|dkrtd��|rd|vrt�d�dSd|vr t�d�dS|d}|d}t�}t|�||��S)z�
    .. versionadded:: 2017.7.0

    Show details of an existing GCE Subnetwork. Must specify name and region.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_subnetwork gce name=mysubnet region=us-west1

    r�zBThe show_subnetwork function must be called with -f or --function.rFr�FrEr�)rr)r*r7r=Zex_get_subnetwork)r�rXrFrErYrrr�show_subnetworkOs
�

r�c
Cs*|dkrtd��|rd|vrt�d�dSd|vr t�d�dS|d}|�dd	�}t|d�}|�d
d�}|�dd
�}|�dd
�}|rI|�d�}|rP|�d�}|rW|�d�}t�}tdddd|||dd�tdtdd�|j	||||||d�}	tdddd|||dd�tdtdd�t
|	�S)z�
    Create a GCE firewall rule. The 'default' network is used if not specified.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_fwrule gce name=allow-http allow=tcp:80
    r�z@The create_fwrule function must be called with -f or --function.rFz7A name must be specified when creating a firewall rule.Fr�z4Must use "allow" to specify allowed protocols/ports.r�rj�	src_rangez	0.0.0.0/0�src_tagsN�dst_tagsr�r�r�zcreate firewallzsalt/cloud/firewall/creating)rFr�r�r�r�r�)r�Z
source_rangesZsource_tagsZtarget_tagszcreated firewallzsalt/cloud/firewall/created)rr)r*r�r�r�r7rVrZex_create_firewallr=)
r�rXrFZnetwork_namer�r�r�r�rYZfwrulerrr�
create_fwruleos`
�




�	�	�r�c
Cr�)z�
    Permanently delete a firewall rule.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_fwrule gce name=allow-http
    r�z@The delete_fwrule function must be called with -f or --function.rFz7A name must be specified when deleting a firewall rule.Fr�r�zdelete firewallzsalt/cloud/firewall/deletingr�r�r�z(Rule %s was not found. Exception was: %sr�Nzdeleted firewallzsalt/cloud/firewall/deleted)rr)r*r7rVrZex_destroy_firewall�ex_get_firewallrr�r�r�rrr�
delete_fwrule�r�r�cCr�)z�
    Show the details of an existing firewall rule.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_fwrule gce name=allow-http
    r�z>The show_fwrule function must be called with -f or --function.rFr�F)rr)r*r7r=r�r�rrr�show_fwrule�r�r�c
Cs|dkrtd��|rd|vrt�d�dS|d}|�dd�}|�dd�}|�d	d�}|�d
d�}|�dd�}|�dd�}|�d
d�}	t�}
tdddd||||||||	d�tdtdd�|
j||||||||	d�}tdddd||||||||	d�tdtdd�t|�S)z�
    Create an HTTP health check configuration.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_hc gce name=hc path=/healthy port=80
    r�z<The create_hc function must be called with -f or --function.rF�6A name must be specified when creating a health check.F�hostNr�port�interval�timeout�unhealthy_threshold�healthy_thresholdr�r�zcreate health_checkzsalt/cloud/healthcheck/creating)rFr�rr�r�r�r�r�r�r�r�)r�rr�r�r�r�r�zcreated health_checkzsalt/cloud/healthcheck/created)	rr)r*r�r7rVrZex_create_healthcheckr=)r�rXrFr�rr�r�r�r�r�rYrOrrr�	create_hc�sv
�
�
���
�r�c
Cr�)z�
    Permanently delete a health check.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_hc gce name=hc
    r��<The delete_hc function must be called with -f or --function.rF�6A name must be specified when deleting a health check.Fr�r�zdelete health_checkzsalt/cloud/healthcheck/deletingr�r�r�z0Health check %s was not found. Exception was: %sr�Nzdeleted health_checkzsalt/cloud/healthcheck/deleted)rr)r*r7rVrZex_destroy_healthcheck�ex_get_healthcheckrr�r�r�rrr�	delete_hcKr�r�cCr�)z�
    Show the details of an existing health check.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_hc gce name=hc
    r�z:The show_hc function must be called with -f or --function.rFz"Must specify name of health check.F)rr)r*r7r=r�r�rrr�show_hc�r�r�cCs�|dkrtd��|rd|vrt�d�dSd|vr t�d�dS|d}|d}|�dd	�}d|ji|d<t�}td
ddd
tjj	�
|�tdtdd�|�|||�}td
dddtjj	�
|�tdtdd�t�
d|�t|�S)z�
    Create a static address in a region.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_address gce name=my-ip region=us-central1 address=IP
    r�zAThe create_address function must be called with -f or --function.rFz2A name must be specified when creating an address.FrEz+A region must be specified for the address.�addressNr�r�zcreate addresszsalt/cloud/address/creatingr�r�r�zcreated addresszsalt/cloud/address/createdzCreated GCE Address %s)rr)r*r�rFr7rVrr�dataZsimple_types_filterrZex_create_addressr�rH)r�rXrF�	ex_region�
ex_addressrYrGrrrr��sD
�

�	�	r�c
Cs|dkrtd��|rd|vrt�d�dS|rd|vr"t�d�dS|d}|d}t�}tdd	d
dd|itdtd
d�z|�|�||��}Wntyh}ztjd|||t	j
d�WYd}~dSd}~wwtdd	ddd|itdtd
d�t�d|�|S)z�
    Permanently delete a static address.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_address gce name=my-ip
    r�zAThe delete_address function must be called with -f or --function.rFz2A name must be specified when deleting an address.FrEz4A region must be specified when deleting an address.r�r�zdelete addresszsalt/cloud/address/deletingr�r�r�z8Address %s in region %s was not found. Exception was: %sr�Nzdeleted addresszsalt/cloud/address/deletedzDeleted GCE Address %s)rr)r*r7rVrZex_destroy_addressr�rr�r�r�)r�rXrFr�rYr�r�rrr�delete_address�sX
�

�	���
�	r�cCsb|dkrtd��|rd|vrt�d�dS|rd|vr"t�d�dSt�}t|�|d|d��S)z�
    Show the details of an existing static address.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_address gce name=mysnapshot region=us-central1
    r��@The show_snapshot function must be called with -f or --function.rF�Must specify name.FrEzMust specify region.)rr)r*r7rHr�r�rrr�show_addresss
�

r�c

CsV|dkrtd��|rd|vrt�d�dSd|vr t�d�dSd|vr+t�d	�dSd
|vr6t�d�dS|d}|d}|d}|�d
��d�}|�d
d�}|�dd�}|�dd�}t�}	t|	�}
|�dd�}|durst|	||�}|rz|�d�}tdddd|t	dt	dd�|
j
||||||||d�}tdddd|t	dt	dd�t|�S)z�
    Create a load-balancer configuration.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_lb gce name=lb region=us-central1 ports=80
    r�z<The create_lb function must be called with -f or --function.rFr�Fr�z=A port or port-range must be specified for the load-balancer.rEz1A region must be specified for the load-balancer.�membersz4A comma-separated list of members must be specified.r��protocolr��	algorithmNrIr�r�r�zcreate load_balancerz salt/cloud/loadbalancer/creatingr�r�r�)r��ex_healthchecksr�zcreated load_balancerzsalt/cloud/loadbalancer/created)rr)r*r�r�r7r/r�rVrZcreate_balancerrS)
r�rXrFr�r�r�r�r�r�rY�lb_connr�rNrrr�	create_lb%sr
�




�	��r�c
Cs�|dkrtd��|rd|vrt�d�dS|d}tt��}tdddd	d|itd
tdd�z
|�|�|��}Wnt	yW}ztjd
||t
jd�WYd}~dSd}~wwtddddd|itd
tdd�|S)z�
    Permanently delete a load-balancer.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_lb gce name=lb
    r�r�rFr�Fr�r�zdelete load_balancerz salt/cloud/loadbalancer/deletingr�r�r�z1Load balancer %s was not found. Exception was: %sr�Nzdeleted load_balancerzsalt/cloud/loadbalancer/deleted)rr)r*r/r7rVrZdestroy_balancer�get_balancerrr�r�)r�rXrFr�r�r�rrr�	delete_lbusL
�

�	���	�r�cCsF|dkrtd��|rd|vrt�d�dStt��}t|�|d��S)z�
    Show the details of an existing load-balancer.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_lb gce name=lb
    r�z:The show_lb function must be called with -f or --function.rFz#Must specify name of load-balancer.F)rr)r*r/r7rSr�)r�rXr�rrr�show_lb�s
�

r�cCs�|dkrtd��|rd|vrt�d�dSd|vr t�d�dSt�}|�|d�}t|�}|�|d�}tdd	d
d|tdtd
d�|�	||�}tdd	dd|tdtd
d�t
|�S)z�
    Add an existing node/member to an existing load-balancer configuration.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f attach_lb gce name=lb member=myinstance
    r�z<The attach_lb function must be called with -f or --function.rF�'A load-balancer name must be specified.F�member�#A node name name must be specified.r�r�zattach load_balancerz!salt/cloud/loadbalancer/attachingr�r�r�zattached load_balancerz salt/cloud/loadbalancer/attached)rr)r*r7rUr/r�rVrZbalancer_attach_compute_noder=)r�rXrYrAr�rNr�rrr�	attach_lb�s@
�

�	�r�c	Cs�|dkrtd��|rd|vrt�d�dSd|vr t�d�dSt�}t|�}|�|d�}|�|�}d}|D]
}|j|dkrD|}nq7|sTt�d	|d|d�dStd
ddd
|t	dt	dd�|�
||�}td
ddd|t	dt	dd�|S)z�
    Remove an existing node/member from an existing load-balancer configuration.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f detach_lb gce name=lb member=myinstance
    r�z<The detach_lb function must be called with -f or --function.rFr�Fr�r�Nz2The specified member %s was not a member of LB %s.r�r�zdetach load_balancerz!salt/cloud/loadbalancer/detachingr�r�r�zdetached load_balancerz salt/cloud/loadbalancer/detached)rr)r*r7r/r�Zbalancer_list_members�idrVrZbalancer_detach_member)	r�rXrYr�rNZmember_listZ
remove_memberr�r�rrr�	detach_lb�sZ
�


���	�r�c
Cr�)z�
    Permanently delete a disk snapshot.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_snapshot gce name=disk-snap-1
    r�zBThe delete_snapshot function must be called with -f or --function.rFz2A name must be specified when deleting a snapshot.Fr�r�zdelete snapshotzsalt/cloud/snapshot/deletingr�r�r�z,Snapshot %s was not found. Exception was: %sr�Nzdeleted snapshotzsalt/cloud/snapshot/deleted)rr)r*r7rVrZdestroy_volume_snapshot�ex_get_snapshotrr�r�r�rrr�delete_snapshot1r�rc
Cs�|dkrtd��|rd|vrt�d�dSt�}|�|�d��}tdddd	|j|jd
j|j	d�t
dt
d
d�z|�|�}Wnty_}ztjd|j|t
jd�WYd}~dSd}~wwtdddd|j|jd
j|j	d�t
dt
d
d�|S)z�
    Permanently delete a persistent disk.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f delete_disk gce disk_name=pd
    r�z>The delete_disk function must be called with -f or --function.�	disk_namez3A disk_name must be specified when deleting a disk.Fr�r��delete disk�salt/cloud/disk/deletingr?)rFrnr]r�r�r�zjDisk %s is in use and must be detached before deleting.
The following exception was thrown by libcloud:
%sr�N�deleted disk�salt/cloud/disk/deleted)rr)r*r7�
ex_get_volumer�rVrFr>r]r�destroy_volumerr�r�)r�rXrYrCr�r�rrr�delete_diskfsX
�

��
���

��rc	CsD|dkrtd��|duri}|�dd�}|�dd�}|�dd�}|�dd�}|�dd�}|�d	d
�}|dur=t�d�dS|durHt�d
�dS|dur[|dur[|dur[t�d�dSt�}|�|d�}d}	tdddd||j||d�tdtdd�|�	||||||	|�}
tdddd||j||d�tdtdd�t
|
�S)a�
    Create a new persistent disk. Must specify `disk_name` and `location`,
    and optionally can specify 'disk_type' as pd-standard or pd-ssd, which
    defaults to pd-standard. Can also specify an `image` or `snapshot` but
    if neither of those are specified, a `size` (in GB) is required.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_disk gce disk_name=pd size=300 location=us-central1-b
    r�z>The create_disk function must be called with -f or --function.Nrrhrnr]�snapshot�type�pd-standardz9A location (zone) must be specified when creating a disk.Fz3A disk_name must be specified when creating a disk.z&Must specify image, snapshot, or size.Tr�r�zcreate diskzsalt/cloud/disk/creating)rFrnrhr	r�r�r�zcreated diskzsalt/cloud/disk/created)rr�r)r*r7rorVrFrZ
create_volumerD)r�rXrFrhrnr]r	Z	disk_typerYZuse_existingrCrrr�create_disk�sh
�


�����
rc
Cs|dkrtd��|rd|vrt�d�dSd|vr t�d�dSt�}|�d�}|�d�}z|�|�}WntyQ}ztjd||tjd	�WYd
}~dSd
}~wwt	ddd
d||d�t
dt
dd�|�||�}t	dddd||d�t
dt
dd�t|�S)z�
    Create a new disk snapshot. Must specify `name` and  `disk_name`.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f create_snapshot gce name=snap1 disk_name=pd
    r�zBThe create_snapshot function must be called with -f or --function.rFz2A name must be specified when creating a snapshot.Frz7A disk_name must be specified when creating a snapshot.z(Disk %s was not found. Exception was: %sr�Nr�r�zcreate snapshotzsalt/cloud/snapshot/creating�rFrr�r�r�zcreated snapshotzsalt/cloud/snapshot/created)
rr)r*r7r�rrr�r�rVrZcreate_volume_snapshotr=)r�rXrYrFrrCr�r	rrr�create_snapshot�sV
�



���	�	�rcCs2|rd|vr
t�d�dSt�}t|�|d��S)z�
    Show the details of an existing disk.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a show_disk myinstance disk_name=mydisk
        salt-cloud -f show_disk gce disk_name=mydisk
    rzMust specify disk_name.F)r)r*r7rDr)rFr�rXrYrrr�	show_disk1s

rcCr�)z�
    Show the details of an existing snapshot.

    CLI Example:

    .. code-block:: bash

        salt-cloud -f show_snapshot gce name=mysnapshot
    r�r�rFr�F)rr)r*r7r=r�r�rrr�
show_snapshotDr�rc	Cs�|dkrtd��|st�d�dS|rd|vrt�d�dS|}|d}t�}|�|�}|�|�}tddd	d
||d�tdtd
d�|�||�}tdddd||d�tdtd
d�|S)z�
    Detach a disk from an instance.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a detach_disk myinstance disk_name=mydisk
    rTz:The detach_Disk action must be called with -a or --action.�Must specify an instance name.Frz#Must specify a disk_name to detach.r�r�zdetach diskzsalt/cloud/disk/detachingr
r�r�r�z
detached diskzsalt/cloud/disk/detached)	rr)r*r7rUrrVrZ
detach_volume)	rFr�rX�	node_namerrYrArCr�rrr�detach_diskZsB
�



�	�rc	Cs&|dkrtd��|st�d�dS|rd|vrt�d�dS|}|d}|�dd���}|�d	d�}|�d
d�}|rC|��dvrCd}nd}|d
vrPt�d�dSt�}|�|�}	|�|�}
t	dddd||||d�t
dt
dd�|j|	|
|||d�}t	dddd||||d�t
dt
dd�|S)z�
    Attach an existing disk to an existing instance.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a attach_disk myinstance disk_name=mydisk mode=READ_WRITE
    rTz:The attach_disk action must be called with -a or --action.rFrz#Must specify a disk_name to attach.r��
READ_WRITE�boot�auto_delete)�trueZyes�enabledT)rZ	READ_ONLYz6Mode must be either READ_ONLY or (default) READ_WRITE.r�r�zattach diskzsalt/cloud/disk/attaching)rFrr�rr�r�r�)Zex_modeZex_bootZex_auto_deletez
attached diskzsalt/cloud/disk/attached)rr)r*r��upperr�r7rUrrVrZ
attach_volume)rFr�rXrrr�rrrYrArCr�rrr�attach_disk�sX
�




�	
��rcC�|dkrtd��t�}tdddd|�d�d|itd	td
d�|�|�|��}tdddd|�d�d|itd	td
d�|S)
z�
    Call GCE 'reset' on the instance.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a reboot myinstance
    rTz5The reboot action must be called with -a or --action.r�r�zreboot instance�salt/cloud/z
/rebootingrFr�r�r�z	/rebooted)rr7rVrZreboot_noderU�rWrXrYr�rrr�reboot�s.
�
�	
�	rcCr)
z�
    Call GCE 'start on the instance.

    .. versionadded:: 2017.7.0

    CLI Example:

    .. code-block:: bash

        salt-cloud -a start myinstance
    rTz4The start action must be called with -a or --action.r�r�zstart instancerz	/startingrFr�r�r�z/started)rr7rVrZ
ex_start_noderUrrrr�start�s.�
�	
�	rcCr)
z�
    Call GCE 'stop' on the instance.

    .. versionadded:: 2017.7.0

    CLI Example:

    .. code-block:: bash

        salt-cloud -a stop myinstance
    rTz3The stop action must be called with -a or --action.r�r�z
stop instancerz	/stoppingrFr�r�r�z/stopped)rr7rVrZex_stop_noderUrrrr�stops*
�	
�	r c

Csb|r
|dkr
td��t�}z|�|�}Wnty2}ztjd||tjd�td|�d���d}~wwtdd	d
d|�d�d
|it	dt	dd�d}|j
drkd|j
dvrk|j
ddD]}|ddkrj|d}q^t�}d}|r�||dvr�d|d|vr�|d|d}z|�|�}	Wnty�}ztjd||tjd�td|�d���d}~wwtdd	d
d|�d�d
|it	dt	dd�|�rt�
d�tdd	ddd
|it	dt	dd�z
|�|�|��Wnt�y}ztjd||tjd�WYd}~nd}~wwtdd	d d!d
|it	dt	dd�t	�d"d�d#u�r/td$|t��d%�d&t	�|	S)'z�
    Call 'destroy' on the instance.  Can be called with "-a destroy" or -d

    CLI Example:

    .. code-block:: bash

        salt-cloud -a destroy myinstance1 myinstance2 ...
        salt-cloud -d myinstance1 myinstance2 ...
    rTz:The destroy action must be called with -d or "-a destroy".z{Could not locate instance %s

The following exception was thrown by libcloud when trying to run the initial deployment: 
%sr�zCould not find instance �.Nr�r�zdelete instancerz	/deletingrFr�r�r�rrr�r�r!F�profiles�delete_boot_pdz|Could not destroy instance %s

The following exception was thrown by libcloud when trying to run the initial deployment: 
%szCould not destroy instance z/deletedzMdelete_boot_pd is enabled for the instance profile, attempting to delete diskrrzxCould not destroy disk %s

The following exception was thrown by libcloud when trying to run the initial deployment: 
%srrZupdate_cachedirTzcloud.delete_minion_cachedirr�r)rr7rUr@r)r*r�r�rVrr>rZdestroy_noder�rrr�r#r�)
rWrXrYrAr�r�r�rlr#Zinst_deletedrrr�destroyGs�����
�
�����

�	������	�r$cCs�|dkrtd��t|d�}|d}t�}t|�|��}td�d}t|�D]>\}}	|�dt|d|���}
|
|d	d
d|	d|	�d
d�|	�dd�|	�dd�|	�dd�d�}t	|d�t
||d�q&dS)a)
    .. versionadded:: 2017.7.0

    Create and attach multiple volumes to a node. The 'volumes' and 'node'
    arguments are required, where 'node' is a libcloud node, and 'volumes'
    is a list of maps, where each map contains:

    size
        The size of the new disk in GB. Required.

    type
        The disk type, either pd-standard or pd-ssd. Optional, defaults to pd-standard.

    image
        An image to use for this new disk. Optional.

    snapshot
        A snapshot to use for this new disk. Optional.

    auto_delete
        An option(bool) to keep or remove the disk upon instance deletion.
        Optional, defaults to False.

    Volumes are attached in the order in which they are given, thus on a new
    node the first volume will be /dev/sdb, the second /dev/sdc, and so on.
    rTzDThe create_attach_volumes action must be called with -a or --action.�volumesrA�ar�z-sdr�r>r?rFr]r
rrhNr	rF)rrnr]r
rhr	rr�)rrr7rBrU�ord�	enumerate�chrr�rr)rFr�rXr%rArY�	node_dataZletter�idxZvolumeZvolume_nameZvolume_dictrrr�create_attach_volumes�s,�



�

�r,c	
Cs�t�|d�std��z|dr#tjtt�pd|d|d�dur#WdSWn	ty-Ynwtddd	d
�	|d�tdd|gd
��tdtdd�t
�}|dt||�t||�t
||�t|�t||�t|�t|�t|�d�	}tjd|tdd�}|��dkr�d}||d<n"|dkr�d}||d<nt||�}t|||�}|j|j|jjd�|d<||d<tdkr�|�tjd|tdd�tjd|tdd�tjd|tdd�tjd|tdd�tjd|tdd�tjd |tdd�d!��|�d�d"vr�td#��td$k�r|�tjd%|tdd�tjd&|tdd�d'��|�d%��rt�d(�|�d)d*i�t�d+|d|d,j�t� d-|�tddd.d/�	|d�tdd0|gd
��tdtdd�z
|j!d;i|��}Wn t"�yw}ztj#d1|d|t$j%d2�WYd}~dSd}~wwtjd3|tdd4�}|�r�tddd5d6�	|d�d3|itdtdd�t�d7|d�t&|d||d8�d9d:�zt'|dd9�}W||fSt(�y�t'|jd9�}Y||fSw)<z[
    Request a single GCE instance from a data dict.

    .. versionchanged:: 2017.7.0
    rFzeVM names must start with a letter, only contain letters, numbers, or dashes and cannot end in a dash.r�rr�Fr�r�zcreate instancezsalt/cloud/{}/creating�cloud.filter_eventZcreating�rFr�rr5r�r�r�)	rFr]rhrnrsZ
ex_networkr�Zex_tagsZex_metadatar�Z	ephemeralr��NoneN)rFr�rE)r�r��ex_disk_typer�ex_disk_auto_deleteT�ex_disks_gce_struct�ex_service_accountsZ
ip_forwardingZpreemptible)r1r2r3r4Zex_can_ip_forwardZex_preemptible)rzpd-ssdzGThe value of 'ex_disk_type' needs to be one of: 'pd-standard', 'pd-ssd')r��r�ex_accelerator_type�ex_accelerator_count)r6r7z}An accelerator is being attached to this instance, the ex_on_host_maintenance setting is being set to 'TERMINATE' as a resultZex_on_host_maintenanceZ	TERMINATEzCreating GCE instance %s in %srnzCreate instance kwargs %szrequesting instancezsalt/cloud/{}/requestingZ
requestingzwError creating %s on GCE

The following exception was thrown by libcloud when trying to run the initial deployment: 
%sr�r%r�zattaching volumeszsalt/cloud/{}/attaching_volumesz$Create and attach volumes to node %s)r%rArT)rXr))�GCE_VM_NAME_REGEX�matchrr&Zis_profile_configuredrr#r"rV�formatr7rqrmrpryr�r�r~r�r1r�r�r�rFr�rEZLIBCLOUD_VERSION_INFOr9r�r)�warningr�r�Zcreate_noder@r*r�r�r,rZ�	TypeError)	rlrYr�r�rEr*r�r%�	node_dictrrr�request_instance�s������
����



����������
����
�
�������	���r>c	Cs�|rtd��t|�}t|t�rtd��|d}|d}t|�\}}t||�|d<||d<td|t�}|�|�t	�
d|d	�t	�d
|d	t�
|��tddd
d�|d	�tdd|gd��tdtdd�|S)z8
    Create a single GCE instance from a data dict.
    z,You cannot create an instance with -a or -f.z-There was an error creating the GCE instance.rr�Zssh_hostZkey_filenamezcloud.bootstrapzCreated Cloud VM '%s'rFz'%s' VM creation details:
%sr�r�zcreated instancezsalt/cloud/{}/createdr-�createdr.r�r�r�)rr>ru�boolr�r�rVrr9r)r�Ztrace�pprintZpformatr:)rlrXZ	node_infor=r*r�r�r<rrr�create�	s2


��rBcCstd}tjjj|ddd�}tj�tdd�}tjj�	|d��}tjj
�|d|�Wd	�dS1s3wYdS)
z�
    Download most recent pricing information from GCE and save locally

    CLI Examples:

    .. code-block:: bash

        salt-cloud -f update_pricing my-gce-config

    .. versionadded:: 2015.8.0
    zEhttps://cloudpricingcalculator.appspot.com/static/data/pricelist.jsonT�json)�decodeZdecode_type�cachedir�
gce-pricing.p�wrvN)rr�http�queryrrr�r�files�fopen�msgpack�dump)r�rX�urlZ
price_jsonZoutfile�fhorrr�update_pricing�	s
��rPcCs�td�|di�}|sddiS|�dd�}|�d�}t|�dks'|d	d
kr+ddiS|�dd
��d�}|d}d�|d���}tj�tdd�}tj�	|�sSt
�tjj
�|d��}tjj�|�}	Wd�n1smwYt|	d||�}
t|	ddd�}t|	ddd�}t|	ddd�}
t|	ddd�}|
d|}|
d|}|
d|
}|
d|}|	}i}|
|d<|dd|d<|dd |d!<|||||d"<|d"d#|d$<|�d%d&�r�||d'<|d|iS)(a
    Show pricing for a particular profile. This is only an estimate, based on
    unofficial pricing sources.

    .. versionadded:: 2015.8.0

    CLI Examples:

    .. code-block:: bash

        salt-cloud -f show_pricing my-gce-config profile=my-profile
    r"r��Errorz#The requested profile was not foundrz0:0r�r�r�rz,The requested profile does not belong to GCErn�usr�rzCP-COMPUTEENGINE-VMIMAGE-{}r]rErF�rNZgcp_price_listZsustained_use_tiersz0.25z0.50z0.75z1.0g�f@�per_hour�Zper_day�Zper_weekZ	per_month�Zper_year�rawFZ_raw)rr�r�r�r:rrrr��existsrPrrrJrKrL�load�float)r�rXr�r�compsrEr]Z	pricefilerOr\rTZweek1_discountZweek2_discountZweek3_discountZweek4_discountZweek1Zweek2Zweek3Zweek4rXr<rrr�show_pricing�	sF

�r]r )NN)NNN)u�__doc__r�rrA�re�sys�astrZsalt.configr&Zsalt.utils.cloudrZsalt.utils.filesZsalt.utils.httpZsalt.utils.msgpackZsalt.cloud.libcloudfuncsZsalt.exceptionsrZsalt.utils.functoolsrZsalt.utils.versionsrr(rZlibcloud.common.googlerrZlibcloud.compute.providersr	Zlibcloud.compute.typesr
Zlibcloud.loadbalancer.providersr,Zlibcloud.loadbalancer.typesr-r
�ImportErrorr'�	getLogger�__name__r)rr3r4Zavail_locations�globalsZscriptr$Z
list_nodesZlist_nodes_fullZlist_nodes_select�compiler8rr#rrr/r7r=rBrDrHrSrZr^rfrmrprqryr~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrrrrr r,r>rBrPr]rrrr�<module>s�0�

 
		

(

0



#

8
5

K
<
 
B
5

O
5

6
?

P
5

2
?
5
?
O
=


3
@
(
*
(
|7
0
&