summaryrefslogtreecommitdiffstats
path: root/base/common/python/pki/info.py
blob: 65d4825686d87c0d53eb8f5e7b4e87c47131e98e (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
89
90
91
92
93
94
95
96
97
98
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
#  along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Red Hat, Inc.
# All rights reserved.
#
# Author:
#     Ade Lee <alee@redhat.com>
#
"""
Module containing the Python client classes for the InfoClient
"""
from six import iteritems

import pki


class Info(object):
    """
    This class encapsulates the parameters returned by the server's
    InfoService.
    """

    json_attribute_names = {
        'Version': 'version',
        'Banner': 'banner'
    }

    def __init__(self, version=None, banner=None):
        """ Constructor """
        self.version = version
        self.banner = banner

    @classmethod
    def from_json(cls, attr_list):
        """ Return Info from JSON dict """
        info = cls()
        for k, v in iteritems(attr_list):
            if k in Info.json_attribute_names:
                setattr(info, Info.json_attribute_names[k], v)
            else:
                setattr(info, k, v)
        return info


class Version(object):
    """
    This class encapsulates a version object as returned from
    a Dogtag server and decomposes it into major, minor, etc.
    """

    def __init__(self, version_string):
        for idx, val in enumerate(version_string.split('.')):
            if idx == 0:
                self.major = val
            if idx == 1:
                self.minor = val
            if idx == 2:
                self.patch = val


class InfoClient(object):
    """
    Class encapsulating and mirroring the functionality in the
    InfoResource Java interface class defining the REST API for
    server Info resources.
    """

    def __init__(self, connection):
        """ Constructor """
        self.connection = connection

    @pki.handle_exceptions()
    def get_info(self):
        """ Return an Info object form a PKI server """

        url = '/pki/rest/info'
        headers = {'Content-type': 'application/json',
                   'Accept': 'application/json'}
        r = self.connection.get(url, headers, use_root_uri=True)
        return Info.from_json(r.json())

    @pki.handle_exceptions()
    def get_version(self):
        """ return Version object from server """
        version_string = self.get_info().version
        return Version(version_string)