Merge "Add InstanceId property to EIP resource"

This commit is contained in:
Jenkins 2013-06-18 03:11:00 +00:00 committed by Gerrit Code Review
commit ef5b44f083
2 changed files with 45 additions and 10 deletions

View File

@ -49,7 +49,19 @@ class ElasticIp(resource.Resource):
self.ipaddress = ips.ip
self.resource_id_set(ips.id)
if self.properties['InstanceId']:
server = self.nova().servers.get(self.properties['InstanceId'])
res = server.add_floating_ip(self._ipaddress())
def handle_delete(self):
if self.properties['InstanceId']:
try:
server = self.nova().servers.get(self.properties['InstanceId'])
if server:
server.remove_floating_ip(self._ipaddress())
except clients.novaclient.exceptions.NotFound as ex:
pass
"""De-allocate a floating IP."""
if self.resource_id is not None:
self.nova().floating_ips.delete(self.resource_id)
@ -67,7 +79,7 @@ class ElasticIp(resource.Resource):
class ElasticIpAssociation(resource.Resource):
properties_schema = {'InstanceId': {'Type': 'String',
'Required': True},
'Required': False},
'EIP': {'Type': 'String'},
'AllocationId': {'Type': 'String',
'Implemented': False}}
@ -81,18 +93,20 @@ class ElasticIpAssociation(resource.Resource):
(self.properties['InstanceId'],
self.properties['EIP']))
server = self.nova().servers.get(self.properties['InstanceId'])
server.add_floating_ip(self.properties['EIP'])
if self.properties['InstanceId']:
server = self.nova().servers.get(self.properties['InstanceId'])
server.add_floating_ip(self.properties['EIP'])
self.resource_id_set(self.properties['EIP'])
def handle_delete(self):
"""Remove a floating IP address from a server."""
try:
server = self.nova().servers.get(self.properties['InstanceId'])
if server:
server.remove_floating_ip(self.properties['EIP'])
except clients.novaclient.exceptions.NotFound as ex:
pass
if self.properties['InstanceId']:
try:
server = self.nova().servers.get(self.properties['InstanceId'])
if server:
server.remove_floating_ip(self.properties['EIP'])
except clients.novaclient.exceptions.NotFound as ex:
pass
def resource_mapping():

View File

@ -24,6 +24,25 @@ from heat.tests.utils import parse_stack
eip_template = '''
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "EIP Test",
"Parameters" : {},
"Resources" : {
"IPAddress" : {
"Type" : "AWS::EC2::EIP",
"Properties" : {
"InstanceId" : { "Ref" : "WebServer" }
}
},
"WebServer": {
"Type": "AWS::EC2::Instance",
}
}
}
'''
eip_template_ipassoc = '''
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "EIP Test",
@ -77,6 +96,8 @@ class EIPTest(HeatTestCase):
def test_eip(self):
eip.ElasticIp.nova().MultipleTimes().AndReturn(self.fc)
self.fc.servers.get('WebServer').AndReturn(self.fc.servers.list()[0])
self.fc.servers.get('WebServer')
self.m.ReplayAll()
@ -113,7 +134,7 @@ class EIPTest(HeatTestCase):
self.m.ReplayAll()
t = template_format.parse(eip_template)
t = template_format.parse(eip_template_ipassoc)
stack = parse_stack(t)
rsrc = self.create_eip(t, stack, 'IPAddress')