-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmodels.py
379 lines (313 loc) · 14.3 KB
/
models.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import uuid
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from marshmallow import fields
from marshmallow import Schema
from marshmallow import validate
from marshmallow import validates
from marshmallow import ValidationError
from sqlalchemy import Index
from sqlalchemy import orm
from sqlalchemy import text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import UUID
from app.exceptions import InputFormatException
from app.exceptions import InventoryException
from app.logging import get_logger
from app.validators import verify_uuid_format
logger = get_logger(__name__)
db = SQLAlchemy()
def _set_display_name_on_save(context):
"""
This method sets the display_name if it has not been set previously.
This logic happens during the saving of the host record so that
the id exists and can be used as the display_name if necessary.
"""
params = context.get_current_parameters()
if not params["display_name"]:
return params["canonical_facts"].get("fqdn") or params["id"]
class Host(db.Model):
__tablename__ = "hosts"
# These Index entries are essentially place holders so that the
# alembic autogenerate functionality does not try to remove the indexes
__table_args__ = (
Index("idxinsightsid", text("(canonical_facts ->> 'insights_id')")),
Index("idxgincanonicalfacts", "canonical_facts"),
Index("idxaccount", "account"),
Index("hosts_subscription_manager_id_index", text("(canonical_facts ->> 'subscription_manager_id')")),
)
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
account = db.Column(db.String(10))
display_name = db.Column(db.String(200), default=_set_display_name_on_save)
ansible_host = db.Column(db.String(255))
created_on = db.Column(db.DateTime, default=datetime.utcnow)
modified_on = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
facts = db.Column(JSONB)
tags = db.Column(JSONB)
canonical_facts = db.Column(JSONB)
system_profile_facts = db.Column(JSONB)
def __init__(
self,
canonical_facts,
display_name=None,
ansible_host=None,
account=None,
facts=None,
system_profile_facts=None,
):
if not canonical_facts:
raise InventoryException(
title="Invalid request", detail="At least one of the canonical fact fields must be present."
)
self.canonical_facts = canonical_facts
if display_name:
# Only set the display_name field if input the display_name has
# been set...this will make it so that the "default" logic will
# get called during the save to fill in an empty display_name
self.display_name = display_name
self._update_ansible_host(ansible_host)
self.account = account
self.facts = facts
self.system_profile_facts = system_profile_facts or {}
@classmethod
def from_json(cls, d):
canonical_facts = CanonicalFacts.from_json(d)
facts = Facts.from_json(d.get("facts"))
return cls(
canonical_facts,
d.get("display_name", None),
d.get("ansible_host"),
d.get("account"),
facts,
d.get("system_profile", {}),
)
def to_json(self):
json_dict = CanonicalFacts.to_json(self.canonical_facts)
json_dict["id"] = str(self.id)
json_dict["account"] = self.account
json_dict["display_name"] = self.display_name
json_dict["ansible_host"] = self.ansible_host
json_dict["facts"] = Facts.to_json(self.facts)
json_dict["created"] = self.created_on.isoformat() + "Z"
json_dict["updated"] = self.modified_on.isoformat() + "Z"
return json_dict
def to_system_profile_json(self):
json_dict = {"id": str(self.id), "system_profile": self.system_profile_facts or {}}
return json_dict
def save(self):
db.session.add(self)
def update(self, input_host):
self.update_canonical_facts(input_host.canonical_facts)
self.update_display_name(input_host.display_name)
self._update_ansible_host(input_host.ansible_host)
self.update_facts(input_host.facts)
def patch(self, patch_data):
logger.debug("patching host (id=%s) with data: %s", self.id, patch_data)
if not patch_data:
raise InventoryException(title="Bad Request", detail="Patch json document cannot be empty.")
self._update_ansible_host(patch_data.get("ansible_host"))
self.update_display_name(patch_data.get("display_name"))
def _update_ansible_host(self, ansible_host):
if ansible_host is not None:
# Allow a user to clear out the ansible host with an empty string
self.ansible_host = ansible_host
def update_display_name(self, input_display_name):
if input_display_name:
self.display_name = input_display_name
elif not self.display_name:
# This is the case where the display_name is not set on the
# existing host record and the input host does not have it set
if "fqdn" in self.canonical_facts:
self.display_name = self.canonical_facts["fqdn"]
else:
self.display_name = self.id
def update_canonical_facts(self, canonical_facts):
logger.debug(
"Updating host's (id=%s) canonical_facts (%s) with input canonical_facts=%s",
self.id,
self.canonical_facts,
canonical_facts,
)
self.canonical_facts.update(canonical_facts)
logger.debug("Host (id=%s) has updated canonical_facts (%s)", self.id, self.canonical_facts)
orm.attributes.flag_modified(self, "canonical_facts")
def update_facts(self, facts_dict):
if facts_dict:
if not self.facts:
self.facts = facts_dict
return
for input_namespace, input_facts in facts_dict.items():
self.replace_facts_in_namespace(input_namespace, input_facts)
def replace_facts_in_namespace(self, namespace, facts_dict):
self.facts[namespace] = facts_dict
orm.attributes.flag_modified(self, "facts")
def merge_facts_in_namespace(self, namespace, facts_dict):
if not facts_dict:
return
if self.facts[namespace]:
self.facts[namespace] = {**self.facts[namespace], **facts_dict}
else:
# The value currently stored in the namespace is None so replace it
self.facts[namespace] = facts_dict
orm.attributes.flag_modified(self, "facts")
def _update_system_profile(self, input_system_profile):
logger.debug("Updating host's (id=%s) system profile", self.id)
if not self.system_profile_facts:
self.system_profile_facts = input_system_profile
else:
# Update the fields that were passed in
self.system_profile_facts = {**self.system_profile_facts, **input_system_profile}
orm.attributes.flag_modified(self, "system_profile_facts")
def __repr__(self):
return (
f"<Host id='{self.id}' account='{self.account}' display_name='{self.display_name}' "
f"canonical_facts={self.canonical_facts}>"
)
class CanonicalFacts:
"""
There is a mismatch between how the canonical facts are sent as JSON
and how the canonical facts are stored in the DB. This class contains
the logic that is responsible for performing the conversion.
The canonical facts will be stored as a dict in a single json column
in the DB.
"""
field_names = (
"insights_id",
"rhel_machine_id",
"subscription_manager_id",
"satellite_id",
"bios_uuid",
"ip_addresses",
"fqdn",
"mac_addresses",
"external_id",
)
@staticmethod
def from_json(json_dict):
canonical_fact_list = {}
for cf in CanonicalFacts.field_names:
# Do not allow the incoming canonical facts to be None or ''
if cf in json_dict and json_dict[cf]:
canonical_fact_list[cf] = json_dict[cf]
return canonical_fact_list
@staticmethod
def to_json(internal_dict):
canonical_fact_dict = dict.fromkeys(CanonicalFacts.field_names, None)
for cf in CanonicalFacts.field_names:
if cf in internal_dict:
canonical_fact_dict[cf] = internal_dict[cf]
return canonical_fact_dict
class Facts:
"""
There is a mismatch between how the facts are sent as JSON
and how the facts are stored in the DB. This class contains
the logic that is responsible for performing the conversion.
The facts will be stored as a dict in a single json column
in the DB.
"""
@staticmethod
def from_json(fact_list):
if fact_list is None:
fact_list = []
fact_dict = {}
for fact in fact_list:
if "namespace" in fact and "facts" in fact:
if fact["namespace"] in fact_dict:
fact_dict[fact["namespace"]].update(fact["facts"])
else:
fact_dict[fact["namespace"]] = fact["facts"]
else:
# The facts from the request are formatted incorrectly
raise InputFormatException(
"Invalid format of Fact object. Fact must contain 'namespace' and 'facts' keys."
)
return fact_dict
@staticmethod
def to_json(fact_dict):
fact_list = [
{"namespace": namespace, "facts": facts if facts else {}} for namespace, facts in fact_dict.items()
]
return fact_list
class DiskDeviceSchema(Schema):
device = fields.Str(validate=validate.Length(max=2048))
label = fields.Str(validate=validate.Length(max=1024))
options = fields.Dict()
mount_point = fields.Str(validate=validate.Length(max=2048))
type = fields.Str(validate=validate.Length(max=256))
class YumRepoSchema(Schema):
name = fields.Str(validate=validate.Length(max=1024))
gpgcheck = fields.Bool()
enabled = fields.Bool()
base_url = fields.Str(validate=validate.Length(max=2048))
class InstalledProductSchema(Schema):
name = fields.Str(validate=validate.Length(max=512))
id = fields.Str(validate=validate.Length(max=64))
status = fields.Str(validate=validate.Length(max=256))
class NetworkInterfaceSchema(Schema):
ipv4_addresses = fields.List(fields.Str())
ipv6_addresses = fields.List(fields.Str())
state = fields.Str(validate=validate.Length(max=25))
mtu = fields.Int()
mac_address = fields.Str(validate=validate.Length(max=18))
name = fields.Str(validate=validate.Length(min=1, max=50))
type = fields.Str(validate=validate.Length(max=18))
class SystemProfileSchema(Schema):
number_of_cpus = fields.Int()
number_of_sockets = fields.Int()
cores_per_socket = fields.Int()
system_memory_bytes = fields.Int()
infrastructure_type = fields.Str(validate=validate.Length(max=100))
infrastructure_vendor = fields.Str(validate=validate.Length(max=100))
network_interfaces = fields.List(fields.Nested(NetworkInterfaceSchema()))
disk_devices = fields.List(fields.Nested(DiskDeviceSchema()))
bios_vendor = fields.Str(validate=validate.Length(max=100))
bios_version = fields.Str(validate=validate.Length(max=100))
bios_release_date = fields.Str(validate=validate.Length(max=50))
cpu_flags = fields.List(fields.Str(validate=validate.Length(max=30)))
os_release = fields.Str(validate=validate.Length(max=100))
os_kernel_version = fields.Str(validate=validate.Length(max=100))
arch = fields.Str(validate=validate.Length(max=50))
kernel_modules = fields.List(fields.Str(validate=validate.Length(max=255)))
last_boot_time = fields.Str(validate=validate.Length(max=50))
running_processes = fields.List(fields.Str(validate=validate.Length(max=1000)))
subscription_status = fields.Str(validate=validate.Length(max=100))
subscription_auto_attach = fields.Str(validate=validate.Length(max=100))
katello_agent_running = fields.Bool()
satellite_managed = fields.Bool()
cloud_provider = fields.Str(validate=validate.Length(max=100))
yum_repos = fields.List(fields.Nested(YumRepoSchema()))
installed_products = fields.List(fields.Nested(InstalledProductSchema()))
insights_client_version = fields.Str(validate=validate.Length(max=50))
insights_egg_version = fields.Str(validate=validate.Length(max=50))
installed_packages = fields.List(fields.Str(validate=validate.Length(max=512)))
installed_services = fields.List(fields.Str(validate=validate.Length(max=512)))
enabled_services = fields.List(fields.Str(validate=validate.Length(max=512)))
class FactsSchema(Schema):
namespace = fields.Str()
facts = fields.Dict()
class HostSchema(Schema):
display_name = fields.Str(validate=validate.Length(min=1, max=200))
ansible_host = fields.Str(validate=validate.Length(min=0, max=255))
account = fields.Str(required=True, validate=validate.Length(min=1, max=10))
insights_id = fields.Str(validate=verify_uuid_format)
rhel_machine_id = fields.Str(validate=verify_uuid_format)
subscription_manager_id = fields.Str(validate=verify_uuid_format)
satellite_id = fields.Str(validate=verify_uuid_format)
fqdn = fields.Str(validate=validate.Length(min=1, max=255))
bios_uuid = fields.Str(validate=verify_uuid_format)
ip_addresses = fields.List(fields.Str(validate=validate.Length(min=1, max=255)))
mac_addresses = fields.List(fields.Str(validate=validate.Length(min=1, max=255)))
external_id = fields.Str(validate=validate.Length(min=1, max=500))
facts = fields.List(fields.Nested(FactsSchema))
system_profile = fields.Nested(SystemProfileSchema)
@validates("ip_addresses")
def validate_ip_addresses(self, ip_address_list):
if len(ip_address_list) < 1:
raise ValidationError("Array must contain at least one item")
@validates("mac_addresses")
def validate_mac_addresses(self, mac_address_list):
if len(mac_address_list) < 1:
raise ValidationError("Array must contain at least one item")
class PatchHostSchema(Schema):
ansible_host = fields.Str(validate=validate.Length(min=0, max=255))
display_name = fields.Str(validate=validate.Length(min=1, max=200))