1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 OpenStack, LLC.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
"""Volume-related Utilities and helpers."""
19
from nova import flags
20
from nova import log as logging
21
from nova.notifier import api as notifier_api
22
from nova import utils
26
LOG = logging.getLogger(__name__)
29
def notify_usage_exists(context, volume_ref, current_period=False):
30
""" Generates 'exists' notification for a volume for usage auditing
33
Generates usage for last completed period, unless 'current_period'
35
begin, end = utils.last_completed_audit_period()
38
audit_end = utils.utcnow()
43
extra_usage_info = dict(audit_period_beginning=str(audit_start),
44
audit_period_ending=str(audit_end))
46
notify_about_volume_usage(
47
context, volume_ref, 'exists', extra_usage_info=extra_usage_info)
50
def _usage_from_volume(context, volume_ref, **kw):
52
return str(s) if s else ''
55
tenant_id=volume_ref['project_id'],
56
user_id=volume_ref['user_id'],
57
volume_id=volume_ref['id'],
58
volume_type=volume_ref['volume_type'],
59
display_name=volume_ref['display_name'],
60
launched_at=null_safe_str(volume_ref['launched_at']),
61
created_at=null_safe_str(volume_ref['created_at']),
62
status=volume_ref['status'],
63
snapshot_id=volume_ref['snapshot_id'],
64
size=volume_ref['size'])
70
def notify_about_volume_usage(context, volume, event_suffix,
71
extra_usage_info=None, host=None):
75
if not extra_usage_info:
78
usage_info = _usage_from_volume(
79
context, volume, **extra_usage_info)
81
notifier_api.notify(context, 'volume.%s' % host,
82
'volume.%s' % event_suffix,
83
notifier_api.INFO, usage_info)