@@ -68,6 +68,7 @@ my $THIS_FILE = "Database.pm";
68
68
# insert_or_update_files
69
69
# insert_or_update_health
70
70
# insert_or_update_hosts
71
+ # insert_or_update_host_variable
71
72
# insert_or_update_ip_addresses
72
73
# insert_or_update_jobs
73
74
# insert_or_update_mail_servers
@@ -10297,6 +10298,191 @@ WHERE
10297
10298
}
10298
10299
10299
10300
10301
+ =head2 insert_or_update_host_variables
10302
+
10303
+ This updates (or inserts) a record in the 'host_variabless' table. The C<< host_variable_host_uuid >> UUID will be returned.
10304
+
10305
+ If there is an error, an empty string is returned.
10306
+
10307
+ Parameters;
10308
+
10309
+ =head3 host_variable_uuid (optional)
10310
+
10311
+ If this is specified, this will be the record updated. If this is not passed, a check will be made to see if there's an existing C<< host_variable_host_uuid >> and C<< host_variable_name >>. If matches are found, the record is updated. If not, a new entry is made.
10312
+
10313
+ =head3 host_variable_host_uuid (required, default Get->host_uuid)
10314
+
10315
+ This is the C<< host_uuid >> that the variable is being stored for.
10316
+
10317
+ =head3 host_variable_name (required)
10318
+
10319
+ This is the name of the variable.
10320
+
10321
+ =head3 host_variable_value (optional)
10322
+
10323
+ This is the value of the variable.
10324
+
10325
+ =cut
10326
+ sub insert_or_update_host_variables
10327
+ {
10328
+ my $self = shift;
10329
+ my $parameter = shift;
10330
+ my $anvil = $self->parent;
10331
+ my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
10332
+ $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_host_variables()" }});
10333
+
10334
+ my $uuid = defined $parameter->{uuid} ? $parameter->{uuid} : "";
10335
+ my $file = defined $parameter->{file} ? $parameter->{file} : "";
10336
+ my $line = defined $parameter->{line} ? $parameter->{line} : "";
10337
+ my $host_variable_uuid = defined $parameter->{host_variable_uuid} ? $parameter->{host_variable_uuid} : "";
10338
+ my $host_variable_host_uuid = defined $parameter->{host_variable_host_uuid} ? $parameter->{host_variable_host_uuid} : "";
10339
+ my $host_variable_name = defined $parameter->{host_variable_name} ? $parameter->{host_variable_name} : "";
10340
+ my $host_variable_value = defined $parameter->{host_variable_value} ? $parameter->{host_variable_value} : "";
10341
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
10342
+ uuid => $uuid,
10343
+ file => $file,
10344
+ line => $line,
10345
+ host_variable_uuid => $host_variable_uuid,
10346
+ host_variable_host_uuid => $host_variable_host_uuid,
10347
+ host_variable_name => $host_variable_name,
10348
+ host_variable_value => $host_variable_value,
10349
+ }});
10350
+
10351
+ # Do we have what we need?
10352
+ if (not $host_variable_host_uuid)
10353
+ {
10354
+ $host_variable_host_uuid = $anvil->Get->host_uuid();
10355
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_variable_host_uuid => $host_variable_host_uuid }});
10356
+ }
10357
+ if (not $host_variable_name)
10358
+ {
10359
+ # Throw an error and exit.
10360
+ $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->insert_or_update_host_variables()", parameter => "host_variable_name" }});
10361
+ return("");
10362
+ }
10363
+
10364
+ if (not $host_variable_uuid)
10365
+ {
10366
+ # We'll try to find the existing interface a couple ways. First we'll look up using
10367
+ # '_on_uuid' as that's as specific as it gets.
10368
+ my $query = "
10369
+ SELECT
10370
+ host_variable_uuid
10371
+ FROM
10372
+ host_variables
10373
+ WHERE
10374
+ host_variable_host_uuid = ".$anvil->Database->quote($host_variable_host_uuid)."
10375
+ AND
10376
+ host_variable_name = ".$anvil->Database->quote($host_variable_name)."
10377
+ ;";
10378
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
10379
+
10380
+ my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
10381
+ my $count = @{$results};
10382
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
10383
+ results => $results,
10384
+ count => $count,
10385
+ }});
10386
+ if ($count)
10387
+ {
10388
+ $host_variable_uuid = $results->[0]->[0];
10389
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_variable_uuid => $host_variable_uuid }});
10390
+ }
10391
+ }
10392
+
10393
+ # INSERT or UPDATE?
10394
+ if (not $host_variable_uuid)
10395
+ {
10396
+ # INSERT
10397
+ $host_variable_uuid = $anvil->Get->uuid();
10398
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_variable_uuid => $host_variable_uuid }});
10399
+
10400
+ my $query = "
10401
+ INSERT INTO
10402
+ host_variables
10403
+ (
10404
+ host_variable_uuid,
10405
+ host_variable_host_uuid,
10406
+ host_variable_name,
10407
+ host_variable_value,
10408
+ modified_date
10409
+ ) VALUES (
10410
+ ".$anvil->Database->quote($host_variable_uuid).",
10411
+ ".$anvil->Database->quote($host_variable_host_uuid).",
10412
+ ".$anvil->Database->quote($host_variable_name).",
10413
+ ".$anvil->Database->quote($host_variable_value).",
10414
+ ".$anvil->Database->quote($anvil->Database->refresh_timestamp)."
10415
+ );
10416
+ ";
10417
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
10418
+ $anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
10419
+ }
10420
+ else
10421
+ {
10422
+ # Query the rest of the values and see if anything changed.
10423
+ my $query = "
10424
+ SELECT
10425
+ host_variable_host_uuid,
10426
+ host_variable_name,
10427
+ host_variable_value
10428
+ FROM
10429
+ host_variables
10430
+ WHERE
10431
+ host_variable_uuid = ".$anvil->Database->quote($host_variable_uuid)."
10432
+ ;";
10433
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
10434
+
10435
+ my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
10436
+ my $count = @{$results};
10437
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
10438
+ results => $results,
10439
+ count => $count,
10440
+ }});
10441
+ if (not $count)
10442
+ {
10443
+ # I have a host_variable_uuid but no matching record. Probably an error.
10444
+ $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0216", variables => { uuid_name => "host_variable_uuid", uuid => $host_variable_uuid }});
10445
+ return("");
10446
+ }
10447
+ foreach my $row (@{$results})
10448
+ {
10449
+ my $old_host_variable_host_uuid = $row->[0];
10450
+ my $old_host_variable_name = $row->[1];
10451
+ my $old_host_variable_value = $row->[2];
10452
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
10453
+ old_host_variable_host_uuid => $old_host_variable_host_uuid,
10454
+ old_host_variable_name => $old_host_variable_name,
10455
+ old_host_variable_value => $old_host_variable_value,
10456
+ }});
10457
+
10458
+ # Anything change?
10459
+ if (($old_host_variable_host_uuid ne $host_variable_host_uuid) or
10460
+ ($old_host_variable_name ne $host_variable_name) or
10461
+ ($old_host_variable_value ne $host_variable_value))
10462
+ {
10463
+ # Something changed, save.
10464
+ my $query = "
10465
+ UPDATE
10466
+ host_variables
10467
+ SET
10468
+ host_variable_host_uuid = ".$anvil->Database->quote($host_variable_host_uuid).",
10469
+ host_variable_name = ".$anvil->Database->quote($host_variable_name).",
10470
+ host_variable_value = ".$anvil->Database->quote($host_variable_value).",
10471
+ modified_date = ".$anvil->Database->quote($anvil->Database->refresh_timestamp)."
10472
+ WHERE
10473
+ host_variable_uuid = ".$anvil->Database->quote($host_variable_uuid)."
10474
+ ";
10475
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }});
10476
+ $anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__});
10477
+ }
10478
+ }
10479
+ }
10480
+
10481
+ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_variable_host_uuid => $host_variable_host_uuid }});
10482
+ return($host_variable_host_uuid);
10483
+ }
10484
+
10485
+
10300
10486
=head2 insert_or_update_ip_addresses
10301
10487
10302
10488
This updates (or inserts) a record in the 'ip_addresses' table. The C<< ip_address_uuid >> referencing the database row will be returned.
0 commit comments