summaryrefslogtreecommitdiffstats
path: root/src/software/openlmi/software/core/MethodResult.py
blob: 715a554ded1b4008fdd38f566a28375bbb8a0bcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- encoding: utf-8 -*-
# Software Management Providers
#
# Copyright (C) 2012-2013 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Just a common functionality related to class LMI_SoftwareMethodResult.
"""

import pywbem

from openlmi.common import cmpi_logging
from openlmi.software.core import Job
from openlmi.software.core import InstMethodCall
from openlmi.software.yumdb import jobs, errors, YumDB

@cmpi_logging.trace_function
def object_path2jobid(op):
    """
    @param op must contain precise InstanceID of job
    """
    if not isinstance(op, pywbem.CIMInstanceName):
        raise TypeError("op must be a CIMInstanceName")
    if not op["InstanceID"].lower().startswith('lmi:lmi_softwaremethodresult:'):
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Missing 'LMI:LMI_SoftwareMethodResult:' prefix in InstanceID.")
    instid = op['InstanceID'][len('LMI:LMI_SoftwareMethodResult:'):]
    try:
        instid = int(instid)
    except ValueError:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND,
                "Failed to parse InstanceID.")
    return instid

@cmpi_logging.trace_function
def object_path2job(op):
    """
    @param op must contain precise InstanceID of job
    """
    instid = object_path2jobid(op)
    try:
        return YumDB.get_instance().get_job(instid)
    except errors.JobNotFound:
        raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND, "No such job.")

@cmpi_logging.trace_function
def job2model(job, keys_only=True, model=None):
    """
    This accepts job's id and produces corresponding result model.
    """
    if not isinstance(job, jobs.YumJob):
        raise TypeError("job must be an instance of YumJob")
    if model is None:
        model = pywbem.CIMInstanceName("LMI_SoftwareMethodResult",
                namespace="root/cimv2")
        if not keys_only:
            model = pywbem.CIMInstance("LMI_SoftwareMethodResult", path=model)
    model['InstanceID'] = "LMI:LMI_SoftwareMethodResult:"+str(job.jobid)
    if not keys_only:
        model.path['InstanceID'] = model['InstanceID']  #pylint: disable=E1103
        method_name = Job.JOB_METHOD_NAMES[job.metadata['method']]
        model['Caption'] = 'Result of method %s' % method_name
        model['Description'] = (
                'Result of asynchronous job number %d created upon invocation'
                " of %s's %s method." % (job.jobid,
                "LMI_SoftwareInstallationService", method_name))
        model['ElementName'] = 'MethodResult-%d' % job.jobid
        model['PostCallIndication'] = pywbem.CIMProperty("PostCallIndication",
                type="instance",
                value=InstMethodCall.job2model(job, pre=False))
        model['PreCallIndication'] = pywbem.CIMProperty("PreCallIndication",
                type="instance",
                value=InstMethodCall.job2model(job))
    return model