Add get and show for config.hosts

Change-Id: Iaf7289161d73b4d06d1baaef7022f70b7c849bb9
This commit is contained in:
Vincent Fournier 2015-05-07 14:25:23 -04:00
parent 20c7af4675
commit 2d1c47f18a
7 changed files with 161 additions and 5 deletions

2
.gitignore vendored
View File

@ -3,6 +3,8 @@
# documentation
doc/build
doc/source/_build
AUTHORS
ChangeLog
.testrepository
dist

View File

@ -35,3 +35,13 @@ To use the python API, simply create a client with the endpoint::
auth_url='http://localhost:8080/v2/auth',
version='2_0')
hosts = c.config.hosts.list()
How to use it
-------------
Config-host-update::
surveil config-host-update [host_name] --address [ADDRESS] --custom_fields '{"_field1": "value1", "_field2": "value2"}'

View File

@ -113,7 +113,10 @@ class HTTPClient(object):
kwargs['body'] = json.dumps(kwargs['body'])
resp, body = self.request(url, method, **kwargs)
return resp, json.loads(body)
if body != "":
body = json.loads(body)
return resp, body
def request(self, url, method, **kwargs):
"""Send an http request with the specified characteristics.

View File

@ -53,6 +53,30 @@ def json_formatter(js):
return jsonutils.dumps(js, indent=2, ensure_ascii=False)
def print_item(objs, properties):
""" Add the missing properties to the objs """
for prop in properties:
if prop not in objs:
objs[prop] = ""
cols = [
'Property',
'Value'
]
""" Override the properties keys pass in parameter """
list = []
for value in properties:
list.append({'prop': value, 'value': objs[value].__str__()})
formatters = {
'Property': lambda x: x['prop'],
'Value': lambda x: x['value']
}
print_list(list, cols, formatters=formatters)
def print_list(objs, fields, field_labels=None, formatters={}, sortby=None):
field_labels = field_labels or fields
pt = prettytable.PrettyTable([f for f in field_labels],

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import httpretty
from surveilclient.tests.v2_0 import clienttest
@ -37,16 +39,60 @@ class TestHosts(clienttest.ClientTest):
def test_create(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:8080/v2/config/hosts",
body='{"host_name": "new_host"}'
body='{"host_name": "new_host", "address": "192.168.2.1"}'
)
self.client.config.hosts.create(
host_name="new_host"
host_name="new_host",
address="192.168.2.1"
)
self.assertEqual(
httpretty.last_request().body.decode(),
u'{"host_name": "new_host"}'
json.loads(httpretty.last_request().body.decode()),
{
"host_name": "new_host",
"address": "192.168.2.1"
}
)
@httpretty.activate
def test_show(self):
httpretty.register_uri(
httpretty.GET,
"http://localhost:8080/v2/config/hosts/host_name_to_show",
body='{"host_name": "host_name_to_show"}'
)
host = self.client.config.hosts.get(
host_name="host_name_to_show"
)
self.assertEqual(
host,
{"host_name": "host_name_to_show"}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
"http://localhost:8080/v2/config/hosts/host_name_to_update",
body='{"test": "test"}'
)
self.client.config.hosts.update(
"host_name_to_update",
address="192.168.0.1",
check_period="24x7"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"check_period": u"24x7",
"host_name": u"host_name_to_update",
"address": u"192.168.0.1"
}
)
@httpretty.activate

View File

@ -11,6 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
from surveilclient.common import surveil_manager
@ -27,12 +28,37 @@ class HostsManager(surveil_manager.SurveilManager):
def create(self, **kwargs):
"""Create a new host."""
if "custom_fields" in kwargs:
kwargs["custom_fields"] = json.loads(kwargs["custom_fields"])
resp, body = self.http_client.json_request(
HostsManager.base_url, 'POST',
body=kwargs
)
return body
def get(self, host_name):
"""Get a new host."""
resp, body = self.http_client.json_request(
HostsManager.base_url + '/' + host_name, 'GET',
body=''
)
return body
def update(self, host_name, **kwargs):
"""Update a host."""
kwargs["host_name"] = host_name
if "custom_fields" in kwargs:
kwargs["custom_fields"] = json.loads(kwargs["custom_fields"])
resp, body = self.http_client.json_request(
HostsManager.base_url + '/' + host_name, 'PUT',
body=kwargs
)
return body
def delete(self, host_name):
"""Delete a host."""
resp, body = self.http_client.request(

View File

@ -45,12 +45,38 @@ def do_config_host_list(sc, args):
utils.print_list(hosts, cols, formatters=formatters)
@cliutils.arg("host_name", help="Name of the host")
@cliutils.arg("--address", help="Address of the host")
@cliutils.arg("--max_check_attempts")
@cliutils.arg("--check_period")
@cliutils.arg("--contacts")
@cliutils.arg("--contact_groups")
@cliutils.arg("--custom_fields")
@cliutils.arg("--notification_interval")
@cliutils.arg("--notification_period")
@cliutils.arg("--use")
def do_config_host_update(sc, args):
"""Create a config host."""
arg_names = ['address',
'max_check_attempts',
'check_period',
'contacts',
'contact_groups',
'custom_fields',
'notification_interval',
'notification_period',
'use']
host = _dict_from_args(args, arg_names)
sc.config.hosts.update(args.host_name, **host)
@cliutils.arg("--host_name", help="Name of the host")
@cliutils.arg("--address", help="Address of the host")
@cliutils.arg("--max_check_attempts")
@cliutils.arg("--check_period")
@cliutils.arg("--contacts")
@cliutils.arg("--contact_groups")
@cliutils.arg("--custom_fields")
@cliutils.arg("--notification_interval")
@cliutils.arg("--notification_period")
@cliutils.arg("--use")
@ -62,6 +88,7 @@ def do_config_host_create(sc, args):
'check_period',
'contacts',
'contact_groups',
'custom_fields',
'notification_interval',
'notification_period',
'use']
@ -69,6 +96,24 @@ def do_config_host_create(sc, args):
sc.config.hosts.create(**host)
@cliutils.arg("host_name", help="Name of the host")
def do_config_host_show(sc, args):
"""Show a specific host."""
host = sc.config.hosts.get(args.host_name)
if args.json:
print(utils.json_formatter(host))
elif host:
""" Specify the shown order and all the properties to display """
hostProperties = [
'host_name', 'address', 'check_period', 'contact_groups',
'contacts', 'custom_fields', 'max_check_attempts',
'notification_interval', 'notification_period', 'use'
]
utils.print_item(host, hostProperties)
@cliutils.arg("--host_name", help="Name of the host")
def do_config_host_delete(sc, args):
"""Create a config host."""