Skip to content

Commit 855f372

Browse files
committed
Add server side query object
1 parent 60d4b16 commit 855f372

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

awsshell/resource/index.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Index and retrive information from the resource JSON."""
22
import jmespath
3+
from collections import namedtuple
4+
5+
# service - The name of the AWS service
6+
# operation - The name of the AWS operation
7+
# params - A dict of params to send in the request (not implemented yet)
8+
# path - A JMESPath expression to select the expected elements.
9+
ServerCompletion = namedtuple('ServerCompletion',
10+
['service', 'operation', 'params', 'path'])
311

412

513
def extract_field_from_jmespath(expression):
@@ -57,6 +65,44 @@ def build_index(self, resource_data):
5765
return index
5866

5967

68+
class CompleterQuery(object):
69+
"""Describes how to autocomplete a resource."""
70+
def __init__(self, resource_index):
71+
self._index = resource_index
72+
73+
def describe_autocomplete(self, service, operation, param):
74+
"""
75+
76+
:type service: str
77+
:param service: The AWS service name.
78+
79+
:type operation: str
80+
:param operation: The AWS operation name.
81+
82+
:type param: str
83+
:param param: The name of the parameter being completed. This must
84+
match the casing in the service model (e.g. InstanceIds, not
85+
--instance-ids).
86+
87+
:rtype: ServerCompletion
88+
:return: A ServerCompletion object that describes what API call to make
89+
in order to complete the response.
90+
91+
"""
92+
service_index = self._index[service]
93+
if param not in service_index.get('operations', {}).get(operation, {}):
94+
return None
95+
p = service_index['operations'][operation][param]
96+
resource_name = p['resourceName']
97+
resource_identifier = p['resourceIdentifier']
98+
99+
resource_index = service_index['resources'][resource_name]
100+
completion_operation = resource_index['operation']
101+
path = resource_index['resourceIdentifier'][resource_identifier]
102+
return ServerCompletion(service=service, operation=completion_operation,
103+
params={}, path=path)
104+
105+
60106
def main():
61107
import sys
62108
import json

tests/test_resources.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,33 @@ def test_resource_not_included_if_no_has_many():
149149
'operations': {},
150150
'resources': {},
151151
}
152+
153+
154+
def test_can_complete_query():
155+
built_index = {
156+
'dynamodb': {
157+
'operations': {
158+
'DeleteTable': {
159+
'TableName': {
160+
'resourceName': 'Table',
161+
'resourceIdentifier': 'Name',
162+
}
163+
}
164+
},
165+
'resources': {
166+
'Table': {
167+
'operation': 'ListTables',
168+
'resourceIdentifier': {
169+
'Name': 'TableNames[]',
170+
}
171+
}
172+
}
173+
}
174+
}
175+
q = index.CompleterQuery(built_index)
176+
result = q.describe_autocomplete(
177+
'dynamodb', 'DeleteTable', 'TableName')
178+
assert result.service == 'dynamodb'
179+
assert result.operation == 'ListTables'
180+
assert result.params == {}
181+
assert result.path == 'TableNames[]'

0 commit comments

Comments
 (0)