blob: 283eae85cf6f107fbb9092fdcdd2ec6497a2cb73 [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 openstack_plugin_common import (
19 transform_resource_name,
20 with_nova_client,
21 delete_resource_and_runtime_properties
22)
23from openstack_plugin_common.security_group import (
24 build_sg_data,
25 process_rules,
26 use_external_sg,
27 set_sg_runtime_properties,
28 delete_sg,
29 sg_creation_validation,
30 RUNTIME_PROPERTIES_KEYS
31)
32
33
34@operation
35@with_nova_client
36def create(nova_client, args, **kwargs):
37
38 security_group = build_sg_data(args)
39 security_group['description'] = ctx.node.properties['description']
40
41 sgr_default_values = {
42 'ip_protocol': 'tcp',
43 'from_port': 1,
44 'to_port': 65535,
45 'cidr': '0.0.0.0/0',
46 # 'group_id': None,
47 # 'parent_group_id': None,
48 }
49 sg_rules = process_rules(nova_client, sgr_default_values,
50 'cidr', 'group_id', 'from_port', 'to_port')
51
52 if use_external_sg(nova_client):
53 return
54
55 transform_resource_name(ctx, security_group)
56
57 sg = nova_client.security_groups.create(
58 security_group['name'], security_group['description'])
59
60 set_sg_runtime_properties(sg, nova_client)
61
62 try:
63 for sgr in sg_rules:
64 sgr['parent_group_id'] = sg.id
65 nova_client.security_group_rules.create(**sgr)
66 except Exception:
67 delete_resource_and_runtime_properties(ctx, nova_client,
68 RUNTIME_PROPERTIES_KEYS)
69 raise
70
71
72@operation
73@with_nova_client
74def delete(nova_client, **kwargs):
75 delete_sg(nova_client)
76
77
78@operation
79@with_nova_client
80def creation_validation(nova_client, **kwargs):
81 sg_creation_validation(nova_client, 'cidr')