def delete(ctx, **_): '''Deletes an AWS API Gateway REST API Deployment''' props = ctx.node.properties # Get a connection to the service client = APIGatewayConnection(ctx.node).client() # Get the parent API api_resource_id = props.get( 'api_id', utils.get_ancestor_resource_id(ctx.instance, NODE_TYPE_API)) # Get the resource ID (must exist at this point) resource_id = utils.get_resource_id() if not resource_id: ctx.logger.warn('Missing resource ID. Skipping workflow...') return # Delete the resource (if needed) if props['use_external_resource']: return if ctx.operation.retry_number == 0: stages = client.get_stages(restApiId=api_resource_id, deploymentId=resource_id) # There are stages left that must be removed if stages and 'item' in stages and len(stages['item']): for item in stages['item']: ctx.logger.debug('Removing deployment stage "%s"' % item['stageName']) client.delete_stage(restApiId=api_resource_id, stageName=item['stageName']) ctx.logger.info('Deleting Deployment "%s"' % resource_id) try: client.delete_deployment(restApiId=api_resource_id, deploymentId=resource_id) ctx.logger.debug('Deployment "%s" deleted' % resource_id) except ClientError as exc: raise RecoverableError('Error deleting Deployment: %s' % str(exc))
def configure(ctx, **_): '''Configures an AWS API Gateway REST API''' props = ctx.node.properties if 'import' not in props or not props['import']: ctx.logger.debug('Skipping API import') return # Get a connection to the service client = APIGatewayConnection(ctx.node).client() # Get the resource ID (must exist at this point) resource_id = utils.get_resource_id(raise_on_missing=True) # Import data params = dict(restApiId=resource_id, mode=props.get('import_mode', 'overwrite')) if isinstance(ctx.node.properties['import'], dict): ctx.logger.debug('Importing data from YAML definition') params['body'] = json.dumps(ctx.node.properties['import']) elif isinstance(ctx.node.properties['import'], str): ctx.logger.debug('Importing data from file') params['body'] = open(ctx.node.properties['import'], 'rb') else: raise NonRecoverableError( 'Invalid import data format. Expected either dict or str.') ctx.logger.debug('Importing REST API data') resource = client.put_rest_api(**params) ctx.logger.debug('Response: %s' % resource)
def delete(ctx, **_): '''Deletes an AWS API Gateway REST API''' # Get a connection to the service client = APIGatewayConnection(ctx.node).client() # Get the resource ID (must exist at this point) resource_id = utils.get_resource_id() if not resource_id: ctx.logger.warn('Missing resource ID. Skipping workflow...') return # Delete the resource (if needed) if ctx.node.properties['use_external_resource']: return if ctx.operation.retry_number == 0: ctx.logger.info('Deleting REST API "%s"' % resource_id) try: client.delete_rest_api(restApiId=resource_id) ctx.logger.debug('REST API "%s" deleted' % resource_id) except ClientError as exc: raise RecoverableError('Error deleting REST API: %s' % str(exc))
def create(ctx, **_): '''Creates an AWS API Gateway REST API Deployment''' props = ctx.node.properties # Get a connection to the service client = APIGatewayConnection(ctx.node).client() # Get the parent API api_resource_id = props.get( 'api_id', utils.get_ancestor_resource_id(ctx.instance, NODE_TYPE_API)) # Check if we are creating something or not if not props['use_external_resource']: # Actually create the resource ctx.logger.info('Creating Deployment') # Parameters params = dict(restApiId=api_resource_id, stageName=props['name'], stageDescription=props.get('description', '')) resource = client.create_deployment(**params) ctx.logger.debug('Response: %s' % resource) utils.update_resource_id(ctx.instance, resource['id']) # Get the resource ID (must exist at this point) resource_id = utils.get_resource_id(raise_on_missing=True) # Get the resource ctx.logger.debug('Getting Deployment "%s" properties' % resource_id) try: resource = client.get_deployment(restApiId=api_resource_id, deploymentId=resource_id) stages = client.get_stages(restApiId=api_resource_id, deploymentId=resource_id) if not stages or 'item' not in stages or not len(stages['item']): raise NonRecoverableError('No stages found for deployment!') ctx.logger.debug('Deployment "%s": %s' % (resource_id, resource)) ctx.instance.runtime_properties['stage_name'] = \ stages['item'][0]['stageName'] ctx.instance.runtime_properties['invoke_url'] = \ 'https://%s.execute-api.%s.amazonaws.com/%s' % ( api_resource_id, client._client_config.region_name, stages['item'][0]['stageName']) except ClientError: raise NonRecoverableError('Error creating Deployment')
def create(ctx, **_): '''Creates an AWS API Gateway REST API''' props = ctx.node.properties # Get a connection to the service client = APIGatewayConnection(ctx.node).client() # Check if we are creating something or not if not props['use_external_resource']: # Actually create the resource ctx.logger.info('Creating REST API') resource = client.create_rest_api(**dict( name=props['name'], description=props['description'], version=props['version'])) ctx.logger.debug('Response: %s' % resource) utils.update_resource_id(ctx.instance, resource['id']) # Get the resource ID (must exist at this point) resource_id = utils.get_resource_id(raise_on_missing=True) # Get the resource ctx.logger.debug('Getting REST API "%s" properties' % resource_id) try: resource = client.get_rest_api(restApiId=resource_id) ctx.logger.debug('REST API "%s": %s' % (resource_id, resource)) except ClientError: raise NonRecoverableError('Error creating REST API')