def index(self, request, cu=None, roleIds=None, repos=None, *args, **kwargs): label = request.GET.get('label', None) name = request.GET.get('name', None) latest = request.GET.get('latest', 1) latest = (latest != '0') types = request.GET.get('type', []) if type(types) != list: types = [types] types = set(types) troves = repquery.searchNodes(cu, roleIds, label=label, mkUrl=request.makeUrl, filterSet=types, db=repos.db, name=name, latest=latest) return XMLResponse(xobj.toxml(troves, None))
def index(self, request, cu = None, roleIds = None, *args, **kwargs): label = request.GET.get('label', None) name = request.GET.get('name', None) latest = request.GET.get('latest', 1) latest = (latest != '0') types = request.GET.get('type', []) if type(types) != list: types = [ types ] types = set(types) start = int(request.GET.get('start', 0)) if 'limit' in request.GET: limit = int(request.GET['limit']) else: limit = None troves = repquery.searchTroves(cu, roleIds, label = label, filterSet = types, latest = latest, mkUrl = request.makeUrl, start = start, limit = limit, name = name) return XMLResponse(xobj.toxml(troves, None))
def index(self, request, cu=None, roleIds=None, *args, **kwargs): label = request.GET.get('label', None) name = request.GET.get('name', None) latest = request.GET.get('latest', 1) latest = (latest != '0') types = request.GET.get('type', []) if type(types) != list: types = [types] types = set(types) start = int(request.GET.get('start', 0)) if 'limit' in request.GET: limit = int(request.GET['limit']) else: limit = None troves = repquery.searchTroves(cu, roleIds, label=label, filterSet=types, latest=latest, mkUrl=request.makeUrl, start=start, limit=limit, name=name) return XMLResponse(xobj.toxml(troves, None))
def _serialize_document(self, doc): """ Serialize a object tree into xml. """ # Serialize document to xml. if isinstance(doc, xobj.Document): xml = doc.toxml() else: meta = getattr(doc, '_xobj', None) # If this is not an xobj and it is a string, but it doesn't look # like an xml document or doc is a file object, assume that the # client meant to upload this document to the server. if (meta is None and (isinstance(doc, file) or (isinstance(doc, types.StringTypes) and not util.isXML(doc)) or httputil.isHTTPData(doc))): return doc # Raise an exception if we can't figure out the tag. if meta is None or not meta.tag: raise SerializationError(instance=doc, msg='Can not determine tag') xml = xobj.toxml(doc, meta.tag) return xml
def info(self, request, cu, roleIds = None, fileId = None, **kwargs): path = request.GET.get('path', None) noContent = request.GET.get('nocontent', False) x = repquery.getFileInfo(cu, roleIds, fileId, mkUrl = request.makeUrl, path = path, noContent = noContent) if x is None: return response.Response(status=404) return XMLResponse(xobj.toxml(x, None))
def create(self, imageType, arch, message): ''' Create an image definition @param imageType: type of image defintion to create @type imageType: rObj(image_type) @return: image defintion @rtype: rObj(...) ''' dc = self.handle.DescriptorConfig rb = self.handle.facade.rbuilder pd = self.handle.product ps = self.handle.productStore descriptor = xobj.toxml(imageType.descriptor._root) ddata = dc.createDescriptorData(fromStream=descriptor) imageTypeDef = rb.getImageTypeDef(imageType.name, arch) containerRef = imageTypeDef.container.name flavorSetRef = imageTypeDef.flavorSet.name architectureRef = imageTypeDef.architecture.name self._checkBuildDef(containerRef, architectureRef, flavorSetRef) imageFields = {} for field in imageTypeDef.options._xobj.attributes: imageFields[field] = getattr(imageTypeDef.options, field) for field in ddata.getFields(): name = field.getName() if name.startswith(DESCRIPTOR_PREFIX): name = name.replace(DESCRIPTOR_PREFIX, '') imageFields[name] = field.getValue() # FIXME: Map allowSnapshots -> vmSnapshots since the smartform and # proddef differ. (RCE-2743) if 'allowSnapshots' in imageFields: imageFields['vmSnapshots'] = imageFields.pop('allowSnapshots') stages = [s.name for s in pd.getStages()] pd.addBuildDefinition( name=ddata.getField('displayName'), containerTemplateRef=containerRef, architectureRef=architectureRef, flavorSetRef=flavorSetRef, image=pd.imageType(None, imageFields), stages=stages, ) with open(ps.getProductDefinitionXmlPath(), 'w') as fh: pd.serialize(fh, validate=True) if message is None: message = 'Add image def %s' % ddata.getField('displayName') ps.commit(message=message) ps.update()
def get(self, request, cu = None, roleIds = None, troveString = None, repos = None, *args, **kwargs): name, version = troveString.split('=', 2) x = repquery.getTroves(cu, roleIds, name, version, mkUrl=request.makeUrl, thisHost=request.headers['Host']) if x is None: return response.Response(status=404) return XMLResponse(xobj.toxml(x, None))
def message(self): if self._doc is not None: return self._doc elif self._model is not None: if self._tag is not None: tag = self._tag else: tag = self._model.__tag__ return xobj.toxml(self._model, tag) else: return ''
def render(self, object_list): for obj in list(object_list): if getattr(obj, 'populateElements'): obj.populateElements(self.request) if self.model_list: self.model_list.addQueryset(object_list) obj = self.model_list response = xobj.toxml(obj, obj.__class__.__name__) return response
def info(self, request, cu, roleIds=None, fileId=None, **kwargs): path = request.GET.get('path', None) noContent = request.GET.get('nocontent', False) x = repquery.getFileInfo(cu, roleIds, fileId, mkUrl=request.makeUrl, path=path, noContent=noContent) if x is None: return response.Response(status=404) return XMLResponse(xobj.toxml(x, None))
def testGetSession(self): # Unauthenticated. We get back an empty <user/> response = self._get('session') self.failUnlessEqual(response.status_code, 200) sess = self.toXObj(response.content) self.assertXMLEquals(xobj.toxml(sess.user, xml_declaration=False), "<user/>") # Authenticated response = self._get('session', username="******", password="******") self.failUnlessEqual(response.status_code, 200) sess = self.toXObj(response.content) self.failUnlessEqual(sess.user.user_id, '2000')
def index(self, request, cu = None, roleIds = None, repos = None, *args, **kwargs): label = request.GET.get('label', None) name = request.GET.get('name', None) latest = request.GET.get('latest', 1) latest = (latest != '0') types = request.GET.get('type', []) if type(types) != list: types = [ types ] types = set(types) troves = repquery.searchNodes(cu, roleIds, label = label, mkUrl = request.makeUrl, filterSet = types, db = repos.db, name = name, latest = latest) return XMLResponse(xobj.toxml(troves, None))
def get(self, request, cu=None, roleIds=None, troveString=None, repos=None, *args, **kwargs): name, version = troveString.split('=', 2) x = repquery.getTroves(cu, roleIds, name, version, mkUrl=request.makeUrl, thisHost=request.headers['Host']) if x is None: return response.Response(status=404) return XMLResponse(xobj.toxml(x, None))
def editImageDefinition(self, buildDef, message): dc = self.handle.DescriptorConfig pd = self.handle.product ps = self.handle.productStore rb = self.handle.facade.rbuilder oldName = buildDef.name currentValues = dict(('options.%s' % k, v) for k, v in vars(buildDef.image).items()) currentValues['displayName'] = buildDef.name imageTypeDef = rb.getImageTypeDef(buildDef.containerTemplateRef, buildDef.architectureRef) descriptor = xobj.toxml(imageTypeDef.descriptor._root) ddata = dc.createDescriptorData( fromStream=descriptor, defaults=currentValues) for field in ddata.getFields(): fname = field.getName().replace('options.', '') fvalue = field.getValue() if fname == 'displayName': fname = 'name' set_func = getattr(buildDef, 'set_%s' % fname, None) else: set_func = getattr(buildDef.image, 'set_%s' % fname, None) if set_func: set_func(fvalue) with open(ps.getProductDefinitionXmlPath(), 'w') as fh: pd.serialize(fh) if message is None: message = 'Update image definition %s' % oldName ps.commit(message=message) ps.update()
def editImageDefinition(self, buildDef, message): dc = self.handle.DescriptorConfig pd = self.handle.product ps = self.handle.productStore rb = self.handle.facade.rbuilder oldName = buildDef.name currentValues = dict( ('options.%s' % k, v) for k, v in vars(buildDef.image).items()) currentValues['displayName'] = buildDef.name imageTypeDef = rb.getImageTypeDef(buildDef.containerTemplateRef, buildDef.architectureRef) descriptor = xobj.toxml(imageTypeDef.descriptor._root) ddata = dc.createDescriptorData(fromStream=descriptor, defaults=currentValues) for field in ddata.getFields(): fname = field.getName().replace('options.', '') fvalue = field.getValue() if fname == 'displayName': fname = 'name' set_func = getattr(buildDef, 'set_%s' % fname, None) else: set_func = getattr(buildDef.image, 'set_%s' % fname, None) if set_func: set_func(fvalue) with open(ps.getProductDefinitionXmlPath(), 'w') as fh: pd.serialize(fh) if message is None: message = 'Update image definition %s' % oldName ps.commit(message=message) ps.update()
def get(self, request, cu=None, roleIds=None, troveString=None, repos=None, *args, **kwargs): name, rest = troveString.split('=', 2) version, flavor = rest.split("[", 2) flavor = flavor[:-1] x = repquery.getTrove(cu, roleIds, name, version, flavor, mkUrl=request.makeUrl, thisHost=request.headers['Host'], excludeCapsules=kwargs['excludeCapsules']) if x is None: return response.Response(status=404) return XMLResponse(xobj.toxml(x, None))
def index(self, request, cu = None, roleIds = None, *args, **kwargs): l = repquery.getRepository(cu, roleIds, mkUrl = request.makeUrl) return XMLResponse(xobj.toxml(l, None))
def testGetStageLatestImages(self): stage = self.testGetProjectBranchStageImages() # Add some files to the images for i, image in enumerate(stage.images.all()): for j in range(2): k = 2 * i + j buildFile = imagesmodels.BuildFile.objects.create(image=image, size=k, sha1='%040x' % k, title='foo-%s' % i) fileUrl = imagesmodels.FileUrl.objects.create(url_type=0, url='http://example.com/%s/' % k) imagesmodels.BuildFilesUrlsMap.objects.create( file=buildFile, url=fileUrl) prj = stage.project branch = stage.project_branch url = ('projects/%s/project_branches/%s/project_branch_stages/%s/images_by_name/%s/latest_file' % (prj.short_name, branch.label, stage.name, 'image 1-1devel')) # Should be unauthenticated response = self._get_internal(url, follow=False) self.assertEquals(response.status_code, 307) self.assertEquals(response['Location'], 'http://testserver/downloadImage?fileId=8&urlType=0') # image 1-0devel has status 300, so a redirect should be issued imgName = "image 1-0devel" url = ('projects/%s/project_branches/%s/project_branch_stages/%s/images_by_name/%s/latest_file' % (prj.short_name, branch.label, stage.name, imgName)) response = self._get_internal(url, follow=False) self.assertEquals(response.status_code, 307) self.assertEquals(response['Location'], 'http://testserver/downloadImage?fileId=4&urlType=0') # Now pretend latest image with that name is still building (non-300 # status) image = stage.images.get(name=imgName, trove_version='/foo@rpath:1/12345:1-1') image.status = 200 image.save() response = self._get_internal(url, follow=False) self.assertEquals(response.status_code, 307) self.assertEquals(response['Location'], 'http://testserver/downloadImage?fileId=2&urlType=0') # Test 404 url = ('projects/%s/project_branches/%s/project_branch_stages/%s/images_by_name/%s/latest_file' % (prj.short_name, branch.label, stage.name, 'NoWayJoseph')) response = self._get_internal(url, follow=False) self.assertEquals(response.status_code, 404) url = ('projects/%s/project_branches/%s/project_branch_stages/%s/images' % (prj.short_name, branch.label, stage.name)) response = self._get(url, username='******', password='******') self.assertEquals(response.status_code, 200) doc = xobj.parse(response.content) # Make sure we don't see duplicates self.assertXMLEquals(xobj.toxml(doc.images.latest_files), """\ <latest_files> <latest_file id="http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-1/project_branch_stages/Development/images_by_name/image%201-0devel/latest_file"> <image_name>image 1-0devel</image_name> </latest_file> <latest_file id="http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-1/project_branch_stages/Development/images_by_name/image%201-1devel/latest_file"> <image_name>image 1-1devel</image_name> </latest_file> </latest_files>""")
def index(self, request, cu=None, roleIds=None, *args, **kwargs): l = repquery.getRepository(cu, roleIds, mkUrl=request.makeUrl) return XMLResponse(xobj.toxml(l, None))
def testGetProjectBranchStage(self): self._initProject() prj = models.Project.objects.get(name='chater-foo') branch = models.ProjectVersion.objects.get( project__project_id=prj.project_id, name='trunk') stage = models.Stage.objects.get( project_branch__branch_id=branch.branch_id, name='Development') # First image has no stage reference image = imagesmodels.Image(name="image-1", description="image-1", project=prj, project_branch=branch, _image_type=10, stage_name=stage.name) image.save() # Second image has a stage reference image = imagesmodels.Image(name="image-2", description="image-2", project=prj, project_branch=branch, project_branch_stage=stage, _image_type=10) image.save() url = ('projects/%s/project_branches/%s/project_branch_stages/%s' % (prj.short_name, branch.label, stage.name)) response = self._get(url, username='******', password='******') self.assertEquals(response.status_code, 200) stg = xobj.parse(response.content).project_branch_stage # Make sure we have a link for images self.failUnlessEqual(stg.images.id, 'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Development/images') # Make sure we have a project_branch self.failUnlessEqual(stg.project_branch.id, 'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk') self.failUnlessEqual(stg.project_branch.name, 'trunk') url += '/images' response = self._get(url, username="******", password="******") self.assertEquals(response.status_code, 200) images = xobj.parse(response.content).images self.failUnlessEqual(images.id, 'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Development/images;start_index=0;limit=10') imagesNodes = images.image self.failUnlessEqual([ x.name for x in imagesNodes ], ['image from fixture', 'image-1', 'image-2' ]) self.assertXMLEquals(xobj.toxml(images.latest_files), """\ <latest_files> <latest_file id="http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Development/images_by_name/image-2/latest_file"> <image_name>image-2</image_name> </latest_file> </latest_files>""") # Test project images too url = 'projects/%s' % (prj.short_name, ) response = self._get(url, username="******", password="******") self.assertEquals(response.status_code, 200) project = xobj.parse(response.content).project self.failUnlessEqual(project.images.id, 'http://testserver/api/v1/projects/chater-foo/images') url += '/images' response = self._get(url, username="******", password="******") self.assertEquals(response.status_code, 200) images = xobj.parse(response.content).images self.failUnlessEqual(images.id, 'http://testserver/api/v1/projects/chater-foo/images;start_index=0;limit=10') images = images.image self.failUnlessEqual([ x.name for x in images ], [u'image from fixture', u'image-1', u'image-2'])
def toText(self, modelInstance, context): xobjObject = self.getXobjObject(modelInstance, context) nsmap = getattr(modelInstance, 'nsmap', {}) return xobj.toxml(xobjObject, xobjObject.__class__.__name__, nsmap=nsmap)