blob: 6e97c967558957092f5c55910067e37d1c0277d1 [file] [log] [blame]
dfilppi9981f552017-08-07 20:10:53 +00001#########
2# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# * See the License for the specific language governing permissions and
14# * limitations under the License.
15
16from cloudify import ctx
17from cloudify.decorators import operation
18from cloudify.exceptions import NonRecoverableError
19from openstack_plugin_common import (
20 with_neutron_client,
21 transform_resource_name,
22 get_resource_id,
23 get_openstack_id_of_single_connected_node_by_openstack_type,
24 delete_resource_and_runtime_properties,
25 delete_runtime_properties,
26 use_external_resource,
27 validate_resource,
28 validate_ip_or_range_syntax,
29 OPENSTACK_ID_PROPERTY,
30 OPENSTACK_TYPE_PROPERTY,
31 OPENSTACK_NAME_PROPERTY,
32 COMMON_RUNTIME_PROPERTIES_KEYS
33)
34
35from neutron_plugin.network import NETWORK_OPENSTACK_TYPE
36
37SUBNET_OPENSTACK_TYPE = 'subnet'
38
39# Runtime properties
40RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS
41
42
43@operation
44@with_neutron_client
45def create(neutron_client, args, **kwargs):
46
47 if use_external_resource(ctx, neutron_client, SUBNET_OPENSTACK_TYPE):
48 try:
49 net_id = \
50 get_openstack_id_of_single_connected_node_by_openstack_type(
51 ctx, NETWORK_OPENSTACK_TYPE, True)
52
53 if net_id:
54 subnet_id = \
55 ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
56
57 if neutron_client.show_subnet(
58 subnet_id)['subnet']['network_id'] != net_id:
59 raise NonRecoverableError(
60 'Expected external resources subnet {0} and network'
61 ' {1} to be connected'.format(subnet_id, net_id))
62 return
63 except Exception:
64 delete_runtime_properties(ctx, RUNTIME_PROPERTIES_KEYS)
65 raise
66
67 net_id = get_openstack_id_of_single_connected_node_by_openstack_type(
68 ctx, NETWORK_OPENSTACK_TYPE)
69 subnet = {
70 'name': get_resource_id(ctx, SUBNET_OPENSTACK_TYPE),
71 'network_id': net_id,
72 }
73 subnet.update(ctx.node.properties['subnet'], **args)
74 transform_resource_name(ctx, subnet)
75
76 s = neutron_client.create_subnet({'subnet': subnet})['subnet']
77 ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = s['id']
78 ctx.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] = \
79 SUBNET_OPENSTACK_TYPE
80 ctx.instance.runtime_properties[OPENSTACK_NAME_PROPERTY] = subnet['name']
81
82
83@operation
84@with_neutron_client
85def delete(neutron_client, **kwargs):
86 delete_resource_and_runtime_properties(ctx, neutron_client,
87 RUNTIME_PROPERTIES_KEYS)
88
89
90@operation
91@with_neutron_client
92def creation_validation(neutron_client, args, **kwargs):
93 validate_resource(ctx, neutron_client, SUBNET_OPENSTACK_TYPE)
94 subnet = dict(ctx.node.properties['subnet'], **args)
95
96 if 'cidr' not in subnet:
97 err = '"cidr" property must appear under the "subnet" property of a ' \
98 'subnet node'
99 ctx.logger.error('VALIDATION ERROR: ' + err)
100 raise NonRecoverableError(err)
101 validate_ip_or_range_syntax(ctx, subnet['cidr'])