Skip to content

Commit

Permalink
openqa-load-templates: Slightly simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
okurz committed Oct 11, 2024
1 parent 2ebe306 commit 23ecf8d
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions script/openqa-load-templates
Original file line number Diff line number Diff line change
Expand Up @@ -104,53 +104,53 @@ sub print_error ($res) {
if (my $err = $res->error) {
if (my $json = $res->json) {
if (my $json_err = $json->{error}) {
die "$err->{message}: " . join("\n", @{$json_err})
if ref $json_err eq 'ARRAY';
die "$err->{message}: $json_err";
die "$err->{message}: " . ref $json_err eq 'ARRAY' ? join("\n", @{$json_err}) : $json_err;
}
}
die "ERROR: $err->{code} - $err->{message}" if $err->{code};
}
die "unknown error code - host $url->{host} unreachable?";
}

sub post_entry ($table, $entry) {
my %param;

if ($table eq 'JobGroups') {
# Create the group first
my $job_groups_url = $url->clone->path($options{apibase} . '/job_groups');
my $res = $client->post($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error $res unless $res->is_success;
sub handle_job_groups ($entry) {
# Create the group first
my $job_groups_url = $url->clone->path($options{apibase} . '/job_groups');
my $res = $client->post($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error $res unless $res->is_success;

# Post the job template YAML
my $job_templates_url = $url->clone->path($options{apibase} . '/job_templates_scheduling');
$res
= $client->post($job_templates_url,
form => {name => $entry->{group_name}, template => $entry->{template}, schema => 'JobTemplates-01.yaml'})
->res;
print_error $res unless $res->is_success;
return 1;
}
# Post the job template YAML
my $job_templates_url = $url->clone->path($options{apibase} . '/job_templates_scheduling');
$res
= $client->post($job_templates_url,
form => {name => $entry->{group_name}, template => $entry->{template}, schema => 'JobTemplates-01.yaml'})
->res;
print_error $res unless $res->is_success;
return 1;
}

if ($table eq 'JobTemplates') {
unless (defined($entry->{prio})) {
# we have to migrate the prio from the TestSuite to the JobTemplate
for my $ts (@{$info->{TestSuites}}) {
if ($ts->{name} eq $entry->{test_suite}{name}) {
$entry->{prio} = $ts->{prio};
}
}
}
unless (defined($entry->{group_name})) {
# we have to create a group_name from the Product
my $gn = $entry->{product}{distri};
if ($entry->{product}{version} ne '*') {
$gn .= "-" . $entry->{product}{version};
}
$entry->{group_name} = $gn;
sub handle_job_templates ($entry) {
unless (defined($entry->{prio})) {
# we have to migrate the prio from the TestSuite to the JobTemplate
for my $ts (@{$info->{TestSuites}}) {
$entry->{prio} = $ts->{prio} if $ts->{name} eq $entry->{test_suite}{name};
}
}
unless (defined($entry->{group_name})) {
# we have to create a group_name from the Product
my $gn = $entry->{product}{distri};
$gn .= "-" . $entry->{product}{version} if $entry->{product}{version} ne '*';
$entry->{group_name} = $gn;
}
}

sub handle_other_settings ($entry, $key) {
}

sub post_entry ($table, $entry) {
my %param;

return handle_job_groups($entry) if $table eq 'JobGroups';
handle_job_templates($entry) if $table eq 'JobTemplates';

for my $key (keys %{$entry}) {
if ($key eq 'machine' && defined $entry->{machine}{name}) {
Expand All @@ -167,9 +167,7 @@ sub post_entry ($table, $entry) {
}
elsif ($key eq 'settings' && ref($entry->{settings}) eq "ARRAY") {
$param{settings} = {};
for my $var (@{$entry->{settings}}) {
$param{settings}{$var->{key}} = $var->{value};
}
$param{settings}{$_->{key}} = $_->{value} for @{$entry->{settings}};
}
else {
$param{$key} = $entry->{$key};
Expand Down

0 comments on commit 23ecf8d

Please sign in to comment.