~ubuntu-branches/ubuntu/vivid/libamazon-sqs-simple-perl/vivid

« back to all changes in this revision

Viewing changes to lib/Amazon/SQS/Simple/Queue.pm

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2010-05-31 22:29:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100531222945-ja9nn22y6t7zi3kt
Tags: 1.06-0ubuntu1
* New upstream release (LP: #588041)
* Convert to source format 3.0 (quilt)
* Bump Standards-Version to 3.8.4 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
use warnings;
5
5
use Amazon::SQS::Simple::Message;
6
6
use Amazon::SQS::Simple::SendResponse;
 
7
use Carp qw( croak carp );
7
8
 
8
9
use base 'Amazon::SQS::Simple::Base';
9
10
use Amazon::SQS::Simple::Base; # for constants
17
18
 
18
19
sub Delete {
19
20
    my $self = shift;
20
 
    my $force = shift; # from API 2007-05-01
21
21
    my $params = { Action => 'DeleteQueue' };
22
 
    $params->{ForceDeletion} = 'true' if $force && $self->_api_version() eq +SQS_VERSION_2007_05_01;
23
22
    
24
23
    my $href = $self->_dispatch($params);    
25
24
}
32
31
    
33
32
    my $href = $self->_dispatch(\%params);    
34
33
 
35
 
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
36
 
        return new Amazon::SQS::Simple::SendResponse(
37
 
            $href
38
 
        );
39
 
    }
40
 
    else {
41
 
        # default to most recent version
42
 
        return new Amazon::SQS::Simple::SendResponse(
43
 
            $href->{SendMessageResult}
44
 
        );
45
 
    }
 
34
    # default to most recent version
 
35
    return new Amazon::SQS::Simple::SendResponse(
 
36
        $href->{SendMessageResult}
 
37
    );
46
38
}
47
39
 
48
40
sub ReceiveMessage {
54
46
 
55
47
    my @messages = ();
56
48
 
57
 
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
58
 
        if (defined $href->{Message}) {
59
 
            foreach (@{$href->{Message}}) {
60
 
                push @messages, new Amazon::SQS::Simple::Message(
61
 
                    $_,
62
 
                    $self->_api_version()
63
 
                );
64
 
            }
65
 
        }
66
 
    }
67
 
    else {
68
 
        # default to most recent version
69
 
        if (defined $href->{ReceiveMessageResult}{Message}) {
70
 
            foreach (@{$href->{ReceiveMessageResult}{Message}}) {
71
 
                push @messages, new Amazon::SQS::Simple::Message(
72
 
                    $_,
73
 
                    $self->_api_version()
74
 
                );
75
 
            }
 
49
    # default to most recent version
 
50
    if (defined $href->{ReceiveMessageResult}{Message}) {
 
51
        foreach (@{$href->{ReceiveMessageResult}{Message}}) {
 
52
            push @messages, new Amazon::SQS::Simple::Message(
 
53
                $_,
 
54
                $self->_api_version()
 
55
            );
76
56
        }
77
57
    }
78
58
    
89
69
    my ($self, $receipt_handle, %params) = @_;
90
70
    
91
71
    $params{Action} = 'DeleteMessage';
92
 
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
93
 
        $params{MessageId} = $receipt_handle;
94
 
    }
95
 
    else {
96
 
        # default to the current version
97
 
        $params{ReceiptHandle} = $receipt_handle;
98
 
    }
 
72
    $params{ReceiptHandle} = $receipt_handle;
99
73
    
100
74
    my $href = $self->_dispatch(\%params);
101
75
}
102
76
 
 
77
sub ChangeMessageVisibility {
 
78
    my ($self, $receipt_handle, $timeout, %params) = @_;
 
79
    
 
80
    if ($self->_api_version eq +SQS_VERSION_2008_01_01) {
 
81
        carp "ChangeMessageVisibility not supported in this API version";
 
82
    }
 
83
    else {
 
84
        if (!defined($timeout) || $timeout =~ /\D/ || $timeout < 0 || $timeout > 43200) {
 
85
            croak "timeout must be specified and in range 0..43200";
 
86
        }
 
87
 
 
88
        $params{Action}             = 'ChangeMessageVisibility';
 
89
        $params{ReceiptHandle}      = $receipt_handle;
 
90
        $params{VisibilityTimeout}  = $timeout;
 
91
 
 
92
        my $href = $self->_dispatch(\%params);
 
93
    }
 
94
}
 
95
 
 
96
our %valid_permission_actions = map { $_ => 1 } qw(* SendMessage ReceiveMessage DeleteMessage ChangeMessageVisibility GetQueueAttributes);
 
97
 
 
98
sub AddPermission {
 
99
    my ($self, $label, $account_actions, %params) = @_;
 
100
    
 
101
    if ($self->_api_version eq +SQS_VERSION_2008_01_01) {
 
102
        carp "AddPermission not supported in this API version";
 
103
    }
 
104
    else {
 
105
        $params{Action} = 'AddPermission';
 
106
        $params{Label}  = $label;
 
107
        my $i = 1;
 
108
        foreach my $account_id (keys %$account_actions) {
 
109
            $account_id =~ /^\d{12}$/ or croak "Account IDs passed to AddPermission should be 12 digit AWS account numbers, no hyphens";
 
110
            my $actions = $account_actions->{$account_id};
 
111
            my @actions;
 
112
            if (UNIVERSAL::isa($actions, 'ARRAY')) {
 
113
                @actions = @$actions;
 
114
            } else {
 
115
                @actions = ($actions);
 
116
            }
 
117
            foreach my $action (@actions) {
 
118
                exists $valid_permission_actions{$action} 
 
119
                    or croak "Action passed to AddPermission must be one of " 
 
120
                     . join(', ', sort keys %valid_permission_actions);
 
121
            
 
122
                $params{"AWSAccountId.$i"} = $account_id;
 
123
                $params{"ActionName.$i"}   = $action;
 
124
                $i++;
 
125
            }
 
126
        }
 
127
        my $href = $self->_dispatch(\%params);
 
128
    }
 
129
}
 
130
 
 
131
sub RemovePermission {
 
132
    my ($self, $label, %params) = @_;
 
133
    
 
134
    if ($self->_api_version eq +SQS_VERSION_2008_01_01) {
 
135
        carp "RemovePermission not supported in this API version";
 
136
    }
 
137
    else {
 
138
        $params{Action} = 'RemovePermission';
 
139
        $params{Label}  = $label;
 
140
        my $href = $self->_dispatch(\%params);
 
141
    }
 
142
}
 
143
 
103
144
sub GetAttributes {
104
145
    my ($self, %params) = @_;
105
146
    
106
 
    $params{Action}          = 'GetQueueAttributes';
 
147
    $params{Action} = 'GetQueueAttributes';
107
148
 
108
149
    my %result;
109
 
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
110
 
        $params{Attribute} ||= 'All';
111
 
    
112
 
        my $href = $self->_dispatch(\%params, [ 'AttributedValue' ]);
113
 
 
114
 
        if ($href->{'AttributedValue'}) {
115
 
            foreach my $attr (@{$href->{'AttributedValue'}}) {
116
 
                $result{$attr->{Attribute}} = $attr->{Value};
117
 
            }
118
 
        }
119
 
    }
120
 
    else {
121
 
        # default to the current version
122
 
        $params{AttributeName} ||= 'All';
123
 
    
124
 
        my $href = $self->_dispatch(\%params, [ 'Attribute' ]);
125
 
 
126
 
        if ($href->{GetQueueAttributesResult}) {
127
 
            foreach my $attr (@{$href->{GetQueueAttributesResult}{Attribute}}) {
128
 
                $result{$attr->{Name}} = $attr->{Value};
129
 
            }
130
 
        }
131
 
    }
132
 
    
 
150
    # default to the current version
 
151
    $params{AttributeName} ||= 'All';
 
152
 
 
153
    my $href = $self->_dispatch(\%params, [ 'Attribute' ]);
 
154
 
 
155
    if ($href->{GetQueueAttributesResult}) {
 
156
        foreach my $attr (@{$href->{GetQueueAttributesResult}{Attribute}}) {
 
157
            $result{$attr->{Name}} = $attr->{Value};
 
158
        }
 
159
    }
133
160
    return \%result;
134
161
}
135
162
 
137
164
    my ($self, $key, $value, %params) = @_;
138
165
    
139
166
    $params{Action}             = 'SetQueueAttributes';
140
 
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
141
 
        $params{Attribute} = $key;
142
 
        $params{Value}     = $value;
143
 
    }
144
 
    else {
145
 
        # default to the current version
146
 
        $params{'Attribute.Name'}   = $key;
147
 
        $params{'Attribute.Value'}  = $value;
148
 
    }
 
167
    $params{'Attribute.Name'}   = $key;
 
168
    $params{'Attribute.Value'}  = $value;
149
169
    
150
170
    my $href = $self->_dispatch(\%params);
151
171
}
231
251
 
232
252
Delete the message with the specified receipt handle from the queue
233
253
 
 
254
=item B<ChangeMessageVisibility($receipt_handle, $timeout, [%opts])>
 
255
 
 
256
NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
 
257
 
 
258
Changes the visibility of the message with the specified receipt handle to
 
259
C<$timeout> seconds. C<$timeout> must be in the range 0..43200.
 
260
 
 
261
=item B<AddPermission($label, $account_actions, [%opts])>
 
262
 
 
263
NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
 
264
 
 
265
Sets a permissions policy with the specified label. C<$account_actions>
 
266
is a reference to a hash mapping 12-digit AWS account numbers to the action(s)
 
267
you want to permit for those account IDs. The hash value for each key can 
 
268
be a string (e.g. "ReceiveMessage") or a reference to an array of strings 
 
269
(e.g. ["ReceiveMessage", "DeleteMessage"])
 
270
 
 
271
=item B<RemovePermission($label, [%opts])>
 
272
 
 
273
NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
 
274
 
 
275
Removes the permissions policy with the specified label.
 
276
 
234
277
=item B<GetAttributes([%opts])>
235
278
 
236
279
Get the attributes for the queue. Returns a reference to a hash