~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to nova/volume/utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2012 OpenStack, LLC.
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
 
 
17
"""Volume-related Utilities and helpers."""
 
18
 
 
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
 
23
 
 
24
 
 
25
FLAGS = flags.FLAGS
 
26
LOG = logging.getLogger(__name__)
 
27
 
 
28
 
 
29
def notify_usage_exists(context, volume_ref, current_period=False):
 
30
    """ Generates 'exists' notification for a volume for usage auditing
 
31
        purposes.
 
32
 
 
33
        Generates usage for last completed period, unless 'current_period'
 
34
        is True."""
 
35
    begin, end = utils.last_completed_audit_period()
 
36
    if current_period:
 
37
        audit_start = end
 
38
        audit_end = utils.utcnow()
 
39
    else:
 
40
        audit_start = begin
 
41
        audit_end = end
 
42
 
 
43
    extra_usage_info = dict(audit_period_beginning=str(audit_start),
 
44
                            audit_period_ending=str(audit_end))
 
45
 
 
46
    notify_about_volume_usage(
 
47
            context, volume_ref, 'exists', extra_usage_info=extra_usage_info)
 
48
 
 
49
 
 
50
def _usage_from_volume(context, volume_ref, **kw):
 
51
    def null_safe_str(s):
 
52
        return str(s) if s else ''
 
53
 
 
54
    usage_info = dict(
 
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'])
 
65
 
 
66
    usage_info.update(kw)
 
67
    return usage_info
 
68
 
 
69
 
 
70
def notify_about_volume_usage(context, volume, event_suffix,
 
71
                                extra_usage_info=None, host=None):
 
72
    if not host:
 
73
        host = FLAGS.host
 
74
 
 
75
    if not extra_usage_info:
 
76
        extra_usage_info = {}
 
77
 
 
78
    usage_info = _usage_from_volume(
 
79
            context, volume, **extra_usage_info)
 
80
 
 
81
    notifier_api.notify(context, 'volume.%s' % host,
 
82
                        'volume.%s' % event_suffix,
 
83
                        notifier_api.INFO, usage_info)