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

Commit 7952f16

Browse files
authored
basic list proxy functionality (#905)
1 parent 2241dcc commit 7952f16

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

src/api-service/__app__/proxy/__init__.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from onefuzztypes.enums import ErrorCode, VmState
1010
from onefuzztypes.models import Error
1111
from onefuzztypes.requests import ProxyCreate, ProxyDelete, ProxyGet, ProxyReset
12-
from onefuzztypes.responses import BoolResult, ProxyGetResult
12+
from onefuzztypes.responses import BoolResult, ProxyGetResult, ProxyInfo, ProxyList
1313

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

39-
scaleset = Scaleset.get_by_id(request.scaleset_id)
40-
if isinstance(scaleset, Error):
41-
return not_ok(scaleset, context="ProxyGet")
42-
43-
proxy = Proxy.get_or_create(scaleset.region)
44-
forwards = ProxyForward.search_forward(
45-
scaleset_id=request.scaleset_id,
46-
machine_id=request.machine_id,
47-
dst_port=request.dst_port,
48-
)
49-
if not forwards:
50-
return not_ok(
51-
Error(
52-
code=ErrorCode.INVALID_REQUEST,
53-
errors=["no forwards for scaleset and node"],
54-
),
55-
context="debug_proxy get",
39+
if (
40+
request.scaleset_id is not None
41+
and request.machine_id is not None
42+
and request.dst_port is not None
43+
):
44+
scaleset = Scaleset.get_by_id(request.scaleset_id)
45+
if isinstance(scaleset, Error):
46+
return not_ok(scaleset, context="ProxyGet")
47+
48+
proxy = Proxy.get_or_create(scaleset.region)
49+
forwards = ProxyForward.search_forward(
50+
scaleset_id=request.scaleset_id,
51+
machine_id=request.machine_id,
52+
dst_port=request.dst_port,
5653
)
57-
58-
return ok(get_result(forwards[0], proxy))
54+
if not forwards:
55+
return not_ok(
56+
Error(
57+
code=ErrorCode.INVALID_REQUEST,
58+
errors=["no forwards for scaleset and node"],
59+
),
60+
context="debug_proxy get",
61+
)
62+
63+
return ok(get_result(forwards[0], proxy))
64+
else:
65+
proxies = [
66+
ProxyInfo(region=x.region, proxy_id=x.proxy_id, state=x.state)
67+
for x in Proxy.search()
68+
]
69+
return ok(ProxyList(proxies=proxies))
5970

6071

6172
def post(req: func.HttpRequest) -> func.HttpResponse:

src/cli/onefuzz/api.py

+3
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,9 @@ def create(
14681468
),
14691469
)
14701470

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

14721475
class Command:
14731476
def __init__(self, onefuzz: "Onefuzz", logger: logging.Logger):

src/pytypes/onefuzztypes/requests.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# Copyright (c) Microsoft Corporation.
44
# Licensed under the MIT License.
55

6-
from typing import Dict, List, Optional
6+
from typing import Any, Dict, List, Optional
77
from uuid import UUID
88

9-
from pydantic import AnyHttpUrl, BaseModel, Field, validator
9+
from pydantic import AnyHttpUrl, BaseModel, Field, root_validator, validator
1010

1111
from .consts import ONE_HOUR, SEVEN_DAYS
1212
from .enums import (
@@ -107,9 +107,20 @@ class PoolStop(BaseRequest):
107107

108108

109109
class ProxyGet(BaseRequest):
110-
scaleset_id: UUID
111-
machine_id: UUID
112-
dst_port: int
110+
scaleset_id: Optional[UUID]
111+
machine_id: Optional[UUID]
112+
dst_port: Optional[int]
113+
114+
@root_validator()
115+
def check_proxy_get(cls, value: Any) -> Any:
116+
check_keys = ["scaleset_id", "machine_id", "dst_port"]
117+
included = [x in value for x in check_keys]
118+
if any(included) and not all(included):
119+
raise ValueError(
120+
"ProxyGet must provide all or none of the following: %s"
121+
% ", ".join(check_keys)
122+
)
123+
return value
113124

114125

115126
class ProxyCreate(BaseRequest):

src/pytypes/onefuzztypes/responses.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# Copyright (c) Microsoft Corporation.
44
# Licensed under the MIT License.
55

6-
from typing import Dict, Optional
6+
from typing import Dict, List, Optional
77
from uuid import UUID
88

99
from pydantic import BaseModel
1010

11+
from .enums import VmState
1112
from .models import Forward, NodeCommandEnvelope
1213
from .primitives import Region
1314

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

2728

29+
class ProxyInfo(BaseModel):
30+
region: Region
31+
proxy_id: UUID
32+
state: VmState
33+
34+
35+
class ProxyList(BaseResponse):
36+
proxies: List[ProxyInfo]
37+
38+
2839
class Version(BaseResponse):
2940
git: str
3041
build: str

0 commit comments

Comments
 (0)