Ejemplo n.º 1
0
 def filter(self, output):
     server = response.header("Server")
     if server:
         server += " "
     server += "rhevm-api/%s" % ".".join(map(str, rhevm.version))
     server += " rhevm/%s" % ".".join(map(str, powershell.version))
     response.set_header("Server", server)
     return output
Ejemplo n.º 2
0
 def filter(self, output):
     server = response.header('Server')
     if server:
         server += ' '
     server += 'rhevm-api/%s' % '.'.join(map(str, rhevm.version))
     server += ' rhevm/%s' % '.'.join(map(str, powershell.version))
     response.set_header('Server', server)
     return output
Ejemplo n.º 3
0
 def filter(self, output):
     response.status = http.CREATED
     if isinstance(output, tuple):
         url, object = output
     elif output:
         url, object = output, ''
     url = make_absolute(url)
     response.set_header('Location', url)
     # See RFC5023 section 9.2
     if object:
         response.set_header('Content-Location', url)
     return object
Ejemplo n.º 4
0
 def create(self, vm, input):
     filter = create_filter(vmid=vm)
     result = powershell.execute('Select-Vm | %s'
                                 ' | Tee-Object -Variable vm' % filter)
     if len(result) != 1:
         raise KeyError
     # With RHEVM-2.1, when adding a disk with Add-Disk, the new SnapshotId
     # is not returned. Therefore we need to compare disk images before and
     # after to conclude what our new SnapshotId is. On RHEVM-2.2 the
     # SnapshotId seems to be returned.
     result = powershell.execute('$vm.GetDiskImages()')
     old = set((disk['SnapshotId'] for disk in result))
     create = {'DiskSize': input.pop('DiskSize')}
     cmdline = create_cmdline(**create)
     updates = create_setattr('disk', **input)
     powershell.execute('$disk = New-Disk %s; %s' % (cmdline, updates))
     if powershell.version >= (2, 2):
         vmref = '-VmObject $vm'
     else:
         vmref = '-VmId $vm.VmId'
     if input.get('VolumeType') == 'Preallocated':
         result = powershell.execute(
             'Add-Disk -DiskObject $disk %s -Async' % vmref)
         tasks = powershell.execute('Get-LastCommandTasks')
         async = True
     else:
         result = powershell.execute('Add-Disk -DiskObject $disk %s' %
                                     vmref)
         async = False
     result = powershell.execute('$vm.GetDiskImages()')
     new = set((disk['SnapshotId'] for disk in result))
     diskid = (new - old).pop()
     filter = create_filter(snapshotid=diskid)
     result = powershell.execute('$vm.GetDiskImages() | %s' % filter)
     if async:
         response.status = http.ACCEPTED
         url = mapper.url_for(collection='tasks',
                              action='show',
                              id=tasks[0]['TaskId'])
         response.set_header('Link', '<%s>; rel=status' % url)
     url = mapper.url_for(collection=self.name,
                          action='show',
                          id=result[0]['SnapshotId'],
                          vm=vm)
     return url, result[0]
Ejemplo n.º 5
0
 def create(self, vm, input):
     filter = create_filter(vmid=vm)
     result = powershell.execute('Select-Vm | %s'
                                 ' | Tee-Object -Variable vm' % filter)
     if len(result) != 1:
         raise KeyError
     # With RHEVM-2.1, when adding a disk with Add-Disk, the new SnapshotId
     # is not returned. Therefore we need to compare disk images before and
     # after to conclude what our new SnapshotId is. On RHEVM-2.2 the
     # SnapshotId seems to be returned.
     result = powershell.execute('$vm.GetDiskImages()')
     old = set((disk['SnapshotId'] for disk in result))
     create = { 'DiskSize': input.pop('DiskSize') }
     cmdline = create_cmdline(**create)
     updates = create_setattr('disk', **input)
     powershell.execute('$disk = New-Disk %s; %s' % (cmdline, updates))
     if powershell.version >= (2, 2):
         vmref = '-VmObject $vm'
     else:
         vmref = '-VmId $vm.VmId'
     if input.get('VolumeType') == 'Preallocated':
         result = powershell.execute('Add-Disk -DiskObject $disk %s -Async'
                                     % vmref)
         tasks = powershell.execute('Get-LastCommandTasks')
         async = True
     else:
         result = powershell.execute('Add-Disk -DiskObject $disk %s'
                                     % vmref)
         async = False
     result = powershell.execute('$vm.GetDiskImages()')
     new = set((disk['SnapshotId'] for disk in result))
     diskid = (new - old).pop()
     filter = create_filter(snapshotid=diskid)
     result = powershell.execute('$vm.GetDiskImages() | %s' % filter)
     if async:
         response.status = http.ACCEPTED
         url = mapper.url_for(collection='tasks', action='show',
                              id=tasks[0]['TaskId'])
         response.set_header('Link', '<%s>; rel=status' % url)
     url = mapper.url_for(collection=self.name, action='show', 
                          id=result[0]['SnapshotId'], vm=vm)
     return url, result[0]
Ejemplo n.º 6
0
 def format(self, object):
     """Format an entity."""
     if not isinstance(object, dict) and not isinstance(object, list):
         return object
     accept = request.header('Accept', '*/*')
     ctype = http.select_content_type(self.formatters.keys(), accept)
     if not ctype:
         raise HTTPReturn(http.NOT_ACCEPTABLE,
                          reason='No acceptable content-type in: %s' %
                          accept)
     accept = request.header('Accept-Charset', '*')
     charset = http.select_charset(('utf-8', ), accept)
     if not charset:
         raise HTTPReturn(http.NOT_ACCEPTABLE,
                          reason='No acceptable charset in: %s' % accept)
     formatter = self.formatters[ctype]
     output = formatter.format(object, charset)
     response.set_header('Content-Type',
                         '%s; charset=%s' % (ctype, charset))
     response.set_header('Content-Length', str(len(output)))
     return output