Skip to content

Commit

Permalink
Merge pull request #90 from HewlettPackard/refactor-search-functions
Browse files Browse the repository at this point in the history
Move search() into the base class ORapi
  • Loading branch information
jofegan committed Nov 14, 2020
2 parents 1c33d66 + b351e5e commit 9b20ba3
Show file tree
Hide file tree
Showing 23 changed files with 415 additions and 373 deletions.
7 changes: 7 additions & 0 deletions opsramp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ def b64encode_payload(fname):

def get(self, suffix='', headers=None):
return self.api.get(suffix, headers)

def search(self, pattern='', headers=None, suffix='search'):
if pattern:
if pattern[0] != '?':
pattern = '?' + pattern
suffix += pattern
return self.api.get(suffix, headers)
26 changes: 18 additions & 8 deletions opsramp/devmgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ def __init__(self, parent):
super(Policies, self).__init__(parent.api, 'policies/management')

def search(self, policy_name=''):
suffix = 'search'
if policy_name:
suffix += '?name=' + policy_name
return self.api.get(suffix)
'''For historical reasons this class's search function might be
called with just a name. We have to cope and convert it into a
proper query string.'''
if policy_name and '=' not in policy_name:
qstring = 'name=' + policy_name
else:
qstring = policy_name
# we have assembled a proper query string now so use regular search.
return super(Policies, self).search(pattern=qstring)

def create(self, definition):
return self.api.post('', json=definition)
Expand All @@ -52,10 +57,15 @@ def __init__(self, parent):
super(Discovery, self).__init__(parent.api, 'policies/discovery')

def search(self, profile_name=''):
suffix = 'search'
if profile_name:
suffix += '?name=' + profile_name
return self.api.get(suffix)
'''For historical reasons this class's search function might be
called with just a name. We have to cope and convert it into a
proper query string.'''
if profile_name and '=' not in profile_name:
qstring = 'name=' + profile_name
else:
qstring = profile_name
# we have assembled a proper query string now so use regular search.
return super(Discovery, self).search(pattern=qstring)

def create(self, definition):
return self.api.post('', json=definition)
Expand Down
6 changes: 0 additions & 6 deletions opsramp/escalations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class Escalations(ORapi):
def __init__(self, parent):
super(Escalations, self).__init__(parent.api, 'escalations')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)

def create(self, definition):
return self.api.post('', json=definition)

Expand Down
7 changes: 4 additions & 3 deletions opsramp/first_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def __init__(self, parent):
'policies/firstResponse')

def search(self, pattern=''):
if pattern:
pattern = '?' + pattern
return self.api.get(pattern)
return super(First_Response, self).search(
pattern=pattern,
suffix=''
)

def policy_detail(self, uuid):
return self.api.get('%s' % uuid)
Expand Down
13 changes: 1 addition & 12 deletions opsramp/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ class Types(ORapi):
def __init__(self, parent):
super(Types, self).__init__(parent.api, 'available')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)


class Instances(ORapi):
def __init__(self, parent):
Expand All @@ -84,12 +78,6 @@ def __init__(self, parent):
self.creator_api = parent.api.clone()
self.creator_api.chroot('install')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)

def get_kubernetes_configuration(self, uuid):
return self.api.get('%s/configFile/kubernetes' % uuid)

Expand Down Expand Up @@ -120,6 +108,7 @@ def disable(self, uuid):
return self.api.post('%s/disable' % uuid)

def notifier(self, uuid, definition):
# Create or update installed integration base notifier
return self.api.post('%s/notifier' % uuid, json=definition)

# A helper function that extracts the authentication types from typical
Expand Down
6 changes: 0 additions & 6 deletions opsramp/mgmt_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ class Profiles(ORapi):
def __init__(self, parent):
super(Profiles, self).__init__(parent.api, 'managementProfiles')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)

def create(self, definition):
return self.api.post('', json=definition)

Expand Down
6 changes: 0 additions & 6 deletions opsramp/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,3 @@ def templates(self):
class Templates(ORapi):
def __init__(self, parent):
super(Templates, self).__init__(parent.api, 'templates')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)
12 changes: 8 additions & 4 deletions opsramp/msp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ def get(self, suffix='/minimal'):
return self.api.get(suffix)

def search(self, query_string=''):
suffix = 'search'
if query_string:
suffix += '?queryString=' + query_string
return self.api.get(suffix)
# For historical reasons the caller is allowed to omit the
# queryString prefix, so add it if necessary.
key = 'queryString='
if query_string and key not in query_string:
query_string = key + query_string
return super(Clients, self).search(
pattern=query_string
)

def create(self, definition):
assert 'name' in definition
Expand Down
16 changes: 8 additions & 8 deletions opsramp/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ def delete(self, uuid):

def search(self, pattern=''):
'''returns *verbose* details about resources on this tenant'''
suffix = 'search'
if pattern:
suffix += '?' + pattern
simple_list = self.api.get(suffix)
simple_list = super(Resources, self).search(
pattern=pattern,
suffix='search'
)
return list2ormp(simple_list)

def minimal(self, pattern=''):
'''returns *minimal* details about resources on this tenant'''
url_suffix = 'minimal'
if pattern:
url_suffix += '?{0}'.format(pattern)
simple_list = self.api.get(url_suffix)
simple_list = super(Resources, self).search(
pattern=pattern,
suffix='minimal'
)
return list2ormp(simple_list)

def applications(self, uuid):
Expand Down
13 changes: 4 additions & 9 deletions opsramp/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class Roles(ORapi):
def __init__(self, parent):
super(Roles, self).__init__(parent.api, 'roles')

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)

def create(self, definition):
return self.api.post('', json=definition)

Expand All @@ -48,6 +42,7 @@ def __init__(self, parent):
super(PermissionSets, self).__init__(parent.api, 'permissionSets')

def search(self, pattern=''):
if pattern:
pattern = '?' + pattern
return self.api.get(pattern)
return super(PermissionSets, self).search(
pattern=pattern,
suffix=''
)
6 changes: 0 additions & 6 deletions opsramp/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ def __init__(self, parent):
def get(self, suffix='/minimal'):
return self.api.get(suffix)

def search(self, pattern=''):
suffix = 'search'
if pattern:
suffix += '?' + pattern
return self.api.get(suffix)

def create(self, definition):
return self.api.post('', json=definition)

Expand Down
65 changes: 34 additions & 31 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# (c) Copyright 2019 Hewlett Packard Enterprise Development LP
# (c) Copyright 2019-2020 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,79 +94,82 @@ def setUp(self):
assert self.clients

def test_search(self):
url = self.clients.api.compute_url('search')
expected = ['unit', 'test', 'list']
thisid = 444444
expected = {'id': thisid}
url = self.clients.api.compute_url('search?queryString=id:%s' % thisid)
with requests_mock.Mocker() as m:
m.get(url, json=expected)
actual = self.clients.search('id=whatever')
m.get(url, json=expected, complete_qs=True)
actual = self.clients.search('id:%s' % thisid)
assert actual == expected

def test_minimal(self):
thisid = 555555
expected = {'id': thisid}
url = self.clients.api.compute_url('minimal')
expected = ['unit', 'test', 'list']
with requests_mock.Mocker() as m:
m.get(url, json=expected)
# default suffix
actual = self.clients.get()
assert actual == expected
# specific suffix
m.get(url, json=expected, complete_qs=True)
# specific suffix.
actual = self.clients.get('minimal')
assert actual == expected
# default suffix should be the same.
actual = self.clients.get()
assert actual == expected

def test_create_update(self):
url = self.clients.api.compute_url()
expected = ['unit', 'test', 'list']
bad_definition = {'bogus': 'dude'}
with requests_mock.Mocker() as m:
m.post(url, json=expected)
assert 'name' not in expected
# the name field is missing so we should get an error.
assert 'name' not in bad_definition
with self.assertRaises(AssertionError):
actual = self.clients.create(definition=expected)
actual = self.clients.create(definition=bad_definition)
assert m.call_count == 0
# now let's try a valid one.
fake_definition = {
good_definition = {
'name': 'elvis',
'address': 'graceland',
'timeZone': 'UTC',
'country': 'US'
}
thisid = 555555
expected = {'id': thisid}
url = self.clients.api.compute_url()
with requests_mock.Mocker() as m:
m.post(url, json=fake_definition)
actual = self.clients.create(definition=fake_definition)
assert actual == fake_definition
# and try an update
thisid = 123456
m.post(url, json=expected, complete_qs=True)
actual = self.clients.create(definition=good_definition)
assert actual == expected
# now try update
url = self.clients.api.compute_url(thisid)
with requests_mock.Mocker() as m:
m.post(url, json=fake_definition)
m.post(url, json=expected, complete_qs=True)
actual = self.clients.update(
uuid=thisid,
definition=fake_definition
definition=good_definition
)
assert actual == fake_definition
assert actual == expected

def test_suspend(self):
thisid = 789012
expected = {'id': thisid}
url = self.clients.api.compute_url('%s/suspend' % thisid)
expected = ['unit', 'test', 'list']
with requests_mock.Mocker() as m:
m.post(url, json=expected)
m.post(url, json=expected, complete_qs=True)
actual = self.clients.suspend(uuid=thisid)
assert actual == expected

def test_activate(self):
thisid = 345678
expected = {'id': thisid}
url = self.clients.api.compute_url('%s/activate' % thisid)
expected = ['unit', 'test', 'list']
with requests_mock.Mocker() as m:
m.post(url, json=expected)
m.post(url, json=expected, complete_qs=True)
actual = self.clients.activate(uuid=thisid)
assert actual == expected

def test_terminate(self):
thisid = 901234
expected = {'id': thisid}
url = self.clients.api.compute_url('%s/terminate' % thisid)
expected = ['unit', 'test', 'list']
with requests_mock.Mocker() as m:
m.post(url, json=expected)
m.post(url, json=expected, complete_qs=True)
actual = self.clients.terminate(uuid=thisid)
assert actual == expected

0 comments on commit 9b20ba3

Please sign in to comment.