Skip to content

Commit

Permalink
Add filter by type to credential API
Browse files Browse the repository at this point in the history
  • Loading branch information
kholdaway committed Dec 7, 2017
1 parent 3372366 commit 0ca58ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions qpc/cred/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def __init__(self, subparsers):
CliCommand.__init__(self, self.SUBCOMMAND, self.ACTION,
subparsers.add_parser(self.ACTION), GET,
credential.CREDENTIAL_URI, [codes.ok])
self.parser.add_argument('--type', dest='type',
choices=[credential.NETWORK_CRED_TYPE,
credential.VCENTER_CRED_TYPE],
metavar='TYPE',
help=_(messages.CRED_TYPE_FILTER_HELP),
required=False)

def _build_req_params(self):
"""Sub-commands can override to construct request parameters."""
if 'type' in self.args and self.args.type:
self.req_params = {'cred_type': self.args.type}

def _handle_response_success(self):
json_data = self.response.json()
Expand Down
20 changes: 20 additions & 0 deletions qpc/cred/tests_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,23 @@ def test_list_cred_data(self):
'"username":"root"}]'
self.assertEqual(cred_out.getvalue().replace('\n', '')
.replace(' ', '').strip(), expected)

def test_list_filtered_cred_data(self):
"""Testing the list credential with filter by cred type."""
cred_out = StringIO()
url = BASE_URL + CREDENTIAL_URI
credential_entry = {'id': 1, 'name': 'cred1', 'cred_type': 'network',
'username': 'root',
'password': '********'}
data = [credential_entry]
with requests_mock.Mocker() as mocker:
mocker.get(url, status_code=200, json=data)
alc = CredListCommand(SUBPARSER)
args = Namespace(type='network')
with redirect_stdout(cred_out):
alc.main(args)
expected = '[{"cred_type":"network","id":1,'\
'"name":"cred1","password":"********",' \
'"username":"root"}]'
self.assertEqual(cred_out.getvalue().replace('\n', '')
.replace(' ', '').strip(), expected)
4 changes: 3 additions & 1 deletion qpc/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"""CLI messages for translation."""

CRED_NAME_HELP = 'credential credential name'
CRED_TYPE_HELP = 'type of scan job. Valid values: vcenter, network'
CRED_TYPE_HELP = 'type of credential job. Valid values: vcenter, network'
CRED_TYPE_FILTER_HELP = 'type filter for listing credentials. Valid '\
'values: vcenter, network'
CRED_USER_HELP = 'user name for authenticating against target system'
CRED_PWD_HELP = 'password for authenticating against target system'
CRED_SSH_HELP = 'file containing SSH key'
Expand Down

0 comments on commit 0ca58ce

Please sign in to comment.