예제 #1
0
class InternalAccountViewSet(
    InternalViewSetMixin,
    mixins.CreateModelMixin,
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin,
    AccountViewSet,
):
    """
    Create, retrieve, update, delete, or list AwsCloudAccounts for internal use.

    Unlike most of our other internal ViewSets, this one extends our existing public
    AccountViewSet in order to move our "legacy" writable API for accounts out of the
    public space and into the internal space.

    This ViewSet uses custom permission and authentication handling to force only the
    "create" action (from HTTP POST) to have a proper User from the identity header. We
    need that authenticated User in order to create the CloudAccount object.
    """

    queryset = models.CloudAccount.objects.all()
    schema = schemas.DescriptiveAutoSchema(
        "cloud account",
        custom_responses={"DELETE": {"202": {"description": ""}}},
        operation_id_base="Account",
    )
    serializer_class = CloudAccountSerializer

    def get_permissions(self):
        """Instantiate and return the list of permissions that this view requires."""
        if self.action == "create":
            permission_classes = [permissions.IsAuthenticated]
        else:
            permission_classes = self.permission_classes
        return [permission() for permission in permission_classes]

    def get_authenticators(self):
        """Instantiate and return the list of authenticators that this view can use."""
        # Note: We can't use .action like get_permissions because get_authenticators is
        # called from initialize_request *before* .action is set on the request object.
        if self.request.method.lower() == "post":
            authentication_classes = [IdentityHeaderAuthenticationInternalCreateUser]
        else:
            authentication_classes = self.authentication_classes
        return [auth() for auth in authentication_classes]

    def destroy(self, request, *args, **kwargs):
        """Destroy the CloudAccount but return 202 instead of 204."""
        response = super(InternalAccountViewSet, self).destroy(request, *args, **kwargs)
        if response.status_code == status.HTTP_204_NO_CONTENT:
            response = Response(
                data=response.data,
                status=status.HTTP_202_ACCEPTED,
                template_name=response.template_name,
                content_type=response.content_type,
            )
        return response

    def perform_destroy(self, instance):
        """Delay an async task to destroy the instance."""
        tasks.delete_cloud_account.delay(instance.id)
예제 #2
0
class AccountViewSet(viewsets.ReadOnlyModelViewSet):
    """List all or retrieve a single cloud account."""

    schema = schemas.DescriptiveAutoSchema("cloud account", tags=["api-v2"])
    serializer_class = serializers.CloudAccountSerializer
    queryset = models.CloudAccount.objects.all()
    filter_backends = (
        django_filters.DjangoFilterBackend,
        filters.CloudAccountRequestIsUserFilterBackend,
    )
예제 #3
0
class MachineImageViewSet(viewsets.ReadOnlyModelViewSet):
    """List all or retrieve a single machine image."""

    schema = schemas.DescriptiveAutoSchema("image", tags=["api-v2"])
    serializer_class = serializers.MachineImageSerializer
    queryset = models.MachineImage.objects.all()
    filter_backends = (
        django_filters.DjangoFilterBackend,
        filters.MachineImageRequestIsUserFilterBackend,
    )
    filterset_fields = ("architecture", "status")
예제 #4
0
class InstanceViewSet(viewsets.ReadOnlyModelViewSet):
    """List all or retrieve a single instance."""

    schema = schemas.DescriptiveAutoSchema("instance", tags=["api-v2"])
    serializer_class = serializers.InstanceSerializer
    queryset = models.Instance.objects.all()
    filter_backends = (
        django_filters.DjangoFilterBackend,
        filters.InstanceRequestIsUserFilterBackend,
    )
    filterset_class = filters.InstanceFilterSet