VPC Implementation In OpenStack Heat
a) CreateVPC == Create Virtual Network
b) CreateSubnet == Create Subnet in Virtual Network(VPC)
c) CreateInternetGateway == Get external network defined in the Project
d) AttachInternetGateway == Connect external network to routers in the Virtual Network(VPC)
e) CreateRouteTable == Create a router and attach to Virtual Network(VPC)
f) AssociateRouteTable == Attach subnet to router
g) CreateEIP == Attach floating ip to instance
2. 1) List of VPC APIs implemented in Heat
a) CreateVPC == Create Virtual Network
b) CreateSubnet == Create Subnet in Virtual Network(VPC)
c) CreateInternetGateway == Get external network defined in the
Project
d) AttachInternetGateway == Connect external network to routers in
the Virtual Network(VPC)
e) CreateRouteTable == Create a router and attach to Virtual
Network(VPC)
f) AssociateRouteTable == Attach subnet to router
g) CreateEIP == Attach floating ip to instance
3. 2) Resource
heat/heat/engine/resource.py
class Resource(object):
@scheduler.wrappertask
def create(self):
'''
Create the resource. Subclasses should provide a handle_create() method
to customise creation.
'''
@scheduler.wrappertask
def update(self, after, before=None, prev_resource=None):
'''
update the resource. Subclasses should provide a handle_update()
method
to customise update, the base-class handle_update will fail by default.
'''
def resource_id_set(self, inst):
self.resource_id = inst
5. Continue ...
def action_handler_task(self, action, args=[], action_prefix=None):
'''
A task to call the Resource subclass's handler methods for an action.
Calls the handle_<ACTION>() method for the given action and then calls
the check_<ACTION>_complete() method with the result in a loop until it
returns True. If the methods are not provided, the call is omitted.
Any args provided are passed to the handler.
If a prefix is supplied, the handler method handle_<PREFIX>_<ACTION>()
is called instead.
'''
def physical_resource_name(self):
name = '%s-%s-%s' % (self.stack.name,
self.name,
short_id.get_id(self.id))
return name
def neutron(self):
return self.client('neutron')
6. 3) VPC Resource
heat/heat/engine/resources/vpc.py
class VPC(resource.Resource):
PROPERTIES = (
CIDR_BLOCK, INSTANCE_TENANCY, TAGS,
) = (
'CidrBlock', 'InstanceTenancy', 'Tags',
)
properties_schema = { .... }
def handle_create(self):
client = self.neutron()
# The VPC's net and router are associated by having identical names.
net_props = {'name': self.physical_resource_name()}
router_props = {'name': self.physical_resource_name()}
net = client.create_network({'network': net_props})['network']
self.resource_id_set(net['id'])
client.create_router({'router': router_props})['router']
14. 9) ElasticIp Resource
heat/heat/engine/resources/eip.py
class ElasticIp(resource.Resource):
PROPERTIES = (DOMAIN, INSTANCE_ID,) = ('Domain', 'InstanceId', )
properties_schema = { .... }
def handle_create(self):
"""Allocate a floating IP for the current tenant."""
ips = None
if self.properties[self.DOMAIN]:
from heat.engine.resources import internet_gateway
ext_net = internet_gateway.InternetGateway.get_external_network_id(self.neutron())
props = {'floating_network_id': ext_net}
ips = self.neutron().create_floatingip({
'floatingip': props})['floatingip']
self.ipaddress = ips['floating_ip_address']
self.resource_id_set(ips['id'])
instance_id = self.properties[self.INSTANCE_ID]
if instance_id:
server = self.nova().servers.get(instance_id)
server.add_floating_ip(self._ipaddress())
def resource_mapping():
return {
'AWS::EC2::EIP': ElasticIp,
}
15. 10) ElasticIpAssociation Resource
heat/heat/engine/resources/eip.py
class ElasticIpAssociation(resource.Resource):
PROPERTIES = (
INSTANCE_ID, EIP, ALLOCATION_ID, NETWORK_INTERFACE_ID,
) = (
'InstanceId', 'EIP', 'AllocationId', 'NetworkInterfaceId',
)
properties_schema = { .... }
def handle_create(self):
"""Add a floating IP address to a server."""
if self.properties[self.EIP]:
server = self.nova().servers.get(self.properties[self.INSTANCE_ID])
server.add_floating_ip(self.properties[self.EIP])
self.resource_id_set(self.properties[self.EIP])
def resource_mapping():
return {
'AWS::EC2::EIPAssociation': ElasticIpAssociation,
}
16. 11) VPC UnitTest
heat/heat/tests/test_vpc.py
class VPCTestBase(common.HeatTestCase):
class VPCTest(VPCTestBase):
class SubnetTest(VPCTestBase):
class NetworkInterfaceTest(VPCTestBase):
class InternetGatewayTest(VPCTestBase):
class RouteTableTest(VPCTestBase):