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/states/__pycache__/boto_ec2.cpython-310.pyc
o

�N�g��@s�dZddlZddlmZmZddlZddlmmZddl	m
Z
mZe�e
�Zdd�Z						d&dd�Zd'd	d
�Z												d(dd
�Zdd�Zdd�Z	d)dd�Z		d*dd�Z																																										d+dd�Z								d,dd�Z									d-dd�Z	d)dd�Z															d.d d!�Z								d/d"d#�Z							d0d$d%�ZdS)1a�
Manage EC2

.. versionadded:: 2015.8.0

This module provides an interface to the Elastic Compute Cloud (EC2) service
from AWS.

The below code creates a key pair:

.. code-block:: yaml

    create-key-pair:
      boto_ec2.key_present:
        - name: mykeypair
        - save_private: /root/
        - region: eu-west-1
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs

.. code-block:: yaml

    import-key-pair:
       boto_ec2.key_present:
        - name: mykeypair
        - upload_public: 'ssh-rsa AAAA'
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs

You can also use salt:// in order to define the public key.

.. code-block:: yaml

    import-key-pair:
       boto_ec2.key_present:
        - name: mykeypair
        - upload_public: salt://mybase/public_key.pub
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs

The below code deletes a key pair:

.. code-block:: yaml

    delete-key-pair:
      boto_ec2.key_absent:
        - name: mykeypair
        - region: eu-west-1
        - keyid: GKTADJGHEIQSXMKKRBJ08H
        - key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs
�N)�sleep�time)�CommandExecutionError�SaltInvocationErrorcCsdtvrdSdS)z)
    Only load if boto is available.
    �boto_ec2.get_keyZboto_ec2)Fz#boto_ec2 module could not be loaded)�__salt__�rr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/boto_ec2.py�__virtual__?sr
c
Cs�|ddid�}td|||||�}t�d|�|durMd|vrMztd|�}Wn%tyL}	zt�|	�d	|�d
�|d<d|d
<|WYd}	~	Sd}	~	ww|s�tdrad|�d�|d<d|d
<|S|r�|s�td||||||�}
|
r�d|d
<d|�d�|d<|
|dd<|Sd|d
<d|�d�|d<|S|s�|r�td||||||�}|r�d|d
<d|�d�|d<d|dd<||dd<|Sd|d
<d|�d�|d<|Sd|d
<d|d<|Sd|d
<d|�d�|d<|S)z%
    Ensure key pair is present.
    T���name�result�comment�changesr�exists is %sNzsalt://zcp.get_file_strzFile � not found.rFr�test�The key � is set to be created.zboto_ec2.create_keyz is created.r�newzCould not create key � zboto_ec2.import_key�oldz0You can either upload or download a private key �
The key name z already exists)r�log�debug�OSError�__opts__)r
Zsave_privateZ
upload_public�region�key�keyid�profile�ret�exists�e�createdZimportedrrr	�key_presentHsb
������
���r&cCs�|ddid�}td|||||�}|rYtdr%d|�d�|d<d	|d
<|Std|||||�}t�d|�|rKd|d
<d|�d
�|d<||dd<|Sd|d
<d|�d�|d<|Sd|d
<d|�d�|d<|S)z
    Deletes a key pair
    Trrrrr� is set to be deleted.rNrzboto_ec2.delete_keyrz is deleted.rrFzCould not delete key rrz does not exist)rrrr)r
rrr r!r"r#Zdeletedrrr	�
key_absent�s(��r(Tc

Cshtjj�||f�s
td��|std��t|t�std��t|t�s%td��|ddid�}
td||	|
||d	�}d
|vrLd|
d<d
�	|d
d�|
d<|
S|ds�t
drxd|
d<|red�|
ddg�|
d<|rrd�|
ddg�|
d<d|
d<|
Std|||||||	|
||d�
}d
|vr�d|
d<d�	|d
d�|
d<|
S|d|d<d|��|
d<|dd|
dd<nht|dd||	|
||�}t
�|
d|d�|
d<|d|
d<|ds�|d|
d<|
ddur�|
St|d||	|
||�}t
�|
d|d�|
d<d�|
d|dg�|
d<|d�s|d|
d<|
ddu�r|
St|dd||	|
||�}t
�|
d|d�|
d<d�|
d|dg�|
d<|d�sO|d|
d<|
S|�r�d|dv�r�t
d�rjd�|
ddg�|
d<nw|d k�rqd nd}td!||	|
||d"�}|�r�td#ddd|d$|dddd|	|
||d%�}|�s�td&d|d$|	|
||d'�}d|
d<d(�	|�r�d)nd*�}d�|
d|g�|
d<|
Snd|
d<d�|
dd+g�|
d<|
Sd�|
dd,g�|
d<|�r�|D]�}d-|v�r�d.}t|��t�d/|�d}d0}d1|d2<d}d3|v�r
|�d3�}|�r2d4|dv�r |dd4|d5<nd6|v�r,|d6|d5<nd7}t|��|dd8|d5<d9|v�rD|�d9�}|d0k�rmd:|v�rR||d:<d;|v�r[|
|d;<d<|v�rd||d<<d=|v�rm|	|d=<td>�|d?g�dAi|��}t�d@|�t
�|
d|d�|
d<d�|
d|dg�|
d<|d�s�|d|
d<|
ddu�r�|
S�q�|
S)Ba�
    Ensure the EC2 ENI exists.

    .. versionadded:: 2016.3.0

    name
        Name tag associated with the ENI.

    subnet_id
        The VPC subnet ID the ENI will exist within.

    subnet_name
        The VPC subnet name the ENI will exist within.

    private_ip_address
        The private ip address to use for this ENI. If this is not specified
        AWS will automatically assign a private IP address to the ENI. Must be
        specified at creation time; will be ignored afterward.

    description
        Description of the key.

    groups
        A list of security groups to apply to the ENI.

    source_dest_check
        Boolean specifying whether source/destination checking is enabled on
        the ENI.

    allocate_eip
        allocate and associate an EIP to the ENI. Could be 'standard' to
        allocate Elastic IP to EC2 region or 'vpc' to get it for a
        particular VPC

        .. versionchanged:: 2016.11.0

    arecords
        A list of arecord dicts with attributes needed for the DNS add_record state.
        By default the boto_route53.add_record state will be used, which requires: name, zone, ttl, and identifier.
        See the boto_route53 state for information about these attributes.
        Other DNS modules can be called by specifying the provider keyword.
        By default, the private ENI IP address will be used, set 'public: True' in the arecord dict to use the ENI's public IP address

        .. versionadded:: 2016.3.0

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.
    z@One (but not both) of subnet_id or subnet_name must be provided.zgroups is a required argument.zgroups must be a list.z!source_dest_check must be a bool.Trr�boto_ec2.get_network_interface�r
rrr r!�errorFr�&Error when attempting to find eni: {}.�messagerrzENI is set to be created.rz4An EIP is set to be allocated/assocaited to the ENI.z A records are set to be created.Nz!boto_ec2.create_network_interface)	�	subnet_id�subnet_name�private_ip_address�description�groupsrrr r!zFailed to create ENI: {}zCreated ENI �idrr1�source_dest_check�allocationIdz8An EIP is set to be allocated and assocaited to the ENI.�vpc�boto_ec2.allocate_eip_address��domainrrr r!�boto_ec2.associate_eip_address�
allocation_id)�instance_id�
instance_name�	public_ipr;�network_interface_idr0Zallow_reassociationrrr r!�boto_ec2.release_eip_address�r>r;rrr r!zGFailed to assocaite the allocated EIP address with the ENI.  The EIP {}zwas successfully released.zwas NOT RELEASED.z!Failed to allocate an EIP addressz1An EIP is already allocated/assocaited to the ENIr
z+The arecord must contain a "name" property.zprocessing arecord %sZboto_route53�AZrecord_typeZpublicZpublicIp�valuer>zsUnable to add an A record for the public IP address, a public IP address does not seem to be allocated to this ENI.r0Zproviderr!rr r�.Zpresentz"ret from dns_provider.present = %sr)�salt�utils�data�exactly_oner�
isinstance�list�boolr�formatr�join�_eni_attribute�
dictupdate�update�_eni_groupsrr�poprZ
__states__)r
r.r/r0r1r2r4�allocate_eipZarecordsrrr r!r"�rZ
result_create�_retr9Z	eip_alloc�msgZarecordZdns_providerZpublic_ip_arecordrrr	�eni_present�s`I�


�
���
��
��
�

�
�
�
�
�����
�
�



�







�rWc
	Cs�ddid�}|||kr|Stdr d|�d�|d<d|d<|Std	|d
||||||d�}d|vrGd
}	d|d<|	�||dd�|d<|Sd|�d�|d<|||d�|d|<|S)NTr�rrrrzENI set to have z	 updated.rr�+boto_ec2.modify_network_interface_attributer3�r?�attrrCrrr r!r+zFailed to update ENI {0}: {1}.Fr-zUpdated ENI rD�rrr)rrrL)
�metadatar[rCrrr r!r"�
result_updaterVrrr	rN�s0�	�rNc	Cs�ddid�}dd�|dD�}|��td||d||||d	�}|s+d
|d<d|d
<|��||kr5|StdrCd|d<d|d
<|Std|dd|||||d�}	d|	vrid}
d|d
<|
�|	dd�|d<|Sd|d<||d�|dd<|S)NTrrXcSsg|]}|d�qS)r3r)�.0�grrr	�
<listcomp>�sz_eni_groups.<locals>.<listcomp>r2z"boto_secgroup.convert_to_group_ids�vpc_id)rbrrr r!z0Could not find secgroup ids for provided groups.rFrrzENI set to have groups updated.rYr3rZr+z!Failed to update ENI groups: {1}.r-zUpdated ENI groups.r\r)�sortrrrL)r]r2rrr r!r"Z	group_idsZ_groupsr^rVrrr	rQ�sL��	�rQFcCs�|ddid�}td|||||d�}d|vr'd|d<d	�|dd
�|d<|S|ds;tdr9d
|d<d|d<|S|Stdr\d
|d<|rVd|dvrVd�|ddg�|d<d|d<|Sd|ddvr�td|d||||d�}d|vr�d|d<d�|dd
�|d<|Std|||||d�}	d|	vr�d|d<d�|	dd
�|d<|Sd|��|d<d|dd<|r�d|dvr�tdd|dd||||d�}
|
s�d�|ddg�|d<d|d<|Sd�|ddg�|d<d|dd<|S) a�
    Ensure the EC2 ENI is absent.

    .. versionadded:: 2016.3.0

    name
        Name tag associated with the ENI.

    release_eip
        True/False - release any EIP associated with the ENI

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.
    Trrr)r*r+Frr,r-rrzENI is set to be deleted.Nr5rz.Allocated/associated EIP is set to be releasedr3�
attachmentz!boto_ec2.detach_network_interface)r
�forcerrr r!zFailed to detach ENI: {}z!boto_ec2.delete_network_interfacezFailed to delete ENI: {}zDeleted ENI rr@rAz+Failed to release EIP allocated to the ENI.z
EIP released.zeip released)rrLrrM)r
�release_eiprrr r!r"rTZ
result_detachZ
result_deleterUrrr	�
eni_absent�s�
�
�7�
��
�
�
�
�
�rg�,c	Ks�|ddid�}tdd||d�|��s d|��|d<d|d	<|Sd
|��|d<||i|dd<|s3|St�}	td
d|dd�|��}|rO|djdkrO	|St�||krr|rddj|djd�|d<ndj|d�|d<d|d	<|Std�q7)zS
    Create a snapshot from the given instance

    .. versionadded:: 2016.3.0
    Trrzboto_ec2.create_image)�ami_namer=zFailed to create new AMI rFrzCreated new AMI rr�boto_ec2.find_images)ri�return_objsrZ	availablez(AMI still in state {state} after timeout)�statez1AMI with name {ami_name} not found after timeout.)ri�Nr)rrrlrLr)	r
rir=Zwait_until_availableZwait_timeout_seconds�kwargsr"Z	starttimeZimagesrrr	�snapshot_createdJsH
���������roc+Csf|ddid�}+d},d}-i}.tjj�||f�std��|$s |%s |&r.tjj�|$|%|&f�s.td��|rAtd||'|(|)|*|-d	�}/|/s@d},n'td
|rH|n||'|(|)|*|-d�}0|0sVd},nt|0�dkrdt�d
�d}n|0d}|,�r�t	dr}d|�d�|+d<d|+d<|+S|r�||'|(|)|*d�}1tddhi|1��}2|2r�|2d}n|}td||r�|n|fid��d|�d|�d|�d|	�d|
�d|�d|�d |
�d!|�d"|�d#|�d$|�d%|�d&|�d'|�d(|�d)|�d*|�d+|�d,|�d-|�d.|�d/|�d0|�d1|�d2|�d3| �d4|!�d5|'�d6|(�d7|)�d8|*��}3|3�rd9|3v�r#d|+d<d:�
|�r|n|�|+d<|+S|3d9}iid;�|+d<<d|+d<d=d9<||+d<d>d9<|&�rt	d�rPd?|+d<d|+d<|+S|
�sV|�rXd@nd}4tdA|4|'|(|)|*dB�}3|3�srd|+d<dC|+d<|+S|3dD}%t�dE|3dF�nt�dG�|$�s�|%�rEdH}5dI}6t|5�D]%}7tdJ|$|%|'|(|)|*dK�}3|3�r�nt�dL|5|6|$�p�|%�t
�|6��q�|3�s�d|+d<dM�
|$�p�|%�|+d<|+S|3ddF}8|3d�d9��r�|3dd9|k�r�d|+d<dN�
|$�r�|$n|%|3dd9�|+d<|+SnKt	d�r
dO|�dP�|+d<d|+d<|+StdQ||$|%|'|(|)|*dR�}3|3�r3d>|+d<v�r*i|+d<d><|8|+d<d>dF<nd|+d<dS�
|�r>|n|�|+d<|+S|"�r�|"��D]�\}9}:tdT|9||'|(|)|*dU�};t|;t��sdin|;};|;�|9�|:k�rp�qLt	d�r�dV�
|9|;�|9�|:�|.|9<�qLztdW|9|:||'|(|)|*dX�}3Wn$t�y�}<zd|+d<dY�
|9|:|�|+d<|+WYd}<~<Sd}<~<ww|+d<�r�|+d<niid;�|+d<<|;�|9�|+d<d=|9<|:|+d<d>|9<�qLt	d�r�|.�r�|.|+d<d><d|+d<ndZ�
|�r�|n|�|+d<d|+d<��r1|du�r1t���ttd[d\|i|'|(|)|*d]��|i���t����}=t����}>t|=|>�}?�fd^d_�|>|=D�}@��fd`d_��D�}A|@�|A�|@�sP|?�r1t	d�r�d=|+d<v�rb|+d<d=ni|+d<d=<d>|+d<v�ru|+d<d>ni|+d<d><�|+d<d=d<�|+d<d>d<|+dda�
|�r�|n|�7<|+S|?�r�tdb||?|'|(|)|*dc��s�dd�
|�r�|n|�}Bt�|B�|+dde|B7<d|+d<|+S|@�r�tdf||@|'|(|)|*dc��s�dg�
|�r�|n|�}Bt�|B�|+dde|B7<d|+d<|+Sd=|+d<v�r|+d<d=ni|+d<d=<d>|+d<v�r|+d<d>ni|+d<d><�|+d<d=d<�|+d<d>d<|+S)iu�
    Ensure an EC2 instance is running with the given attributes and state.

    name
        (string) - The name of the state definition.  Recommended that this
        match the instance_name attribute (generally the FQDN of the instance).
    instance_name
        (string) - The name of the instance, generally its FQDN.  Exclusive with
        'instance_id'.
    instance_id
        (string) - The ID of the instance (if known).  Exclusive with
        'instance_name'.
    image_id
        (string) – The ID of the AMI image to run.
    image_name
        (string) – The name of the AMI image to run.
    tags
        (dict) - Tags to apply to the instance.
    key_name
        (string) – The name of the key pair with which to launch instances.
    security_groups
        (list of strings) – The names of the EC2 classic security groups with
        which to associate instances
    user_data
        (string) – The Base64-encoded MIME user data to be made available to the
        instance(s) in this reservation.
    instance_type
        (string) – The EC2 instance size/type.  Note that only certain types are
        compatible with HVM based AMIs.
    placement
        (string) – The Availability Zone to launch the instance into.
    kernel_id
        (string) – The ID of the kernel with which to launch the instances.
    ramdisk_id
        (string) – The ID of the RAM disk with which to launch the instances.
    vpc_id
        (string) - The ID of a VPC to attach the instance to.
    vpc_name
        (string) - The name of a VPC to attach the instance to.
    monitoring_enabled
        (bool) – Enable detailed CloudWatch monitoring on the instance.
    subnet_id
        (string) – The ID of the subnet within which to launch the instances for
        VPC.
    subnet_name
        (string) – The name of the subnet within which to launch the instances
        for VPC.
    private_ip_address
        (string) – If you’re using VPC, you can optionally use this parameter to
        assign the instance a specific available IP address from the subnet
        (e.g., 10.0.0.25).
    block_device_map
        (boto.ec2.blockdevicemapping.BlockDeviceMapping) – A BlockDeviceMapping
        data structure describing the EBS volumes associated with the Image.
    disable_api_termination
        (bool) – If True, the instances will be locked and will not be able to
        be terminated via the API.
    instance_initiated_shutdown_behavior
        (string) – Specifies whether the instance stops or terminates on
        instance-initiated shutdown. Valid values are:

        - 'stop'
        - 'terminate'

    placement_group
        (string) – If specified, this is the name of the placement group in
        which the instance(s) will be launched.
    client_token
        (string) – Unique, case-sensitive identifier you provide to ensure
        idempotency of the request. Maximum 64 ASCII characters.
    security_group_ids
        (list of strings) – The IDs of the VPC security groups with which to
        associate instances.
    security_group_names
        (list of strings) – The names of the VPC security groups with which to
        associate instances.
    additional_info
        (string) – Specifies additional information to make available to the
        instance(s).
    tenancy
        (string) – The tenancy of the instance you want to launch. An instance
        with a tenancy of ‘dedicated’ runs on single-tenant hardware and can
        only be launched into a VPC. Valid values are:”default” or “dedicated”.
        NOTE: To use dedicated tenancy you MUST specify a VPC subnet-ID as well.
    instance_profile_arn
        (string) – The Amazon resource name (ARN) of the IAM Instance Profile
        (IIP) to associate with the instances.
    instance_profile_name
        (string) – The name of the IAM Instance Profile (IIP) to associate with
        the instances.
    ebs_optimized
        (bool) – Whether the instance is optimized for EBS I/O. This
        optimization provides dedicated throughput to Amazon EBS and a tuned
        configuration stack to provide optimal EBS I/O performance. This
        optimization isn’t available with all instance types.
    network_interfaces
        (boto.ec2.networkinterface.NetworkInterfaceCollection) – A
        NetworkInterfaceCollection data structure containing the ENI
        specifications for the instance.
    network_interface_name
         (string) - The name of Elastic Network Interface to attach

        .. versionadded:: 2016.11.0

    network_interface_id
         (string) - The id of Elastic Network Interface to attach

        .. versionadded:: 2016.11.0

    attributes
        (dict) - Instance attributes and value to be applied to the instance.
        Available options are:

        - instanceType - A valid instance type (m1.small)
        - kernel - Kernel ID (None)
        - ramdisk - Ramdisk ID (None)
        - userData - Base64 encoded String (None)
        - disableApiTermination - Boolean (true)
        - instanceInitiatedShutdownBehavior - stop|terminate
        - blockDeviceMapping - List of strings - ie: [‘/dev/sda=false’]
        - sourceDestCheck - Boolean (true)
        - groupSet - Set of Security Groups or IDs
        - ebsOptimized - Boolean (false)
        - sriovNetSupport - String - ie: ‘simple’

    target_state
        (string) - The desired target state of the instance.  Available options
        are:

        - running
        - stopped

        Note that this option is currently UNIMPLEMENTED.
    public_ip:
        (string) - The IP of a previously allocated EIP address, which will be
        attached to the instance.  EC2 Classic instances ONLY - for VCP pass in
        an allocation_id instead.
    allocation_id:
        (string) - The ID of a previously allocated EIP address, which will be
        attached to the instance.  VPC instances ONLY - for Classic pass in
        a public_ip instead.
    allocate_eip:
        (bool) - Allocate and attach an EIP on-the-fly for this instance.  Note
        you'll want to release this address when terminating the instance,
        either manually or via the 'release_eip' flag to 'instance_absent'.
    region
        (string) - Region to connect to.
    key
        (string) - Secret key to be used.
    keyid
        (string) - Access key to be used.
    profile
        (variable) - A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.

    .. versionadded:: 2016.3.0
    TrrF��pendingZ	rebooting�runningZstopping�stoppedz7Exactly one of image_id OR image_name must be provided.zHAt most one of public_ip, allocation_id OR allocate_eip may be provided.zboto_ec2.exists)r<rrr r!�	in_states�boto_ec2.find_instances�r
rrr r!rt�zTMultiple instances matching criteria found - cannot determine a singular instance-idNrr�
The instance rrr)rirrr r!rjzboto_ec2.run�tags�key_name�security_groups�	user_data�
instance_type�	placement�	kernel_id�
ramdisk_idrb�vpc_name�monitoring_enabledr.r/r0�block_device_map�disable_api_termination�$instance_initiated_shutdown_behavior�placement_group�client_token�security_group_ids�security_group_names�additional_info�tenancy�instance_profile_arn�instance_profile_name�
ebs_optimized�network_interfaces�network_interface_namer?rrr r!r<zFailed to create instance {}.r\rrrzNew EIP would be allocated.r6r7r8zFailed to allocate new EIP.r;z"New EIP with address %s allocated.r>zEIP not requested.�
��boto_ec2.get_eip_address_info)�	addressesZallocation_idsrrr r!z8Waiting up to %s secs for new EIP %s to become availablezFailed to lookup EIP {}.z.EIP {} is already associated with instance {}.�	Instance z to be updated.r:)r<r>r;rrr r!z$Failed to attach EIP to instance {}.�boto_ec2.get_attribute�r<rrr r!zAThe instance attribute {} is set to be changed from '{}' to '{}'.zboto_ec2.set_attribute)Z	attributeZattribute_valuer<rrr r!z0Failed to set attribute {} to {} on instance {}.z#Instance {} is in the correct statezboto_ec2.get_all_tagszresource-id)�filtersrrr r!csi|]}|�|�qSrr�r_�t)ryrr	�
<dictcomp>Vsz$instance_present.<locals>.<dictcomp>cs*i|]}��|���|�kr|�|�qSr)�getr��Z	curr_tagsryrr	r�Ws*z'  Tags would be updated on instance {}.zboto_ec2.delete_tags)Zresource_idsryrrr r!z(Error while deleting tags on instance {}z  zboto_ec2.create_tagsz(Error while creating tags on instance {}r)rErFrGrHrr�lenrrrrL�info�rangerrr��itemsrI�dict�set�keysrJrPr+)Cr
r=r<Zimage_idZ
image_nameryrzr{r|r}r~rr�rbr�r�r.r/r0r�r�r�r�r�r�r�r�r�r�r�r�r�r�r?Z
attributesZtarget_stater>r;rSrrr r!r"�_create�running_statesZ
changed_attrsr#�	instances�argsZ	image_idsrTr9ZtriesZsecsr��ip�k�v�currr$�currentZdesired�remove�add�replacerVrr�r	�instance_present~s�L�����
���

�������	�
���
������������������� �!�"�#�%�

�
������
	�	��
�
�	����	�

���	�


�
��-���
��

�
�r�c	
Cs�|ddid�}	d}
|s@ztd|r|n||||||
|d�}Wn!ty?}zd|	d<d	�|p/|�|	d
<|	WYd}~Sd}~wwtd|||||d|d�}|s]d|	d<d
|�d�|	d
<|	S|d}
tdd|||||d�}|�d�dur�d|	d<d�|�|	d
<|	Stdr�d|�d�|	d
<d|	d<|	Std||||||d�}|s�d|	d<d|�d�|	d
<|	Sd|i|	dd<d|	dd<|�r]t|
d d�}|�r]||||d!�}d}d}d}t|
d"d��rtd#d/d$|i|��}|r�d%|dvr�|dd%}|d�d&�}nd|	d<d'|�d�|	d
<|	S|
j}|�r$td(d/d&|i|���s$t�d)|�td*d/||d+�|���rOt�	d,|�p<|dd-�|�pF|dd-|	ddd-<|	Sd|	d<d.|�d�|	d
<|	S|	S)0a�
    Ensure an EC2 instance does not exist (is stopped and removed).

    .. versionchanged:: 2016.11.0

    name
        (string) - The name of the state definition.
    instance_name
        (string) - The name of the instance.
    instance_id
        (string) - The ID of the instance.
    release_eip
        (bool)   - Release any associated EIPs during termination.
    region
        (string) - Region to connect to.
    key
        (string) - Secret key to be used.
    keyid
        (string) - Access key to be used.
    profile
        (variable) - A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.
    filters
        (dict) - A dict of additional filters to use in matching the instance to
        delete.

    YAML example fragment:

    .. code-block:: yaml

        - filters:
            vpc-id: vpc-abcdef12
    Trrrp�boto_ec2.get_id)r
rrr r!rtr�Nrz1Couldn't determine current status of instance {}.rru)r<rrr r!rkr�r�z is already gone.rr�ZdisableApiTerminationr�Fz3Termination of instance {} via the API is disabled.rrxr'zboto_ec2.terminate)r<r
rrr r!zFailed to terminate instance rDr<rrr�
ip_address�rrr r!rbr�r�r;Zassociation_idz)Can't determine AllocationId for address z!boto_ec2.disassociate_eip_addresszFailed to disassociate EIP %s.r@)r;r>zReleased EIP address %sr>zFailed to release EIP r)
rrrLr�r�getattrr�r�warningr�)r
r=r<rfrrr r!r�r"r�r$r��instanceZ	no_can_dorTr�Z	base_argsr>Zalloc_idZassoc_idrrr	�instance_absent�s�.

�	����	����
�����r�c
Cs�|ddid�}
i}d}tjj�||||f�std��|s|r$|s$td��|r-|�d|i�|r6|�d|i�|rOtd	|||||	|d
�}|sOd�|�|
d<|
S|rX|�d
|i�|ra|�d|i�||||	d�}
tdd&d|i|
��}t|�dkrd|
d<|
St|�dkr�d�|�}t	�
|�||
d<d|
d<|
S|d}t	�d|�tdr�d|�d�|
d<d|
d<|
Stdd&|dd�|
��r�d|�d �|
d<d!|id!did"�|
d#<|
Sd$|�d%�|
d<d|
d<|
S)'a�
    Ensure the EC2 volume is detached and absent.

    .. versionadded:: 2016.11.0

    name
        State definition name.

    volume_name
        Name tag associated with the volume.  For safety, if this matches more than
        one volume, the state will refuse to apply.

    volume_id
        Resource ID of the volume.

    instance_name
        Only remove volume if it is attached to instance with this Name tag.
        Exclusive with 'instance_id'.  Requires 'device'.

    instance_id
        Only remove volume if it is attached to this instance.
        Exclusive with 'instance_name'.  Requires 'device'.

    device
        Match by device rather than ID.  Requires one of 'instance_name' or
        'instance_id'.

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.

    Trrrpz^Exactly one of 'volume_name', 'volume_id', 'instance_name', or 'instance_id' must be provided.zYParameter 'device' is required when either 'instance_name' or 'instance_id' is specified.z	volume-id�tag:Namer�rvzCInstance with Name {} not found.  Assuming associated volumes gone.rzattachment.instance-idzattachment.devicer��boto_ec2.get_all_volumesr�rwz;Volume matching criteria not found, assuming already absentzAMore than one volume matched criteria, can't continue in state {}FrrzMatched Volume ID %srzThe volume r'Nzboto_ec2.delete_volume)�	volume_idre�Volume z	 deleted.r�r\rzError deleting volume rDr)
rErFrGrHrrPrrLr�rr+r�r)r
�volume_namer�r=r<�devicerrr r!r"r�r�r��volsrV�volrrr	�
volume_absent8s|6
�������
�r�c
Cs�|ddid�}||||||d�}tdrDd|d<tddi|��}	|	dr:|	�d	�r8d
|d<|	d	|d	<d|d
<|Sd|d<d|d
<|Stddi|��}	|	drb|	�d	�r`d|d<|	d	|d	<|Sd|d<d|d
<|S)a
    Ensure EC2 volume(s) matching the given filters have the defined tags.

    .. versionadded:: 2016.11.0

    name
        State definition name.

    tag_maps
        List of dicts of filters and tags, where 'filters' is a dict suitable for passing
        to the 'filters' argument of boto_ec2.get_all_volumes(), and 'tags' is a dict of
        tags to be set on volumes as matched by the given filters.  The filter syntax is
        extended to permit passing either a list of volume_ids or an instance_name (with
        instance_name being the Name tag of the instance to which the desired volumes are
        mapped).  Each mapping in the list is applied separately, so multiple sets of
        volumes can be all tagged differently with one call to this function.

    YAML example fragment:

    .. code-block:: yaml

        - filters:
            attachment.instance_id: i-abcdef12
          tags:
            Name: dev-int-abcdef12.aws-foo.com
        - filters:
            attachment.device: /dev/sdf
          tags:
            ManagedSnapshots: true
            BillingGroup: bubba.hotep@aws-foo.com
        - filters:
            instance_name: prd-foo-01.aws-foo.com
          tags:
            Name: prd-foo-01.aws-foo.com
            BillingGroup: infra-team@aws-foo.com
        - filters:
            volume_ids: [ vol-12345689, vol-abcdef12 ]
          tags:
            BillingGroup: infra-team@aws-foo.com

    authoritative
        Should un-declared tags currently set on matched volumes be deleted?  Boolean.

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.

    Trr)�tag_maps�
authoritativerrr r!r�dry_run�boto_ec2.set_volumes_tags�successrzTags would be updated.rNrz'Error validating requested volume tags.Fz
Tags applied.z%Error updating requested volume tags.r)rrr�)
r
r�r�rrr r!r"r�rTrrr	�volumes_tagged�s:=�	
�
�r�cCs�|ddid�}i}i}d}tjj�||f�std��tjj�||f�s'td��|dur/td��||
||d	�}|rNtd
d;||d�|��}|sNtd|�d
���tdd;|dd�|��}|d}|r�i}|�d|i�tdd;d|i|��}t|�dkr�d�||�}t|��t|�dkr�t	dr�d�|||�|d<d|d<|Stdd;|j
||||	|
|dd�|��}d|vr�|d}ntd|�d���tdd;dd |gid!|id"�gi|��}|d#d$ur�td%�|||d���d|d&<||d&<n|d}tdd;|gdd'�|��}t|�dk�rtd(|�d)���|d}|j|j
k�r&td*�||j||j
���|j}|du�r�|j
du�r�||j
k�rK||jk�rKd+�|||�|d<|St	d�rcd,�|j
|j|||�|d<d|d<|Std-d;|dd.�|���r�d/�||j
|j�|d<|j
|d0<|j|d1<ntd2�||j
|j���||d0<d|d1<t	d�r�d3�|||�|d<d|d<|Std4d;|||d5�|���r�d6�|dd7�|||�g�|d<||d0<||d1<||d8�|d9<|Sd:�|||�|d<d$|d<|S)<a_	
    Ensure the EC2 volume is present and attached.

    ..

    name
        State definition name.

    volume_name
        The Name tag value for the volume. If no volume with that matching name tag is found,
        a new volume will be created. If multiple volumes are matched, the state will fail.

    volume_id
        Resource ID of the volume. Exclusive with 'volume_name'.

    instance_name
        Attach volume to instance with this Name tag.
        Exclusive with 'instance_id'.

    instance_id
        Attach volume to instance with this ID.
        Exclusive with 'instance_name'.

    device
        The device on the instance through which the volume is exposed (e.g. /dev/sdh)

    size
        The size of the new volume, in GiB. If you're creating the volume from a snapshot
        and don't specify a volume size, the default is the snapshot size. Optionally specified
        at volume creation time; will be ignored afterward. Requires 'volume_name'.

    snapshot_id
        The snapshot ID from which the new Volume will be created. Optionally specified
        at volume creation time; will be ignored afterward. Requires 'volume_name'.

    volume_type
        The type of the volume. Optionally specified at volume creation time; will be ignored afterward.
        Requires 'volume_name'.
        Valid volume types for AWS can be found here:
        http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html

    iops
        The provisioned IOPS you want to associate with this volume. Optionally specified
        at volume creation time; will be ignored afterward. Requires 'volume_name'.

    encrypted
        Specifies whether the volume should be encrypted. Optionally specified
        at volume creation time; will be ignored afterward. Requires 'volume_name'.

    kms_key_id
        If encrypted is True, this KMS Key ID may be specified to encrypt volume with this key.
        Optionally specified at volume creation time; will be ignored afterward.
        Requires 'volume_name'.
        e.g.: arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456a-a12b-a123b4cd56ef

    region
        Region to connect to.

    key
        Secret key to be used.

    keyid
        Access key to be used.

    profile
        A dict with region, key and keyid, or a pillar key (string)
        that contains a dict with region, key and keyid.

    Trr)rrrsz=Exactly one of 'volume_name', 'volume_id',  must be provided.zBExactly one of 'instance_name', or 'instance_id' must be provided.NzParameter 'device' is required.r�r�)r
rtzInstance with Name rru)r<rkrr�r�r�rwzGMore than one volume matched volume name {}, can't continue in state {}rzDThe volume with name {} is set to be created and attached on {}({}).rrzboto_ec2.create_volume)Z	zone_name�size�snapshot_id�volume_type�iops�	encrypted�
kms_key_idZwait_for_creationz Error creating volume with name rDr�r��
volume_ids�Name)r�ryr�Fz3Error updating requested volume {} with name {}. {}r�)r�rkr�z
 do not existz3Volume {} in {} cannot attach to instance {} in {}.z$The volume {} is attached on {}({}).zFThe volume {} is set to be detached from {}({} and attached on {}({}).zboto_ec2.detach_volume)r�Zwait_for_detachementz"Volume {} is detached from {}({}).r<r�zFThe volume {} is already attached on instance {}({}). Failed to detachz.The volume {} is set to be attached on {}({}).zboto_ec2.attach_volume)r�r<r�rz Volume {} is attached on {}({}).r\rz-Error attaching volume {} to instance {}({}).r)rErFrGrHrrrPr�rLrr~�zone�attach_datar<r�ZdevicrM)r
r�r�r=r<r�r�r�r�r�r�r�rrr r!r"Zold_dictZnew_dictr�r�r�r�r�r�rVZ_rtr�r�rrr	�volume_presents8X�������
��	�

�
����	
��
�����
��	��
�

��
��������r�c	Cs`tjj�||f�s
td��|std��|ddggd�d�}	||||||d�}
tddi|
��}|rN|�d	i��d
�rN|d	d
D]
}|	dd�|d
�q@g}
|D]}||	ddvra|
�|�qR|
�rtds�||
|||||d�}tddi|��tddi|
��}|r�|�d	i��d
d�r�|d	d
D]
}|	dd�|d
�q�g}|D]}||	ddvr�|�|�q�|r�d|	d	<d�	dd�
|	dd�dd�
|
�dd�
|��|	d<nd�	dd�
|
��|	d<|	dd|	ddkr�i|	d<|	Sd�	dd�
|	dd�dd�
|
��|	d<i|	d<d|	d	<|	Sd�	dd�
|	dd��|	d<i|	d<|	S)ax
    Ensure an ENI has secondary private ip addresses associated with it

    name
        (String) - State definition name
    network_interface_id
        (String) - The EC2 network interface id, example eni-123456789
    private_ip_addresses
        (List or String) - The secondary private ip address(es) that should be present on the ENI.
    allow_reassignment
        (Boolean) - If true, will reassign a secondary private ip address associated with another
        ENI. If false, state will fail if the secondary private ip address is associated with
        another ENI.
    region
        (string) - Region to connect to.
    key
        (string) - Secret key to be used.
    keyid
        (string) - Access key to be used.
    profile
        (variable) - A dict with region, key and keyid, or a pillar key (string) that contains a
        dict with region, key and keyid.
    �PExactly one of 'network_interface_name', 'network_interface_id' must be providedzCYou must provide the private_ip_addresses to associate with the ENITrr\r�r
r?rrr r!r)r�private_ip_addressesrrr0r)r?r��allow_reassignmentrrr r!z$boto_ec2.assign_private_ip_addressesNrFzHips on eni: {}
attempted to add: {}
could not add the following ips: {}
�
	- rz
added ips: {}z+ips on eni: {}
ips that would be added: {}
zips on eni: {}r)rErFrGrHrrr��appendrrLrM)r
r�r?r�r�rrr r!r"�get_eni_args�eni�eni_pipZ
ips_to_add�
private_ip�assign_ips_argsZ
ips_not_addedrrr	�private_ips_presents�#����	
��

���
���
��r�c	Cs�tjj�||f�s
td��|std��t|t�s|g}|ddggd�d�}||||||d�}	tddi|	��}
d	}|
r`|
�d
i��d�r`|
d
dD]}|dd
�	|d�|dr_|d}qJg}
|D]6}||dd
vrs|
�	|�||kr�d|d
<d�
|dd�|dd
�dd�|��|d<i|d<|Sqd|
�rUtd�s5||
||||d�}tddi|��tddi|	��}
|
r�|
�d
i��dd	�r�|
d
dD]
}|dd�	|d�q�g}|D]}||ddvr�|�	|�q�|�rd|d
<d�
dd�|dd�dd�|
�dd�|��|d<nd�
dd�|
��|d<|dd
|ddk�r3i|d<|Sd�
dd�|dd
�dd�|
��|d<i|d<d	|d
<|Sd�
dd�|dd
��|d<i|d<|S)a�
    Ensure an ENI does not have secondary private ip addresses associated with it

    name
        (String) - State definition name
    network_interface_id
        (String) - The EC2 network interface id, example eni-123456789
    private_ip_addresses
        (List or String) - The secondary private ip address(es) that should be absent on the ENI.
    region
        (string) - Region to connect to.
    key
        (string) - Secret key to be used.
    keyid
        (string) - Access key to be used.
    profile
        (variable) - A dict with region, key and keyid, or a pillar key (string) that contains a
        dict with region, key and keyid.
    r�zEYou must provide the private_ip_addresses to unassociate with the ENITr)rrrr�r)Nrr�rrr0ZprimaryFziYou cannot unassign the primary private ip address ({}) on an eni
ips on eni: {}
attempted to remove: {}
r�rr)r?r�rrr r!z&boto_ec2.unassign_private_ip_addressesrzNips on eni: {}
attempted to remove: {}
could not remove the following ips: {}
zremoved ips: {}z-ips on eni: {}
ips that would be removed: {}
zips on network interface: {}r)
rErFrGrHrrIrJrr�r�rLrMr)r
r�r?r�rrr r!r"r�r�Zprimary_private_ipr�Z
ips_to_remover�r�Zips_not_removedrrr	�private_ips_absent�s���
��	�
��
�
�	
���
���
��r�)NNNNNN)NNNN)NNNNNTNNNNNN)FNNNN)Trh)*NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNFNNNN)NNFNNNNN)	NNNNNNNNN)NNNNNNNNNFNNNNN)NNNFNNNN)NNNNNNN)�__doc__�loggingrrZsalt.utils.datarEZsalt.utils.dictupdaterFrOZsalt.exceptionsrr�	getLogger�__name__rr
r&r(rWrNrQrgror�r�r�r�r�r�r�rrrr	�<module>s4

�
<
�*
�g
�6
�
�'
�
�`
�z
��