def create_method(self, restid, resourceid, method, authorizationtype, apikeyreq=False, further_opts=None): """ This function creates a method object :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param authorizationtype: :type authorizationtype: basestring :param apikeyreq: should apikey be required :type apikeyreq: bool :param further_opts: This opt passes in json_data fur not mandatory options :type further_opts: dict :return: the created method object """ if self.dryrun: logger.info("Dryrun requested no changes will be done") return None if isinstance(apikeyreq, bool) is False: logger.debug("apikey is not boolean, converting") apikeyreq = Misc.str2bool(apikeyreq) opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'authorizationType': authorizationtype, 'apiKeyRequired': apikeyreq} if 'requestParameters' in further_opts: opts['requestParameters'] = further_opts['requestParameters'] if 'requestModels' in further_opts: opts['requestModels'] = further_opts['requestModels'] logger.debug("The opts sent to create method %s" % opts) resp = self.apigateway_client.put_method(**opts) super(Apigateway, self).query_information(query=resp) return resp
def test_str2bool_false(self): self.assertFalse(Misc.str2bool("false")) self.assertFalse(Misc.str2bool("False")) self.assertFalse(Misc.str2bool("f")) self.assertFalse(Misc.str2bool("0")) self.assertFalse(Misc.str2bool("no")) self.assertFalse(Misc.str2bool("ILIKECARS"))
def get_all_subnets(self, filters=None, subnetids=None): """ This function returns all subnets, or filters them as requested :param filters: A dict list with the boto3 filters :param subnetids: A list of subnetids that should only be returned :return: A list of subnets that were requested """ if subnetids: response = self.vpc_client.describe_subnets(SubnetIds=subnetids) elif filters: response = self.vpc_client.describe_subnets(Filters=filters) else: response = self.vpc_client.describe_subnets() result = [] for s in response['Subnets']: allowed = Misc.get_value_from_array_hash(dictlist=s.get('Tags'), key="Allowed") if Misc.str2bool(allowed): result.append(s) logger.debug("Allowed az subnets are: %s" % (result,)) return result
def get_all_subnets(self, filters=None, subnetids=None): """ This function returns all subnets, or filters them as requested :param filters: A dict list with the boto3 filters :param subnetids: A list of subnetids that should only be returned :return: A list of subnets that were requested """ if subnetids: response = self.vpc_client.describe_subnets(SubnetIds=subnetids) elif filters: response = self.vpc_client.describe_subnets(Filters=filters) else: response = self.vpc_client.describe_subnets() result = [] for s in response['Subnets']: allowed = Misc.get_value_from_array_hash(dictlist=s.get('Tags'), key="Allowed") if Misc.str2bool(allowed): result.append(s) logger.debug("Allowed az subnets are: %s" % (result, )) return result
def test_str2bool_true(self): self.assertTrue(Misc.str2bool("true")) self.assertTrue(Misc.str2bool("True")) self.assertTrue(Misc.str2bool("t")) self.assertTrue(Misc.str2bool("1")) self.assertTrue(Misc.str2bool("yes"))
def create_ec2_instance(self, puppet_role, env, requester, customer, xively_service, base_ami, iam, instance_type, dry_run, shutdown, monitoring, fillup, num, keypair, availability=None): """ This function creates an ec2 instance :param puppet_role: the Puppet_role that should be used :param env: the environment where we should provision to :param requester: the user/team requesting the machine :param customer: For future use only :param xively_service: the Xively_service that should be used :param base_ami: the base_ami that should be used. Can default to puppet_role :param iam: The iam role that should be attached, defaults to ec2-base :param instance_type: the type of instance requested :param dry_run: No changes should be done :param shutdown: The shutdown behavior to use :param monitoring: Should monitoring be enabled :param fillup: Should fillup algorithym be used or round robin :param num: the number of instances :return: a list of instance objects """ from wrapper.ec2 import Ec2 from wrapper.vpc import Vpc from misc import Misc from core.stackdata import stackdata stackdata_object = stackdata(session=self.session) ec2 = Ec2(session=self.session) vpc = Vpc(session=self.session) lambda_function_args = { 'env': env, 'puppet_role': puppet_role, 'requester': requester, 'xively_service': xively_service, 'customer': customer, 'shutdown': shutdown, 'dry_run': dry_run } stack_data = stackdata_object.get_stack_data( puppet_role=puppet_role, xively_service=xively_service) vpc_obj = vpc.get_vpc_from_env(env=env) ## Find the baseami object that needs to be used if base_ami: base_ami = base_ami elif 'ami' in stack_data: base_ami = stack_data['ami'] else: logger.info("Falling back to puppet_role as AMI name") base_ami = puppet_role logger.info("The baseami that is going to be used: %s" % (base_ami, )) baseami_object = self.get_ami_from_tag(puppet_role=base_ami) ## Get values for lambda function lambda_function_args['baseamiid'] = baseami_object.get('ImageId') if (availability == None): availability = Misc.get_value_from_array_hash( dictlist=baseami_object.get('Tags'), key='Availability') lambda_function_args['ostype'] = Misc.get_value_from_array_hash( dictlist=baseami_object.get('Tags'), key='Os') if keypair is not None: lambda_function_args['keypair'] = keypair else: lambda_function_args['keypair'] = Misc.get_value_from_array_hash( dictlist=vpc_obj.get('Tags'), key='Keypair') ## Find the instance_type that needs to be used if instance_type: inst_type_final = instance_type elif 'instance_type' in stack_data and env in stack_data[ 'instance_type']: inst_type_final = stack_data['instance_type'][env] else: inst_type_final = Misc.get_value_from_array_hash( dictlist=baseami_object.get('Tags'), key='Instancetype') logger.info("Instance type that will be used: %s" % (inst_type_final, )) lambda_function_args['instance_type'] = inst_type_final ## Find the instance profile that needs to be used if iam: iam_name = iam elif 'iam' in stack_data: iam_name = "%s-%s" % (env, stack_data['iam']['name_postfix']) else: iam_name = "ec2-base" logger.info("Base iam instance profile name: %s" % (iam_name, )) lambda_function_args['iam'] = iam_name ## Find value for ebsoptimized if 'ebsoptimized' in stack_data and env in stack_data['ebsoptimized']: lambda_function_args['ebsoptimized'] = Misc.str2bool( stack_data['ebsoptimized'][env]) else: lambda_function_args['ebsoptimized'] = False ## Find value for monitoring enablement if monitoring: lambda_function_args['monitoring'] = monitoring elif 'monitoring' in stack_data and env in stack_data['monitoring']: lambda_function_args['monitoring'] = Misc.str2bool( stack_data['monitoring'][env]) else: lambda_function_args['monitoring'] = False ## Generate instance names for all required instances lambda_function_args['instance_name'] = ec2.generate_ec2_unique_name( env=env, puppet_role=puppet_role, num=num) ## Gather all security groups needed for creating an instance stack lambda_function_args[ 'securitygroup'] = ec2.get_security_group_ids_for_stack( vpcid=vpc_obj.get('VpcId'), puppet_role=puppet_role, ostype=lambda_function_args['ostype'], xively_service=xively_service) # We need to retrieve the subnets from Vpc object, and pass it to Ec2 object subnets = vpc.get_all_subnets(filters=[{ 'Name': 'tag:Network', 'Values': [availability] }, { 'Name': 'vpc-id', 'Values': [vpc_obj.get('VpcId')] }]) lambda_function_args['subnet'] = ec2.get_subnet_with_algorithym( puppet_role=puppet_role, subnets=subnets, num=num, fillup=fillup, xively_service=xively_service) instances = Misc.parallel_map_reduce( lambda x: self.create_instance_lamdba(args=lambda_function_args), lambda x, y: x + [y], xrange(0, num), []) return instances