[PATCH 0/4] template supports networks

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> add 'networks' option for template get/create/update ShaoHe Feng (4): template supports networks: let template xml support more networks template supports networks: update API template supports networks: update controller and json schema template supports networks: update model docs/API.md | 4 ++++ src/kimchi/API.json | 14 ++++++++++++++ src/kimchi/controller.py | 3 ++- src/kimchi/model.py | 6 ++++++ src/kimchi/osinfo.py | 5 +++-- src/kimchi/vmtemplate.py | 20 ++++++++++++++++---- 6 files changed, 45 insertions(+), 7 deletions(-) -- 1.8.4.2

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> If a VM just in one network, it may not access other hosts in different networks. In most product environment, more networks are needed. such as a management network, this network just let a service can manage all the VMs in this this network. And a more publick network, a VM can access the internet. Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/osinfo.py | 5 +++-- src/kimchi/vmtemplate.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/kimchi/osinfo.py b/src/kimchi/osinfo.py index 20a93a6..3e980d6 100644 --- a/src/kimchi/osinfo.py +++ b/src/kimchi/osinfo.py @@ -170,8 +170,9 @@ isolinks = { }, } -defaults = {'network': 'default', 'storagepool': '/storagepools/default', - 'domain': 'kvm', 'arch': os.uname()[4] +defaults = {'networks': ['default'], + 'storagepool': '/storagepools/default', + 'domain': 'kvm', 'arch': os.uname()[4] } def lookup(distro, version): diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py index dd43faa..5d31f2a 100644 --- a/src/kimchi/vmtemplate.py +++ b/src/kimchi/vmtemplate.py @@ -182,11 +182,26 @@ class VMTemplate(object): ret.append(info) return ret + def _get_networks_xml(self): + network = """ + <interface type='network'> + <source network='%(network)s'/> + <model type='%(nic_model)s'/> + </interface> + """ + networks = "" + net_info = {"nic_model": self.info['nic_model']} + for nw in self.info['networks']: + net_info['network'] = nw + networks += network % net_info + return networks + def to_vm_xml(self, vm_name, vm_uuid, libvirt_stream = False, qemu_stream_dns = False): params = dict(self.info) params['name'] = vm_name params['uuid'] = vm_uuid params['disks'] = self._get_disks_xml(vm_uuid) + params['networks'] = self._get_networks_xml() params['qemu-namespace'] = '' params['cdroms'] = '' params['qemu-stream-cmdline'] = '' @@ -220,10 +235,7 @@ class VMTemplate(object): <devices> %(disks)s %(cdroms)s - <interface type='network'> - <source network='%(network)s'/> - <model type='%(nic_model)s'/> - </interface> + %(networks)s <graphics type='vnc' /> <sound model='ich6' /> <memballoon model='virtio' /> -- 1.8.4.2

On 12/26/2013 08:21 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
If a VM just in one network, it may not access other hosts in different networks.
typo: VM is just s/hosts/machines
In most product environment, more networks are needed.
such as a management network, this network just let a service can manage all the
network management
VMs in this this network.
typo: this this
And a more publick network, a VM can access the internet.
typo: publick
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/osinfo.py | 5 +++-- src/kimchi/vmtemplate.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/src/kimchi/osinfo.py b/src/kimchi/osinfo.py index 20a93a6..3e980d6 100644 --- a/src/kimchi/osinfo.py +++ b/src/kimchi/osinfo.py @@ -170,8 +170,9 @@ isolinks = { }, }
-defaults = {'network': 'default', 'storagepool': '/storagepools/default', - 'domain': 'kvm', 'arch': os.uname()[4] +defaults = {'networks': ['default'], + 'storagepool': '/storagepools/default', + 'domain': 'kvm', 'arch': os.uname()[4] }
def lookup(distro, version): diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py index dd43faa..5d31f2a 100644 --- a/src/kimchi/vmtemplate.py +++ b/src/kimchi/vmtemplate.py @@ -182,11 +182,26 @@ class VMTemplate(object): ret.append(info) return ret
+ def _get_networks_xml(self): + network = """ + <interface type='network'> + <source network='%(network)s'/> + <model type='%(nic_model)s'/> + </interface> + """ + networks = "" + net_info = {"nic_model": self.info['nic_model']} + for nw in self.info['networks']: + net_info['network'] = nw + networks += network % net_info + return networks + def to_vm_xml(self, vm_name, vm_uuid, libvirt_stream = False, qemu_stream_dns = False): params = dict(self.info) params['name'] = vm_name params['uuid'] = vm_uuid params['disks'] = self._get_disks_xml(vm_uuid) + params['networks'] = self._get_networks_xml() params['qemu-namespace'] = '' params['cdroms'] = '' params['qemu-stream-cmdline'] = '' @@ -220,10 +235,7 @@ class VMTemplate(object): <devices> %(disks)s %(cdroms)s - <interface type='network'> - <source network='%(network)s'/> - <model type='%(nic_model)s'/> - </interface> + %(networks)s <graphics type='vnc' /> <sound model='ich6' /> <memballoon model='virtio' />

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> add 'networks' option for template get/create/update Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- docs/API.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/API.md b/docs/API.md index 9edc551..a2b35bb 100644 --- a/docs/API.md +++ b/docs/API.md @@ -120,6 +120,8 @@ Represents a snapshot of the Virtual Machine's primary monitor. * cdrom *(required)*: A volume name or URI to an ISO image. * storagepool *(optional)*: URI of the storagepool. Default is '/storagepools/default' + * networks *(optional)*: name list of which networks will be assigned to the a new VM. + Default is '[default]' * disks *(optional)*: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index @@ -144,6 +146,7 @@ Represents a snapshot of the Virtual Machine's primary monitor. * memory: The amount of memory assigned to the VM * cdrom: A volume name or URI to an ISO image * storagepool: URI of the storagepool where template allocates vm storage. + * networks *(optional)*: name list of which networks will be assigned to the a new VM. * disks: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index @@ -162,6 +165,7 @@ Represents a snapshot of the Virtual Machine's primary monitor. * memory: The amount of memory assigned to the VM * cdrom: A volume name or URI to an ISO image * storagepool: URI of the storagepool where template allocates vm storage. + * networks *(optional)*: name list of which networks will be assigned to the a new VM. * disks: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index -- 1.8.4.2

On 12/26/2013 08:21 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
add 'networks' option for template get/create/update
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- docs/API.md | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/docs/API.md b/docs/API.md index 9edc551..a2b35bb 100644 --- a/docs/API.md +++ b/docs/API.md @@ -120,6 +120,8 @@ Represents a snapshot of the Virtual Machine's primary monitor. * cdrom *(required)*: A volume name or URI to an ISO image. * storagepool *(optional)*: URI of the storagepool. Default is '/storagepools/default' + * networks *(optional)*: name list of which networks will be assigned to the a new VM.
s/name list/list
+ Default is '[default]' * disks *(optional)*: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index @@ -144,6 +146,7 @@ Represents a snapshot of the Virtual Machine's primary monitor. * memory: The amount of memory assigned to the VM * cdrom: A volume name or URI to an ISO image * storagepool: URI of the storagepool where template allocates vm storage. + * networks *(optional)*: name list of which networks will be assigned to the a new VM.
here too
* disks: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index @@ -162,6 +165,7 @@ Represents a snapshot of the Virtual Machine's primary monitor. * memory: The amount of memory assigned to the VM * cdrom: A volume name or URI to an ISO image * storagepool: URI of the storagepool where template allocates vm storage. + * networks *(optional)*: name list of which networks will be assigned to the a new VM.
here too
* disks: An array of requested disks with the following optional fields (either *size* or *volume* must be specified): * index: The device index

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> for json schema, verify the 'networks' option when create template or update template. for controller, add 'networks' attribute when GET template. Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/API.json | 14 ++++++++++++++ src/kimchi/controller.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 7b90826..6f72d13 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -125,6 +125,13 @@ "type": "string", "pattern": "^/storagepools/[^/]+/?$" }, + "networks": { + "description": "name list of which networks will be assigned to the a new VM.", + "type": "array", + "minItems": 1, + "items": { "type": "string" }, + "uniqueItems": true + }, "folder": { "description": "Folder", "type": "array", @@ -198,6 +205,13 @@ "type": "string", "pattern": "^/storagepools/[^/]+/?$" }, + "networks": { + "description": "name list of which networks will be assigned to the a new VM.", + "type": "array", + "minItems": 1, + "items": { "type": "string" }, + "uniqueItems": true + }, "folder": { "description": "Folder", "type": "array", diff --git a/src/kimchi/controller.py b/src/kimchi/controller.py index 2940278..4a88a2d 100644 --- a/src/kimchi/controller.py +++ b/src/kimchi/controller.py @@ -406,7 +406,7 @@ class Template(Resource): super(Template, self).__init__(model, ident) self.update_params = ["name", "folder", "icon", "os_distro", "storagepool", "os_version", "cpus", - "memory", "cdrom", "disks"] + "memory", "cdrom", "disks", "networks"] self.uri_fmt = "/templates/%s" @property @@ -420,6 +420,7 @@ class Template(Resource): 'cdrom': self.info['cdrom'], 'disks': self.info['disks'], 'storagepool': self.info['storagepool'], + 'networks': self.info['networks'], 'folder': self.info.get('folder', [])} -- 1.8.4.2

On 12/26/2013 08:21 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
for json schema, verify the 'networks' option when create template or update template. for controller, add 'networks' attribute when GET template.
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/API.json | 14 ++++++++++++++ src/kimchi/controller.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 7b90826..6f72d13 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -125,6 +125,13 @@ "type": "string", "pattern": "^/storagepools/[^/]+/?$" }, + "networks": { + "description": "name list of which networks will be assigned to the a new VM.",
s/name list/list
+ "type": "array", + "minItems": 1,
Can't I create a VM without network?
+ "items": { "type": "string" }, + "uniqueItems": true + }, "folder": { "description": "Folder", "type": "array", @@ -198,6 +205,13 @@ "type": "string", "pattern": "^/storagepools/[^/]+/?$" }, + "networks": { + "description": "name list of which networks will be assigned to the a new VM.", + "type": "array", + "minItems": 1, + "items": { "type": "string" }, + "uniqueItems": true
Same as I commented above
+ }, "folder": { "description": "Folder", "type": "array", diff --git a/src/kimchi/controller.py b/src/kimchi/controller.py index 2940278..4a88a2d 100644 --- a/src/kimchi/controller.py +++ b/src/kimchi/controller.py @@ -406,7 +406,7 @@ class Template(Resource): super(Template, self).__init__(model, ident) self.update_params = ["name", "folder", "icon", "os_distro", "storagepool", "os_version", "cpus", - "memory", "cdrom", "disks"] + "memory", "cdrom", "disks", "networks"] self.uri_fmt = "/templates/%s"
@property @@ -420,6 +420,7 @@ class Template(Resource): 'cdrom': self.info['cdrom'], 'disks': self.info['disks'], 'storagepool': self.info['storagepool'], + 'networks': self.info['networks'], 'folder': self.info.get('folder', [])}

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> check all networks exist Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/model.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 1a547ec..ea28fec 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -678,6 +678,9 @@ class Model(object): def templates_create(self, params): name = params['name'] + for net_name in params.get(u'networks', []): + self._get_network(net_name) + with self.objstore as session: if name in session.get_list('template'): raise InvalidOperation("Template already exists") @@ -698,6 +701,9 @@ class Model(object): except Exception as e: raise InvalidParameter("Storagepool specified is not valid: %s." % e.message) + for net_name in params.get(u'networks', []): + self._get_network(net_name) + self.template_delete(name) try: ident = self.templates_create(new_t) -- 1.8.4.2

Reviewed-by: Aline Manera <alinefm@linux.vnet.ibm.com> On 12/26/2013 08:21 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
check all networks exist
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/model.py | 6 ++++++ 1 file changed, 6 insertions(+)
diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 1a547ec..ea28fec 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -678,6 +678,9 @@ class Model(object):
def templates_create(self, params): name = params['name'] + for net_name in params.get(u'networks', []): + self._get_network(net_name) + with self.objstore as session: if name in session.get_list('template'): raise InvalidOperation("Template already exists") @@ -698,6 +701,9 @@ class Model(object): except Exception as e: raise InvalidParameter("Storagepool specified is not valid: %s." % e.message)
+ for net_name in params.get(u'networks', []): + self._get_network(net_name) + self.template_delete(name) try: ident = self.templates_create(new_t)

On 12/26/2013 08:21 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
add 'networks' option for template get/create/update
ShaoHe Feng (4): template supports networks: let template xml support more networks template supports networks: update API template supports networks: update controller and json schema template supports networks: update model
docs/API.md | 4 ++++ src/kimchi/API.json | 14 ++++++++++++++ src/kimchi/controller.py | 3 ++- src/kimchi/model.py | 6 ++++++ src/kimchi/osinfo.py | 5 +++-- src/kimchi/vmtemplate.py | 20 ++++++++++++++++---- 6 files changed, 45 insertions(+), 7 deletions(-)
You also need to update mockmodel and add tests for it
participants (2)
-
Aline Manera
-
shaohef@linux.vnet.ibm.com