Skip to content

Commit baf0255

Browse files
authored
Merge pull request #5 from cloudify-incubator/network_reuse
Network reuse support
2 parents 336b7ce + f2cdf3e commit baf0255

13 files changed

Lines changed: 102 additions & 18 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Description for Network
5454
**Supported properties:**
5555
* `libvirt_auth`: connection url, by default: `qemu:///system`
5656
* `backup_dir`: directory for save backups, by default: `./`
57+
* `params`: params used for create object.
58+
* `use_external_resource`: (optional) Use external object. The default is `false`.
59+
* `resource_id`: (optional) Used to identify the object when `use_external_resource` is true.
60+
* `dev`: Device name
61+
* `forwards`: settings for network `forwards`.
62+
* `ips`: settings for network `ips`.
5763

5864
**Inputs for actions:**
5965
* `create`:

cloudify_libvirt/domain_tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def configure(**kwargs):
7575
template_params['memory_size'] = memory_size * 2
7676
if not template_params.get("instance_uuid"):
7777
template_params["instance_uuid"] = str(uuid.uuid4())
78+
if not template_params.get("domain_type"):
79+
template_params["domain_type"] = "qemu"
7880

7981
params = {"ctx": ctx}
8082
params.update(template_params)

cloudify_libvirt/network_tasks.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,30 @@ def create(**kwargs):
3636
'Failed to open connection to the hypervisor'
3737
)
3838

39+
if not template_params:
40+
template_params = {}
41+
42+
if not template_params.get("resource_id"):
43+
template_params["resource_id"] = ctx.instance.id
44+
if not template_params.get("instance_uuid"):
45+
template_params["instance_uuid"] = str(uuid.uuid4())
46+
3947
try:
48+
if template_params.get("use_external_resource"):
49+
# lookup the default network by name
50+
network = conn.networkLookupByName(template_params["resource_id"])
51+
if network is None:
52+
raise cfy_exc.NonRecoverableError(
53+
'Failed to find the network'
54+
)
55+
56+
# save settings
57+
ctx.instance.runtime_properties['params'] = template_params
58+
ctx.instance.runtime_properties['resource_id'] = network.name()
59+
ctx.instance.runtime_properties['use_external_resource'] = True
60+
return
61+
62+
# templates
4063
network_file = kwargs.get('network_file')
4164
network_template = kwargs.get('network_template')
4265

@@ -54,13 +77,6 @@ def create(**kwargs):
5477
network_template = domain_desc.read()
5578

5679
template_engine = Template(network_template)
57-
if not template_params:
58-
template_params = {}
59-
60-
if not template_params.get("resource_id"):
61-
template_params["resource_id"] = ctx.instance.id
62-
if not template_params.get("instance_uuid"):
63-
template_params["instance_uuid"] = str(uuid.uuid4())
6480

6581
params = {"ctx": ctx}
6682
params.update(template_params)
@@ -78,6 +94,7 @@ def create(**kwargs):
7894
ctx.logger.info('Params: ' + repr(template_params))
7995
ctx.instance.runtime_properties['params'] = template_params
8096
ctx.instance.runtime_properties['resource_id'] = network.name()
97+
ctx.instance.runtime_properties['use_external_resource'] = False
8198

8299
active = network.isActive()
83100
if active == 1:
@@ -97,6 +114,10 @@ def delete(**kwargs):
97114
ctx.logger.info("No network for delete")
98115
return
99116

117+
if ctx.instance.runtime_properties.get('use_external_resource'):
118+
ctx.logger.info("External resource, skip")
119+
return
120+
100121
libvirt_auth, _ = common.get_libvirt_params(**kwargs)
101122
conn = libvirt.open(libvirt_auth)
102123
if conn is None:

cloudify_libvirt/templates/domain.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" xmlns="" type="qemu">
1+
<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" xmlns="" type="{{ domain_type }}">
22
<resource>
33
<partition>/machine</partition>
44
</resource>
@@ -61,6 +61,11 @@
6161
<type machine="pc" arch="x86_64">hvm</type>
6262
<boot dev="hd"/>
6363
</os>
64-
<clock offset="utc"/>
64+
<cpu mode="host-model"/>
65+
<clock offset="utc">
66+
<timer name="rtc" tickpolicy="catchup"/>
67+
<timer name="pit" tickpolicy="delay"/>
68+
<timer name="hpet" present="no"/>
69+
</clock>
6570
<name>{{ resource_id }}</name>
6671
</domain>

cloudify_libvirt/tests/test_network.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,52 @@ def _test_empty_network(self, func):
166166
"cloudify_libvirt.network_tasks.libvirt.open",
167167
func, [], {'ctx': _ctx}, 'resource')
168168

169+
def test_reuse_network_create_not_exist(self):
170+
# check correct handle exception with empty network
171+
_ctx = self._create_ctx()
172+
self._check_no_such_object_network(
173+
"cloudify_libvirt.network_tasks.libvirt.open",
174+
network_tasks.create, [], {'ctx': _ctx, 'params': {
175+
"resource_id": 'resource',
176+
"use_external_resource": True,
177+
}}, 'resource')
178+
179+
def test_reuse_network_create_exist(self):
180+
# check that we can use network
181+
_ctx = self._create_ctx()
182+
183+
network = mock.Mock()
184+
network.name = mock.Mock(return_value="resource")
185+
186+
connect = self._create_fake_connection()
187+
connect.networkLookupByName = mock.Mock(return_value=network)
188+
with mock.patch(
189+
"cloudify_libvirt.domain_tasks.libvirt.open",
190+
mock.Mock(return_value=connect)
191+
):
192+
network_tasks.create(ctx=_ctx, params={
193+
"resource_id": 'resource',
194+
"use_external_resource": True})
195+
connect.networkLookupByName.assert_called_with('resource')
196+
self.assertEqual(
197+
_ctx.instance.runtime_properties['resource_id'], 'resource'
198+
)
199+
self.assertTrue(
200+
_ctx.instance.runtime_properties['use_external_resource']
201+
)
202+
203+
def _test_reused_network(self, func, use_existed=True):
204+
# check use prexisted network
205+
_ctx = self._create_ctx()
206+
_ctx.instance.runtime_properties['resource_id'] = 'resource'
207+
_ctx.instance.runtime_properties['use_external_resource'] = use_existed
208+
connect = self._create_fake_connection()
209+
with mock.patch(
210+
"cloudify_libvirt.network_tasks.libvirt.open",
211+
mock.Mock(return_value=connect)
212+
):
213+
func(ctx=_ctx)
214+
169215
def _test_empty_network_backup(self, func):
170216
# check correct handle exception with empty network
171217
_ctx = self._create_ctx()
@@ -407,6 +453,7 @@ def test_delete(self):
407453
self._test_no_resource_id(network_tasks.delete)
408454
self._test_empty_connection(network_tasks.delete)
409455
self._test_empty_network(network_tasks.delete)
456+
self._test_reused_network(network_tasks.delete)
410457

411458
# delete with error
412459
_ctx = self._create_ctx()
@@ -494,6 +541,9 @@ def test_create(self):
494541
self.assertEqual(
495542
_ctx.instance.runtime_properties['resource_id'], "network_name"
496543
)
544+
self.assertFalse(
545+
_ctx.instance.runtime_properties['use_external_resource']
546+
)
497547

498548
# unactive
499549
network.isActive = mock.Mock(return_value=0)

examples/cluster.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ imports:
77
- http://www.getcloudify.org/spec/cloudify/4.3.1/types.yaml
88
- http://www.getcloudify.org/spec/fabric-plugin/1.5.1/plugin.yaml
99
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-utilities-plugin/1.7.1/plugin.yaml
10-
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4/plugin.yaml
10+
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4.1/plugin.yaml
1111

1212
inputs:
1313

examples/vm_agent.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tosca_definitions_version: cloudify_dsl_1_3
33
imports:
44
- http://www.getcloudify.org/spec/cloudify/4.3.1/types.yaml
55
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-utilities-plugin/1.7.1/plugin.yaml
6-
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4/plugin.yaml
6+
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4.1/plugin.yaml
77

88
inputs:
99

examples/vm_centos.amd64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tosca_definitions_version: cloudify_dsl_1_3
33
imports:
44
- http://www.getcloudify.org/spec/cloudify/4.3.1/types.yaml
55
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-utilities-plugin/1.7.1/plugin.yaml
6-
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4/plugin.yaml
6+
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4.1/plugin.yaml
77

88
inputs:
99

examples/vm_fabric.arm64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tosca_definitions_version: cloudify_dsl_1_3
33
imports:
44
- http://www.getcloudify.org/spec/cloudify/4.3.1/types.yaml
55
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-utilities-plugin/1.7.1/plugin.yaml
6-
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4/plugin.yaml
6+
- https://raw.githubusercontent.com/cloudify-incubator/cloudify-libvirt-plugin/0.4.1/plugin.yaml
77

88
inputs:
99

plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ plugins:
22
libvirt:
33
executor: central_deployment_agent
44
package_name: cloudify-libvirt-plugin
5-
package_version: '0.4.1'
6-
source: https://github.com/cloudify-incubator/cloudify-libvirt-plugin/archive/0.4.1.zip
5+
package_version: '0.5.0'
6+
source: https://github.com/cloudify-incubator/cloudify-libvirt-plugin/archive/0.5.0.zip
77

88
node_types:
99

0 commit comments

Comments
 (0)