Skip to content

Commit 857623d

Browse files
authored
Merge pull request #257 from linode/bug/defaults-only-where-requested
bug: Only allow requested defaults to actions
2 parents 69320a5 + 5635ccc commit 857623d

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
@@ -206,7 +206,10 @@ def bake(self, spec):
206206

207207
args = {}
208208
required_fields = []
209+
allowed_defaults = None
209210
if m in ('post','put') and 'requestBody' in data[m]:
211+
allowed_defaults = data[m]['requestBody'].get("x-linode-cli-allowed-defaults", None)
212+
210213
if 'application/json' in data[m]['requestBody']['content']:
211214
body_schema = data[m]['requestBody']['content']['application/json']['schema']
212215

@@ -288,7 +291,8 @@ def bake(self, spec):
288291

289292
self.ops[command][action] = CLIOperation(m, use_path, summary,
290293
cli_args, response_model,
291-
use_params, use_servers)
294+
use_params, use_servers,
295+
allowed_defaults=allowed_defaults)
292296

293297
# hide the base_url from the spec away
294298
self.ops['_base_url'] = spec['servers'][0]['url']
@@ -433,7 +437,7 @@ def do_request(self, operation, args, filter_header=None, skip_error_handling=Fa
433437
headers["X-Filter"] = json.dumps(filters)
434438
else:
435439
if self.defaults:
436-
parsed_args = self.config.update(parsed_args)
440+
parsed_args = self.config.update(parsed_args, operation.allowed_defaults)
437441

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

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)