Skip to content

Commit 6355bc6

Browse files
authored
Merge pull request #38 from hpe-container-platform-community/query_filter
Query filter - great job, @bharath-m-g
2 parents 951198a + 20253dd commit 6355bc6

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"python.formatting.provider": "black",
2525
"python.pythonPath": "/bin/python3",
2626
"python.envFile": "${workspaceFolder}/gitpod.env",
27-
"editor.formatOnSave": true
27+
"editor.formatOnSave": true,
28+
"files.insertFinalNewline": true
2829
}

bin/cli.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections import OrderedDict
1111

1212
import fire
13+
import jmespath
1314
import yaml
1415

1516
from hpecp.logger import Logger
@@ -23,11 +24,12 @@
2324
K8sClusterStatus,
2425
)
2526
from hpecp.user import User
27+
2628
from hpecp import (
27-
ContainerPlatformClient,
28-
ContainerPlatformClientException,
2929
APIException,
3030
APIItemConflictException,
31+
ContainerPlatformClient,
32+
ContainerPlatformClientException,
3133
)
3234
from hpecp.k8s_worker import WorkerK8sStatus
3335

@@ -141,11 +143,16 @@ def get(
141143
print(response.json)
142144

143145
def list(
144-
self, output="table", columns=Gateway.default_display_fields,
146+
self, output="table", columns=Gateway.default_display_fields, query={}
145147
):
146148
"""Retrieve the list of Gateways
147149
148150
:param output: how to display the output [text|table|json]
151+
:param query: jmespath (https://jmespath.org/) query
152+
153+
Example::
154+
155+
hpecp gateway list --output json --query '[0].ip'
149156
"""
150157
if output == "table":
151158
print(get_client().gateway.list().tabulate(columns=columns))
@@ -158,7 +165,11 @@ def list(
158165
)
159166
)
160167
else:
161-
print(get_client().gateway.list().json)
168+
data = get_client().gateway.list().json
169+
if query:
170+
print(jmespath.search(str(query), data))
171+
else:
172+
print(data)
162173

163174
def delete(
164175
self, gateway_id, wait_for_delete_secs=0,
@@ -212,7 +223,7 @@ def wait_for_state(
212223
success = get_client().gateway.wait_for_state(
213224
gateway_id=gateway_id, state=gateway_states,
214225
)
215-
except:
226+
except Exception:
216227
success = False
217228

218229
if not success:
@@ -280,7 +291,7 @@ def list(
280291
get_client()
281292
.k8s_worker.list()
282293
.tabulate(
283-
columns=columns, style="plain", display_headers=False,
294+
columns=columns, style="plain", display_headers=False
284295
)
285296
)
286297
else:
@@ -840,7 +851,6 @@ def list(
840851
)
841852
else:
842853
print(get_client().user.list().json)
843-
# raise NotImplementedError
844854

845855

846856
class AutoComplete:

hpecp/k8s_worker.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import absolute_import
2-
from .logger import Logger
3-
4-
from .exceptions import APIItemNotFoundException
52

3+
import re
4+
from enum import Enum
65
from operator import attrgetter
7-
from tabulate import tabulate
6+
87
import polling
9-
from enum import Enum
10-
import re
8+
from tabulate import tabulate
9+
10+
from .exceptions import APIItemNotFoundException
1111

1212
try:
1313
basestring
@@ -129,7 +129,7 @@ def create_with_ssh_key(self, ip, ssh_key_data, tags=[]):
129129
ip: str
130130
The IP address of the proxy host. Used for internal communication.
131131
ssh_key_data: str
132-
The ssh key data as a string.
132+
The ssh key data as a string.
133133
tags: list
134134
Tags to use, e.g. "{ 'tag1': 'foo', 'tag2', 'bar' }".
135135
@@ -206,7 +206,7 @@ def wait_for_status(self, worker_id, status=[], timeout_secs=1200):
206206
207207
Returns:
208208
bool: True if status was found before timeout, otherwise False
209-
209+
210210
Raises:
211211
APIItemNotFoundException: if the item is not found and status is not empty
212212
APIException: if a generic API exception occurred

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ configparser; python_version == "2.7"
66
polling
77
pyyaml
88
fire
9+
jmespath

0 commit comments

Comments
 (0)