Skip to content

Commit f0f3e2f

Browse files
author
János Kukovecz
committed
firmware upload: add analysis configuration selection
In the new API, analysis configuration can be selected for a firmware. There should always be an analysis configuration called `Default`. This patch extends the `upload-firmware` CLI with analysis configuration selection by name. Not providing the name using the new `--analysis-configuration` option will result using the `Default` configuration, much like during Product Group selection.
1 parent a5c0283 commit f0f3e2f

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ query {
107107
"""
108108
res = client.query(GET_PRODUCT_GROUPS)
109109
default_product_group = next(pg for pg in res["allProductGroups"] if pg["name"] == "Default")
110+
111+
GET_ANALYSIS_CONFIGURATIONS = """
112+
query {
113+
allAnalysisConfigurations {
114+
id
115+
name
116+
}
117+
}
118+
"""
119+
res = client.query(GET_ANALYSIS_CONFIGURATIONS)
120+
default_analysis_configuration = next(conf for conf in res["allAnalysisConfigurations"] if conf["name"] == "Default")
110121
```
111122

112123
You can upload firmwares:
@@ -117,6 +128,7 @@ metadata = FirmwareMetadata(
117128
vendor_name="myVendor",
118129
product_name="myProduct",
119130
product_group_id=default_product_group["id"],
131+
analysis_configuration_id=default_analysis_configuration["id"],
120132
)
121133

122134
firmware_path = Path("/path/to/firmware.bin")

examples/upload_firmware.py

+12
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@
3232
response = client.query(GET_ALL_PRODUCT_GROUP_IDS)
3333
product_group_ids = [pg["id"] for pg in response["allProductGroups"]]
3434

35+
GET_ANALYSIS_CONFIGURATIONS = """
36+
query {
37+
allAnalysisConfigurations {
38+
id
39+
name
40+
}
41+
}
42+
"""
43+
response = client.query(GET_ANALYSIS_CONFIGURATIONS)
44+
analysis_configuration_ids = [c["id"] for c in response["allAnalysisConfigurations"]]
45+
3546

3647
metadata = FirmwareMetadata(
3748
name="myFirmware",
3849
vendor_name="myVendor",
3950
product_name="myProduct",
4051
product_group_id=product_group_ids[0],
52+
analysis_configuration_id=analysis_configuration_ids[0],
4153
)
4254

4355
firmware_path = Path(sys.argv[2])

onekey_client/cli/firmware_upload.py

+42-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
required=True,
2424
help="Product group name to add the firmware",
2525
)
26+
@click.option(
27+
"--analysis-configuration",
28+
"analysis_configuration_name",
29+
default="Default",
30+
show_default=True,
31+
required=True,
32+
help="Analysis configuration name",
33+
)
2634
@click.option("--version", help="Firmware version")
2735
@click.option("--name", help="Firmware name")
2836
@click.argument("filename", type=click.Path(exists=True, path_type=Path))
@@ -32,22 +40,17 @@ def upload_firmware(
3240
product_name: str,
3341
vendor_name: str,
3442
product_group_name: str,
43+
analysis_configuration_name: str,
3544
version: Optional[str],
3645
name: Optional[str],
3746
filename: Path,
3847
):
3948
"""Uploads a firmware to the ONEKEY platform"""
4049

41-
product_groups = client.get_product_groups()
42-
43-
try:
44-
product_group_id = product_groups[product_group_name]
45-
except KeyError:
46-
click.echo(f"Missing product group: {product_group_name}")
47-
click.echo("Available product groups:")
48-
for pg in product_groups.keys():
49-
click.echo(f"- {pg}")
50-
sys.exit(10)
50+
product_group_id = _get_product_group_id_by_name(client, product_group_name)
51+
analysis_configuration_id = _get_analysis_configuration_id_by_name(
52+
client, analysis_configuration_name
53+
)
5154

5255
if name is None:
5356
name = (
@@ -62,6 +65,7 @@ def upload_firmware(
6265
product_name=product_name,
6366
product_group_id=product_group_id,
6467
version=version,
68+
analysis_configuration_id=analysis_configuration_id,
6569
)
6670

6771
try:
@@ -72,3 +76,31 @@ def upload_firmware(
7276
for error in e._errors:
7377
click.echo(f"- {error['message']}")
7478
sys.exit(11)
79+
80+
81+
def _get_product_group_id_by_name(client: Client, product_group_name: str):
82+
product_groups = client.get_product_groups()
83+
84+
try:
85+
return product_groups[product_group_name]
86+
except KeyError:
87+
click.echo(f"Missing product group: {product_group_name}")
88+
click.echo("Available product groups:")
89+
for pg in product_groups.keys():
90+
click.echo(f"- {pg}")
91+
sys.exit(10)
92+
93+
94+
def _get_analysis_configuration_id_by_name(
95+
client: Client, analysis_configuration_name: str
96+
):
97+
analysis_configurations = client.get_analysis_configurations()
98+
99+
try:
100+
return analysis_configurations[analysis_configuration_name]
101+
except KeyError:
102+
click.echo(f"Missing analysis configuration {analysis_configuration_name}")
103+
click.echo("Available analysis configurations:")
104+
for config in analysis_configurations.keys():
105+
click.echo(f"- {config}")
106+
sys.exit(12)

onekey_client/client.py

+9
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def upload_firmware(
209209
"releaseDate": metadata.release_date,
210210
"notes": metadata.notes,
211211
"enableMonitoring": enable_monitoring,
212+
"analysisConfigurationId": str(metadata.analysis_configuration_id),
212213
},
213214
"vendorName": metadata.vendor_name,
214215
"productName": metadata.product_name,
@@ -234,6 +235,14 @@ def get_product_groups(self):
234235
response = self.query(product_groups_query)
235236
return {pg["name"]: pg["id"] for pg in response["allProductGroups"]}
236237

238+
@_tenant_required
239+
def get_analysis_configurations(self):
240+
analysis_configurations_query = load_query(
241+
"get_analysis_configurations.graphql"
242+
)
243+
response = self.query(analysis_configurations_query)
244+
return {c["name"]: c["id"] for c in response["allAnalysisConfigurations"]}
245+
237246
def logout(self):
238247
del self._state
239248
gc.collect()

onekey_client/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ class FirmwareMetadata(BaseModel):
1818
product_name: str
1919
product_category: Optional[str] = None
2020
product_group_id: UUID
21+
analysis_configuration_id: UUID
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
query {
2+
allAnalysisConfigurations {
3+
id
4+
name
5+
}
6+
}

0 commit comments

Comments
 (0)