<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto3
from orcalib.aws_config import AwsConfig
from orcalib.aws_config import OrcaConfig


class AwsServiceEC2(object):
    '''
    The class provides a simpler abstraction to the AWS boto3
    EC2 client interface
    '''
    def __init__(self,
                 profile_names=None,
                 access_key_id=None,
                 secret_access_key=None):
        '''
        Create a EC2 service client to one ore more environments by name.
        '''
        service = 'ec2'

        orca_config = OrcaConfig()
        self.regions = orca_config.get_regions()
        self.clients = {}

        if profile_names is not None:
            for profile_name in profile_names:
                session = boto3.Session(profile_name=profile_name)
                self.clients[profile_name] = session.client(service)
        elif access_key_id is not None and secret_access_key is not None:
            self.clients['default'] = boto3.client(
                service,
                aws_access_key_id=access_key_id,
                aws_secret_access_key=secret_access_key)
        else:
            awsconfig = AwsConfig()
            profiles = awsconfig.get_profiles()

            for profile in profiles:
                session = boto3.Session(profile_name=profile)
                self.clients[profile] = {}
                for region in self.regions:
                    self.clients[profile][region] = \
                        session.client(service, region_name=region)

    def list_vms(self, profile_names=None, regions=None):
        '''
        Return all the vms.

        :type profile_names: List of Strings
        :param profile_names: List of profiles. If Not set then get list
            of buckets from all profiles/environments.

        '''
        vm_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue

                vms = self.clients[profile][region].describe_instances()
                for vm in vms['Reservations']:
                    vm['region'] = region
                    vm['profile_name'] = profile
                    vm_list.append(vm)

        return vm_list

    def list_reserved_vms(self, profile_names=None, regions=None):
        '''
        Return reserved vms.

        :type profile_names: List of Strings
        :param profile_names: List of profiles. If Not set then get list
            of reserved vms from all profiles/environments.

        '''
        vm_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue

                vms = self.clients[profile][region].\
                    describe_reserved_instances()
                for vm in vms['ReservedInstances']:
                    vm['region'] = region
                    vm['profile_name'] = profile
                    vm_list.append(vm)

        return vm_list

    def list_network_interfaces(self, profile_names=None, regions=None):

        '''

        :param profile_names: List of Strings
        :param regions: List of profiles
        :return:
        '''

        nw_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue
                nws = self.clients[profile][region].\
                    describe_network_interfaces()
                for nw in nws['NetworkInterfaces']:
                    nw['region'] = region
                    nw['profile_name'] = profile
                    nw_list.append(nw)

        return nw_list

    def list_images(self, profile_names=None, regions=None):

        '''

        :param profile_names: List of Strings
        :param regions: List of profiles
        :return:
        '''

        image_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue
                images = self.clients[profile][region].\
                    describe_images()
                for image in images['Images']:
                    image['region'] = region
                    image['profile_name'] = profile
                    image_list.append(image)

        return image_list

    def list_volumes(self, profile_names=None, regions=None):
        vol_list = []
        for profile in self.clients.keys():
            if profile_names is not None and profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and region not in regions:
                    continue

                vols = self.clients[profile][region].describe_volumes()

                for vol in vols['Volumes']:
                    vol['region'] = region
                    vol['profile_name'] = profile
                    vol_list.append(vol)

        return vol_list
<commit_msg>Add `list_snapshots` for EC2 service<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto3
from orcalib.aws_config import AwsConfig
from orcalib.aws_config import OrcaConfig


class AwsServiceEC2(object):
    '''
    The class provides a simpler abstraction to the AWS boto3
    EC2 client interface
    '''
    def __init__(self,
                 profile_names=None,
                 access_key_id=None,
                 secret_access_key=None):
        '''
        Create a EC2 service client to one ore more environments by name.
        '''
        service = 'ec2'

        orca_config = OrcaConfig()
        self.regions = orca_config.get_regions()
        self.clients = {}

        if profile_names is not None:
            for profile_name in profile_names:
                session = boto3.Session(profile_name=profile_name)
                self.clients[profile_name] = session.client(service)
        elif access_key_id is not None and secret_access_key is not None:
            self.clients['default'] = boto3.client(
                service,
                aws_access_key_id=access_key_id,
                aws_secret_access_key=secret_access_key)
        else:
            awsconfig = AwsConfig()
            profiles = awsconfig.get_profiles()

            for profile in profiles:
                session = boto3.Session(profile_name=profile)
                self.clients[profile] = {}
                for region in self.regions:
                    self.clients[profile][region] = \
                        session.client(service, region_name=region)

    def list_vms(self, profile_names=None, regions=None):
        '''
        Return all the vms.

        :type profile_names: List of Strings
        :param profile_names: List of profiles. If Not set then get list
            of buckets from all profiles/environments.

        '''
        vm_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue

                vms = self.clients[profile][region].describe_instances()
                for vm in vms['Reservations']:
                    vm['region'] = region
                    vm['profile_name'] = profile
                    vm_list.append(vm)

        return vm_list

    def list_reserved_vms(self, profile_names=None, regions=None):
        '''
        Return reserved vms.

        :type profile_names: List of Strings
        :param profile_names: List of profiles. If Not set then get list
            of reserved vms from all profiles/environments.

        '''
        vm_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue

                vms = self.clients[profile][region].\
                    describe_reserved_instances()
                for vm in vms['ReservedInstances']:
                    vm['region'] = region
                    vm['profile_name'] = profile
                    vm_list.append(vm)

        return vm_list

    def list_network_interfaces(self, profile_names=None, regions=None):

        '''

        :param profile_names: List of Strings
        :param regions: List of profiles
        :return:
        '''

        nw_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue
                nws = self.clients[profile][region].\
                    describe_network_interfaces()
                for nw in nws['NetworkInterfaces']:
                    nw['region'] = region
                    nw['profile_name'] = profile
                    nw_list.append(nw)

        return nw_list

    def list_images(self, profile_names=None, regions=None):

        '''

        :param profile_names: List of Strings
        :param regions: List of profiles
        :return:
        '''

        image_list = []
        for profile in self.clients.keys():
            if profile_names is not None and \
                    profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and \
                        region not in regions:
                    continue
                images = self.clients[profile][region].\
                    describe_images()
                for image in images['Images']:
                    image['region'] = region
                    image['profile_name'] = profile
                    image_list.append(image)

        return image_list

    def list_volumes(self, profile_names=None, regions=None):
        vol_list = []
        for profile in self.clients.keys():
            if profile_names is not None and profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and region not in regions:
                    continue

                vols = self.clients[profile][region].describe_volumes()

                for vol in vols['Volumes']:
                    vol['region'] = region
                    vol['profile_name'] = profile
                    vol_list.append(vol)

        return vol_list

    def list_snapshots(self, profile_names=None, regions=None):
        snapshots = list()
        for profile in self.clients.keys():
            if profile_names is not None and profile not in profile_names:
                continue
            for region in self.regions:
                if regions is not None and region not in regions:
                    continue

                snaps = self.clients[profile][region].describe_snapshots()

                for s in snaps['Snapshots']:
                    s['region'] = region
                    s['profile_name'] = profile
                    snapshots.append(s)

        return snapshots

{'fraction_non_alphanumeric': 0.07150436699044976, 'fraction_numerical': 0.0017141457840176312, 'mean_word_length': 1.4523618895116093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '623544', 'n_tokens_mistral': 3157, 'n_tokens_neox': 2917, 'n_words': 883}