<commit_before># Copyright (c) 2006-2008 <NAME> http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

"""
Represents a connection to the EC2 service.
"""

import urllib
import xml.sax
import base64
import boto
from boto import config
from boto.connection import AWSQueryConnection
from boto.resultset import ResultSet
from boto.ec2.image import Image, ImageAttribute
from boto.ec2.instance import Reservation, Instance, ConsoleOutput
from boto.ec2.keypair import KeyPair
from boto.ec2.address import Address
from boto.ec2.volume import Volume
from boto.ec2.snapshot import Snapshot
from boto.ec2.zone import Zone
from boto.ec2.securitygroup import SecurityGroup
from boto.ec2.regioninfo import RegionInfo
from boto.ec2.instanceinfo import InstanceInfo
from boto.ec2.reservedinstance import ReservedInstancesOffering, ReservedInstance
from boto.exception import EC2ResponseError

class EC2Connection(AWSQueryConnection):

    APIVersion = boto.config.get('Boto', 'ec2_version', '2009-04-04')
    DefaultRegionName = boto.config.get('Boto', 'ec2_region_name', 'us-east-1')
    DefaultRegionEndpoint = boto.config.get('Boto', 'ec2_region_endpoint',
                                            'us-east-1.ec2.amazonaws.com')
    SignatureVersion = '2'
    ResponseError = EC2ResponseError

    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, host=None, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/'):
        """
        Init method to create a new connection to EC2.
        
        B{Note:} The host argument is overridden by the host specified in the boto configuration file.        
        """
        if not region:
            region = RegionInfo(self, self.DefaultRegionName, self.DefaultRegionEndpoint)
        self.region = region
        AWSQueryConnection.__init__(self, aws_access_key_id, aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port, proxy_user, proxy_pass,
                                    self.region.endpoint, debug, https_connection_factory, path)

    def get_params(self):
        """
        Returns a dictionary containing the value of of all of the keyword
        arguments passed when constructing this connection.
        """
        param_names = ['aws_access_key_id', 'aws_secret_access_key', 'is_secure',
                       'port', 'proxy', 'proxy_port', 'proxy_user', 'proxy_pass',
                       'debug', 'https_connection_factory']
        params = {}
        for name in param_names:
            params[name] = getattr(self, name)
        return params

    # Image methods
        
    def get_all_images(self, image_ids=None, owners=None, executable_by=None):
        """
        Retrieve all the EC2 images available on your account.
        
        @type image_ids: list
        @param image_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @type executable_by: 
        @param executable_by: 
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        params = {}
        if image_ids:
            self.build_list_params(params, image_ids, 'ImageId')
        if owners:
            self.build_list_params(params, owners, 'Owner')
        if executable_by:
            self.build_list_params(params, executable_by, 'ExecutableBy')
        return self.get_list('DescribeImages', params, [('item', Image)])

    def get_all_kernels(self, kernel_ids=None, owners=None):
        """
        Retrieve all the EC2 kernels available on your account.  Simply filters the list returned
        by get_all_images because EC2 does not provide a way to filter server-side.
        
        @type kernel_ids: list
        @param kernel_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        rs = self.get_all_images(kernel_ids, owners)
        kernels = []
        for image in rs:
            if image.type == 'kernel':
                kernels.append(image)
        return kernels

    def get_all_ramdisks(self, ramdisk_ids=None, owners=None):
        """
        Retrieve all the EC2 ramdisks available on your account.  Simply filters the list returned
        by get_all_images because EC2 does not provide a way to filter server-side.
        
        @type ramdisk_ids: list
        @param ramdisk_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        rs = self.get_all_images(ramdisk_ids, owners)
        ramdisks = []
        for image in rs:
            if image.type == 'ramdisk':
                ramdisks.append(image)
        return ramdisks

    def get_image(self, image_id):
        """
        Shortcut method to retrieve a specific image (AMI).
        
        @type image_id: string
        @param image_id: the ID of the Image to retrieve
        
        @rtype: L{Image<boto.ec2.image.Image>}
        @return: The EC2 Image specified or None if the image is not found
        """
        try:
            return self.get_all_images(image_ids=[image_id])[0]
        except IndexError: # None of those images available
            return None

    def register_image(self, image_location):
        """
        Register an image.

        @type image_location: string
        @param image_location: Full path to your AMI manifest in Amazon S3 storage.

        @rtype: string
        @return: The new image id
        """
        params = {'ImageLocation':image_location}
        rs = self.get_object('RegisterImage', params, ResultSet)
        return rs.imageId
        
    def deregister_image(self, image_id):
        """
        Unregister an AMI.
        
        @type image_id: string
        @param image_id: the ID of the Image to unregister
        
        @rtype: bool
        @return: True if successful
        """
        return self.get_status('DeregisterImage', {'ImageId':image_id})
        
    # ImageAttribute methods
        
    def get_image_attribute(self, image_id, attribute='launchPermission'):
        """
        Gets an attribute from an image.
        See http://docs.amazonwebservices.com/AWSEC2/2008-02-01/DeveloperGuide/ApiReference-Query-DescribeImageAttribute.html
        
        @type image_id: string
        @param image_id: The Amazon image id for which you want info about
        
        @type attribute: string
        @param attribute: The attribute you need information about
        
        @rtype: L{ImageAttribute<boto.ec2.image.ImageAttribute>}
        @return: An ImageAttribute object representing the value of the attribute requested
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute}
        return self.get_object('DescribeImageAttribute', params, ImageAttribute)
        
    def modify_image_attribute(self, image_id, attribute='launchPermission',
                               operation='add', user_ids=None, groups=None,
                               product_codes=None):
        """
        Changes an attribute of an image.
        See http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-ModifyImageAttribute.html
        
        @type image_id: string
        @param image_id: The image id you wish to change
        
        @type attribute: string
        @param attribute: The attribute you wish to change
        
        @type operation: string
        @param operation: Either add or remove (this is required for changing launchPermissions)
        
        @type user_ids: list
        @param user_ids: The Amazon IDs of users to add/remove attributes
        
        @type groups: list
        @param groups: The groups to add/remove attributes

        @type product_codes: list
        @param product_codes: Amazon DevPay product code. Currently only one
                              product code can be associated with an AMI. Once
                              set, the product code cannot be changed or reset.
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute,
                  'OperationType' : operation}
        if user_ids:
            self.build_list_params(params, user_ids, 'UserId')
        if groups:
            self.build_list_params(params, groups, 'UserGroup')
        if product_codes:
            self.build_list_params(params, product_codes, 'ProductCode')
        return self.get_status('ModifyImageAttribute', params)

    def reset_image_attribute(self, image_id, attribute='launchPermission'):
        """
        Resets an attribute of an AMI to its default value.
        See http://docs.amazonwebservices.com/AWSEC2/2008-02-01/DeveloperGuide/ApiReference-Query-ResetImageAttribute.html
        
        @type image_id: string
        @param image_id: ID of the AMI for which an attribute will be described
        
        @type attribute: string
        @param attribute: The attribute to reset
        
        @rtype: bool
        @return: Whether the operation succeeded or not
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute}
        return self.get_status('ResetImageAttribute', params)
        
    # Instance methods
        
    def get_all_instances(self, instance_ids=None):
        """
        Retrieve all the instances associated with your account.
        
        @type instance_ids: list
        @param instance_ids: A list of strings of instance IDs
        
        @rtype: list
        @return: A list of L{Instances<boto.ec2.instance.Instance>}
        """
        params = {}
        if instance_ids:
            self.build_list_params(params, instance_ids, 'InstanceId')
        return self.get_list('DescribeInstances', params, [('item', Reservation)])

    def run_instances(self, image_id, min_count=1, max_count=1,
                      key_name=None, security_groups=None,
                      user_data=None, addressing_type=None,
                      instance_type='m1.small', placement=None,
                      kernel_id=None, ramdisk_id=None):
        """<commit_msg>Fixed return type in doc string.  Fixes issue 237.<commit_after># Copyright (c) 2006-2008 <NAME> http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

"""
Represents a connection to the EC2 service.
"""

import urllib
import xml.sax
import base64
import boto
from boto import config
from boto.connection import AWSQueryConnection
from boto.resultset import ResultSet
from boto.ec2.image import Image, ImageAttribute
from boto.ec2.instance import Reservation, Instance, ConsoleOutput
from boto.ec2.keypair import KeyPair
from boto.ec2.address import Address
from boto.ec2.volume import Volume
from boto.ec2.snapshot import Snapshot
from boto.ec2.zone import Zone
from boto.ec2.securitygroup import SecurityGroup
from boto.ec2.regioninfo import RegionInfo
from boto.ec2.instanceinfo import InstanceInfo
from boto.ec2.reservedinstance import ReservedInstancesOffering, ReservedInstance
from boto.exception import EC2ResponseError

class EC2Connection(AWSQueryConnection):

    APIVersion = boto.config.get('Boto', 'ec2_version', '2009-04-04')
    DefaultRegionName = boto.config.get('Boto', 'ec2_region_name', 'us-east-1')
    DefaultRegionEndpoint = boto.config.get('Boto', 'ec2_region_endpoint',
                                            'us-east-1.ec2.amazonaws.com')
    SignatureVersion = '2'
    ResponseError = EC2ResponseError

    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, host=None, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/'):
        """
        Init method to create a new connection to EC2.
        
        B{Note:} The host argument is overridden by the host specified in the boto configuration file.        
        """
        if not region:
            region = RegionInfo(self, self.DefaultRegionName, self.DefaultRegionEndpoint)
        self.region = region
        AWSQueryConnection.__init__(self, aws_access_key_id, aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port, proxy_user, proxy_pass,
                                    self.region.endpoint, debug, https_connection_factory, path)

    def get_params(self):
        """
        Returns a dictionary containing the value of of all of the keyword
        arguments passed when constructing this connection.
        """
        param_names = ['aws_access_key_id', 'aws_secret_access_key', 'is_secure',
                       'port', 'proxy', 'proxy_port', 'proxy_user', 'proxy_pass',
                       'debug', 'https_connection_factory']
        params = {}
        for name in param_names:
            params[name] = getattr(self, name)
        return params

    # Image methods
        
    def get_all_images(self, image_ids=None, owners=None, executable_by=None):
        """
        Retrieve all the EC2 images available on your account.
        
        @type image_ids: list
        @param image_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @type executable_by: 
        @param executable_by: 
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        params = {}
        if image_ids:
            self.build_list_params(params, image_ids, 'ImageId')
        if owners:
            self.build_list_params(params, owners, 'Owner')
        if executable_by:
            self.build_list_params(params, executable_by, 'ExecutableBy')
        return self.get_list('DescribeImages', params, [('item', Image)])

    def get_all_kernels(self, kernel_ids=None, owners=None):
        """
        Retrieve all the EC2 kernels available on your account.  Simply filters the list returned
        by get_all_images because EC2 does not provide a way to filter server-side.
        
        @type kernel_ids: list
        @param kernel_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        rs = self.get_all_images(kernel_ids, owners)
        kernels = []
        for image in rs:
            if image.type == 'kernel':
                kernels.append(image)
        return kernels

    def get_all_ramdisks(self, ramdisk_ids=None, owners=None):
        """
        Retrieve all the EC2 ramdisks available on your account.  Simply filters the list returned
        by get_all_images because EC2 does not provide a way to filter server-side.
        
        @type ramdisk_ids: list
        @param ramdisk_ids: A list of strings with the image IDs wanted
        
        @type owners: list
        @param owners: A list of owner IDs
        
        @rtype: list
        @return: A list of L{Images<boto.ec2.image.Image>}
        """
        rs = self.get_all_images(ramdisk_ids, owners)
        ramdisks = []
        for image in rs:
            if image.type == 'ramdisk':
                ramdisks.append(image)
        return ramdisks

    def get_image(self, image_id):
        """
        Shortcut method to retrieve a specific image (AMI).
        
        @type image_id: string
        @param image_id: the ID of the Image to retrieve
        
        @rtype: L{Image<boto.ec2.image.Image>}
        @return: The EC2 Image specified or None if the image is not found
        """
        try:
            return self.get_all_images(image_ids=[image_id])[0]
        except IndexError: # None of those images available
            return None

    def register_image(self, image_location):
        """
        Register an image.

        @type image_location: string
        @param image_location: Full path to your AMI manifest in Amazon S3 storage.

        @rtype: string
        @return: The new image id
        """
        params = {'ImageLocation':image_location}
        rs = self.get_object('RegisterImage', params, ResultSet)
        return rs.imageId
        
    def deregister_image(self, image_id):
        """
        Unregister an AMI.
        
        @type image_id: string
        @param image_id: the ID of the Image to unregister
        
        @rtype: bool
        @return: True if successful
        """
        return self.get_status('DeregisterImage', {'ImageId':image_id})
        
    # ImageAttribute methods
        
    def get_image_attribute(self, image_id, attribute='launchPermission'):
        """
        Gets an attribute from an image.
        See http://docs.amazonwebservices.com/AWSEC2/2008-02-01/DeveloperGuide/ApiReference-Query-DescribeImageAttribute.html
        
        @type image_id: string
        @param image_id: The Amazon image id for which you want info about
        
        @type attribute: string
        @param attribute: The attribute you need information about
        
        @rtype: L{ImageAttribute<boto.ec2.image.ImageAttribute>}
        @return: An ImageAttribute object representing the value of the attribute requested
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute}
        return self.get_object('DescribeImageAttribute', params, ImageAttribute)
        
    def modify_image_attribute(self, image_id, attribute='launchPermission',
                               operation='add', user_ids=None, groups=None,
                               product_codes=None):
        """
        Changes an attribute of an image.
        See http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-ModifyImageAttribute.html
        
        @type image_id: string
        @param image_id: The image id you wish to change
        
        @type attribute: string
        @param attribute: The attribute you wish to change
        
        @type operation: string
        @param operation: Either add or remove (this is required for changing launchPermissions)
        
        @type user_ids: list
        @param user_ids: The Amazon IDs of users to add/remove attributes
        
        @type groups: list
        @param groups: The groups to add/remove attributes

        @type product_codes: list
        @param product_codes: Amazon DevPay product code. Currently only one
                              product code can be associated with an AMI. Once
                              set, the product code cannot be changed or reset.
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute,
                  'OperationType' : operation}
        if user_ids:
            self.build_list_params(params, user_ids, 'UserId')
        if groups:
            self.build_list_params(params, groups, 'UserGroup')
        if product_codes:
            self.build_list_params(params, product_codes, 'ProductCode')
        return self.get_status('ModifyImageAttribute', params)

    def reset_image_attribute(self, image_id, attribute='launchPermission'):
        """
        Resets an attribute of an AMI to its default value.
        See http://docs.amazonwebservices.com/AWSEC2/2008-02-01/DeveloperGuide/ApiReference-Query-ResetImageAttribute.html
        
        @type image_id: string
        @param image_id: ID of the AMI for which an attribute will be described
        
        @type attribute: string
        @param attribute: The attribute to reset
        
        @rtype: bool
        @return: Whether the operation succeeded or not
        """
        params = {'ImageId' : image_id,
                  'Attribute' : attribute}
        return self.get_status('ResetImageAttribute', params)
        
    # Instance methods
        
    def get_all_instances(self, instance_ids=None):
        """
        Retrieve all the instances associated with your account.
        
        @type instance_ids: list
        @param instance_ids: A list of strings of instance IDs
        
        @rtype: list
        @return: A list of  L{Reservation<boto.ec2.instance.Reservation>}
        """
        params = {}
        if instance_ids:
            self.build_list_params(params, instance_ids, 'InstanceId')
        return self.get_list('DescribeInstances', params, [('item', Reservation)])

    def run_instances(self, image_id, min_count=1, max_count=1,
                      key_name=None, security_groups=None,
                      user_data=None, addressing_type=None,
                      instance_type='m1.small', placement=None,
                      kernel_id=None, ramdisk_id=None):
        """
{'fraction_non_alphanumeric': 0.07703907966737042, 'fraction_numerical': 0.0068507906415614635, 'mean_word_length': 2.3067388516882747, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7353617', 'n_tokens_mistral': 6323, 'n_tokens_neox': 6157, 'n_words': 2030}