Exemplo n.º 1
0
    def post(self, request, provider_uuid, identity_uuid, instance_id, *args,
             **kwargs):
        """Create a new tag resource
        Params:name -- Name of the new Tag
        Returns:
        Status Code: 201 Body: A new Tag object
        Status Code: 400 Body: Errors (Duplicate/Invalid Name)
        """
        user = request.user
        data = request.data.copy()
        if 'name' not in data:
            return Response("Missing 'name' in POST data",
                            status=status.HTTP_400_BAD_REQUEST)

        core_instance = get_core_instance(request, provider_uuid,
                                          identity_uuid, instance_id)
        if not core_instance:
            instance_not_found(instance_id)

        same_name_tags = CoreTag.objects.filter(name__iexact=data['name'])
        if same_name_tags:
            add_tag = same_name_tags[0]
        else:
            data['user'] = user.username
            data['name'] = data['name'].lower()
            serializer = TagSerializer(data=data)
            if not serializer.is_valid():
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            add_tag = serializer.save()
        core_instance.tags.add(add_tag)
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 2
0
    def post(self, request, provider_uuid, identity_uuid, instance_id,
             *args, **kwargs):
        """Create a new tag resource
        Params:name -- Name of the new Tag
        Returns:
        Status Code: 201 Body: A new Tag object
        Status Code: 400 Body: Errors (Duplicate/Invalid Name)
        """
        user = request.user
        data = request.data.copy()
        if 'name' not in data:
            return Response("Missing 'name' in POST data",
                            status=status.HTTP_400_BAD_REQUEST)

        core_instance = get_core_instance(request,
                                          provider_uuid,
                                          identity_uuid,
                                          instance_id)
        if not core_instance:
            instance_not_found(instance_id)

        same_name_tags = CoreTag.objects.filter(name__iexact=data['name'])
        if same_name_tags:
            add_tag = same_name_tags[0]
        else:
            data['user'] = user.username
            data['name'] = data['name'].lower()
            serializer = TagSerializer(data=data)
            if not serializer.is_valid():
                return Response(
                    serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            add_tag = serializer.save()
        core_instance.tags.add(add_tag)
        return Response(status=status.HTTP_204_NO_CONTENT)
 def get(self, request, provider_uuid, identity_uuid, instance_id, *args, **kwargs):
     """
     List all public tags.
     """
     core_instance = get_core_instance(request, provider_uuid, identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     tags = core_instance.tags.all()
     serializer = TagSerializer(tags, many=True)
     return Response(serializer.data)
Exemplo n.º 4
0
 def get(self, request, provider_uuid, identity_uuid, instance_id, *args,
         **kwargs):
     """
     List all public tags.
     """
     core_instance = get_core_instance(request, provider_uuid,
                                       identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     tags = core_instance.tags.all()
     serializer = TagSerializer(tags, many=True)
     return Response(serializer.data)
 def get(self, request, provider_uuid, identity_uuid, instance_id, tag_slug, *args, **kwargs):
     """
     Return the credential information for this tag
     """
     core_instance = get_core_instance(request, provider_uuid, identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     try:
         tag = core_instance.tags.get(name__iexact=tag_slug)
     except CoreTag.DoesNotExist:
         return Response(["Tag does not exist"], status=status.HTTP_404_NOT_FOUND)
     serializer = TagSerializer(tag)
     return Response(serializer.data)
 def delete(self, request, provider_uuid, identity_uuid, instance_id, tag_slug, *args, **kwargs):
     """
     Remove the tag, if it is no longer in use.
     """
     core_instance = get_core_instance(request, provider_uuid, identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     try:
         tag = core_instance.tags.get(name__iexact=tag_slug)
     except CoreTag.DoesNotExist:
         return failure_response(status.HTTP_404_NOT_FOUND, "Tag %s not found on instance" % tag_slug)
     core_instance.tags.remove(tag)
     return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 7
0
 def delete(self, request, provider_uuid, identity_uuid, instance_id,
            tag_slug, *args, **kwargs):
     """
     Remove the tag, if it is no longer in use.
     """
     core_instance = get_core_instance(request, provider_uuid,
                                       identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     try:
         tag = core_instance.tags.get(name__iexact=tag_slug)
     except CoreTag.DoesNotExist:
         return failure_response(status.HTTP_404_NOT_FOUND,
                                 'Tag %s not found on instance' % tag_slug)
     core_instance.tags.remove(tag)
     return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 8
0
    def get(self, request, provider_uuid, identity_uuid, instance_id):
        """
        Authentication Required, get instance details.
        """
        user = request.user
        # NOTE: This 'Scheme' should be used across
        #       the ENTIRE API v1 (Machines, Volumes, Sizes)
        # NOTE: Especially the part below, where you end date
        #       all the things that are 'inactive'
        try:
            provider = Provider.objects.get(uuid=provider_uuid)
            if not provider.is_current():
                raise ProviderNotActive(provider)
        except Provider.DoesNotExist:
            return invalid_creds(provider_uuid, identity_uuid)
        except ProviderNotActive as pna:
            return inactive_provider(pna)

        # Cleared provider testing -- ready for driver prep.
        try:
            esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
            if not esh_driver:
                return invalid_creds(provider_uuid, identity_uuid)
            logger.info("InstanceQuery Looking for %s" % instance_id)
            esh_instance = esh_driver.get_instance(instance_id)
            logger.info("InstanceQuery Found instance %s" % esh_instance)
        except (socket_error, ConnectionFailure):
            logger.exception("Connection failure prevented InstanceQuery")
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            logger.exception("Invalid credentialsprevented InstanceQuery")
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT,
                                    str(exc.message))

        # NOTE: Especially THIS part below, where you end date all the
        #       things that are 'inactive'
        if not esh_instance:
            try:
                core_inst = CoreInstance.objects.get(
                    provider_alias=instance_id,
                    source__provider__uuid=provider_uuid,
                    created_by_identity__uuid=identity_uuid)
                core_inst.end_date_all()
            except CoreInstance.DoesNotExist:
                pass
            return instance_not_found(instance_id)

        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serialized_data = InstanceSerializer(
            core_instance,
            context={"request": request}).data
        response = Response(serialized_data)
        response['Cache-Control'] = 'no-cache'
        return response
Exemplo n.º 9
0
    def get(self, request, provider_uuid, identity_uuid, instance_id):
        """
        Authentication Required, get instance details.
        """
        user = request.user
        # NOTE: This 'Scheme' should be used across
        #       the ENTIRE API v1 (Machines, Volumes, Sizes)
        # NOTE: Especially the part below, where you end date
        #       all the things that are 'inactive'
        try:
            provider = Provider.objects.get(uuid=provider_uuid)
            if not provider.is_active():
                raise ProviderNotActive(provider)
        except Provider.DoesNotExist:
            return invalid_creds(provider_uuid, identity_uuid)
        except ProviderNotActive as pna:
            return inactive_provider(pna)

        # Cleared provider testing -- ready for driver prep.
        try:
            esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
            if not esh_driver:
                return invalid_creds(provider_uuid, identity_uuid)
            logger.info("InstanceQuery Looking for %s" % instance_id)
            esh_instance = esh_driver.get_instance(instance_id)
            logger.info("InstanceQuery Found instance %s" % esh_instance)
        except (socket_error, ConnectionFailure):
            logger.exception("Connection failure prevented InstanceQuery")
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            logger.exception("Invalid credentialsprevented InstanceQuery")
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))

        # NOTE: Especially THIS part below, where you end date all the
        #       things that are 'inactive'
        if not esh_instance:
            try:
                core_inst = CoreInstance.objects.get(
                    provider_alias=instance_id,
                    source__provider__uuid=provider_uuid,
                    created_by_identity__uuid=identity_uuid)
                core_inst.end_date_all()
            except CoreInstance.DoesNotExist:
                pass
            return instance_not_found(instance_id)

        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serialized_data = InstanceSerializer(core_instance,
                                             context={
                                                 "request": request
                                             }).data
        response = Response(serialized_data)
        response['Cache-Control'] = 'no-cache'
        return response
Exemplo n.º 10
0
 def put(self, request, provider_uuid, identity_uuid, instance_id):
     """Authentication Required, update metadata about the instance"""
     user = request.user
     data = request.data
     # Ensure item exists on the server first
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     if not can_use_instance(user, instance_id, leader_required=True):
         return member_action_forbidden(user.username, instance_id)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except LibcloudInvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
     if not esh_instance:
         return instance_not_found(instance_id)
     # Gather the DB related item and update
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serializer = InstanceSerializer(core_instance,
                                     data=data,
                                     context={"request": request})
     identity = Identity.objects.get(uuid=identity_uuid)
     if serializer.is_valid():
         logger.info('metadata = %s' % data)
         #NOTE: We shouldn't allow 'full replacement' of metadata..
         # We should also validate against potentional updating of 'atmo-used metadata'
         update_metadata.s(esh_driver.__class__,
                           esh_driver.provider,
                           esh_driver.identity,
                           esh_instance.id,
                           data,
                           replace_metadata=False).apply()
         new_instance = serializer.save()
         boot_scripts = data.pop('boot_scripts', [])
         if boot_scripts:
             new_instance = _save_scripts_to_instance(
                 new_instance, boot_scripts)
             serializer = InstanceSerializer(new_instance,
                                             context={"request": request})
         invalidate_cached_instances(identity=identity)
         response = Response(serializer.data)
         logger.info('data = %s' % serializer.data)
         response['Cache-Control'] = 'no-cache'
         return response
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 11
0
    def patch(self, request, provider_uuid, identity_uuid, instance_id):
        """Authentication Required, update metadata about the instance"""
        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)
        try:
            esh_instance = esh_driver.get_instance(instance_id)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
        if not esh_instance:
            return instance_not_found(instance_id)
        # Gather the DB related item and update
        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serializer = InstanceSerializer(core_instance,
                                        data=data,
                                        context={"request": request},
                                        partial=True)
        identity = Identity.objects.get(uuid=identity_uuid)
        provider = identity.provider

        if serializer.is_valid():
            logger.info('metadata = %s' % data)

            driver_class = esh_driver.__class__
            update_metadata.s(driver_class,
                              provider,
                              identity,
                              esh_instance.id,
                              data,
                              replace_metadata=False).apply()
            instance = serializer.save()
            boot_scripts = data.pop('boot_scripts', [])
            if boot_scripts:
                _save_scripts_to_instance(instance, boot_scripts)
            invalidate_cached_instances(identity=identity)
            response = Response(serializer.data)
            logger.info('data = %s' % serializer.data)
            response['Cache-Control'] = 'no-cache'
            return response
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 12
0
 def put(self, request, provider_uuid, identity_uuid, instance_id):
     """Authentication Required, update metadata about the instance"""
     user = request.user
     data = request.data
     # Ensure item exists on the server first
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     if not can_use_instance(user, instance_id, leader_required=True):
         return member_action_forbidden(user.username, instance_id)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except LibcloudInvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT,
                                 str(exc.message))
     if not esh_instance:
         return instance_not_found(instance_id)
     # Gather the DB related item and update
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serializer = InstanceSerializer(core_instance, data=data,
                                     context={"request": request})
     identity = Identity.objects.get(uuid=identity_uuid)
     if serializer.is_valid():
         logger.info('metadata = %s' % data)
         #NOTE: We shouldn't allow 'full replacement' of metadata..
         # We should also validate against potentional updating of 'atmo-used metadata'
         update_metadata.s(esh_driver.__class__, esh_driver.provider, esh_driver.identity, esh_instance.id,
                           data, replace_metadata=False).apply()
         new_instance = serializer.save()
         boot_scripts = data.pop('boot_scripts', [])
         if boot_scripts:
             new_instance = _save_scripts_to_instance(new_instance,
                                                      boot_scripts)
             serializer = InstanceSerializer(
                 new_instance,
                 context={"request": request})
         invalidate_cached_instances(identity=identity)
         response = Response(serializer.data)
         logger.info('data = %s' % serializer.data)
         response['Cache-Control'] = 'no-cache'
         return response
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 13
0
    def get(self, request, provider_uuid, identity_uuid, instance_id, tag_slug,
            *args, **kwargs):
        """
        Return the credential information for this tag
        """
        try:
            core_instance = get_core_instance(request, provider_uuid,
                                              identity_uuid, instance_id)
        except ProviderNotActive as pna:
            return inactive_provider(pna)
        except Exception as e:
            return failure_response(status.HTTP_409_CONFLICT, e.message)

        if not core_instance:
            instance_not_found(instance_id)
        try:
            tag = core_instance.tags.get(name__iexact=tag_slug)
        except CoreTag.DoesNotExist:
            return Response(['Tag does not exist'],
                            status=status.HTTP_404_NOT_FOUND)
        serializer = TagSerializer(tag)
        return Response(serializer.data)
Exemplo n.º 14
0
    def patch(self, request, provider_uuid, identity_uuid, instance_id):
        """Authentication Required, update metadata about the instance"""
        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)
        try:
            esh_instance = esh_driver.get_instance(instance_id)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except InvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT,
                                    str(exc.message))
        if not esh_instance:
            return instance_not_found(instance_id)
        # Gather the DB related item and update
        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serializer = InstanceSerializer(
            core_instance, data=data,
            context={"request": request}, partial=True)
        identity = Identity.objects.get(uuid=identity_uuid)
        provider = identity.provider

        if serializer.is_valid():
            logger.info('metadata = %s' % data)

            driver_class = esh_driver.__class__
            update_metadata.s(driver_class, provider, identity, esh_instance.id,
                              data, replace_metadata=False).apply()
            instance = serializer.save()
            boot_scripts = data.pop('boot_scripts', [])
            if boot_scripts:
                _save_scripts_to_instance(instance, boot_scripts)
            invalidate_cached_instances(identity=identity)
            response = Response(serializer.data)
            logger.info('data = %s' % serializer.data)
            response['Cache-Control'] = 'no-cache'
            return response
        else:
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 15
0
    def get(self, request, provider_uuid, identity_uuid, instance_id,
            tag_slug, *args, **kwargs):
        """
        Return the credential information for this tag
        """
        try:
            core_instance = get_core_instance(request, provider_uuid,
                                              identity_uuid, instance_id)
        except ProviderNotActive as pna:
            return inactive_provider(pna)
        except Exception as e:
            return failure_response(
                status.HTTP_409_CONFLICT,
                e.message)

        if not core_instance:
            instance_not_found(instance_id)
        try:
            tag = core_instance.tags.get(name__iexact=tag_slug)
        except CoreTag.DoesNotExist:
            return Response(['Tag does not exist'],
                            status=status.HTTP_404_NOT_FOUND)
        serializer = TagSerializer(tag)
        return Response(serializer.data)
    def get(self, request, provider_uuid, identity_uuid, instance_id):
        """
        Authentication Required, get instance details.
        """
        user = request.user
        # NOTE: This 'Scheme' should be used across
        #       the ENTIRE API v1 (Machines, Volumes, Sizes)
        # NOTE: Especially the part below, where you end date
        #       all the things that are 'inactive'
        try:
            provider = Provider.objects.get(uuid=provider_uuid)
        except Provider.DoesNotExist:
            return invalid_creds(provider_uuid, identity_uuid)
        if provider.is_active():
            esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
            try:
                esh_instance = esh_driver.get_instance(instance_id)
            except (socket_error, ConnectionFailure):
                return connection_failure(provider_uuid, identity_uuid)
            except InvalidCredsError:
                return invalid_creds(provider_uuid, identity_uuid)
            except Exception as exc:
                logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT")
                return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
        else:
            esh_instance = None

        # NOTE: Especially THIS part below, where you end date all the
        #       things that are 'inactive'
        if not esh_instance:
            try:
                core_inst = CoreInstance.objects.get(
                    provider_alias=instance_id,
                    source__provider__uuid=provider_uuid,
                    created_by_identity__uuid=identity_uuid,
                )
                core_inst.end_date_all()
            except CoreInstance.DoesNotExist:
                pass
            return instance_not_found(instance_id)

        core_instance = convert_esh_instance(esh_driver, esh_instance, provider_uuid, identity_uuid, user)
        serialized_data = InstanceSerializer(core_instance, context={"request": request}).data
        response = Response(serialized_data)
        response["Cache-Control"] = "no-cache"
        return response