Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Commit

Permalink
Support path to tagging resources during start and removing tags duri…
Browse files Browse the repository at this point in the history
…ng stop

update plugin yaml for base ec2 type with tags
  • Loading branch information
earthmant committed Oct 18, 2018
1 parent ed683d6 commit 69b28f2
Show file tree
Hide file tree
Showing 39 changed files with 226 additions and 125 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2.7.0: Add Tag property/runtime property/operation input for EC2 resource node templates (without the previously only available Tagging node type): Customer Gateway, Volumes, Elastic IPs, Elastic Network Interfaces, Instances, Internet Gateways, Keypairs, NAT Gateways, Network ACLs, Route Tables, Security Groups, Subnets, VPCs, VPC Peerings, and VPN Gateways.
2.6.0:
- Add Modify VPC Attribute
- Add Modify Subnet Attribute
Expand Down
48 changes: 43 additions & 5 deletions cloudify_awssdk/common/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def wrapper_outer(function):
def wrapper_inner(**kwargs):
'''Inner, worker function'''
ctx = kwargs['ctx']
_, _, _, operation_name = ctx.operation.name.split('.')
props = ctx.node.properties
runtime_instance_properties = ctx.instance.runtime_properties

# Override the resource ID if needed
resource_id = kwargs.get(EXT_RES_ID)
if resource_id and not \
Expand Down Expand Up @@ -178,11 +178,11 @@ def wrapper_inner(**kwargs):
kwargs['resource_config'] =\
runtime_instance_properties['resource_config']
resource_config = kwargs['resource_config']

resource_id = utils.get_resource_id(
node=ctx.node,
instance=ctx.instance)
# Check if using external
if ctx.node.properties.get('use_external_resource', False):
resource_id = utils.get_resource_id(
node=ctx.node, instance=ctx.instance)
ctx.logger.info('%s ID# "%s" is user-provided.'
% (resource_type, resource_id))
if not kwargs.get('force_operation', False):
Expand All @@ -192,7 +192,6 @@ def wrapper_inner(**kwargs):
# Set "resource_config" and "EXT_RES_ID"
ctx.instance.runtime_properties[
'resource_config'] = resource_config
_, _, _, operation_name = ctx.operation.name.split('.')
ctx.instance.runtime_properties[EXT_RES_ID] = resource_id
if operation_name not in ['delete', 'create'] and \
not kwargs['iface'].verify_resource_exists():
Expand Down Expand Up @@ -361,3 +360,42 @@ def wrapper(**kwargs):

return func(**kwargs)
return wrapper


def tag_resources(fn):
def wrapper(**kwargs):
result = fn(**kwargs)
ctx = kwargs.get('ctx')
iface = kwargs.get('iface')
resource_id = utils.get_resource_id(
node=ctx.node,
instance=ctx.instance)
tags = utils.get_tags_list(
ctx.node.properties.get('Tags'),
ctx.instance.runtime_properties.get('Tags'),
kwargs.get('Tags'))
if iface and tags and resource_id:
iface.tag({
'Tags': tags,
'Resources': [resource_id]})
return result
return wrapper


def untag_resources(fn):
def wrapper(**kwargs):
ctx = kwargs.get('ctx')
iface = kwargs.get('iface')
resource_id = utils.get_resource_id(
node=ctx.node,
instance=ctx.instance)
tags = utils.get_tags_list(
ctx.node.properties.get('Tags'),
ctx.instance.runtime_properties.get('Tags'),
kwargs.get('Tags'))
if iface and tags and resource_id:
iface.untag({
'Tags': tags,
'Resources': [resource_id]})
return fn(**kwargs)
return wrapper
2 changes: 1 addition & 1 deletion cloudify_awssdk/common/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_mock_ctx(self,
ctx_operation_name=None):

operation_ctx = {
'retry_number': 0
'retry_number': 0, 'name': 'cloudify.interfaces.lifecycle.'
} if not ctx_operation_name else {
'retry_number': 0, 'name': ctx_operation_name
}
Expand Down
11 changes: 11 additions & 0 deletions cloudify_awssdk/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,14 @@ def generate_swift_access_config(auth_url, username, password):
# client
token = resp.headers['X-Auth-Token']
return endpoint_url, token


def get_tags_list(node_prop, runtime_prop, input_prop):
tags_list = []
if isinstance(node_prop, list):
tags_list = node_prop
if isinstance(runtime_prop, list):
tags_list = list(set(tags_list + runtime_prop))
if isinstance(input_prop, list):
tags_list = list(set(tags_list + input_prop))
return tags_list
14 changes: 14 additions & 0 deletions cloudify_awssdk/ec2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ def create(self, params):
def delete(self, params=None):
"""Deletes a resource"""
raise NotImplementedError()

def tag(self, params):
"""Creates a resource"""
self.logger.info('Tagging %s.' % params)
res = self.client.create_tags(**params)
self.logger.debug('Response: %s' % res)
return res

def untag(self, params):
"""Creates a resource"""
self.logger.info('Untagging %s.' % params)
res = self.client.delete_tags(**params)
self.logger.debug('Response: %s' % res)
return res
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/customer_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def prepare(ctx, resource_config, **_):
@decorators.aws_resource(EC2CustomerGateway, RESOURCE_TYPE)
@decorators.wait_for_status(status_good=['available'],
status_pending=['pending'])
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 Customer Gateway"""

Expand Down Expand Up @@ -117,6 +118,7 @@ def create(ctx, iface, resource_config, **_):
ignore_properties=True)
@decorators.wait_for_delete(status_deleted=['deleted'],
status_pending=['deleting'])
@decorators.untag_resources
def delete(iface, resource_config, **_):
"""Deletes an AWS EC2 Customer Gateway"""

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/ebs.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def prepare(ctx, resource_config, **_):

@decorators.aws_resource(EC2Volume, RESOURCE_TYPE_VOLUME)
@decorators.wait_for_status(status_good=[AVAILABLE], status_pending=[CREATING])
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""
Creates an AWS EC2 EBS Volume
Expand Down Expand Up @@ -184,6 +185,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2Volume, RESOURCE_TYPE_VOLUME,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
"""
Deletes an AWS EC2 EBS Volume
Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/elasticip.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def prepare(ctx, resource_config, **_):


@decorators.aws_resource(EC2ElasticIP, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 ElasticIP"""

Expand All @@ -125,6 +126,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2ElasticIP, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
"""Deletes an AWS EC2 ElasticIP"""

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def prepare(ctx, resource_config, **_):

@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE)
@decorators.wait_for_status(status_good=['available'])
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 NetworkInterface"""

Expand Down Expand Up @@ -178,6 +179,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
"""Deletes an AWS EC2 NetworkInterface"""

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def prepare(ctx, iface, resource_config, **_):
@decorators.wait_for_status(
status_good=[RUNNING, PENDING],
fail_on_missing=False)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates AWS EC2 Instances'''

Expand Down Expand Up @@ -350,6 +351,7 @@ def stop(ctx, iface, resource_config, **_):
@decorators.wait_for_delete(
status_deleted=[TERMINATED],
status_pending=[PENDING, STOPPING, SHUTTING_DOWN])
@decorators.untag_resources
def delete(iface, resource_config, **_):
'''Deletes AWS EC2 Instances'''

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/internet_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def prepare(ctx, resource_config, **_):


@decorators.aws_resource(EC2InternetGateway, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates an AWS EC2 Internet Gateway'''
params = dict() if not resource_config else resource_config.copy()
Expand All @@ -119,6 +120,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2InternetGateway, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(iface, resource_config, **_):
'''Deletes an AWS EC2 Internet Gateway'''
params = dict() if not resource_config else resource_config.copy()
Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def prepare(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2Keypair, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates AWS EC2 Keypairs'''

Expand Down Expand Up @@ -166,6 +167,7 @@ def create(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2Keypair, RESOURCE_TYPE)
@decorators.untag_resources
def delete(iface, resource_config, **_):
'''Deletes AWS EC2 Keypairs'''

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/nat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def prepare(ctx, resource_config, **_):
status_good=['available'],
status_pending=['pending'],
fail_on_missing=False)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 NAT Gateway"""

Expand Down Expand Up @@ -144,6 +145,7 @@ def create(ctx, iface, resource_config, **_):
@decorators.wait_for_delete(
status_deleted=['deleted'],
status_pending=['deleting'])
@decorators.untag_resources
def delete(iface, resource_config, **_):
"""Deletes an AWS EC2 NAT Gateway"""

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/networkacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def prepare(ctx, resource_config, **_):


@decorators.aws_resource(EC2NetworkAcl, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 NetworkAcl"""

Expand Down Expand Up @@ -149,6 +150,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2NetworkAcl, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
"""Deletes an AWS EC2 NetworkAcl"""

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/routetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def prepare(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2RouteTable, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates an AWS EC2 Route Table'''
params = dict() if not resource_config else resource_config.copy()
Expand Down Expand Up @@ -135,6 +136,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2RouteTable, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
'''Deletes an AWS EC2 Route Table'''
params = \
Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/securitygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def prepare(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2SecurityGroup, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates an AWS EC2 Security Group'''
params = \
Expand Down Expand Up @@ -170,6 +171,7 @@ def create(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2SecurityGroup, RESOURCE_TYPE)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
'''Deletes an AWS EC2 Security Group'''

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/subnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def prepare(ctx, iface, resource_config, **_):
@decorators.aws_resource(EC2Subnet, RESOURCE_TYPE)
@decorators.wait_for_status(status_good=['available'],
status_pending=['pending'])
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates an AWS EC2 Subnet'''
params = dict() if not resource_config else resource_config.copy()
Expand Down Expand Up @@ -152,6 +153,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2Subnet, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
'''Deletes an AWS EC2 Subnet'''
params = \
Expand Down
24 changes: 2 additions & 22 deletions cloudify_awssdk/ec2/resources/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,6 @@ def status(self):
'''Gets the status of an external resource'''
return None

def create(self, params):
'''
Create a new AWS EC2 Tags.
'''
self.logger.debug('Creating %s with parameters: %s'
% (self.type_name, params))
res = self.client.create_tags(**params)
self.logger.debug('Response: %s' % res)
return res

def delete(self, params=None):
'''
Deletes an existing AWS EC2 Tags.
'''
self.logger.debug('Deleting %s with parameters: %s'
% (self.type_name, params))
res = self.client.delete_tags(**params)
self.logger.debug('Response: %s' % res)
return res


@decorators.aws_resource(EC2Tags, resource_type=RESOURCE_TYPE)
def prepare(ctx, iface, resource_config, **_):
Expand All @@ -99,7 +79,7 @@ def create(ctx, iface, resource_config, **_):
params['Resources'] = resources

# Actually create the resource
create_response = iface.create(params)
create_response = iface.tag(params)
ctx.instance.runtime_properties['create_response'] = \
utils.JsonCleanuper(create_response).to_dict()

Expand All @@ -122,4 +102,4 @@ def delete(ctx, iface, resource_config, **_):
.get(EXTERNAL_RESOURCE_ID) for rel in targets]
params['Resources'] = resources

iface.delete(params)
iface.untag(params)
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def prepare(ctx, iface, resource_config, **_):
@decorators.aws_resource(EC2Vpc, RESOURCE_TYPE)
@decorators.wait_for_status(status_good=['available'],
status_pending=['pending'])
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
'''Creates an AWS EC2 Vpc'''
params = \
Expand All @@ -124,6 +125,7 @@ def create(ctx, iface, resource_config, **_):

@decorators.aws_resource(EC2Vpc, RESOURCE_TYPE,
ignore_properties=True)
@decorators.untag_resources
def delete(iface, resource_config, **_):
'''Deletes an AWS EC2 Vpc'''

Expand Down
2 changes: 2 additions & 0 deletions cloudify_awssdk/ec2/resources/vpc_peering.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def prepare(ctx, resource_config, **_):


@decorators.aws_resource(EC2VpcPeering, RESOURCE_TYPE)
@decorators.tag_resources
def create(ctx, iface, resource_config, **_):
"""Creates an AWS EC2 Vpc Peering"""
params = dict() if not resource_config else resource_config.copy()
Expand Down Expand Up @@ -182,6 +183,7 @@ def modify(ctx, iface, resource_config, **_):


@decorators.aws_resource(EC2VpcPeering, RESOURCE_TYPE)
@decorators.untag_resources
def delete(ctx, iface, resource_config, **_):
"""Deletes an AWS EC2 Vpc"""
deleted_params = dict()
Expand Down
Loading

0 comments on commit 69b28f2

Please sign in to comment.