Browse Source

Подготовка для работы в качестве модуля Ansible

master
Sergey Kiselev 7 years ago
parent
commit
8a238f6b53
  1. 44
      yamd

44
yamd.pl → yamd

@ -11,11 +11,11 @@ our $api_url = "https://pddimp.yandex.ru/api2/admin";
our $query_options = LoadFile("./yamd_query_options.yml")
or die "Query parameters file not found\n";
our $task_file = shift or die "Usage: yamd.pl <file.yml>\n";
our $task = LoadFile($task_file)
our $task = %{LoadFile($task_file)}{yamd}
or die "Unable to open file '$task_file'\n";
foreach my $check ("domain", "token", "service", "query") {
if (!defined $task->{yamd}->{$check}) {
if (!defined $task->{$check}) {
die "'$check' is undefined in '$task_file'\n";
}
}
@ -25,18 +25,18 @@ my $request_type = "";
my $request_content_type = "";
{
my $service = $task->{yamd}->{service};
my $sub_service = $task->{yamd}->{sub_service};
my $query = $task->{yamd}->{query};
my $service = $task->{service};
my $sub_service = $task->{sub_service};
my $query = $task->{query};
$request_url = $api_url . "/" . $service;
$request_url .= (defined $sub_service) ? "/" . $sub_service : "";
$request_url .= "/" . $query;
if ( $service eq "import" && $query eq "start_import_file" ) {
$request_url .= "?domain=" . $task->{yamd}->{domain};
foreach my $k (keys %{$task->{yamd}->{options}}) {
$request_url .= "&" . $k . "=" . $task->{yamd}->{options}->{$k};
$request_url .= "?domain=" . $task->{domain};
foreach my $k (keys %{$task->{options}}) {
$request_url .= "&" . $k . "=" . $task->{options}->{$k};
}
}
@ -56,15 +56,15 @@ my $request_content_type = "";
my $ua = new LWP::UserAgent;
my $request = new HTTP::Request($request_type => $request_url);
if ( $task->{yamd}->{service} eq "domain" &&
$task->{yamd}->{sub_service} eq "logo" &&
$task->{yamd}->{query} eq "set" ) {
if ( $task->{service} eq "domain" &&
$task->{sub_service} eq "logo" &&
$task->{query} eq "set" ) {
my $boundary = 'X';
my @rand = ('a'..'z', 'A'..'Z');
for (0..14) { $boundary .= $rand[rand(@rand)]; }
$request->header(
'PddToken' => $task->{yamd}->{token},
'PddToken' => $task->{token},
'Content-type' => $request_content_type . "; boundary=" . $boundary
);
@ -72,10 +72,10 @@ if ( $task->{yamd}->{service} eq "domain" &&
'Content-Disposition' => 'form-data; name="domain"',
'Content-Type' => 'text/plain; charset=utf-8'
]);
$field->add_content_utf8($task->{yamd}->{domain});
$field->add_content_utf8($task->{domain});
$request->add_part($field);
open(my $fh, '<', $task->{yamd}->{options}->{file});
open(my $fh, '<', $task->{options}->{file});
my $file_content = new HTTP::Message([
'Content-Disposition' => 'form-data; name="file"; filename="logo"',
'Content-Type' => 'application/octet-stream'
@ -84,18 +84,18 @@ if ( $task->{yamd}->{service} eq "domain" &&
$request->add_part($file_content);
close $fh;
} elsif ( $task->{yamd}->{service} eq "import" &&
$task->{yamd}->{query} eq "start_import_file" ) {
} elsif ( $task->{service} eq "import" &&
$task->{query} eq "start_import_file" ) {
my $boundary = 'X';
my @rand = ('a'..'z', 'A'..'Z');
for (0..14) { $boundary .= $rand[rand(@rand)]; }
$request->header(
'PddToken' => $task->{yamd}->{token},
'PddToken' => $task->{token},
'Content-type' => $request_content_type . "; boundary=" . $boundary
);
open(my $fh, '<', $task->{yamd}->{options}->{file});
open(my $fh, '<', $task->{options}->{file});
my $file_content = new HTTP::Message([
'Content-Disposition' => 'form-data; name="import_list_file"; filename="import_list_file"',
'Content-Type' => 'text/plain',
@ -107,13 +107,13 @@ if ( $task->{yamd}->{service} eq "domain" &&
close $fh;
} else {
$request->header(
'PddToken' => $task->{yamd}->{token},
'PddToken' => $task->{token},
'Content-type' => $request_content_type
);
my $request_content = "domain=" . $task->{yamd}->{domain};
foreach my $k (keys %{$task->{yamd}->{options}}) {
$request_content .= "&" . $k . "=" . $task->{yamd}->{options}->{$k};
my $request_content = "domain=" . $task->{domain};
foreach my $k (keys %{$task->{options}}) {
$request_content .= "&" . $k . "=" . $task->{options}->{$k};
}
$request->content($request_content);