132 lines
5.2 KiB
Python
132 lines
5.2 KiB
Python
# Copyright 2014
|
|
# The Cloudscaling Group, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
|
|
from neutronclient.common import exceptions as neutron_exception
|
|
from oslo.config import cfg
|
|
|
|
from ec2api.api import clients
|
|
from ec2api.api import ec2utils
|
|
from ec2api.api import internet_gateway as internet_gateway_api
|
|
from ec2api.api import route_table as route_table_api
|
|
from ec2api.api import security_group as security_group_api
|
|
from ec2api.api import subnet as subnet_api
|
|
from ec2api.api import utils
|
|
from ec2api.db import api as db_api
|
|
from ec2api import exception
|
|
from ec2api.openstack.common.gettextutils import _
|
|
from ec2api.openstack.common import log as logging
|
|
|
|
|
|
CONF = cfg.CONF
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
"""VPC-object related API implementation
|
|
"""
|
|
|
|
|
|
FILTER_MAP = {'cidr': 'cidrBlock',
|
|
'state': 'state',
|
|
'vpc-id': 'vpcId'}
|
|
|
|
|
|
def create_vpc(context, cidr_block, instance_tenancy='default'):
|
|
ec2utils.validate_vpc_cidr(cidr_block, exception.InvalidVpcRange)
|
|
neutron = clients.neutron(context)
|
|
# TODO(Alex): Handle errors like overlimit
|
|
# TODO(ft) dhcp_options_id
|
|
# TODO(ft): refactor to prevent update created objects
|
|
with utils.OnCrashCleaner() as cleaner:
|
|
os_router_body = {'router': {}}
|
|
os_router = neutron.create_router(os_router_body)['router']
|
|
cleaner.addCleanup(neutron.delete_router, os_router['id'])
|
|
vpc = db_api.add_item(context, 'vpc',
|
|
{'os_id': os_router['id'],
|
|
'cidr_block': cidr_block})
|
|
cleaner.addCleanup(db_api.delete_item, context, vpc['id'])
|
|
route_table = route_table_api._create_route_table(context, vpc)
|
|
cleaner.addCleanup(route_table_api._delete_route_table,
|
|
context, route_table['id'])
|
|
vpc['route_table_id'] = route_table['id']
|
|
db_api.update_item(context, vpc)
|
|
neutron.update_router(os_router['id'], {'router': {'name': vpc['id']}})
|
|
# NOTE(Alex): OpenStack doesn't allow creation of another group
|
|
# named 'default' hence 'Default' is used.
|
|
security_group = security_group_api._create_default_security_group(
|
|
context, vpc)
|
|
return {'vpc': _format_vpc(vpc)}
|
|
|
|
|
|
def delete_vpc(context, vpc_id):
|
|
vpc = ec2utils.get_db_item(context, 'vpc', vpc_id)
|
|
subnets = subnet_api.describe_subnets(context,
|
|
filter=[{'name': 'vpc-id', 'value': [vpc_id]}])['subnetSet']
|
|
internet_gateways = internet_gateway_api.describe_internet_gateways(
|
|
context,
|
|
filter=[{'name': 'attachment.vpc-id',
|
|
'value': [vpc['id']]}])['internetGatewaySet']
|
|
route_tables = route_table_api.describe_route_tables(context,
|
|
filter=[{'name': 'vpc-id', 'value': [vpc['id']]}])['routeTableSet']
|
|
if subnets or internet_gateways or len(route_tables) > 1:
|
|
msg = _("The vpc '%(vpc_id)s' has dependencies and "
|
|
"cannot be deleted.")
|
|
msg = msg % {'vpc_id': vpc['id']}
|
|
raise exception.DependencyViolation(msg)
|
|
|
|
neutron = clients.neutron(context)
|
|
with utils.OnCrashCleaner() as cleaner:
|
|
db_api.delete_item(context, vpc['id'])
|
|
cleaner.addCleanup(db_api.restore_item, context, 'vpc', vpc)
|
|
route_table_api._delete_route_table(context, vpc['route_table_id'],
|
|
cleaner=cleaner)
|
|
security_groups = security_group_api.describe_security_groups(
|
|
context,
|
|
filter=[{'name': 'vpc-id',
|
|
'value': [vpc['id']]}])['securityGroupInfo']
|
|
for security_group in security_groups:
|
|
security_group_api.delete_security_group(
|
|
context, group_id=security_group['groupId'])
|
|
try:
|
|
neutron.delete_router(vpc['os_id'])
|
|
except neutron_exception.NeutronClientException as ex:
|
|
# TODO(ft): do log error
|
|
# TODO(ft): adjust catched exception classes to catch:
|
|
# the router doesn't exist
|
|
# somewhat plugged to the router
|
|
pass
|
|
|
|
return True
|
|
|
|
|
|
def describe_vpcs(context, vpc_id=None, filter=None):
|
|
# TODO(ft): implement filters
|
|
vpcs = ec2utils.get_db_items(context, 'vpc', vpc_id)
|
|
formatted_vpcs = []
|
|
for vpc in vpcs:
|
|
formatted_vpc = _format_vpc(vpc)
|
|
if not utils.filtered_out(formatted_vpc, filter, FILTER_MAP):
|
|
formatted_vpcs.append(formatted_vpc)
|
|
return {'vpcSet': formatted_vpcs}
|
|
|
|
|
|
def _format_vpc(vpc):
|
|
return {'vpcId': vpc['id'],
|
|
'state': "available",
|
|
'cidrBlock': vpc['cidr_block'],
|
|
'isDefault': 'false',
|
|
'dhcpOptionsId': vpc.get('dhcp_options_id', 'default'),
|
|
# 'instanceTenancy': 'default', #TODO(Alex) implement
|
|
}
|