|
16 | 16 | from cloudify import ctx |
17 | 17 | from cloudify.decorators import operation |
18 | 18 |
|
19 | | -from openstack_plugin_common import (with_nova_client, |
20 | | - use_external_resource, |
21 | | - delete_resource_and_runtime_properties, |
22 | | - create_object_dict, |
23 | | - add_list_to_runtime_properties, |
24 | | - set_openstack_runtime_properties, |
25 | | - COMMON_RUNTIME_PROPERTIES_KEYS) |
| 19 | +from openstack_plugin_common import ( |
| 20 | + with_nova_client, |
| 21 | + use_external_resource, |
| 22 | + delete_runtime_properties, |
| 23 | + delete_resource_and_runtime_properties, |
| 24 | + create_object_dict, |
| 25 | + add_list_to_runtime_properties, |
| 26 | + set_openstack_runtime_properties, |
| 27 | + COMMON_RUNTIME_PROPERTIES_KEYS |
| 28 | +) |
26 | 29 |
|
27 | 30 | FLAVOR_OPENSTACK_TYPE = 'flavor' |
28 | 31 |
|
| 32 | +EXTRA_SPECS_PROPERTY = 'extra_specs' |
| 33 | + |
| 34 | +TENANTS_PROPERTY = 'tenants' |
| 35 | + |
29 | 36 | RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS |
30 | 37 |
|
31 | 38 |
|
| 39 | +def _set_extra_specs(ctx, flavor): |
| 40 | + extra_specs = ctx.node.properties.get(EXTRA_SPECS_PROPERTY, {}) |
| 41 | + |
| 42 | + if extra_specs: |
| 43 | + ctx.logger.info( |
| 44 | + 'Setting extra specs: {0} for flavor: {1}' |
| 45 | + .format(extra_specs, flavor.to_dict()) |
| 46 | + ) |
| 47 | + |
| 48 | + flavor.set_keys(extra_specs) |
| 49 | + |
| 50 | + ctx.instance.runtime_properties[EXTRA_SPECS_PROPERTY] = extra_specs |
| 51 | + |
| 52 | + |
| 53 | +def _set_tenants_access(ctx, nova_client, flavor): |
| 54 | + tenants = ctx.node.properties.get(TENANTS_PROPERTY, []) |
| 55 | + |
| 56 | + for tenant in tenants: |
| 57 | + ctx.logger.info( |
| 58 | + 'Adding tenant access: {0} for flavor: {1}' |
| 59 | + .format(tenant, flavor.to_dict()) |
| 60 | + ) |
| 61 | + nova_client.flavor_access.add_tenant_access(flavor, tenant) |
| 62 | + |
| 63 | + ctx.instance.runtime_properties[TENANTS_PROPERTY] = tenants |
| 64 | + |
| 65 | + |
32 | 66 | @operation |
33 | 67 | @with_nova_client |
34 | 68 | def create(nova_client, args, **kwargs): |
35 | 69 | if use_external_resource(ctx, nova_client, FLAVOR_OPENSTACK_TYPE): |
36 | 70 | return |
37 | 71 |
|
38 | 72 | flavor_dict = create_object_dict(ctx, FLAVOR_OPENSTACK_TYPE, args, {}) |
| 73 | + ctx.logger.info('Creating flavor: {0}'.format(flavor_dict)) |
| 74 | + |
39 | 75 | flavor = nova_client.flavors.create(**flavor_dict) |
40 | 76 | set_openstack_runtime_properties(ctx, flavor, FLAVOR_OPENSTACK_TYPE) |
41 | 77 |
|
| 78 | + _set_extra_specs(ctx, flavor) |
| 79 | + _set_tenants_access(ctx, nova_client, flavor) |
| 80 | + |
42 | 81 |
|
43 | 82 | @operation |
44 | 83 | @with_nova_client |
45 | 84 | def delete(nova_client, **kwargs): |
46 | | - delete_resource_and_runtime_properties(ctx, nova_client, |
47 | | - RUNTIME_PROPERTIES_KEYS) |
| 85 | + delete_resource_and_runtime_properties( |
| 86 | + ctx, |
| 87 | + nova_client, |
| 88 | + RUNTIME_PROPERTIES_KEYS |
| 89 | + ) |
| 90 | + |
| 91 | + delete_runtime_properties(ctx, [EXTRA_SPECS_PROPERTY, TENANTS_PROPERTY]) |
48 | 92 |
|
49 | 93 |
|
50 | 94 | @with_nova_client |
|
0 commit comments