forked from whamcloud/integrated-manager-for-lustre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_resource.py
172 lines (133 loc) · 5.61 KB
/
api_resource.py
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
171
172
# Copyright (c) 2018 DDN. All rights reserved.
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.
class ApiResource(object):
def __init__(self, *args, **kwargs):
self.__attributes = kwargs
self.list_columns = ["id"]
def __getattr__(self, key):
return self.__attributes[key]
def __getitem__(self, key):
return self.__attributes[key]
@property
def all_attributes(self):
return self.__attributes
def as_header(self):
return self.list_columns
def as_row(self):
row = []
for key in self.list_columns:
try:
val = getattr(self, key)
# Don't just try: this because catching TypeErrors can
# mask problems inside the callable.
if callable(val):
row.append(val())
else:
row.append(val)
except KeyError:
row.append("")
return row
def __str__(self):
return " | ".join([str(f) for f in self.as_row()])
# http://stackoverflow.com/a/1094933/204920
def fmt_bytes(self, bytes):
import math
if bytes is None or math.isnan(float(bytes)):
return "NaN"
bytes = float(bytes)
for x in ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "YiB", "ZiB"]:
if bytes < 1024.0:
return "%3.1f%s" % (bytes, x)
bytes /= 1024.0
# fallthrough, bigger than ZiB?!?!
return "%d" % bytes
def fmt_num(self, num):
import math
if num is None or math.isnan(float(num)):
return "NaN"
num = float(num)
for x in ["", "K", "M", "G", "P", "E", "Y", "Z"]:
if num < 1000.0:
return "%3.1f%s" % (num, x)
num /= 1000.0
# fallthrough
return "%d" % num
def pretty_time(self, in_time):
from iml_common.lib.date_time import IMLDateTime, FixedOffset, LocalOffset
local_midnight = IMLDateTime.now().replace(hour=0, minute=0, second=0, microsecond=0)
in_time = in_time.replace(tzinfo=FixedOffset(0))
out_time = in_time.astimezone(LocalOffset)
if out_time < local_midnight:
return out_time.strftime("%Y/%m/%d %H:%M:%S")
else:
return out_time.strftime("%H:%M:%S")
class ServerProfile(ApiResource):
def __init__(self, *args, **kwargs):
super(ServerProfile, self).__init__(*args, **kwargs)
self.list_columns.extend(["name", "ui_name", "ui_description", "managed", "repolist"])
def repolist(self):
return ",".join(sorted(self.all_attributes["repolist"]))
class Host(ApiResource):
def __init__(self, *args, **kwargs):
super(Host, self).__init__(*args, **kwargs)
# FIXME: Figure out bad interaction between role as a resource
# field and as a filter.
# self.list_columns.extend(["fqdn", "role", "state", "nids", "last_contact"])
self.list_columns += "fqdn", "state", "nids"
def nids(self):
return ",".join(self.all_attributes["nids"])
class LnetConfiguration(ApiResource):
def __init__(self, *args, **kwargs):
super(LnetConfiguration, self).__init__(*args, **kwargs)
self.list_columns.extend(["host__fqdn"])
class Target(ApiResource):
def __init__(self, *args, **kwargs):
super(Target, self).__init__(*args, **kwargs)
self.list_columns.extend(["name", "state", "primary_path"])
def primary_path(self):
try:
primary_node = [vn for vn in self.volume["volume_nodes"] if vn["primary"]][0]
return "%s:%s" % (primary_node["host_label"], primary_node["path"])
except IndexError:
return "Unknown"
class Filesystem(ApiResource):
def __init__(self, *args, **kwargs):
super(Filesystem, self).__init__(*args, **kwargs)
self.list_columns.extend(["name", "state", "clients", "files", "space"])
def files(self):
# Freaking Nones
files_total = float("nan") if self.files_total is None else self.files_total
files_free = float("nan") if self.files_free is None else self.files_free
return "%s/%s" % (self.fmt_num(files_free), self.fmt_num(files_total))
def space(self):
bytes_total = float("nan") if self.bytes_total is None else self.bytes_total
bytes_free = float("nan") if self.bytes_free is None else self.bytes_free
return "%s/%s" % (self.fmt_bytes(bytes_free), self.fmt_bytes(bytes_total))
def clients(self):
try:
return "%d" % self.client_count
except TypeError:
return "0"
class Volume(ApiResource):
def __init__(self, *args, **kwargs):
super(Volume, self).__init__(*args, **kwargs)
self.list_columns.extend(["name", "size", "filesystem_type", "primary", "failover", "status"])
def name(self):
return " ".join(self.label.split())
def size(self):
return self.fmt_bytes(self.all_attributes["size"])
def primary(self):
for node in self.volume_nodes:
if node["primary"]:
return ":".join([node["host_label"], node["path"]])
def failover(self):
failover_nodes = []
for node in self.volume_nodes:
if not node["primary"]:
failover_nodes.append(":".join([node["host_label"], node["path"]]))
return ";".join(failover_nodes)
class VolumeNode(ApiResource):
def __init__(self, *args, **kwargs):
super(VolumeNode, self).__init__(*args, **kwargs)
self.list_columns.extend(["host_id", "volume_id", "host_label", "path"])