Merge "Removes os-instance-usage-audit-log from the V3 API"
This commit is contained in:
commit
52a191640e
@ -1,17 +0,0 @@
|
||||
{
|
||||
"instance_usage_audit_log": {
|
||||
"hosts_not_run": [
|
||||
"defac351f91940668301096238d26b47"
|
||||
],
|
||||
"log": {},
|
||||
"num_hosts": 1,
|
||||
"num_hosts_done": 0,
|
||||
"num_hosts_not_run": 1,
|
||||
"num_hosts_running": 0,
|
||||
"overall_status": "0 of 1 hosts done. 0 errors.",
|
||||
"period_beginning": "2013-08-01 00:00:00",
|
||||
"period_ending": "2013-09-01 00:00:00",
|
||||
"total_errors": 0,
|
||||
"total_instances": 0
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"instance_usage_audit_log": {
|
||||
"hosts_not_run": [
|
||||
"ccf86bf6d0104fe69ee658eaa31a7469"
|
||||
],
|
||||
"log": {},
|
||||
"num_hosts": 1,
|
||||
"num_hosts_done": 0,
|
||||
"num_hosts_not_run": 1,
|
||||
"num_hosts_running": 0,
|
||||
"overall_status": "0 of 1 hosts done. 0 errors.",
|
||||
"period_beginning": "2012-06-01 00:00:00",
|
||||
"period_ending": "2012-07-01 00:00:00",
|
||||
"total_errors": 0,
|
||||
"total_instances": 0
|
||||
}
|
||||
}
|
@ -159,7 +159,6 @@
|
||||
"compute_extension:instance_actions:events": "rule:admin_api",
|
||||
"compute_extension:v3:os-instance-actions:events": "rule:admin_api",
|
||||
"compute_extension:instance_usage_audit_log": "rule:admin_api",
|
||||
"compute_extension:v3:os-instance-usage-audit-log": "rule:admin_api",
|
||||
"compute_extension:v3:ips:discoverable": "",
|
||||
"compute_extension:keypairs": "",
|
||||
"compute_extension:keypairs:index": "",
|
||||
|
@ -1,137 +0,0 @@
|
||||
# Copyright 2012 OpenStack Foundation
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# 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 datetime
|
||||
|
||||
from oslo.config import cfg
|
||||
import webob.exc
|
||||
|
||||
from nova.api.openstack import extensions
|
||||
from nova import compute
|
||||
from nova.openstack.common.gettextutils import _
|
||||
from nova import utils
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('compute_topic', 'nova.compute.rpcapi')
|
||||
|
||||
ALIAS = "os-instance-usage-audit-log"
|
||||
authorize = extensions.extension_authorizer('compute',
|
||||
'v3:' + ALIAS)
|
||||
|
||||
|
||||
class InstanceUsageAuditLogController(object):
|
||||
def __init__(self):
|
||||
self.host_api = compute.HostAPI()
|
||||
|
||||
@extensions.expected_errors(400)
|
||||
def index(self, req):
|
||||
context = req.environ['nova.context']
|
||||
authorize(context)
|
||||
before = req.GET.get('before', None)
|
||||
if before:
|
||||
try:
|
||||
if '.' in before:
|
||||
before = datetime.datetime.strptime(str(before),
|
||||
"%Y-%m-%d %H:%M:%S.%f")
|
||||
else:
|
||||
before = datetime.datetime.strptime(str(before),
|
||||
"%Y-%m-%d %H:%M:%S")
|
||||
except ValueError:
|
||||
msg = _("Invalid timestamp for date %s") % id
|
||||
raise webob.exc.HTTPBadRequest(explanation=msg)
|
||||
task_log = self._get_audit_task_logs(context, before=before)
|
||||
return {'instance_usage_audit_log': task_log}
|
||||
|
||||
def _get_audit_task_logs(self, context, begin=None, end=None,
|
||||
before=None):
|
||||
"""Returns a full log for all instance usage audit tasks on all
|
||||
computes.
|
||||
|
||||
:param begin: datetime beginning of audit period to get logs for,
|
||||
Defaults to the beginning of the most recently completed
|
||||
audit period prior to the 'before' date.
|
||||
:param end: datetime ending of audit period to get logs for,
|
||||
Defaults to the ending of the most recently completed
|
||||
audit period prior to the 'before' date.
|
||||
:param before: By default we look for the audit period most recently
|
||||
completed before this datetime. Has no effect if both begin and end
|
||||
are specified.
|
||||
"""
|
||||
defbegin, defend = utils.last_completed_audit_period(before=before)
|
||||
if begin is None:
|
||||
begin = defbegin
|
||||
if end is None:
|
||||
end = defend
|
||||
task_logs = self.host_api.task_log_get_all(context,
|
||||
"instance_usage_audit",
|
||||
begin, end)
|
||||
# We do this this way to include disabled compute services,
|
||||
# which can have instances on them. (mdragon)
|
||||
filters = {'topic': CONF.compute_topic}
|
||||
services = self.host_api.service_get_all(context, filters=filters)
|
||||
hosts = set(serv['host'] for serv in services)
|
||||
seen_hosts = set()
|
||||
done_hosts = set()
|
||||
running_hosts = set()
|
||||
total_errors = 0
|
||||
total_items = 0
|
||||
for tlog in task_logs:
|
||||
seen_hosts.add(tlog['host'])
|
||||
if tlog['state'] == "DONE":
|
||||
done_hosts.add(tlog['host'])
|
||||
if tlog['state'] == "RUNNING":
|
||||
running_hosts.add(tlog['host'])
|
||||
total_errors += tlog['errors']
|
||||
total_items += tlog['task_items']
|
||||
log = dict((tl['host'], dict(state=tl['state'],
|
||||
instances=tl['task_items'],
|
||||
errors=tl['errors'],
|
||||
message=tl['message']))
|
||||
for tl in task_logs)
|
||||
missing_hosts = hosts - seen_hosts
|
||||
overall_status = "%s hosts done. %s errors." % (
|
||||
'ALL' if len(done_hosts) == len(hosts)
|
||||
else "%s of %s" % (len(done_hosts), len(hosts)),
|
||||
total_errors)
|
||||
return dict(period_beginning=str(begin),
|
||||
period_ending=str(end),
|
||||
num_hosts=len(hosts),
|
||||
num_hosts_done=len(done_hosts),
|
||||
num_hosts_running=len(running_hosts),
|
||||
num_hosts_not_run=len(missing_hosts),
|
||||
hosts_not_run=list(missing_hosts),
|
||||
total_instances=total_items,
|
||||
total_errors=total_errors,
|
||||
overall_status=overall_status,
|
||||
log=log)
|
||||
|
||||
|
||||
class InstanceUsageAuditLog(extensions.V3APIExtensionBase):
|
||||
"""Admin-only Task Log Monitoring."""
|
||||
name = "InstanceUsageAuditLog"
|
||||
alias = ALIAS
|
||||
version = 1
|
||||
|
||||
def get_resources(self):
|
||||
ext = extensions.ResourceExtension(ALIAS,
|
||||
InstanceUsageAuditLogController())
|
||||
return [ext]
|
||||
|
||||
def get_controller_extensions(self):
|
||||
"""It's an abstract function V3APIExtensionBase and the extension
|
||||
will not be loaded without it.
|
||||
"""
|
||||
return []
|
@ -1,219 +0,0 @@
|
||||
# Copyright (c) 2012 OpenStack Foundation
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# 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 datetime
|
||||
from webob import exc
|
||||
|
||||
from nova.api.openstack.compute.plugins.v3 import \
|
||||
instance_usage_audit_log as ial
|
||||
from nova import context
|
||||
from nova import db
|
||||
from nova.openstack.common import timeutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests.objects import test_service
|
||||
from nova import utils
|
||||
import urllib
|
||||
|
||||
|
||||
service_base = test_service.fake_service
|
||||
TEST_COMPUTE_SERVICES = [dict(service_base, host='foo', topic='compute'),
|
||||
dict(service_base, host='bar', topic='compute'),
|
||||
dict(service_base, host='baz', topic='compute'),
|
||||
dict(service_base, host='plonk', topic='compute'),
|
||||
dict(service_base, host='wibble', topic='bogus'),
|
||||
]
|
||||
|
||||
|
||||
begin1 = datetime.datetime(2012, 7, 4, 6, 0, 0)
|
||||
begin2 = end1 = datetime.datetime(2012, 7, 5, 6, 0, 0)
|
||||
begin3 = end2 = datetime.datetime(2012, 7, 6, 6, 0, 0)
|
||||
end3 = datetime.datetime(2012, 7, 7, 6, 0, 0)
|
||||
|
||||
|
||||
#test data
|
||||
|
||||
|
||||
TEST_LOGS1 = [
|
||||
#all services done, no errors.
|
||||
dict(host="plonk", period_beginning=begin1, period_ending=end1,
|
||||
state="DONE", errors=0, task_items=23, message="test1"),
|
||||
dict(host="baz", period_beginning=begin1, period_ending=end1,
|
||||
state="DONE", errors=0, task_items=17, message="test2"),
|
||||
dict(host="bar", period_beginning=begin1, period_ending=end1,
|
||||
state="DONE", errors=0, task_items=10, message="test3"),
|
||||
dict(host="foo", period_beginning=begin1, period_ending=end1,
|
||||
state="DONE", errors=0, task_items=7, message="test4"),
|
||||
]
|
||||
|
||||
|
||||
TEST_LOGS2 = [
|
||||
#some still running...
|
||||
dict(host="plonk", period_beginning=begin2, period_ending=end2,
|
||||
state="DONE", errors=0, task_items=23, message="test5"),
|
||||
dict(host="baz", period_beginning=begin2, period_ending=end2,
|
||||
state="DONE", errors=0, task_items=17, message="test6"),
|
||||
dict(host="bar", period_beginning=begin2, period_ending=end2,
|
||||
state="RUNNING", errors=0, task_items=10, message="test7"),
|
||||
dict(host="foo", period_beginning=begin2, period_ending=end2,
|
||||
state="DONE", errors=0, task_items=7, message="test8"),
|
||||
]
|
||||
|
||||
|
||||
TEST_LOGS3 = [
|
||||
#some errors..
|
||||
dict(host="plonk", period_beginning=begin3, period_ending=end3,
|
||||
state="DONE", errors=0, task_items=23, message="test9"),
|
||||
dict(host="baz", period_beginning=begin3, period_ending=end3,
|
||||
state="DONE", errors=2, task_items=17, message="test10"),
|
||||
dict(host="bar", period_beginning=begin3, period_ending=end3,
|
||||
state="DONE", errors=0, task_items=10, message="test11"),
|
||||
dict(host="foo", period_beginning=begin3, period_ending=end3,
|
||||
state="DONE", errors=1, task_items=7, message="test12"),
|
||||
]
|
||||
|
||||
|
||||
def fake_task_log_get_all(context, task_name, begin, end,
|
||||
host=None, state=None):
|
||||
assert task_name == "instance_usage_audit"
|
||||
|
||||
if begin == begin1 and end == end1:
|
||||
return TEST_LOGS1
|
||||
if begin == begin2 and end == end2:
|
||||
return TEST_LOGS2
|
||||
if begin == begin3 and end == end3:
|
||||
return TEST_LOGS3
|
||||
raise AssertionError("Invalid date %s to %s" % (begin, end))
|
||||
|
||||
|
||||
def fake_last_completed_audit_period(unit=None, before=None):
|
||||
audit_periods = [(begin3, end3),
|
||||
(begin2, end2),
|
||||
(begin1, end1)]
|
||||
if before is not None:
|
||||
for begin, end in audit_periods:
|
||||
if before > end:
|
||||
return begin, end
|
||||
raise AssertionError("Invalid before date %s" % (before))
|
||||
return begin1, end1
|
||||
|
||||
|
||||
class InstanceUsageAuditLogTest(test.NoDBTestCase):
|
||||
def setUp(self):
|
||||
super(InstanceUsageAuditLogTest, self).setUp()
|
||||
self.context = context.get_admin_context()
|
||||
timeutils.set_time_override(datetime.datetime(2012, 7, 5, 10, 0, 0))
|
||||
self.controller = ial.InstanceUsageAuditLogController()
|
||||
self.host_api = self.controller.host_api
|
||||
|
||||
def fake_service_get_all(context, disabled):
|
||||
self.assertIsNone(disabled)
|
||||
return TEST_COMPUTE_SERVICES
|
||||
|
||||
self.stubs.Set(utils, 'last_completed_audit_period',
|
||||
fake_last_completed_audit_period)
|
||||
self.stubs.Set(db, 'service_get_all',
|
||||
fake_service_get_all)
|
||||
self.stubs.Set(db, 'task_log_get_all',
|
||||
fake_task_log_get_all)
|
||||
|
||||
def tearDown(self):
|
||||
super(InstanceUsageAuditLogTest, self).tearDown()
|
||||
timeutils.clear_time_override()
|
||||
|
||||
def test_index(self):
|
||||
req = fakes.HTTPRequestV3.blank('/os-instance_usage_audit_log')
|
||||
result = self.controller.index(req)
|
||||
self.assertIn('instance_usage_audit_log', result)
|
||||
logs = result['instance_usage_audit_log']
|
||||
self.assertEqual(57, logs['total_instances'])
|
||||
self.assertEqual(0, logs['total_errors'])
|
||||
self.assertEqual(4, len(logs['log']))
|
||||
self.assertEqual(4, logs['num_hosts'])
|
||||
self.assertEqual(4, logs['num_hosts_done'])
|
||||
self.assertEqual(0, logs['num_hosts_running'])
|
||||
self.assertEqual(0, logs['num_hosts_not_run'])
|
||||
self.assertEqual("ALL hosts done. 0 errors.", logs['overall_status'])
|
||||
|
||||
def test_index_with_format1(self):
|
||||
before = urllib.quote("2012-07-05 10:00:00")
|
||||
req = fakes.HTTPRequestV3.blank(
|
||||
'/os-instance_usage_audit_log?before=' + before)
|
||||
result = self.controller.index(req)
|
||||
self.assertIn('instance_usage_audit_log', result)
|
||||
logs = result['instance_usage_audit_log']
|
||||
self.assertEqual(57, logs['total_instances'])
|
||||
self.assertEqual(0, logs['total_errors'])
|
||||
self.assertEqual(4, len(logs['log']))
|
||||
self.assertEqual(4, logs['num_hosts'])
|
||||
self.assertEqual(4, logs['num_hosts_done'])
|
||||
self.assertEqual(0, logs['num_hosts_running'])
|
||||
self.assertEqual(0, logs['num_hosts_not_run'])
|
||||
self.assertEqual("ALL hosts done. 0 errors.", logs['overall_status'])
|
||||
|
||||
def test_index_with_format2(self):
|
||||
before = urllib.quote('2012-07-05 10:00:00.10')
|
||||
req = fakes.HTTPRequestV3.blank(
|
||||
'/os-instance_usage_audit_log?before=' + before)
|
||||
result = self.controller.index(req)
|
||||
self.assertIn('instance_usage_audit_log', result)
|
||||
logs = result['instance_usage_audit_log']
|
||||
self.assertEqual(57, logs['total_instances'])
|
||||
self.assertEqual(0, logs['total_errors'])
|
||||
self.assertEqual(4, len(logs['log']))
|
||||
self.assertEqual(4, logs['num_hosts'])
|
||||
self.assertEqual(4, logs['num_hosts_done'])
|
||||
self.assertEqual(0, logs['num_hosts_running'])
|
||||
self.assertEqual(0, logs['num_hosts_not_run'])
|
||||
self.assertEqual("ALL hosts done. 0 errors.", logs['overall_status'])
|
||||
|
||||
def test_index_with_invalid_format(self):
|
||||
req = fakes.HTTPRequestV3.blank(
|
||||
'/os-instance_usage_audit_log?before=abc')
|
||||
self.assertRaises(exc.HTTPBadRequest, self.controller.index, req)
|
||||
|
||||
def test_index_with_running(self):
|
||||
before = urllib.quote('2012-07-06 10:00:00')
|
||||
req = fakes.HTTPRequestV3.blank(
|
||||
'/os-instance_usage_audit_log?before=' + before)
|
||||
result = self.controller.index(req)
|
||||
self.assertIn('instance_usage_audit_log', result)
|
||||
logs = result['instance_usage_audit_log']
|
||||
self.assertEqual(57, logs['total_instances'])
|
||||
self.assertEqual(0, logs['total_errors'])
|
||||
self.assertEqual(4, len(logs['log']))
|
||||
self.assertEqual(4, logs['num_hosts'])
|
||||
self.assertEqual(3, logs['num_hosts_done'])
|
||||
self.assertEqual(1, logs['num_hosts_running'])
|
||||
self.assertEqual(0, logs['num_hosts_not_run'])
|
||||
self.assertEqual("3 of 4 hosts done. 0 errors.",
|
||||
logs['overall_status'])
|
||||
|
||||
def test_index_with_errors(self):
|
||||
before = urllib.quote('2012-07-07 10:00:00')
|
||||
req = fakes.HTTPRequestV3.blank(
|
||||
'/os-instance_usage_audit_log?before=' + before)
|
||||
result = self.controller.index(req)
|
||||
self.assertIn('instance_usage_audit_log', result)
|
||||
logs = result['instance_usage_audit_log']
|
||||
self.assertEqual(57, logs['total_instances'])
|
||||
self.assertEqual(3, logs['total_errors'])
|
||||
self.assertEqual(4, len(logs['log']))
|
||||
self.assertEqual(4, logs['num_hosts'])
|
||||
self.assertEqual(4, logs['num_hosts_done'])
|
||||
self.assertEqual(0, logs['num_hosts_running'])
|
||||
self.assertEqual(0, logs['num_hosts_not_run'])
|
||||
self.assertEqual("ALL hosts done. 3 errors.",
|
||||
logs['overall_status'])
|
@ -216,7 +216,6 @@ policy_data = """
|
||||
"compute_extension:instance_actions:events": "is_admin:True",
|
||||
"compute_extension:v3:os-instance-actions:events": "is_admin:True",
|
||||
"compute_extension:instance_usage_audit_log": "",
|
||||
"compute_extension:v3:os-instance-usage-audit-log": "",
|
||||
"compute_extension:keypairs": "",
|
||||
"compute_extension:keypairs:index": "",
|
||||
"compute_extension:keypairs:show": "",
|
||||
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"instance_usage_audit_log": {
|
||||
"hosts_not_run": [
|
||||
"%(hostid)s"
|
||||
],
|
||||
"log": {},
|
||||
"num_hosts": 1,
|
||||
"num_hosts_done": 0,
|
||||
"num_hosts_not_run": 1,
|
||||
"num_hosts_running": 0,
|
||||
"overall_status": "0 of 1 hosts done. 0 errors.",
|
||||
"period_beginning": "%(timestamp)s",
|
||||
"period_ending": "%(timestamp)s",
|
||||
"total_errors": 0,
|
||||
"total_instances": 0
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"instance_usage_audit_log": {
|
||||
"hosts_not_run": [
|
||||
"%(hostid)s"
|
||||
],
|
||||
"log": {},
|
||||
"num_hosts": 1,
|
||||
"num_hosts_done": 0,
|
||||
"num_hosts_not_run": 1,
|
||||
"num_hosts_running": 0,
|
||||
"overall_status": "0 of 1 hosts done. 0 errors.",
|
||||
"period_beginning": "%(timestamp)s",
|
||||
"period_ending": "%(timestamp)s",
|
||||
"total_errors": 0,
|
||||
"total_instances": 0
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
# Copyright 2012 Nebula, Inc.
|
||||
# Copyright 2013 IBM Corp.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# 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 urllib
|
||||
|
||||
from nova.tests.integrated.v3 import api_sample_base
|
||||
|
||||
|
||||
class InstanceUsageAuditLogJsonTest(api_sample_base.ApiSampleTestBaseV3):
|
||||
extension_name = "os-instance-usage-audit-log"
|
||||
|
||||
def test_index_instance_usage_audit_log(self):
|
||||
response = self._do_get('os-instance-usage-audit-log')
|
||||
subs = self._get_regexes()
|
||||
subs['hostid'] = '[a-f0-9]+'
|
||||
self._verify_response('inst-usage-audit-log-index-get-resp',
|
||||
subs, response, 200)
|
||||
|
||||
def test_index_instance_usage_audit_log_with_before(self):
|
||||
response = self._do_get('os-instance-usage-audit-log?before=%s' %
|
||||
urllib.quote('2012-07-05 10:00:00'))
|
||||
subs = self._get_regexes()
|
||||
subs['hostid'] = '[a-f0-9]+'
|
||||
self._verify_response(
|
||||
'inst-usage-audit-log-index-with-before-get-resp',
|
||||
subs, response, 200)
|
@ -86,7 +86,6 @@ nova.api.v3.extensions =
|
||||
hypervisors = nova.api.openstack.compute.plugins.v3.hypervisors:Hypervisors
|
||||
instance_actions = nova.api.openstack.compute.plugins.v3.instance_actions:InstanceActions
|
||||
ips = nova.api.openstack.compute.plugins.v3.ips:IPs
|
||||
instance_usage_audit_log = nova.api.openstack.compute.plugins.v3.instance_usage_audit_log:InstanceUsageAuditLog
|
||||
keypairs = nova.api.openstack.compute.plugins.v3.keypairs:Keypairs
|
||||
lock_server = nova.api.openstack.compute.plugins.v3.lock_server:LockServer
|
||||
migrate_server = nova.api.openstack.compute.plugins.v3.migrate_server:MigrateServer
|
||||
|
Loading…
x
Reference in New Issue
Block a user