|
4 | 4 |
|
5 | 5 | * ResourceIndexBuilder - Takes a boto3 resource and converts into the
|
6 | 6 | index format we need to do server side completions.
|
7 |
| -* CompleterQuery - Takes the index from ResourceIndexBuilder and looks |
| 7 | +* CompleterDescriber - Takes the index from ResourceIndexBuilder and looks |
8 | 8 | up how to perform the autocompletion. Note that this class does
|
9 | 9 | *not* actually do the autocompletion. It merely tells you how
|
10 | 10 | you _would_ do the autocompletion if you made the appropriate
|
|
20 | 20 |
|
21 | 21 | import jmespath
|
22 | 22 | from botocore import xform_name
|
| 23 | +from botocore.exceptions import BotoCoreError |
23 | 24 |
|
24 | 25 | LOG = logging.getLogger(__name__)
|
25 | 26 |
|
@@ -86,8 +87,18 @@ def build_index(self, resource_data):
|
86 | 87 | return index
|
87 | 88 |
|
88 | 89 |
|
89 |
| -class CompleterQuery(object): |
90 |
| - """Describes how to autocomplete a resource.""" |
| 90 | +class CompleterDescriber(object): |
| 91 | + """Describes how to autocomplete a resource. |
| 92 | +
|
| 93 | + You give this class a service/operation/param and it will |
| 94 | + describe to you how you can autocomplete values for the |
| 95 | + provided parameter. |
| 96 | +
|
| 97 | + It's up to the caller to actually take that description |
| 98 | + and make the appropriate service calls + filtering to |
| 99 | + extract out the server side values. |
| 100 | +
|
| 101 | + """ |
91 | 102 | def __init__(self, resource_index):
|
92 | 103 | self._index = resource_index
|
93 | 104 |
|
@@ -126,71 +137,115 @@ def describe_autocomplete(self, service, operation, param):
|
126 | 137 | params={}, path=path)
|
127 | 138 |
|
128 | 139 |
|
129 |
| -class ServerSideCompleter(object): |
130 |
| - def __init__(self, session, builder): |
131 |
| - # session is a boto3 session. |
132 |
| - # It is a public attribute as it is intended to be |
133 |
| - # changed if the profile changes. |
134 |
| - self.session = session |
135 |
| - self._loader = session._loader |
136 |
| - self._builder = builder |
| 140 | +class CachedClientCreator(object): |
| 141 | + def __init__(self, session): |
| 142 | + #: A botocore.session.Session object. Only the |
| 143 | + #: create_client() method is used. |
| 144 | + self._session = session |
137 | 145 | self._client_cache = {}
|
138 |
| - self._completer_cache = {} |
139 |
| - self._update_loader_paths() |
| 146 | + |
| 147 | + def create_client(self, service_name): |
| 148 | + if service_name not in self._client_cache: |
| 149 | + client = self._session.create_client(service_name) |
| 150 | + self._client_cache[service_name] = client |
| 151 | + return self._client_cache[service_name] |
| 152 | + |
| 153 | + |
| 154 | +class CompleterDescriberCreator(object): |
| 155 | + """Create and cache CompleterDescriber objects.""" |
| 156 | + def __init__(self, loader): |
| 157 | + #: A botocore.loader.Loader |
| 158 | + self._loader = loader |
| 159 | + self._describer_cache = {} |
| 160 | + self._services_with_completions = None |
| 161 | + |
| 162 | + def create_completer_query(self, service_name): |
| 163 | + """Create a CompleterDescriber for a service. |
| 164 | +
|
| 165 | + :type service_name: str |
| 166 | + :param service_name: The name of the service, e.g. 'ec2' |
| 167 | +
|
| 168 | + :return: A CompleterDescriber object. |
| 169 | +
|
| 170 | + """ |
| 171 | + if service_name not in self._describer_cache: |
| 172 | + query = self._create_completer_query(service_name) |
| 173 | + self._describer_cache[service_name] = query |
| 174 | + return self._describer_cache[service_name] |
| 175 | + |
| 176 | + def _create_completer_query(self, service_name): |
| 177 | + completions_model = self._loader.load_service_model( |
| 178 | + service_name, 'completions-1') |
| 179 | + cq = CompleterDescriber({service_name: completions_model}) |
| 180 | + return cq |
| 181 | + |
| 182 | + def services_with_completions(self): |
| 183 | + if self._services_with_completions is not None: |
| 184 | + return self._services_with_completions |
140 | 185 | self._services_with_completions = set(
|
141 | 186 | self._loader.list_available_services(type_name='completions-1'))
|
| 187 | + return self._services_with_completions |
142 | 188 |
|
143 |
| - def _update_loader_paths(self): |
144 |
| - completions_path = os.path.join( |
145 |
| - os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
146 |
| - 'data') |
147 |
| - self._loader.search_paths.insert(0, completions_path) |
148 |
| - |
149 |
| - def _get_completer_for_service(self, service_name): |
150 |
| - if service_name not in self._completer_cache: |
151 |
| - api_version = self._loader.determine_latest_version( |
152 |
| - service_name, 'completions-1') |
153 |
| - completions_model = self._loader.load_service_model( |
154 |
| - service_name, 'completions-1', api_version) |
155 |
| - cq = CompleterQuery({service_name: completions_model}) |
156 |
| - self._completer_cache[service_name] = cq |
157 |
| - return self._completer_cache[service_name] |
158 |
| - |
159 |
| - def _get_client(self, service_name): |
160 |
| - if service_name in self._client_cache: |
161 |
| - return self._client_cache[service_name] |
162 |
| - client = self.session.client(service_name) |
163 |
| - self._client_cache[service_name] = client |
164 |
| - return client |
| 189 | + |
| 190 | +class ServerSideCompleter(object): |
| 191 | + def __init__(self, client_creator, describer_creator): |
| 192 | + self._client_creator = client_creator |
| 193 | + self._describer_creator = describer_creator |
165 | 194 |
|
166 | 195 | def retrieve_candidate_values(self, service, operation, param):
|
167 |
| - if service not in self._services_with_completions: |
168 |
| - return |
| 196 | + """Retrieve server side completions. |
| 197 | +
|
| 198 | + :type service: str |
| 199 | + :param service: The service name, e.g. 'ec2', 'iam'. |
| 200 | +
|
| 201 | + :type operation: str |
| 202 | + :param operation: The operation name, in the casing |
| 203 | + used by the CLI (words separated by hyphens), e.g. |
| 204 | + 'describe-instances', 'delete-user'. |
| 205 | +
|
| 206 | + :type param: str |
| 207 | + :param param: The param name, as specified in the service |
| 208 | + model, e.g. 'InstanceIds', 'UserName'. |
| 209 | +
|
| 210 | + :rtype: list |
| 211 | + :return: A list of possible completions for the |
| 212 | + service/operation/param combination. If no |
| 213 | + completions were found an empty list is returned. |
| 214 | +
|
| 215 | + """ |
169 | 216 | # Example call:
|
170 |
| - # service='ec2', operation='terminate-instances', |
171 |
| - # param='--instance-ids'. |
172 |
| - # We need to convert this to botocore syntax. |
173 |
| - # First try to load the resource model. |
174 |
| - LOG.debug("Called with: %s, %s, %s", service, operation, param) |
175 |
| - # Now convert operation to the name used by botocore. |
176 |
| - client = self._get_client(service) |
| 217 | + # service='ec2', |
| 218 | + # operation='terminate-instances', |
| 219 | + # param='InstanceIds'. |
| 220 | + if service not in self._describer_creator.services_with_completions(): |
| 221 | + return [] |
| 222 | + try: |
| 223 | + client = self._client_creator.create_client(service) |
| 224 | + except BotoCoreError as e: |
| 225 | + # create_client() could raise an exception if the session |
| 226 | + # isn't fully configured (say it's missing a region). |
| 227 | + # However, we don't want to turn off all server side |
| 228 | + # completions because it's still possible to create |
| 229 | + # clients for some services without a region, e.g. IAM. |
| 230 | + LOG.debug("Error when trying to create a client for %s", |
| 231 | + service, exc_info=True) |
| 232 | + return [] |
177 | 233 | api_operation_name = client.meta.method_to_api_mapping.get(
|
178 | 234 | operation.replace('-', '_'))
|
179 | 235 | if api_operation_name is None:
|
180 |
| - return |
| 236 | + return [] |
181 | 237 | # Now we need to convert the param name to the
|
182 | 238 | # casing used by the API.
|
183 |
| - completer = self._get_completer_for_service(service) |
| 239 | + completer = self._describer_creator.create_completer_query(service) |
184 | 240 | result = completer.describe_autocomplete(
|
185 | 241 | service, api_operation_name, param)
|
186 | 242 | if result is None:
|
187 | 243 | return
|
188 |
| - # DEBUG:awsshell.resource.index:RESULTS: |
189 |
| - # ServerCompletion(service=u'ec2', operation=u'DescribeInstances', |
190 |
| - # params={}, path=u'Reservations[].Instances[].InstanceId') |
191 | 244 | try:
|
192 | 245 | response = getattr(client, xform_name(result.operation, '_'))()
|
193 |
| - except Exception: |
| 246 | + except Exception as e: |
| 247 | + LOG.debug("Error when calling %s.%s: %s", service, |
| 248 | + result.operation, e, exc_info=True) |
194 | 249 | return
|
195 | 250 | results = jmespath.search(result.path, response)
|
196 | 251 | return results
|
|
0 commit comments