def patch(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides coreMachine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, identity_id) esh_machine = esh_driver.get_machine(machine_id) coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff and user is not coreMachine.application.created_by: logger.warn('%s is Non-staff/non-owner trying to update a machine' % (user.username)) errorObj = failureJSON([{ 'code': 401, 'message': 'Only Staff and the machine Owner ' + 'are allowed to change machine info.'}]) return Response(errorObj, status=status.HTTP_401_UNAUTHORIZED) coreMachine.application.update(request.DATA) serializer = ProviderMachineSerializer(coreMachine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def _update_machine(self, request, provider_uuid, identity_uuid, machine_id): # TODO: Determine who is allowed to edit machines besides # core_machine.owner user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not user.is_staff\ and user is not core_machine.application.created_by: logger.warn('%s is Non-staff/non-owner trying to update a machine' % (user.username)) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " "are allowed to change machine info.") partial_update = True if request.method == 'PATCH' else False serializer = ProviderMachineSerializer(core_machine, request_user=request.user, data=data, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() if 'created_by_identity' in request.DATA: identity = serializer.object.created_by_identity update_application_owner(core_machine.application, identity) logger.info(serializer.data) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def _update_machine(self, request, provider_uuid, identity_uuid, machine_id): #TODO: Determine who is allowed to edit machines besides #core_machine.owner user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not user.is_staff\ and user is not core_machine.application.created_by: logger.warn( '%s is Non-staff/non-owner trying to update a machine' % (user.username)) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " + "are allowed to change machine info.") partial_update = True if request.method == 'PATCH' else False serializer = ProviderMachineSerializer(core_machine, request_user=request.user, data=data, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() if 'created_by_identity' in request.DATA: identity = serializer.object.created_by_identity update_application_owner(core_machine.application, identity) logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)