def create(**_): """Creates an EC2 security group. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) name = utils.get_resource_id() if _create_external_securitygroup(name): return ctx.logger.debug( 'Creating Security Group: {0}' .format(name)) create_args = dict( name=name, description=ctx.node.properties['description'], vpc_id=_get_connected_vpc() ) try: group_object = ec2_client.create_security_group(**create_args) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) _create_group_rules(group_object) utils.set_external_resource_id( group_object.id, ctx.instance, external=False)
def create(**kwargs): """Creates a keypair.""" ec2_client = connection.EC2ConnectionClient().client() if _create_external_keypair(): return key_pair_name = utils.get_resource_id() ctx.logger.debug('Attempting to create key pair.') try: kp = ec2_client.create_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Key pair not created. {0}'.format(str(e))) utils.set_external_resource_id( kp.name, ctx.instance, external=False) _save_key_pair(kp) ctx.instance.runtime_properties[constants.AWS_TYPE_PROPERTY] = \ KEYPAIR_AWS_TYPE
def test_utils_get_resource_id_dynamic(self): ctx = self.mock_ctx( 'test_utils_get_resource_id') current_ctx.set(ctx=ctx) ctx.node.properties['resource_id'] = '' resource_id = utils.get_resource_id() self.assertEquals('None-test_utils_get_resource_id', resource_id)
def test_utils_get_resource_id_from_key_path(self): ctx = self.mock_ctx( 'test_utils_get_resource_id_from_key_path') current_ctx.set(ctx=ctx) private_key_path = tempfile.mkdtemp() ctx.node.properties['private_key_path'] = \ '{0}/test_utils_get_resource_id_from_key_path.pem' \ .format(private_key_path) resource_id = utils.get_resource_id() self.assertEquals( 'test_utils_get_resource_id_from_key_path', resource_id)
def creation_validation(**_): """ This validates all EBS volume Nodes before bootstrap. """ for property_key in constants.VOLUME_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) volume_object = _get_volumes_from_id(utils.get_resource_id()) if ctx.node.properties['use_external_resource'] and not volume_object: raise NonRecoverableError('External resource, but the supplied ' 'EBS volume does not exist in the account.') if not ctx.node.properties['use_external_resource'] and volume_object: raise NonRecoverableError('Not external resource, but the supplied ' 'EBS volume exists in the account.')
def creation_validation(**_): """ This validates all Security Group Nodes before bootstrap. """ for property_key in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) security_group = _get_security_group_from_id(utils.get_resource_id()) if ctx.node.properties['use_external_resource'] and not security_group: raise NonRecoverableError( 'External resource, but the supplied ' 'security group does not exist in the account.') if not ctx.node.properties['use_external_resource'] and security_group: raise NonRecoverableError('Not external resource, but the supplied ' 'security group exists in the account.')
def creation_validation(**_): """ This validates all EBS volume Nodes before bootstrap. """ for property_key in constants.VOLUME_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) volume_object = _get_volumes_from_id(utils.get_resource_id()) if ctx.node.properties['use_external_resource'] and not volume_object: raise NonRecoverableError( 'External resource, but the supplied ' 'EBS volume does not exist in the account.') if not ctx.node.properties['use_external_resource'] and volume_object: raise NonRecoverableError( 'Not external resource, but the supplied ' 'EBS volume exists in the account.')
def create(**_): """Creates an EC2 security group. """ ec2_client = connection.EC2ConnectionClient().client() for property_name in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_name, ctx.node.properties) name = utils.get_resource_id() if _create_external_securitygroup(name): return ctx.logger.debug( 'Creating Security Group: {0}' .format(name)) create_args = dict( name=name, description=ctx.node.properties['description'], vpc_id=_get_connected_vpc() ) if ctx.operation.retry_number == 0 and constants.EXTERNAL_RESOURCE_ID \ not in ctx.instance.runtime_properties: try: security_group = ec2_client.create_security_group(**create_args) except (exception.EC2ResponseError, exception.BotoServerError) as e: raise NonRecoverableError('{0}'.format(str(e))) utils.set_external_resource_id( security_group.id, ctx.instance, external=False) security_group = _get_security_group_from_id( ctx.instance.runtime_properties[constants.EXTERNAL_RESOURCE_ID]) if not security_group: return ctx.operation.retry( message='Waiting to verify that security group {0} ' 'has been added.'.format(constants.EXTERNAL_RESOURCE_ID)) _create_group_rules(security_group)
def creation_validation(**_): """ This validates all Security Group Nodes before bootstrap. """ for property_key in constants.SECURITY_GROUP_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) security_group = _get_security_group_from_id( utils.get_resource_id()) if ctx.node.properties['use_external_resource'] and not security_group: raise NonRecoverableError( 'External resource, but the supplied ' 'security group does not exist in the account.') if not ctx.node.properties['use_external_resource'] and security_group: raise NonRecoverableError( 'Not external resource, but the supplied ' 'security group exists in the account.')
def creation_validation(**_): """ This checks that all user supplied info is valid """ for property_key in constants.INSTANCE_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) instance = _get_instance_from_id(utils.get_resource_id()) if ctx.node.properties["use_external_resource"] and not instance: raise NonRecoverableError("External resource, but the supplied " "instance id is not in the account.") if not ctx.node.properties["use_external_resource"] and instance: raise NonRecoverableError("Not external resource, but the supplied " "but the instance already exists.") image_id = ctx.node.properties["image_id"] image_object = _get_image(image_id) if "available" not in image_object.state: raise NonRecoverableError("image_id {0} not available to this account.".format(image_id))
def create(**kwargs): """Creates a keypair.""" ec2_client = connection.EC2ConnectionClient().client() if _create_external_keypair(): return key_pair_name = utils.get_resource_id() ctx.logger.debug('Attempting to create key pair.') try: kp = ec2_client.create_key_pair(key_pair_name) except (boto.exception.EC2ResponseError, boto.exception.BotoServerError, boto.exception.BotoClientError) as e: raise NonRecoverableError('Key pair not created. {0}'.format(str(e))) utils.set_external_resource_id(kp.name, ctx.instance, external=False) _save_key_pair(kp)
def creation_validation(**_): """ This checks that all user supplied info is valid """ for property_key in constants.INSTANCE_REQUIRED_PROPERTIES: utils.validate_node_property(property_key, ctx.node.properties) instance = _get_instance_from_id(utils.get_resource_id()) if ctx.node.properties['use_external_resource'] and not instance: raise NonRecoverableError('External resource, but the supplied ' 'instance id is not in the account.') if not ctx.node.properties['use_external_resource'] and instance: raise NonRecoverableError('Not external resource, but the supplied ' 'but the instance already exists.') image_id = ctx.node.properties['image_id'] image_object = _get_image(image_id) if 'available' not in image_object.state: raise NonRecoverableError( 'image_id {0} not available to this account.'.format(image_id))