def test_prepare(self): name = self.get_config('datacenter') if name is None: raise SkipTest, 'Parameter "datacenter" not set.' datacenter = self.api.get(schema.DataCenter, name=name) if datacenter is None: raise SkipTest, 'Datacenter not found: %s' % name name = self.get_config('cluster') if name is None: raise SkipTest, 'Parameter "cluster" not set.' cluster = self.api.get(schema.Cluster, name=name) if cluster is None: raise SkipTest, 'Cluster not found: %s' % name template = self.api.get(schema.Template, name='Blank') if template is None: raise SkipTest, 'Template not found: Blank' vm = schema.new(schema.VM) vm.name = util.random_name('vm') vm.memory = 512 * 1024**2 vm.type = 'DESKTOP' vm.cluster = schema.ref(cluster) vm.template = schema.ref(template) vm2 = self.api.create(vm) assert vm2 is not None disk = schema.new(schema.Disk) disk.size = 1024**3 disk.format = 'COW' disk.sparse = True disk.wipe_after_delete = False disk2 = self.api.create(disk, base=vm2) assert disk2 is not None self.store.vm = vm2
def test_create_child(self): tag = self.store.tag child = schema.new(schema.Tag) child.name = util.random_name('tag') child.parent = schema.new(schema.TagParent) child.parent.tag = schema.new(schema.Tag) child.parent.tag.id = tag.id child2 = self.api.create(child) assert isinstance(child2, schema.Tag) assert child2.id is not None assert child2.parent.tag.id == tag.id self.store.child = child2
def test_create(self): tag = schema.new(schema.Tag) tag.name = util.random_name('tag') tag2 = self.api.create(tag) assert isinstance(tag2, schema.Tag) assert tag2.id is not None self.store.tag = tag2
def test_prepare(self): dc = schema.new(schema.DataCenter) dc.name = util.random_name('dc') dc.storage_type = 'NFS' dc = self.api.create(dc) assert dc is not None self.store.dc = dc
def update_object(self, obj, options): """Create a new binding type of type `typ', and set its attributes with values from `options'.""" attrs = [ opt for opt in options if not opt.endswith('id') ] attrs.sort() for attr in attrs: baseobj = obj basetype = type(obj) walked = [] path = attr[2:].split('-') for pa in path[:-1]: walked.append(pa) try: subobj = getattr(baseobj, pa) subtype = getattr(basetype, pa) except AttributeError: self.error('no such attribute: %s' % '.'.join(walked)) if subobj is None: subtype = schema.subtype(subtype) if issubclass(subtype, schema.ComplexType): setattr(baseobj, pa, schema.new(subtype)) subobj = getattr(baseobj, pa) baseobj = subobj basetype = subtype if not hasattr(basetype, path[-1]): self.error('no such attribute: %s' % attr) setattr(baseobj, path[-1], options[attr]) return obj
def test_href(self): vm = schema.new(schema.VM) vm.id = 10 vm.href = '/vms/10' vm.description = 'foo' vm2 = schema.href(vm) assert vm2.id == vm.id assert vm2.href == vm.href assert vm2.description is None
def set(self, obj, value, context): obj, attr = self._resolve_parent(obj, self.attribute) match = self._re_version.match(value) if match is None: raise ValueError, 'Illegal version: %s' % value version = schema.new(schema.Version) version.major = match.group(1) version.minor = match.group(2) setattr(obj, attr, version)
def create_object(self, typ, options, scope=None): """Create a new object of type `typ' based on the command-line options in `options'.""" obj = schema.new(typ) fields = metadata.get_fields(typ, 'C', scope) for field in fields: key = '--%s' % field.name if key in options: field.set(obj, options[key], self.context) return obj
def execute(self): """Execute the "create" command.""" self.check_connection() connection = self.context.connection stdout = self.context.terminal.stdout typename = self.arguments[0] typ = self.resolve_singular_type(typename) base = self.resolve_base(self.options) obj = self.read_input() if obj is None: obj = schema.new(typ) obj = self.update_object(obj, self.options) connection.create(obj, base=base)
def execute(self): """Execute the action command.""" self.check_connection() typename, name, action = self.arguments typ = self.resolve_singular_type(typename) base = self.resolve_base(self.options) obj = self.get_object(typ, name, base) actionobj = schema.new(schema.Action) actionobj = self.update_object(actionobj, self.options) connection = self.context.connection try: result = connection.action(obj, action, actionobj) except rhev.Error, e: self.error(str(e))
def test_wait(self): vm = schema.new(schema.VM) vm.name = util.random_name('vm') vm.type = 'SERVER' vm.memory = 512*1024*1024 vm.cluster = schema.new(schema.Cluster) vm.cluster.name = self.get_config('cluster') vm.template = schema.new(schema.Template) vm.template.name = self.get_config('template') vm = self.api.create(vm) assert isinstance(vm, schema.VM) disk = schema.new(schema.Disk) disk.format = 'COW' disk.size = 2024**3 disk.sparse = True disk = self.api.create(disk, base=vm) assert isinstance(disk, schema.Disk) assert self.api.wait(vm, exists=True) assert self.api.wait(vm, status='DOWN') self.api.action(vm, 'start') assert self.api.wait(vm, status='UP') self.api.action(vm, 'stop') assert self.api.wait(vm, status='DOWN') self.api.delete(vm)
def action(self, resource, action, data=None): """Execute an action on a resource. """ if not isinstance(resource, schema.BaseResource): raise TypeError, 'Expecting a binding instance.' if not resource.href: raise ValueError, 'Expecting a created binding instance.' self.connect() url = self._resolve_action(resource.href, action) if data is None: data = schema.new(schema.Action) response = self._make_request('POST', url, body=data.toxml()) if response.status in (http.OK, http.NO_CONTENT): result = self._parse_xml(response) else: raise self._create_exception(response) return result
def create(type, *args, **kwargs): """Create rhev-python objects.""" from rhev import schema from rhev.connection import Connection if issubclass(type, Connection): for key in ('url', 'username', 'password'): if key not in kwargs: name = 'RHEV_%s' % key.upper() value = os.environ.get(name) if value is None: raise Error, '$%s not set' % name kwargs[key] = value obj = type(*args, **kwargs) elif issubclass(type, schema.ComplexType): obj = schema.new(type, *args, **kwargs) else: obj = type(*args, **kwargs) return obj
def _resolve_parent(self, obj, attr): """INTERNAL: Resolve a dotted attribute name `attr' inside `obj' until its parent is reached, creating classes on the fly where required.""" baseobj = obj basetype = type(obj) walked = [] path = attr.split('.') for pa in path[:-1]: walked.append(pa) try: subobj = getattr(baseobj, pa) except AttributeError: m = 'no such attribute: %s' % '.'.join(walked) raise ValueError, m if subobj is None: prop = getattr(type(baseobj), pa) subtype = schema.subtype(prop) if issubclass(subtype, schema.ComplexType): setattr(baseobj, pa, schema.new(subtype)) subobj = getattr(baseobj, pa) baseobj = subobj return baseobj, path[-1]
def test_copy(self): vm = schema.new(schema.VM) vm.description = 'foo' vm2 = schema.copy(vm) assert vm2.description == vm.description
def test_update(self): vm = schema.new(schema.VM) vm2 = schema.new(schema.VM) vm2.description = 'foo' schema.update(vm, vm2) assert vm.description == vm2.description
def test_new(self): vm = schema.new(schema.VM) assert isinstance(vm, schema.VM) vms = schema.new(schema.VMs) assert isinstance(vms, schema.VMs) assert_raises(TypeError, schema.new, object)
import sys from rhev import create, schema from rhev import Connection as RhevConnection from rhev import Error as RhevError try: api = create(RhevConnection) datacenter = api.get(schema.DataCenter, name='default') cluster = api.get(schema.Cluster, name='default') template = api.get(schema.Template, name='blank') network = api.get(schema.Network, base=cluster, filter={'name': 'rhevm'}) vm = schema.new(schema.VM) vm.name = 'myvm' vm.memory = 2 * 1024**3 vm.os = 'Windows 2008 R2' vm.datacenter = schema.ref(datacenter) vm.cluster = schema.ref(cluster) vm.template = schema.ref(template) vm = api.create(vm) disk = create(schema.Disk) disk.size = 16 * 1024**3 disk.type = 'SYSTEM' disk.format = 'COW' disk.sparse = True disk.interface = 'VIRTIO' api.create(disk, base=vm)