-
Notifications
You must be signed in to change notification settings - Fork 77
/
models.py
272 lines (221 loc) · 10.9 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
import uuid
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from marshmallow import Schema, fields, validate, validates, ValidationError
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy import orm, Index, text
from app.exceptions import InventoryException, InputFormatException
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"),)
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=display_name,
ansible_host=None,
account=account,
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 {}
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):
tmpl = "<Host id='%s' account='%s' display_name='%s' canonical_facts=%s>"
return tmpl % (
self.id,
self.account,
self.display_name,
self.canonical_facts,
)
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))