Skip to content

Commit 5635ccc

Browse files
committed
bug: Only allow requested defaults to actions
This should fix the `linode-cli tickets create` action. The Linode CLI, when being configured, accepts default values for "region", "image", and "type". These values were previously added to every input for a PUT or POST endpoint, and then parsed out as normal. This had the effect of defaulting those values to the configured defaults for actions like `linodes create` or `nodebalancers create` without additional interaction. However, the `tickets create` action recently began accepting `--region`, but only for certain types of tickets. This caused ticket creation to fail through the CLI if a default region was configured, unless `--no-defaults` was given, or unless the specific ticket type was being opened. This change allows the spec to specify which actions should use which defaults, allowing this problem to be resolved as well as avoiding it for future changes to actions. :warning: This requires a docs update to allow existing actions to continue using their defaults. Such a PR will be linked below.
1 parent 9b7a170 commit 5635ccc

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

README.rst

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,26 @@ Specification Extensions
384384
In order to be more useful, the following `Specification Extensions`_ have been
385385
added to Linode's OpenAPI spec:
386386

387-
+---------------------+----------+-------------------------------------------------------------------------------------------+
388-
|Attribute | Location | Purpose |
389-
+---------------------+----------+-------------------------------------------------------------------------------------------+
390-
|x-linode-cli-action | method | The action name for operations under this path. If not present, operationId is used. |
391-
+---------------------+----------+-------------------------------------------------------------------------------------------+
392-
|x-linode-cli-color   | property | If present, defines key-value pairs of property value: color. Colors must be understood |
393-
|                     |         | by colorclass.Color. Must include a default.                         |
394-
+---------------------+----------+-------------------------------------------------------------------------------------------+
395-
|x-linode-cli-command | path | The command name for operations under this path. If not present, "default" is used. |
396-
+---------------------+----------+-------------------------------------------------------------------------------------------+
397-
|x-linode-cli-display | property | If truthy, displays this as a column in output. If a number, determines the ordering |
398-
|                     | | (left to right). |
399-
+---------------------+----------+-------------------------------------------------------------------------------------------+
400-
|x-linode-cli-format | property | Overrides the "format" given in this property for the CLI only. Valid values are `file` |
401-
| | | and `json`. |
402-
+---------------------+----------+-------------------------------------------------------------------------------------------+
403-
|x-linode-cli-skip | path | If present and truthy, this method will not be available in the CLI. |
404-
+---------------------+----------+-------------------------------------------------------------------------------------------+
387+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
388+
|Attribute | Location | Purpose |
389+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
390+
|x-linode-cli-action | method | The action name for operations under this path. If not present, operationId is used. |
391+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
392+
|x-linode-cli-color   | property | If present, defines key-value pairs of property value: color. Colors must be understood |
393+
|                     |         | by colorclass.Color. Must include a default.                         |
394+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
395+
|x-linode-cli-command | path | The command name for operations under this path. If not present, "default" is used. |
396+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
397+
|x-linode-cli-display | property | If truthy, displays this as a column in output. If a number, determines the ordering |
398+
|                     | | (left to right). |
399+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
400+
|x-linode-cli-format | property | Overrides the "format" given in this property for the CLI only. Valid values are `file` |
401+
| | | and `json`. |
402+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
403+
|x-linode-cli-skip | path | If present and truthy, this method will not be available in the CLI. |
404+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
405+
+x-linode-cli-allowed-defaults| requestBody | Tells the CLI what configured defaults apply to this request. Value values are "region", |
406+
+ | | "image", and "type". |
407+
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
405408

406409
.. _Specification Extensions: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#specificationExtensions

linodecli/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ def bake(self, spec):
204204

205205
args = {}
206206
required_fields = []
207+
allowed_defaults = None
207208
if m in ('post','put') and 'requestBody' in data[m]:
209+
allowed_defaults = data[m]['requestBody'].get("x-linode-cli-allowed-defaults", None)
210+
208211
if 'application/json' in data[m]['requestBody']['content']:
209212
body_schema = data[m]['requestBody']['content']['application/json']['schema']
210213

@@ -286,7 +289,8 @@ def bake(self, spec):
286289

287290
self.ops[command][action] = CLIOperation(m, use_path, summary,
288291
cli_args, response_model,
289-
use_params, use_servers)
292+
use_params, use_servers,
293+
allowed_defaults=allowed_defaults)
290294

291295
# hide the base_url from the spec away
292296
self.ops['_base_url'] = spec['servers'][0]['url']
@@ -431,7 +435,7 @@ def do_request(self, operation, args, filter_header=None, skip_error_handling=Fa
431435
headers["X-Filter"] = json.dumps(filters)
432436
else:
433437
if self.defaults:
434-
parsed_args = self.config.update(parsed_args)
438+
parsed_args = self.config.update(parsed_args, operation.allowed_defaults)
435439

436440
to_json = {k: v for k, v in vars(parsed_args).items() if v is not None}
437441

linodecli/configuration.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def update_namespace(self, namespace, new_dict):
116116

117117
return argparse.Namespace(**ns_dict)
118118

119-
def update(self, namespace):
119+
def update(self, namespace, allowed_defaults):
120120
"""
121121
This updates a Namespace (as returned by ArgumentParser) with config values
122122
if they aren't present in the Namespace already.
@@ -133,8 +133,12 @@ def update(self, namespace):
133133
print("User {} is not configured.".format(username))
134134
sys.exit(1)
135135

136-
if self.config.has_section(username):
137-
return self.update_namespace(namespace, dict(self.config.items(username)))
136+
if self.config.has_section(username) and allowed_defaults:
137+
update_dicts = {
138+
default_key: self.config.get(username, default_key)
139+
for default_key in allowed_defaults if self.config.has_option(username, default_key)
140+
}
141+
return self.update_namespace(namespace, update_dicts)
138142
return namespace
139143

140144
def get_token(self):

linodecli/operation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,15 @@ class CLIOperation:
123123
responses with the help of their ResponseModel
124124
"""
125125
def __init__(self, method, url, summary, args, response_model,
126-
params, servers):
126+
params, servers, allowed_defaults = None):
127127
self.method = method
128128
self._url = url
129129
self.summary = summary
130130
self.args = args
131131
self.response_model = response_model
132132
self.params = params
133133
self.servers = servers
134+
self.allowed_defaults = allowed_defaults
134135

135136
@property
136137
def url(self):

0 commit comments

Comments
 (0)