Skip to content

Commit 9ac81ad

Browse files
committed
unified paremeters parse and resource_id->name
1 parent 33e422a commit 9ac81ad

10 files changed

Lines changed: 66 additions & 49 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Description for VM
4646
* `resource_id`: (optional) Used to identify the object when
4747
`use_external_resource` is true.
4848
* `params`: params used for create object, useful for embeded template.
49+
* `name`: Domain name
4950
* `vcpu`: CPU count
5051
* `memory_size`: VM memory size in KiB
5152
* `memory_maxsize`: (optional) recomended VM memory size in KiB for
@@ -80,6 +81,7 @@ Description for Network
8081
* `resource_id`: (optional) Used to identify the object when
8182
`use_external_resource` is true.
8283
* `params`: params used for create object.
84+
* `name`: Network name
8385
* `dev`: Device name
8486
* `forwards`: settings for network `forwards`.
8587
* `ips`: settings for network `ips`.

cloudify_libvirt/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import os
16+
import uuid
1617
from jinja2 import Template
1718
from pkg_resources import resource_filename
1819

@@ -35,6 +36,12 @@ def get_libvirt_params(**kwargs):
3536
template_params.update(kwargs.get('params', {}))
3637
ctx.instance.runtime_properties['params'] = template_params
3738

39+
# set default names and instance_uuid
40+
if not template_params.get("name"):
41+
template_params["name"] = ctx.instance.id
42+
if not template_params.get("instance_uuid"):
43+
template_params["instance_uuid"] = str(uuid.uuid4())
44+
3845
# update 'resource_id', 'use_external_resource' from kwargs
3946
for field in ['resource_id', 'use_external_resource']:
4047
if field in kwargs:

cloudify_libvirt/domain_tasks.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import libvirt
1717
import time
18-
import uuid
1918

2019
from cloudify import ctx
2120
from cloudify.decorators import operation
@@ -33,22 +32,14 @@ def create(**kwargs):
3332

3433
def _update_template_params(template_params):
3534
# set all params to default values
36-
if not template_params:
37-
template_params = {}
38-
39-
if not template_params.get("resource_id"):
40-
template_params["resource_id"] = ctx.instance.id
4135
if (not template_params.get("memory_maxsize") and
4236
template_params.get('memory_size')):
4337
# if have no maximum memory size, set current as minimum
4438
# and twised memory as maximum
4539
memory_size = int(template_params['memory_size'])
4640
template_params['memory_maxsize'] = memory_size * 2
47-
if not template_params.get("instance_uuid"):
48-
template_params["instance_uuid"] = str(uuid.uuid4())
4941
if not template_params.get("domain_type"):
5042
template_params["domain_type"] = "qemu"
51-
return template_params
5243

5344

5445
@operation
@@ -62,8 +53,7 @@ def configure(**kwargs):
6253
'Failed to open connection to the hypervisor'
6354
)
6455

65-
template_params = _update_template_params(template_params)
66-
56+
_update_template_params(template_params)
6757
try:
6858
if ctx.instance.runtime_properties.get("use_external_resource"):
6959
# lookup the default domain by name

cloudify_libvirt/network_tasks.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515

1616
import libvirt
17-
import uuid
1817
import time
1918

2019
from cloudify import ctx
@@ -23,21 +22,6 @@
2322
import cloudify_libvirt.common as common
2423

2524

26-
def _update_template_params(template_params):
27-
# set all params to default values
28-
if not template_params:
29-
template_params = {}
30-
31-
if not template_params:
32-
template_params = {}
33-
34-
if not template_params.get("resource_id"):
35-
template_params["resource_id"] = ctx.instance.id
36-
if not template_params.get("instance_uuid"):
37-
template_params["instance_uuid"] = str(uuid.uuid4())
38-
return template_params
39-
40-
4125
@operation
4226
def create(**kwargs):
4327
ctx.logger.info("Creating new network.")
@@ -49,8 +33,6 @@ def create(**kwargs):
4933
'Failed to open connection to the hypervisor'
5034
)
5135

52-
template_params = _update_template_params(template_params)
53-
5436
try:
5537
if ctx.instance.runtime_properties.get("use_external_resource"):
5638
# lookup the default network by name

cloudify_libvirt/templates/domain.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<resource>
33
<partition>/machine</partition>
44
</resource>
5+
<name>{{ name }}</name>
56
<uuid>{{ instance_uuid }}</uuid>
67
<currentMemory unit="KiB">{{ memory_size }}</currentMemory>
78
<on_poweroff>destroy</on_poweroff>
@@ -67,5 +68,4 @@
6768
<timer name="pit" tickpolicy="delay"/>
6869
<timer name="hpet" present="no"/>
6970
</clock>
70-
<name>{{ resource_id }}</name>
7171
</domain>

cloudify_libvirt/templates/network.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<network>
2-
<name>{{ resource_id }}</name>
2+
<name>{{ name }}</name>
33
<uuid>{{ instance_uuid }}</uuid>
44
<bridge name="{{ dev }}" />
55
{% for forward in forwards %}

cloudify_libvirt/tests/test_common.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ def test_get_libvirt_params(self):
3434
)
3535
current_ctx.set(_ctx)
3636

37-
self.assertEqual(common.get_libvirt_params(), (None, {}))
37+
with mock.patch(
38+
"cloudify_libvirt.common.uuid.uuid4",
39+
mock.Mock(return_value="some_uuid")
40+
):
41+
self.assertEqual(common.get_libvirt_params(),
42+
(None, {'name': 'node_name',
43+
'instance_uuid': 'some_uuid'}))
44+
3845
self.assertEqual(_ctx.instance.runtime_properties, {
3946
'libvirt_auth': None,
40-
'params': {}
47+
'params': {'name': 'node_name',
48+
'instance_uuid': 'some_uuid'}
4149
})
4250

4351
# overwrite
@@ -54,21 +62,32 @@ def test_get_libvirt_params(self):
5462
)
5563
current_ctx.set(_ctx)
5664

57-
self.assertEqual(common.get_libvirt_params(
58-
params={'z': 'y'}, libvirt_auth={'w': 'x'}
59-
), ({
60-
'w': 'x'
61-
}, {
62-
'a': 'b',
63-
'c': 'd',
64-
'e': 'g',
65-
'z': 'y'
66-
})
67-
)
65+
with mock.patch(
66+
"cloudify_libvirt.common.uuid.uuid4",
67+
mock.Mock(return_value="some_uuid")
68+
):
69+
self.assertEqual(common.get_libvirt_params(
70+
params={'z': 'y'}, libvirt_auth={'w': 'x'}
71+
), ({
72+
'w': 'x'
73+
}, {
74+
# default values
75+
'instance_uuid': 'some_uuid',
76+
'name': 'node_name',
77+
# combined
78+
'a': 'b',
79+
'c': 'd',
80+
'e': 'g',
81+
'z': 'y'
82+
})
83+
)
6884
self.assertEqual(_ctx.instance.runtime_properties, {
6985
'libvirt_auth': {'w': 'x'},
70-
'params': {'a': 'b', 'c': 'd', 'e': 'g', 'z': 'y'}
71-
})
86+
'params': {
87+
# default values
88+
'instance_uuid': 'some_uuid', 'name': 'node_name',
89+
# combined
90+
'a': 'b', 'c': 'd', 'e': 'g', 'z': 'y'}})
7291

7392
def test_save_node_state(self):
7493
isdir = mock.Mock(return_value=False)

cloudify_libvirt/tests/test_domain.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,18 @@ def test_create(self):
8181
real logic"""
8282
_ctx = self._create_ctx()
8383

84-
domain_tasks.create(ctx=_ctx, params={'z': 'y'},
85-
libvirt_auth={'w': 'x'})
84+
with mock.patch(
85+
"cloudify_libvirt.common.uuid.uuid4",
86+
mock.Mock(return_value="some_uuid")
87+
):
88+
domain_tasks.create(ctx=_ctx, params={'z': 'y'},
89+
libvirt_auth={'w': 'x'})
8690
self.assertEqual(_ctx.instance.runtime_properties, {
8791
'libvirt_auth': {'w': 'x'},
8892
'params': {
93+
# default values
94+
'name': 'node_name', 'instance_uuid': 'some_uuid',
95+
# values from inputs
8996
'a': 'b', 'c': 'd', 'e': 'g', 'z': 'y'
9097
}
9198
})

examples/cluster.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ node_templates:
115115
create:
116116
inputs:
117117
params:
118-
resource_id: manager_network
118+
name: manager_network
119119
dev: virbr9
120120
forwards:
121121
- mode: nat

plugin.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ data_types:
99

1010
network_params:
1111
properties:
12+
name:
13+
required: false
14+
default: ""
15+
description: >
16+
Network name
1217
dev:
1318
required: false
1419
default: ""
@@ -27,6 +32,11 @@ data_types:
2732
2833
domain_params:
2934
properties:
35+
name:
36+
required: false
37+
default: ""
38+
description: >
39+
Domain name
3040
vcpu:
3141
required: false
3242
type: integer

0 commit comments

Comments
 (0)