blob: eadcc3b4e862347a1990c8dd96889c679a08f82f [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 transform_resource_name,
21 with_neutron_client,
22 get_resource_id,
23 is_external_resource,
24 is_external_resource_not_conditionally_created,
25 delete_resource_and_runtime_properties,
26 use_external_resource,
27 validate_resource,
28 OPENSTACK_ID_PROPERTY,
29 OPENSTACK_TYPE_PROPERTY,
30 OPENSTACK_NAME_PROPERTY,
31 COMMON_RUNTIME_PROPERTIES_KEYS
32)
33
34NETWORK_OPENSTACK_TYPE = 'network'
35
36# Runtime properties
37RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS
38
39
40@operation
41@with_neutron_client
42def create(neutron_client, args, **kwargs):
43
44 if use_external_resource(ctx, neutron_client, NETWORK_OPENSTACK_TYPE):
45 return
46
47 network = {
48 'admin_state_up': True,
49 'name': get_resource_id(ctx, NETWORK_OPENSTACK_TYPE),
50 }
51 network.update(ctx.node.properties['network'], **args)
52 transform_resource_name(ctx, network)
53
54 net = neutron_client.create_network({'network': network})['network']
55 ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = net['id']
56 ctx.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] =\
57 NETWORK_OPENSTACK_TYPE
58 ctx.instance.runtime_properties[OPENSTACK_NAME_PROPERTY] = net['name']
59
60
61@operation
62@with_neutron_client
63def start(neutron_client, **kwargs):
64 network_id = ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
65
66 if is_external_resource_not_conditionally_created(ctx):
67 ctx.logger.info('Validating external network is started')
68 if not neutron_client.show_network(
69 network_id)['network']['admin_state_up']:
70 raise NonRecoverableError(
71 'Expected external resource network {0} to be in '
72 '"admin_state_up"=True'.format(network_id))
73 return
74
75 neutron_client.update_network(
76 network_id, {
77 'network': {
78 'admin_state_up': True
79 }
80 })
81
82
83@operation
84@with_neutron_client
85def stop(neutron_client, **kwargs):
86 if is_external_resource(ctx):
87 ctx.logger.info('Not stopping network since an external network is '
88 'being used')
89 return
90
91 neutron_client.update_network(
92 ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY], {
93 'network': {
94 'admin_state_up': False
95 }
96 })
97
98
99@operation
100@with_neutron_client
101def delete(neutron_client, **kwargs):
102 delete_resource_and_runtime_properties(ctx, neutron_client,
103 RUNTIME_PROPERTIES_KEYS)
104
105
106@operation
107@with_neutron_client
108def creation_validation(neutron_client, **kwargs):
109 validate_resource(ctx, neutron_client, NETWORK_OPENSTACK_TYPE)