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

Commit 69b28f2

Browse files
author
earthmant
committed
Support path to tagging resources during start and removing tags during stop
update plugin yaml for base ec2 type with tags
1 parent ed683d6 commit 69b28f2

39 files changed

+226
-125
lines changed

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
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.
12
2.6.0:
23
- Add Modify VPC Attribute
34
- Add Modify Subnet Attribute

cloudify_awssdk/common/decorators.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def wrapper_outer(function):
9797
def wrapper_inner(**kwargs):
9898
'''Inner, worker function'''
9999
ctx = kwargs['ctx']
100+
_, _, _, operation_name = ctx.operation.name.split('.')
100101
props = ctx.node.properties
101102
runtime_instance_properties = ctx.instance.runtime_properties
102-
103103
# Override the resource ID if needed
104104
resource_id = kwargs.get(EXT_RES_ID)
105105
if resource_id and not \
@@ -178,11 +178,11 @@ def wrapper_inner(**kwargs):
178178
kwargs['resource_config'] =\
179179
runtime_instance_properties['resource_config']
180180
resource_config = kwargs['resource_config']
181-
181+
resource_id = utils.get_resource_id(
182+
node=ctx.node,
183+
instance=ctx.instance)
182184
# Check if using external
183185
if ctx.node.properties.get('use_external_resource', False):
184-
resource_id = utils.get_resource_id(
185-
node=ctx.node, instance=ctx.instance)
186186
ctx.logger.info('%s ID# "%s" is user-provided.'
187187
% (resource_type, resource_id))
188188
if not kwargs.get('force_operation', False):
@@ -192,7 +192,6 @@ def wrapper_inner(**kwargs):
192192
# Set "resource_config" and "EXT_RES_ID"
193193
ctx.instance.runtime_properties[
194194
'resource_config'] = resource_config
195-
_, _, _, operation_name = ctx.operation.name.split('.')
196195
ctx.instance.runtime_properties[EXT_RES_ID] = resource_id
197196
if operation_name not in ['delete', 'create'] and \
198197
not kwargs['iface'].verify_resource_exists():
@@ -361,3 +360,42 @@ def wrapper(**kwargs):
361360

362361
return func(**kwargs)
363362
return wrapper
363+
364+
365+
def tag_resources(fn):
366+
def wrapper(**kwargs):
367+
result = fn(**kwargs)
368+
ctx = kwargs.get('ctx')
369+
iface = kwargs.get('iface')
370+
resource_id = utils.get_resource_id(
371+
node=ctx.node,
372+
instance=ctx.instance)
373+
tags = utils.get_tags_list(
374+
ctx.node.properties.get('Tags'),
375+
ctx.instance.runtime_properties.get('Tags'),
376+
kwargs.get('Tags'))
377+
if iface and tags and resource_id:
378+
iface.tag({
379+
'Tags': tags,
380+
'Resources': [resource_id]})
381+
return result
382+
return wrapper
383+
384+
385+
def untag_resources(fn):
386+
def wrapper(**kwargs):
387+
ctx = kwargs.get('ctx')
388+
iface = kwargs.get('iface')
389+
resource_id = utils.get_resource_id(
390+
node=ctx.node,
391+
instance=ctx.instance)
392+
tags = utils.get_tags_list(
393+
ctx.node.properties.get('Tags'),
394+
ctx.instance.runtime_properties.get('Tags'),
395+
kwargs.get('Tags'))
396+
if iface and tags and resource_id:
397+
iface.untag({
398+
'Tags': tags,
399+
'Resources': [resource_id]})
400+
return fn(**kwargs)
401+
return wrapper

cloudify_awssdk/common/tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def get_mock_ctx(self,
100100
ctx_operation_name=None):
101101

102102
operation_ctx = {
103-
'retry_number': 0
103+
'retry_number': 0, 'name': 'cloudify.interfaces.lifecycle.'
104104
} if not ctx_operation_name else {
105105
'retry_number': 0, 'name': ctx_operation_name
106106
}

cloudify_awssdk/common/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,14 @@ def generate_swift_access_config(auth_url, username, password):
444444
# client
445445
token = resp.headers['X-Auth-Token']
446446
return endpoint_url, token
447+
448+
449+
def get_tags_list(node_prop, runtime_prop, input_prop):
450+
tags_list = []
451+
if isinstance(node_prop, list):
452+
tags_list = node_prop
453+
if isinstance(runtime_prop, list):
454+
tags_list = list(set(tags_list + runtime_prop))
455+
if isinstance(input_prop, list):
456+
tags_list = list(set(tags_list + input_prop))
457+
return tags_list

cloudify_awssdk/ec2/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,17 @@ def create(self, params):
5050
def delete(self, params=None):
5151
"""Deletes a resource"""
5252
raise NotImplementedError()
53+
54+
def tag(self, params):
55+
"""Creates a resource"""
56+
self.logger.info('Tagging %s.' % params)
57+
res = self.client.create_tags(**params)
58+
self.logger.debug('Response: %s' % res)
59+
return res
60+
61+
def untag(self, params):
62+
"""Creates a resource"""
63+
self.logger.info('Untagging %s.' % params)
64+
res = self.client.delete_tags(**params)
65+
self.logger.debug('Response: %s' % res)
66+
return res

cloudify_awssdk/ec2/resources/customer_gateway.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def prepare(ctx, resource_config, **_):
8888
@decorators.aws_resource(EC2CustomerGateway, RESOURCE_TYPE)
8989
@decorators.wait_for_status(status_good=['available'],
9090
status_pending=['pending'])
91+
@decorators.tag_resources
9192
def create(ctx, iface, resource_config, **_):
9293
"""Creates an AWS EC2 Customer Gateway"""
9394

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

cloudify_awssdk/ec2/resources/ebs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def prepare(ctx, resource_config, **_):
152152

153153
@decorators.aws_resource(EC2Volume, RESOURCE_TYPE_VOLUME)
154154
@decorators.wait_for_status(status_good=[AVAILABLE], status_pending=[CREATING])
155+
@decorators.tag_resources
155156
def create(ctx, iface, resource_config, **_):
156157
"""
157158
Creates an AWS EC2 EBS Volume
@@ -184,6 +185,7 @@ def create(ctx, iface, resource_config, **_):
184185

185186
@decorators.aws_resource(EC2Volume, RESOURCE_TYPE_VOLUME,
186187
ignore_properties=True)
188+
@decorators.untag_resources
187189
def delete(ctx, iface, resource_config, **_):
188190
"""
189191
Deletes an AWS EC2 EBS Volume

cloudify_awssdk/ec2/resources/elasticip.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def prepare(ctx, resource_config, **_):
105105

106106

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

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

126127
@decorators.aws_resource(EC2ElasticIP, RESOURCE_TYPE,
127128
ignore_properties=True)
129+
@decorators.untag_resources
128130
def delete(ctx, iface, resource_config, **_):
129131
"""Deletes an AWS EC2 ElasticIP"""
130132

cloudify_awssdk/ec2/resources/eni.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def prepare(ctx, resource_config, **_):
125125

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

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

179180
@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE,
180181
ignore_properties=True)
182+
@decorators.untag_resources
181183
def delete(ctx, iface, resource_config, **_):
182184
"""Deletes an AWS EC2 NetworkInterface"""
183185

cloudify_awssdk/ec2/resources/instances.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def prepare(ctx, iface, resource_config, **_):
168168
@decorators.wait_for_status(
169169
status_good=[RUNNING, PENDING],
170170
fail_on_missing=False)
171+
@decorators.tag_resources
171172
def create(ctx, iface, resource_config, **_):
172173
'''Creates AWS EC2 Instances'''
173174

@@ -350,6 +351,7 @@ def stop(ctx, iface, resource_config, **_):
350351
@decorators.wait_for_delete(
351352
status_deleted=[TERMINATED],
352353
status_pending=[PENDING, STOPPING, SHUTTING_DOWN])
354+
@decorators.untag_resources
353355
def delete(iface, resource_config, **_):
354356
'''Deletes AWS EC2 Instances'''
355357

0 commit comments

Comments
 (0)