def put(self, request): if request.user.is_superuser: try: group = poem_models.GroupOfMetrics.objects.get( name=request.data['name'] ) for name in dict(request.data)['items']: metric = poem_models.Metric.objects.get(name=name) if metric.group != group: metric.group = group metric.save() create_history(metric, request.user.username) # remove the metrics that existed before, and now were removed metrics = poem_models.Metric.objects.filter(group=group) for metric in metrics: if metric.name not in dict(request.data)['items']: metric.group = None metric.save() create_history(metric, request.user.username) return Response(status=status.HTTP_201_CREATED) except poem_models.GroupOfMetrics.DoesNotExist: raise NotFound( status=404, detail='Group of metrics does not exist.' ) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to change groups of metrics.' )
def post(self, request): if request.user.is_superuser: try: group = poem_models.GroupOfMetrics.objects.create( name=request.data['name'] ) if 'items' in dict(request.data): for name in dict(request.data)['items']: metric = poem_models.Metric.objects.get(name=name) metric.group = group metric.save() create_history(metric, request.user.username) except IntegrityError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Group of metrics with this name already exists.' ) else: return Response(status=status.HTTP_201_CREATED) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to add groups of metrics.' )
def post(self, request): package_name = request.data['package'].split(' ')[0] package_version = request.data['package'].split(' ')[1][1:-1] try: probe = admin_models.Probe.objects.create( name=request.data['name'], package=admin_models.Package.objects.get( name=package_name, version=package_version), repository=request.data['repository'], docurl=request.data['docurl'], description=request.data['description'], comment=request.data['comment'], user=request.user.username, datetime=datetime.datetime.now()) if request.data['cloned_from']: clone = admin_models.Probe.objects.get( id=request.data['cloned_from']) comment = 'Derived from {} ({}).'.format( clone.name, clone.package.version) create_history(probe, probe.user, comment=comment) else: create_history(probe, probe.user) return Response(status=status.HTTP_201_CREATED) except IntegrityError: return Response({'detail': 'Probe with this name already exists.'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps([request.data['probeexecutable']]) else: probeexecutable = '' try: if request.data['mtype'] == 'Active': mt = admin_models.MetricTemplate.objects.create( name=request.data['name'], mtype=admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), probekey=admin_models.ProbeHistory.objects.get( name=request.data['probeversion'].split(' ')[0], package__version=request.data['probeversion'].split( ' ')[1][1:-1]), parent=parent, probeexecutable=probeexecutable, config=inline_metric_for_db(request.data['config']), attribute=inline_metric_for_db(request.data['attribute']), dependency=inline_metric_for_db( request.data['dependency']), flags=inline_metric_for_db(request.data['flags']), files=inline_metric_for_db(request.data['files']), parameter=inline_metric_for_db(request.data['parameter']), fileparameter=inline_metric_for_db( request.data['fileparameter'])) else: mt = admin_models.MetricTemplate.objects.create( name=request.data['name'], mtype=admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), parent=parent, flags=inline_metric_for_db(request.data['flags'])) if request.data['cloned_from']: clone = admin_models.MetricTemplate.objects.get( id=request.data['cloned_from']) comment = 'Derived from ' + clone.name create_history(mt, request.user.username, comment=comment) else: create_history(mt, request.user.username) return Response(status=status.HTTP_201_CREATED) except IntegrityError: return Response( {'detail': 'Metric template with this name already exists.'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: group = poem_models.GroupOfMetrics.objects.create( name=request.data['name']) if 'items' in dict(request.data): for name in dict(request.data)['items']: metric = poem_models.Metric.objects.get(name=name) metric.group = group metric.save() create_history(metric, request.user.username) except IntegrityError: return Response( {'detail': 'Group of metrics with this name already exists.'}, status=status.HTTP_400_BAD_REQUEST) else: return Response(status=status.HTTP_201_CREATED)
def put(self, request): group = poem_models.GroupOfMetrics.objects.get( name=request.data['name']) for name in dict(request.data)['items']: metric = poem_models.Metric.objects.get(name=name) if metric.group != group: metric.group = group metric.save() create_history(metric, request.user.username) # remove the metrics that existed before, and now were removed metrics = poem_models.Metric.objects.filter(group=group) for metric in metrics: if metric.name not in dict(request.data)['items']: metric.group = None metric.save() create_history(metric, request.user.username) return Response(status=status.HTTP_201_CREATED)
def put(self, request): metric = poem_models.Metric.objects.get(name=request.data['name']) if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps([request.data['probeexecutable']]) else: probeexecutable = '' metric.name = request.data['name'] metric.mtype = poem_models.MetricType.objects.get( name=request.data['mtype']) metric.group = poem_models.GroupOfMetrics.objects.get( name=request.data['group']) metric.parent = parent metric.flags = inline_metric_for_db(request.data['flags']) if request.data['mtype'] == 'Active': metric.probekey = admin_models.ProbeHistory.objects.get( name=request.data['probeversion'].split(' ')[0], package__version=request.data['probeversion'].split( ' ')[1][1:-1]) metric.probeexecutable = probeexecutable metric.config = inline_metric_for_db(request.data['config']) metric.attribute = inline_metric_for_db(request.data['attribute']) metric.dependancy = inline_metric_for_db( request.data['dependancy']) metric.files = inline_metric_for_db(request.data['files']) metric.parameter = inline_metric_for_db(request.data['parameter']) metric.fileparameter = inline_metric_for_db( request.data['fileparameter']) metric.save() create_history(metric, request.user.username) return Response(status=status.HTTP_201_CREATED)
def put(self, request): schemas = list(Tenant.objects.all().values_list('schema_name', flat=True)) schemas.remove(get_public_schema_name()) probe = admin_models.Probe.objects.get(id=request.data['id']) old_name = probe.name package_name = request.data['package'].split(' ')[0] package_version = request.data['package'].split(' ')[1][1:-1] package = admin_models.Package.objects.get(name=package_name, version=package_version) old_version = probe.package.version try: if package.version != old_version: probe.name = request.data['name'] probe.package = package probe.repository = request.data['repository'] probe.docurl = request.data['docurl'] probe.description = request.data['description'] probe.comment = request.data['comment'] probe.user = request.user.username probe.save() create_history(probe, probe.user) if request.data['update_metrics'] in [True, 'true', 'True']: metrictemplates = \ admin_models.MetricTemplate.objects.filter( probekey__name=old_name, probekey__package__version=old_version ) for metrictemplate in metrictemplates: metrictemplate.probekey = \ admin_models.ProbeHistory.objects.get( name=probe.name, package__version=probe.package.version ) metrictemplate.save() create_history(metrictemplate, request.user.username) else: history = admin_models.ProbeHistory.objects.filter( name=old_name, package__version=old_version) probekey = history[0] new_data = { 'name': request.data['name'], 'package': package, 'description': request.data['description'], 'comment': request.data['comment'], 'repository': request.data['repository'], 'docurl': request.data['docurl'], 'user': request.user.username } admin_models.Probe.objects.filter(pk=probe.id).update( **new_data) del new_data['user'] new_data.update({ 'version_comment': update_comment( admin_models.Probe.objects.get(id=request.data['id'])) }) history.update(**new_data) # update Metric history in case probekey name has changed: if request.data['name'] != old_name: for schema in schemas: with schema_context(schema): metrics = poem_models.Metric.objects.filter( probekey=probekey) for metric in metrics: vers = poem_models.TenantHistory.objects.filter( object_id=metric.id) for ver in vers: serialized_data = json.loads( ver.serialized_data) serialized_data[0]['fields']['probekey'] = \ [request.data['name'], package.version] ver.serialized_data = json.dumps( serialized_data) ver.save() return Response(status=status.HTTP_201_CREATED) except IntegrityError: return Response({'detail': 'Probe with this name already exists.'}, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): schemas = list(Tenant.objects.all().values_list('schema_name', flat=True)) schemas.remove(get_public_schema_name()) if request.tenant.schema_name == get_public_schema_name() and \ request.user.is_superuser: try: probe = admin_models.Probe.objects.get(id=request.data['id']) old_name = probe.name package_name = request.data['package'].split(' ')[0] package_version = request.data['package'].split(' ')[1][1:-1] package = admin_models.Package.objects.get( name=package_name, version=package_version) old_version = probe.package.version if package.version != old_version: probe.name = request.data['name'] probe.package = package probe.repository = request.data['repository'] probe.docurl = request.data['docurl'] probe.description = request.data['description'] probe.comment = request.data['comment'] probe.user = request.user.username probe.save() create_history(probe, probe.user) if request.data['update_metrics'] in [ True, 'true', 'True' ]: metrictemplates = \ admin_models.MetricTemplate.objects.filter( probekey__name=old_name, probekey__package__version=old_version ) for metrictemplate in metrictemplates: metrictemplate.probekey = \ admin_models.ProbeHistory.objects.get( name=probe.name, package__version=probe.package.version ) metrictemplate.save() create_history(metrictemplate, request.user.username) else: history = admin_models.ProbeHistory.objects.filter( name=old_name, package__version=old_version) probekey = history[0] new_data = { 'name': request.data['name'], 'package': package, 'description': request.data['description'], 'comment': request.data['comment'], 'repository': request.data['repository'], 'docurl': request.data['docurl'], 'user': request.user.username } admin_models.Probe.objects.filter(pk=probe.id).update( **new_data) del new_data['user'] new_data.update({ 'version_comment': update_comment( admin_models.Probe.objects.get( id=request.data['id'])) }) history.update(**new_data) # update Metric history in case probekey name has changed: if request.data['name'] != old_name: for schema in schemas: with schema_context(schema): metrics = poem_models.Metric.objects.filter( probekey=probekey) for metric in metrics: vers = \ poem_models.TenantHistory.objects.filter( object_id=metric.id ) for ver in vers: serialized_data = json.loads( ver.serialized_data) serialized_data[0]['fields'][ 'probekey' ] = \ [request.data['name'], package.version] ver.serialized_data = json.dumps( serialized_data) ver.save() return Response(status=status.HTTP_201_CREATED) except admin_models.Probe.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Probe does not exist.') except IndexError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Package version should be specified.') except admin_models.Package.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Package does not exist.') except IntegrityError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Probe with this name already exists.') except KeyError as e: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Missing data key: {}'.format( e.args[0])) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to change probes.')
def post(self, request): if request.tenant.schema_name == get_public_schema_name() and \ request.user.is_superuser: try: package_name = request.data['package'].split(' ')[0] package_version = request.data['package'].split(' ')[1][1:-1] probe = admin_models.Probe( name=request.data['name'], package=admin_models.Package.objects.get( name=package_name, version=package_version), repository=request.data['repository'], docurl=request.data['docurl'], description=request.data['description'], comment=request.data['comment'], user=request.user.username, datetime=datetime.datetime.now()) if 'cloned_from' in request.data and \ request.data['cloned_from']: try: clone = admin_models.Probe.objects.get( id=request.data['cloned_from']) comment = 'Derived from {} ({}).'.format( clone.name, clone.package.version) probe.save() create_history(probe, probe.user, comment=comment) except admin_models.Probe.DoesNotExist: return error_response( status_code=status.HTTP_404_NOT_FOUND, detail='Probe from which to clone does not exist.') else: probe.save() create_history(probe, probe.user) return Response(status=status.HTTP_201_CREATED) except IntegrityError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Probe with this name already exists.') except admin_models.Package.DoesNotExist: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Package does not exist.') except IndexError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Package version should be specified.') except KeyError as e: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Missing data key: {}'.format( e.args[0])) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to add probes.')
def put(self, request): if request.tenant.schema_name == get_public_schema_name() and \ request.user.is_superuser: try: metrictemplate = admin_models.MetricTemplate.objects.get( id=request.data['id']) old_name = metrictemplate.name old_probekey = metrictemplate.probekey old_tags = set([tag.name for tag in metrictemplate.tags.all()]) if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps( [request.data['probeexecutable']]) else: probeexecutable = '' new_tags = set() if 'tags' in dict(request.data): new_tags = set(dict(request.data)['tags']) if request.data['probeversion']: probe_name = request.data['probeversion'].split(' ')[0] probe_version = request.data['probeversion'].split( ' ')[1][1:-1] new_probekey = admin_models.ProbeHistory.objects.get( name=probe_name, package__version=probe_version) else: new_probekey = None if request.data['mtype'] == 'Active' and \ old_probekey != new_probekey: metrictemplate.name = request.data['name'] metrictemplate.probekey = new_probekey metrictemplate.description = request.data['description'] metrictemplate.parent = parent metrictemplate.probeexecutable = probeexecutable metrictemplate.config = inline_metric_for_db( request.data['config']) metrictemplate.attribute = inline_metric_for_db( request.data['attribute']) metrictemplate.dependency = inline_metric_for_db( request.data['dependency']) metrictemplate.flags = inline_metric_for_db( request.data['flags']) metrictemplate.files = inline_metric_for_db( request.data['files']) metrictemplate.parameter = inline_metric_for_db( request.data['parameter']) metrictemplate.fileparameter = inline_metric_for_db( request.data['fileparameter']) metrictemplate.save() if old_tags.difference(new_tags): for tag in old_tags.difference(new_tags): metrictemplate.tags.remove( admin_models.MetricTags.objects.get(name=tag)) if new_tags.difference(old_tags): for tag in new_tags.difference(old_tags): try: mtag = admin_models.MetricTags.objects.get( name=tag) except admin_models.MetricTags.DoesNotExist: mtag = admin_models.MetricTags.objects.create( name=tag) metrictemplate.tags.add(mtag) create_history(metrictemplate, request.user.username) else: new_data = { 'name': request.data['name'], 'probekey': new_probekey, 'mtype': admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), 'description': request.data['description'], 'parent': parent, 'probeexecutable': probeexecutable, 'config': inline_metric_for_db(request.data['config']), 'attribute': inline_metric_for_db(request.data['attribute']), 'dependency': inline_metric_for_db(request.data['dependency']), 'flags': inline_metric_for_db(request.data['flags']), 'files': inline_metric_for_db(request.data['files']), 'parameter': inline_metric_for_db(request.data['parameter']), 'fileparameter': inline_metric_for_db(request.data['fileparameter']) } admin_models.MetricTemplate.objects.filter( id=request.data['id']).update(**new_data) mt = admin_models.MetricTemplate.objects.get( pk=request.data['id']) tags_to_remove = None if old_tags.difference(new_tags): tags_to_remove = admin_models.MetricTags.objects.filter( name__in=old_tags.difference(new_tags)) for tag in tags_to_remove: mt.tags.remove(tag) tags_to_add = None if new_tags.difference(old_tags): tags_to_add = [] for tag in new_tags.difference(old_tags): try: mtag = admin_models.MetricTags.objects.get( name=tag) except admin_models.MetricTags.DoesNotExist: mtag = admin_models.MetricTags.objects.create( name=tag) tags_to_add.append(mtag) mt.tags.add(mtag) new_data.update({ 'version_comment': update_comment( admin_models.MetricTemplate.objects.get( id=request.data['id'])) }) admin_models.MetricTemplateHistory.objects.filter( name=old_name, probekey=old_probekey).update(**new_data) history = admin_models.MetricTemplateHistory.objects.get( name=request.data['name'], probekey=new_probekey) if tags_to_remove: for tag in tags_to_remove: history.tags.remove(tag) if tags_to_add: for tag in tags_to_add: history.tags.add(tag) msgs = update_metrics(mt, old_name, old_probekey) if msgs: return error_response( detail='\n'.join(msgs), status_code=status.HTTP_418_IM_A_TEAPOT) return Response(status=status.HTTP_201_CREATED) except admin_models.MetricTemplate.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Metric template does not exist.') except KeyError as e: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Missing data key: {}'.format( e.args[0])) except admin_models.ProbeHistory.DoesNotExist: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Probe version does not exist.') except IndexError: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Probe version not specified.') except IntegrityError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Metric template with this name already exists.') else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to change metric templates.' )
def put(self, request): try: userprofile = poem_models.UserProfile.objects.get( user=request.user) metric = poem_models.Metric.objects.get(name=request.data['name']) init_group = metric.group if not request.user.is_superuser and \ userprofile.groupsofmetrics.all().count() == 0: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to change metrics.') if not request.user.is_superuser and \ init_group not in userprofile.groupsofmetrics.all(): return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to change metrics ' 'in this group.') else: if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps( [request.data['probeexecutable']]) else: probeexecutable = '' if request.data['description']: description = request.data['description'] else: description = '' metric_group = poem_models.GroupOfMetrics.objects.get( name=request.data['group']) metric_type = poem_models.MetricType.objects.get( name=request.data['mtype']) user_perm = request.user.is_superuser or \ metric_group in userprofile.groupsofmetrics.all() if user_perm: metric.name = request.data['name'] metric.mtype = metric_type metric.group = poem_models.GroupOfMetrics.objects.get( name=request.data['group']) metric.description = description metric.parent = parent metric.flags = inline_metric_for_db(request.data['flags']) if request.data['mtype'] == 'Active': metric.probekey = admin_models.ProbeHistory.objects.get( name=request.data['probeversion'].split(' ')[0], package__version=request.data['probeversion']. split(' ')[1][1:-1]) metric.probeexecutable = probeexecutable metric.config = inline_metric_for_db( request.data['config']) metric.attribute = inline_metric_for_db( request.data['attribute']) metric.dependancy = inline_metric_for_db( request.data['dependancy']) metric.files = inline_metric_for_db( request.data['files']) metric.parameter = inline_metric_for_db( request.data['parameter']) metric.fileparameter = inline_metric_for_db( request.data['fileparameter']) metric.save() mt = admin_models.MetricTemplateHistory.objects.get( name=metric.name, probekey=metric.probekey) create_history(metric, request.user.username, tags=mt.tags.all()) return Response(status=status.HTTP_201_CREATED) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to assign metrics ' 'to the given group.') except poem_models.UserProfile.DoesNotExist: return error_response( status_code=status.HTTP_404_NOT_FOUND, detail='No user profile for authenticated user.') except poem_models.Metric.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Metric does not exist.') except poem_models.GroupOfMetrics.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Group of metrics does not exist.') except poem_models.MetricType.DoesNotExist: return error_response(status_code=status.HTTP_404_NOT_FOUND, detail='Metric type does not exist.') except KeyError as e: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Missing data key: {}'.format( e.args[0]))
def import_metrics(metrictemplates, tenant, user): imported = [] warn_imported = [] not_imported = [] unavailable = [] for template in metrictemplates: imported_different_version = False mt = admin_models.MetricTemplate.objects.get(name=template) mtype = poem_models.MetricType.objects.get(name=mt.mtype.name) gr = poem_models.GroupOfMetrics.objects.get(name=tenant.name.upper()) packages = [] for m in poem_models.Metric.objects.all(): if m.probekey: packages.append(m.probekey.package) package_names = [package.name for package in packages] try: if mt.probekey: if mt.probekey.package.name in package_names and \ mt.probekey.package not in packages: package_version = [ package for package in packages if package.name == mt.probekey.package.name ][0] try: ver = admin_models.ProbeHistory.objects.get( name=mt.probekey.name, package=package_version) mt = admin_models.MetricTemplateHistory.objects.get( name=mt.name, probekey=ver) imported_different_version = True except admin_models.ProbeHistory.DoesNotExist: unavailable.append(mt.name) continue except admin_models.MetricTemplateHistory.DoesNotExist: unavailable.append(mt.name) continue else: ver = mt.probekey metric = poem_models.Metric.objects.create( name=mt.name, mtype=mtype, probekey=ver, description=mt.description, parent=mt.parent, group=gr, probeexecutable=mt.probeexecutable, config=mt.config, attribute=mt.attribute, dependancy=mt.dependency, flags=mt.flags, files=mt.files, parameter=mt.parameter, fileparameter=mt.fileparameter) else: metric = poem_models.Metric.objects.create( name=mt.name, mtype=mtype, description=mt.description, parent=mt.parent, flags=mt.flags, group=gr) new_tags = [tag for tag in mt.tags.all()] create_history(metric, user.username, comment='Initial version.', tags=new_tags) if imported_different_version: warn_imported.append(mt.name) else: imported.append(mt.name) except IntegrityError: not_imported.append(mt.name) continue return imported, warn_imported, not_imported, unavailable
def update_metric_in_schema(mt_id, name, pk_id, schema, update_from_history=False, user=''): if update_from_history: mt_model = admin_models.MetricTemplateHistory else: mt_model = admin_models.MetricTemplate metrictemplate = mt_model.objects.get(pk=mt_id) if pk_id: probekey = admin_models.ProbeHistory.objects.get(pk=pk_id) else: probekey = None msgs = '' with schema_context(schema): try: met = poem_models.Metric.objects.get(name=name, probekey=probekey) met.name = metrictemplate.name met.probekey = metrictemplate.probekey met.probeexecutable = metrictemplate.probeexecutable met.description = metrictemplate.description met.parent = metrictemplate.parent met.attribute = metrictemplate.attribute met.dependancy = metrictemplate.dependency met.flags = metrictemplate.flags met.files = metrictemplate.files met.parameter = metrictemplate.parameter met.fileparameter = metrictemplate.fileparameter if metrictemplate.config: if update_from_history: met.config = metrictemplate.config else: for item in json.loads(metrictemplate.config): if item.split(' ')[0] == 'path': objpath = item metconfig = [] for item in json.loads(met.config): if item.split(' ')[0] == 'path': metconfig.append(objpath) else: metconfig.append(item) met.config = json.dumps(metconfig) met.save() tags = [tag for tag in metrictemplate.tags.all()] if update_from_history or met.probekey != probekey: create_history(met, user, tags=tags) else: history = poem_models.TenantHistory.objects.filter( object_id=met.id, content_type=ContentType.objects.get_for_model( poem_models.Metric))[0] history.serialized_data = serialize_metric(met, tags=tags) history.object_repr = met.__str__() history.save() if name != met.name: msgs = update_metrics_in_profiles(name, met.name) except poem_models.Metric.DoesNotExist: pass return msgs
def post(self, request): imported = [] err = [] for template in dict(request.data)['metrictemplates']: metrictemplate = admin_models.MetricTemplate.objects.get( name=template) mt = poem_models.MetricType.objects.get( name=metrictemplate.mtype.name) gr = poem_models.GroupOfMetrics.objects.get( name=request.tenant.name.upper()) try: if metrictemplate.probekey: ver = admin_models.ProbeHistory.objects.get( name=metrictemplate.probekey.name, package__version=metrictemplate.probekey.package. version) metric = poem_models.Metric.objects.create( name=metrictemplate.name, mtype=mt, probekey=ver, parent=metrictemplate.parent, group=gr, probeexecutable=metrictemplate.probeexecutable, config=metrictemplate.config, attribute=metrictemplate.attribute, dependancy=metrictemplate.dependency, flags=metrictemplate.flags, files=metrictemplate.files, parameter=metrictemplate.parameter, fileparameter=metrictemplate.fileparameter) else: metric = poem_models.Metric.objects.create( name=metrictemplate.name, mtype=mt, parent=metrictemplate.parent, flags=metrictemplate.flags, group=gr) create_history(metric, request.user.username, comment='Initial version.') imported.append(metrictemplate.name) except IntegrityError: err.append(metrictemplate.name) continue if imported: if len(imported) == 1: message_bit = '{} has'.format(imported[0]) else: message_bit = ', '.join(msg for msg in imported) + ' have' if err: if len(err) == 1: error_bit = '{} has'.format(err[0]) else: error_bit = ', '.join(msg for msg in err) + ' have' if imported and err: data = { 'imported': '{} been successfully imported.'.format(message_bit), 'err': '{} not been imported, since those metrics already exist ' 'in the database.'.format(error_bit) } elif imported and not err: data = { 'imported': '{} been successfully imported.'.format(message_bit), } elif not imported and err: data = { 'err': '{} not been imported, since those metrics already exist ' 'in the database.'.format(error_bit) } return Response(status=status.HTTP_201_CREATED, data=data)
def put(self, request): metrictemplate = admin_models.MetricTemplate.objects.get( id=request.data['id']) old_name = metrictemplate.name old_probekey = metrictemplate.probekey if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps([request.data['probeexecutable']]) else: probeexecutable = '' if request.data['probeversion']: new_probekey = admin_models.ProbeHistory.objects.get( name=request.data['probeversion'].split(' ')[0], package__version=request.data['probeversion'].split( ' ')[1][1:-1]) else: new_probekey = None try: if request.data['mtype'] == 'Active' and \ old_probekey != new_probekey: metrictemplate.name = request.data['name'] metrictemplate.probekey = new_probekey metrictemplate.parent = parent metrictemplate.probeexecutable = probeexecutable metrictemplate.config = inline_metric_for_db( request.data['config']) metrictemplate.attribute = inline_metric_for_db( request.data['attribute']) metrictemplate.dependency = inline_metric_for_db( request.data['dependency']) metrictemplate.flags = inline_metric_for_db( request.data['flags']) metrictemplate.files = inline_metric_for_db( request.data['files']) metrictemplate.parameter = inline_metric_for_db( request.data['parameter']) metrictemplate.fileparameter = inline_metric_for_db( request.data['fileparameter']) metrictemplate.save() create_history(metrictemplate, request.user.username) else: new_data = { 'name': request.data['name'], 'probekey': new_probekey, 'mtype': admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), 'parent': parent, 'probeexecutable': probeexecutable, 'config': inline_metric_for_db(request.data['config']), 'attribute': inline_metric_for_db(request.data['attribute']), 'dependency': inline_metric_for_db(request.data['dependency']), 'flags': inline_metric_for_db(request.data['flags']), 'files': inline_metric_for_db(request.data['files']), 'parameter': inline_metric_for_db(request.data['parameter']), 'fileparameter': inline_metric_for_db(request.data['fileparameter']) } admin_models.MetricTemplate.objects.filter( id=request.data['id']).update(**new_data) new_data.update({ 'version_comment': update_comment( admin_models.MetricTemplate.objects.get( id=request.data['id'])) }) admin_models.MetricTemplateHistory.objects.filter( name=old_name, probekey=old_probekey).update(**new_data) mt = admin_models.MetricTemplate.objects.get( pk=request.data['id']) update_metrics(mt, old_name, old_probekey) return Response(status=status.HTTP_201_CREATED) except IntegrityError: return Response( {'detail': 'Metric template with this name already exists.'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): if request.tenant.schema_name == get_public_schema_name() and \ request.user.is_superuser: try: if request.data['parent']: parent = json.dumps([request.data['parent']]) else: parent = '' if request.data['probeexecutable']: probeexecutable = json.dumps( [request.data['probeexecutable']]) else: probeexecutable = '' if request.data['mtype'] == 'Active': probe_name = request.data['probeversion'].split(' ')[0] probe_version = request.data['probeversion'].split( ' ')[1][1:-1] mt = admin_models.MetricTemplate.objects.create( name=request.data['name'], mtype=admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), probekey=admin_models.ProbeHistory.objects.get( name=probe_name, package__version=probe_version), description=request.data['description'], parent=parent, probeexecutable=probeexecutable, config=inline_metric_for_db(request.data['config']), attribute=inline_metric_for_db( request.data['attribute']), dependency=inline_metric_for_db( request.data['dependency']), flags=inline_metric_for_db(request.data['flags']), files=inline_metric_for_db(request.data['files']), parameter=inline_metric_for_db( request.data['parameter']), fileparameter=inline_metric_for_db( request.data['fileparameter'])) if 'tags' in dict(request.data): for tag_name in dict(request.data)['tags']: try: tag = admin_models.MetricTags.objects.get( name=tag_name) except admin_models.MetricTags.DoesNotExist: tag = admin_models.MetricTags.objects.create( name=tag_name) mt.tags.add(tag) else: mt = admin_models.MetricTemplate.objects.create( name=request.data['name'], mtype=admin_models.MetricTemplateType.objects.get( name=request.data['mtype']), description=request.data['description'], parent=parent, flags=inline_metric_for_db(request.data['flags'])) if request.data['cloned_from']: clone = admin_models.MetricTemplate.objects.get( id=request.data['cloned_from']) comment = 'Derived from ' + clone.name create_history(mt, request.user.username, comment=comment) else: create_history(mt, request.user.username) return Response(status=status.HTTP_201_CREATED) except IntegrityError: return error_response( status_code=status.HTTP_400_BAD_REQUEST, detail='Metric template with this name already exists.') except admin_models.ProbeHistory.DoesNotExist: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Probe version does not exist.') except IndexError: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Probe version not specified.') except KeyError as e: return error_response(status_code=status.HTTP_400_BAD_REQUEST, detail='Missing data key: {}'.format( e.args[0])) else: return error_response( status_code=status.HTTP_401_UNAUTHORIZED, detail='You do not have permission to add metric templates.')