Add get and show for config.hosts
Change-Id: Iaf7289161d73b4d06d1baaef7022f70b7c849bb9
This commit is contained in:
parent
20c7af4675
commit
2d1c47f18a
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,6 +3,8 @@
|
|||||||
# documentation
|
# documentation
|
||||||
doc/build
|
doc/build
|
||||||
doc/source/_build
|
doc/source/_build
|
||||||
|
AUTHORS
|
||||||
|
ChangeLog
|
||||||
|
|
||||||
.testrepository
|
.testrepository
|
||||||
dist
|
dist
|
||||||
|
10
README.rst
10
README.rst
@ -35,3 +35,13 @@ To use the python API, simply create a client with the endpoint::
|
|||||||
auth_url='http://localhost:8080/v2/auth',
|
auth_url='http://localhost:8080/v2/auth',
|
||||||
version='2_0')
|
version='2_0')
|
||||||
hosts = c.config.hosts.list()
|
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"}'
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,7 +113,10 @@ class HTTPClient(object):
|
|||||||
kwargs['body'] = json.dumps(kwargs['body'])
|
kwargs['body'] = json.dumps(kwargs['body'])
|
||||||
|
|
||||||
resp, body = self.request(url, method, **kwargs)
|
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):
|
def request(self, url, method, **kwargs):
|
||||||
"""Send an http request with the specified characteristics.
|
"""Send an http request with the specified characteristics.
|
||||||
|
@ -53,6 +53,30 @@ def json_formatter(js):
|
|||||||
return jsonutils.dumps(js, indent=2, ensure_ascii=False)
|
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):
|
def print_list(objs, fields, field_labels=None, formatters={}, sortby=None):
|
||||||
field_labels = field_labels or fields
|
field_labels = field_labels or fields
|
||||||
pt = prettytable.PrettyTable([f for f in field_labels],
|
pt = prettytable.PrettyTable([f for f in field_labels],
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
import httpretty
|
import httpretty
|
||||||
|
|
||||||
from surveilclient.tests.v2_0 import clienttest
|
from surveilclient.tests.v2_0 import clienttest
|
||||||
@ -37,16 +39,60 @@ class TestHosts(clienttest.ClientTest):
|
|||||||
def test_create(self):
|
def test_create(self):
|
||||||
httpretty.register_uri(
|
httpretty.register_uri(
|
||||||
httpretty.POST, "http://localhost:8080/v2/config/hosts",
|
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(
|
self.client.config.hosts.create(
|
||||||
host_name="new_host"
|
host_name="new_host",
|
||||||
|
address="192.168.2.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
httpretty.last_request().body.decode(),
|
json.loads(httpretty.last_request().body.decode()),
|
||||||
u'{"host_name": "new_host"}'
|
{
|
||||||
|
"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
|
@httpretty.activate
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
import json
|
||||||
|
|
||||||
from surveilclient.common import surveil_manager
|
from surveilclient.common import surveil_manager
|
||||||
|
|
||||||
@ -27,12 +28,37 @@ class HostsManager(surveil_manager.SurveilManager):
|
|||||||
|
|
||||||
def create(self, **kwargs):
|
def create(self, **kwargs):
|
||||||
"""Create a new host."""
|
"""Create a new host."""
|
||||||
|
|
||||||
|
if "custom_fields" in kwargs:
|
||||||
|
kwargs["custom_fields"] = json.loads(kwargs["custom_fields"])
|
||||||
|
|
||||||
resp, body = self.http_client.json_request(
|
resp, body = self.http_client.json_request(
|
||||||
HostsManager.base_url, 'POST',
|
HostsManager.base_url, 'POST',
|
||||||
body=kwargs
|
body=kwargs
|
||||||
)
|
)
|
||||||
return body
|
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):
|
def delete(self, host_name):
|
||||||
"""Delete a host."""
|
"""Delete a host."""
|
||||||
resp, body = self.http_client.request(
|
resp, body = self.http_client.request(
|
||||||
|
@ -45,12 +45,38 @@ def do_config_host_list(sc, args):
|
|||||||
utils.print_list(hosts, cols, formatters=formatters)
|
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("--host_name", help="Name of the host")
|
||||||
@cliutils.arg("--address", help="Address of the host")
|
@cliutils.arg("--address", help="Address of the host")
|
||||||
@cliutils.arg("--max_check_attempts")
|
@cliutils.arg("--max_check_attempts")
|
||||||
@cliutils.arg("--check_period")
|
@cliutils.arg("--check_period")
|
||||||
@cliutils.arg("--contacts")
|
@cliutils.arg("--contacts")
|
||||||
@cliutils.arg("--contact_groups")
|
@cliutils.arg("--contact_groups")
|
||||||
|
@cliutils.arg("--custom_fields")
|
||||||
@cliutils.arg("--notification_interval")
|
@cliutils.arg("--notification_interval")
|
||||||
@cliutils.arg("--notification_period")
|
@cliutils.arg("--notification_period")
|
||||||
@cliutils.arg("--use")
|
@cliutils.arg("--use")
|
||||||
@ -62,6 +88,7 @@ def do_config_host_create(sc, args):
|
|||||||
'check_period',
|
'check_period',
|
||||||
'contacts',
|
'contacts',
|
||||||
'contact_groups',
|
'contact_groups',
|
||||||
|
'custom_fields',
|
||||||
'notification_interval',
|
'notification_interval',
|
||||||
'notification_period',
|
'notification_period',
|
||||||
'use']
|
'use']
|
||||||
@ -69,6 +96,24 @@ def do_config_host_create(sc, args):
|
|||||||
sc.config.hosts.create(**host)
|
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")
|
@cliutils.arg("--host_name", help="Name of the host")
|
||||||
def do_config_host_delete(sc, args):
|
def do_config_host_delete(sc, args):
|
||||||
"""Create a config host."""
|
"""Create a config host."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user