summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster-gd2/glustolibs/gluster/devices.py
blob: 35efd49fb380265282cf8f673f391e2f29715b19 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#  Copyright (C) 2018-2019  Red Hat, Inc. <http://www.redhat.com>
#
#  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
#  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, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
    Description: Library for gluster device operation.
"""


import httplib
from glusto.core import Glusto as g
from glustolibs.gluster.exceptions import GlusterApiInvalidInputs
from glustolibs.gluster.lib_utils import validate_peer_id
from glustolibs.gluster.rest import RestClient


def rest_call(ops, mnode, method, path, code, data):
    """
    To handle the get methods of devices
    Args:
        ops (str): operation performing on devices
        mnode (str): Node on which commands as to run
        method (str): rest methods, i.e POST, GET, DELETE
        path(str): path of the operation
                   e.g: /v1/devices
        code(str): status code of the operation
        data(dict): json input
    Returns:
        tuple: Tuple containing two elements (ret, out|err)
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'dict' and is the
        error message and code of operation on failure
    """
    output = ""
    ret, out, err = RestClient(mnode).handle_request(method, path, code, data)
    if ret:
        g.log.error("Failed to perform device %s operation", ops)
        output = err
    else:
        output = out
    return ret, output


def device_add(mnode, peerid, device):
    """
    Gluster device add.
    Args:
        mnode (string) : Node on which command as to run
        peerid (string) : Peer UUID
        device (string) : device name
    Returns:
    tuple: Tuple containing two elements (ret, out|err).
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'str' and is the
        error message and code of operation on failure
    """
    validate_peer_id(peerid)
    if not device:
        raise GlusterApiInvalidInputs("Invalid device specified %s" % device)
    data = {
        "Device": device
        }
    return rest_call("add", mnode, "POST",
                     "/v1/devices/%s" % peerid,
                     httplib.CREATED, data)


def device_info(mnode, peerid, device):
    """
    Gluster get devices in peer.
    Args:
        mnode (string) : Node on which command as to run
        peerid (string): peerid returned from peer_add
        devices (dict): device which info needed
    Returns:
    tuple: Tuple containing two elements (ret, out|err).
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'str' and is the
        error message and code of operation on failure
    """
    validate_peer_id(peerid)
    if not device:
        raise GlusterApiInvalidInputs("Invalid device specified %s" % device)
    device = {"device": device}
    return rest_call("info", mnode, "GET",
                     "/v1/devices/%s/%s" % (peerid, device),
                     httplib.OK, None)


def devices_in_peer(mnode, peerid):
    """
    Gluster get devices in peer.
    Args:
        mnode (string) : Node on which command as to run
        peerid (string): peerid returned from peer_add
    Returns:
    tuple: Tuple containing two elements (ret, out|err).
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'str' and is the
        error message and code of operation on failure
    """
    validate_peer_id(peerid)
    return rest_call("list", mnode, "GET",
                     "/v1/devices/%s" % peerid,
                     httplib.OK, None)


def devices(mnode):
    """
    Gluster list all devices.
    Args:
        mnode (string) : Node on which command as to run
    Returns:
    tuple: Tuple containing three elements (ret, out|err).
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'str' and is the
        error message and code of operation on failure.
    """
    return rest_call("list", mnode, "GET",
                     "/v1/devices", httplib.OK, None)


def device_edit(mnode, peerid, device, state):
    """
    Gluster edit the device
    Args:
        mnode (string) : Node on which command as to run
        device (string) : device name
        state (string) : state of the device.
                         Either enabled or disabled
    Returns:
    tuple: Tuple containing two elements (ret, out|err).
        The first element 'ret' is of type 'int' and is the return value
        The second element 'out' is of type 'str' and is the output of
        the operation on success
        The third element 'err' is of type 'str' and is the
        error message and code of operation on failure
    """
    validate_peer_id(peerid)
    if not device:
        raise GlusterApiInvalidInputs("Invalid device specified %s" % device)
    device = {"device": device}
    data = {
        "state": state
        }
    return rest_call("edit", mnode, "POST",
                     "/v1/devices/%s/%s" % (peerid, device),
                     httplib.CREATED, data)