<reponame>bmc-msft/onefuzz<filename>src/api-service/__app__/repro_vms/__init__.py<gh_stars>1-10
#!/usr/bin/env python
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import azure.functions as func
from onefuzztypes.enums import ErrorCode, VmState
from onefuzztypes.models import Error, ReproConfig
from onefuzztypes.requests import ReproGet

from ..onefuzzlib.repro import Repro
from ..onefuzzlib.request import not_ok, ok, parse_request


def get(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproGet, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro_vm get")

    if request.vm_id:
        vm = Repro.get(request.vm_id)
        if not vm:
            return not_ok(
                Error(code=ErrorCode.INVALID_REQUEST, errors=["no such VM"]),
                context=request.vm_id,
            )
        return ok(vm)
    else:
        vms = Repro.search_states(states=VmState.available())
        for vm in vms:
            vm.auth = None
        return ok(vms)


def post(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproConfig, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro_vm create")

    vm = Repro.create(request)
    if isinstance(vm, Error):
        return not_ok(vm, context="repro_vm create")

    return ok(vm)


def delete(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproGet, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro delete")

    if not request.vm_id:
        return not_ok(
            Error(code=ErrorCode.INVALID_REQUEST, errors=["missing vm_id"]),
            context="repro delete",
        )

    vm = Repro.get(request.vm_id)
    if not vm:
        return not_ok(
            Error(code=ErrorCode.INVALID_REQUEST, errors=["no such VM"]),
            context=request.vm_id,
        )

    vm.state = VmState.stopping
    vm.save()
    return ok(vm)


def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.method == "GET":
        return get(req)
    elif req.method == "POST":
        return post(req)
    elif req.method == "DELETE":
        return delete(req)
    else:
        raise Exception("invalid method")

#!/usr/bin/env python
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import azure.functions as func
from onefuzztypes.enums import ErrorCode, VmState
from onefuzztypes.models import Error, ReproConfig
from onefuzztypes.requests import ReproGet

from ..onefuzzlib.repro import Repro
from ..onefuzzlib.request import not_ok, ok, parse_request


def get(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproGet, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro_vm get")

    if request.vm_id:
        vm = Repro.get(request.vm_id)
        if not vm:
            return not_ok(
                Error(code=ErrorCode.INVALID_REQUEST, errors=["no such VM"]),
                context=request.vm_id,
            )
        return ok(vm)
    else:
        vms = Repro.search_states(states=VmState.available())
        for vm in vms:
            vm.auth = None
        return ok(vms)


def post(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproConfig, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro_vm create")

    vm = Repro.create(request)
    if isinstance(vm, Error):
        return not_ok(vm, context="repro_vm create")

    return ok(vm)


def delete(req: func.HttpRequest) -> func.HttpResponse:
    request = parse_request(ReproGet, req)
    if isinstance(request, Error):
        return not_ok(request, context="repro delete")

    if not request.vm_id:
        return not_ok(
            Error(code=ErrorCode.INVALID_REQUEST, errors=["missing vm_id"]),
            context="repro delete",
        )

    vm = Repro.get(request.vm_id)
    if not vm:
        return not_ok(
            Error(code=ErrorCode.INVALID_REQUEST, errors=["no such VM"]),
            context=request.vm_id,
        )

    vm.state = VmState.stopping
    vm.save()
    return ok(vm)


def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.method == "GET":
        return get(req)
    elif req.method == "POST":
        return post(req)
    elif req.method == "DELETE":
        return delete(req)
    else:
        raise Exception("invalid method")
#!/usr/bin/env python # # Copyright (c) Microsoft Corporation. # Licensed under the MIT License.