The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Google::CloudTasks - Perl client library for the Google CloudTasks API (unofficial).

SYNOPSIS

use Google::CloudTasks;

my $client = Google::CloudTasks->client(
    version => 'v2',
    credentials_path => '/path/to/credentials.json',
);

#  Create task
my $project_id = 'myproject';
my $location_id = 'asia-northeast1';
my $queue_id = 'myqueue';
my $parent = "projects/$project_id/locations/$location_id/queues/$queue_id";

my $task = {
    name => "$parent/tasks/mytask-01234567",
    appEngineHttpRequest => {
        relativeUri => '/do_task',
    },
}
my $ret = $client->create_task($parent, $task);

DESCRIPTION

Google::CloudTasks https://cloud.google.com/tasks/docs/reference/rest/

This is a Perl client library for the Google CloudTasks API.

AUTHENTICATION

A service account with appropriate roles is required. You need to download JSON file and specify credentials_path. See also: https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account

METHODS

All methods handle raw hashref (or arrayref of hashref), rather than objects.

Create a client

my $client = Google::CloudTasks->client(
    version => 'v2',
    credentials_path => '/path/to/credentials.json',
);

version is an API version. (Currently only v2 is available) credentials_path is a path to a service account JSON file.

Location

Refer the detailed representation of location at https://cloud.google.com/tasks/docs/reference/rest/Shared.Types/ListLocationsResponse#Location

get_location

Gets information about a location.

my $location = $client->get_location("projects/$PROJECT_ID/locations/$LOCATION_ID");

list_locations

Lists information about all locations under project.

my $ret = $client->list_locations("projects/$PROJECT_ID");
my $locations = $ret->{locations};

Queue

Refer the detailed representation of queue at https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues#Queue

create_queue

Creates a queue.

my $queue = {
    name => 'queue-name',
};
my $created = $client->create_queue("projects/$PROJECT_ID/locations/$LOCATION_ID", $queue);

delete_queue

Deletes a queue.

$client->delete_queue("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID")

get_queue

Gets information of a queue.

my $queue = $client->get_queue("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID");

list_queues

Lists information of all queues.

my $ret = $client->list_queues("projects/$PROJECT_ID/locations/$LOCATION_ID");
my $queues = $ret->{queues};

patch_queue

Updates a queue.

my $queue = {
    retryConfig => {
        maxAttempts => 5,
    },
};
my $update_mask = { updateMask => 'retryConfig.maxAttempts' };
my $updated = $client->patch_queue(
    "projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID",
    $queue,
    $update_mask,   # optional
);

pause_queue

Pauses a queue.

my $queue = $client->pause_queue("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID");

resume_queue

Resumes a queue.

my $queue = $client->resume_queue("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID");

get_iam_policy_queue

Gets the access control policy for a queue.

my $policy = $client->get_iam_policy_queue("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID");

set_iam_policy_queue

Sets the access control policy for a queue.

my $policy = {
    bindings => [
        +{
            role => 'roles/viewer',
            members => [
                'serviceAccount:service-account-name@myproject.gserviceaccount.com',
            ],
        }
    ],
    etag => $etag,  # got via get_iam_policy_queue
};
$policy = $client->set_iam_policy_queue(
    "projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID",
    $policy,
);

Task

Refer the detailed representation of task at https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks#Task

create_task

Creates a task. Note that a request body in appEngineHttpRequest should be base64-encoded.

use MIME::Base64;

my $body = encode_base64('{"name": "TaskTest"}');
chomp($body);

my $task = {
    name => "projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID",
    appEngineHttpRequest => {
        relativeUri => '/path',
        headers => [
            'Content-Type' => 'application/json',
        ],
        body => $body,
    },
};
my $created = $client->create_task(
    "projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID",
    $task
);

delete_task

Deletes a task.

$client->delete_task("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID/tasks/$TASK_ID");

get_task

Gets information of a task.

my $task = $client->get_task("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID/tasks/$TASK_ID");

list_tasks

Lists information of all tasks.

my $ret = $client->list_tasks("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID");
my $tasks = $ret->{tasks};

run_task

Runs a task.

my $ret = $client->run_task("projects/$PROJECT_ID/locations/$LOCATION_ID/queues/$QUEUE_ID/tasks/$TASK_ID");

TODO

The following methods has implemented, but not tested nor documented yet.

Queue.testIamPermissions

LICENSE

Copyright (C) egawata.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

egawata (egawa dot takashi at gmail.com)