Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

basic list proxy functionality #905

Merged
merged 8 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions src/api-service/__app__/proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from onefuzztypes.enums import ErrorCode, VmState
from onefuzztypes.models import Error
from onefuzztypes.requests import ProxyCreate, ProxyDelete, ProxyGet, ProxyReset
from onefuzztypes.responses import BoolResult, ProxyGetResult
from onefuzztypes.responses import BoolResult, ProxyGetResult, ProxyInfo, ProxyList

from ..onefuzzlib.endpoint_authorization import call_if_user
from ..onefuzzlib.events import get_events
Expand All @@ -36,26 +36,37 @@ def get(req: func.HttpRequest) -> func.HttpResponse:
if isinstance(request, Error):
return not_ok(request, context="ProxyGet")

scaleset = Scaleset.get_by_id(request.scaleset_id)
if isinstance(scaleset, Error):
return not_ok(scaleset, context="ProxyGet")

proxy = Proxy.get_or_create(scaleset.region)
forwards = ProxyForward.search_forward(
scaleset_id=request.scaleset_id,
machine_id=request.machine_id,
dst_port=request.dst_port,
)
if not forwards:
return not_ok(
Error(
code=ErrorCode.INVALID_REQUEST,
errors=["no forwards for scaleset and node"],
),
context="debug_proxy get",
if (
ranweiler marked this conversation as resolved.
Show resolved Hide resolved
request.scaleset_id is not None
and request.machine_id is not None
and request.dst_port is not None
):
scaleset = Scaleset.get_by_id(request.scaleset_id)
if isinstance(scaleset, Error):
return not_ok(scaleset, context="ProxyGet")

proxy = Proxy.get_or_create(scaleset.region)
forwards = ProxyForward.search_forward(
scaleset_id=request.scaleset_id,
machine_id=request.machine_id,
dst_port=request.dst_port,
)

return ok(get_result(forwards[0], proxy))
if not forwards:
return not_ok(
Error(
code=ErrorCode.INVALID_REQUEST,
errors=["no forwards for scaleset and node"],
),
context="debug_proxy get",
)

return ok(get_result(forwards[0], proxy))
else:
proxies = [
ProxyInfo(region=x.region, proxy_id=x.proxy_id, state=x.state)
for x in Proxy.search()
]
return ok(ProxyList(proxies=proxies))


def post(req: func.HttpRequest) -> func.HttpResponse:
Expand Down
3 changes: 3 additions & 0 deletions src/cli/onefuzz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,9 @@ def create(
),
)

def list(self) -> responses.ProxyList:
return self._req_model("GET", responses.ProxyList, data=requests.ProxyGet())


class Command:
def __init__(self, onefuzz: "Onefuzz", logger: logging.Logger):
Expand Down
21 changes: 16 additions & 5 deletions src/pytypes/onefuzztypes/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional
from uuid import UUID

from pydantic import AnyHttpUrl, BaseModel, Field, validator
from pydantic import AnyHttpUrl, BaseModel, Field, root_validator, validator

from .consts import ONE_HOUR, SEVEN_DAYS
from .enums import (
Expand Down Expand Up @@ -107,9 +107,20 @@ class PoolStop(BaseRequest):


class ProxyGet(BaseRequest):
scaleset_id: UUID
machine_id: UUID
dst_port: int
scaleset_id: Optional[UUID]
machine_id: Optional[UUID]
dst_port: Optional[int]

@root_validator()
def check_proxy_get(cls, value: Any) -> Any:
check_keys = ["scaleset_id", "machine_id", "dst_port"]
included = [x in value for x in check_keys]
if any(included) and not all(included):
raise ValueError(
"ProxyGet must provide all or none of the following: %s"
% ", ".join(check_keys)
)
return value


class ProxyCreate(BaseRequest):
Expand Down
13 changes: 12 additions & 1 deletion src/pytypes/onefuzztypes/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from typing import Dict, Optional
from typing import Dict, List, Optional
from uuid import UUID

from pydantic import BaseModel

from .enums import VmState
from .models import Forward, NodeCommandEnvelope
from .primitives import Region

Expand All @@ -25,6 +26,16 @@ class ProxyGetResult(BaseResponse):
forward: Forward


class ProxyInfo(BaseModel):
region: Region
proxy_id: UUID
state: VmState


class ProxyList(BaseResponse):
proxies: List[ProxyInfo]


class Version(BaseResponse):
git: str
build: str
Expand Down