SFDC/Metadata.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 118 CC 10 SCAR 24.1
CC
10
Cov
0%
_checkDeployment
30.0
_checkRetrieval
20.0
retrieveMetadata
6.0
CRAP 11.1
1 package WWW::SFDC::Metadata;
2 # ABSTRACT: Interface to the Salesforce.com Metadata API
3  
4 use 5.12.0;
5 use strict;
6 use warnings;
7  
8 use Data::Dumper;
9 use Log::Log4perl ':easy';
10 use SOAP::Lite;
11 use WWW::SFDC::SessionManager;
12  
13 use Moo;
14 with "MooX::Singleton", "WWW::SFDC::Role::Session";
15  
16  
17 has 'apiVersion', is => 'ro', default => 33;
18  
19 has 'uri',
20 is => 'ro',
21 default => "http://soap.sforce.com/2006/04/metadata";
22  
23 sub _extractURL {
24 return $_[1]->{metadataServerUrl};
25 }
26  
27  
28 sub listMetadata {
29  
30 my $self = shift; #remaining elements of @_ are queries;
31  
32 INFO "Listing Metadata...\t";
33  
34 my @result;
35 my @queryData = map {SOAP::Data->new(name => "queries", value => $_)} @_;
36  
37 # listMetadata can only handle 3 requests at a time, so we chunk them.
38 while (my @items = splice @queryData, 0, 3) {
39 push @result, $$_{fileName} for $self->_call('listMetadata', @items);
40 }
41  
42 return @result;
43 }
44  
45  
46 # Sets up an asynchronous metadata retrieval request and
47 # returns just the id, for checking later. Accepts a manifest.
48  
49 sub _startRetrieval {
50 INFO "Starting retrieval";
51  
52 my ($self, $manifest) = @_;
53  
54 # These maps basically preserve the structure passed in,
55 # translating it to salesforce's special package.xml structure.
56 my @queryData = map {
57 SOAP::Data->name (
58 types => \SOAP::Data->value(
59 map {SOAP::Data->name(members => $_) } @{ $$manifest{$_} },
60 SOAP::Data->name(name => $_ )
61 )
62 )
63 } keys %$manifest;
64  
65  
66 return ($self->_call(
67 'retrieve',
68 SOAP::Data->name(
69 retrieveRequest => {
70 # a lower value than 31 means no status is retrieved, causing an error.
71 apiVersion => $self->apiVersion(),
72 unpackaged => \SOAP::Data->value(@queryData)
73 })
74 ) )[0]->{id};
75  
76 }
77  
78 # Uses the id to request a status update from SFDC, and returns
79 # undef unless there's something to give back, in which case it
80 # returns the base64 encoded zip file from the response.
81  
82 sub _checkRetrieval {
83 my ($self, $id) = @_;
84 LOGDIE "No ID was passed in!" unless $id;
85  
86 my ($result) = $self->_call(
87 'checkRetrieveStatus',
88 SOAP::Data->name("asyncProcessId" => $id)
89 );
90  
91 INFO "Status:" . $$result{status};
92  
93 return $result->{zipFile} if $$result{status} eq "Succeeded";
94 return if $$result{status} =~ /Pending|InProgress/;
95 LOGDIE "Check Retrieve had an unexpected result: ".$$result{message};
96 }
97  
98  
99 sub retrieveMetadata {
100  
101 my ($self, $manifest) = @_;
102 LOGDIE "This method must be called with a manifest" unless $manifest;
103  
104 my $requestId = $self->_startRetrieval($manifest);
105  
106 my $result;
107  
108 do { $self->_sleep() } until $result = $self->_checkRetrieval($requestId);
109  
110 return $result;
111 }
112  
113  
114 #Check up on an async deployment request. Returns 1 when complete.
115 sub _checkDeployment {
116 my ($self, $id) = @_;
117 LOGDIE "No ID was passed in" unless $id;
118  
119 my ($result) = $self->_call(
120 'checkDeployStatus',
121 SOAP::Data->name("id" => $id),
122 SOAP::Data->name("includeDetails" => "true")
123 );
124  
125 INFO "Deployment status:\t".$$result{status};
126 return 1 if $$result{status} eq "Succeeded";
127 return if $$result{status} =~ /Queued|Pending|InProgress/;
128 # useful deployment error here please
129 LOGDIE "DEPLOYMENT FAILED: ".Dumper $result->{details}->{componentFailures}
130 if $result->{status} eq "Failed";
131 LOGDIE "Check Deploy had an unexpected result: ".Dumper $result;
132 }
133  
134 sub deployMetadata {
135 my ($self, $zip, $deployOptions) = @_;
136  
137 my ($result) = $self->_call(
138 'deploy',
139 SOAP::Data->name( zipfile => $zip),
140 ($deployOptions ? SOAP::Data->name(DeployOptions=>$deployOptions) : ())
141 );
142  
143 INFO "Deployment status:\t".$$result{state};
144  
145 #do..until guarantees that sleep() executes at least once.
146 do {$self->_sleep()} until $self->_checkDeployment($$result{id});
147  
148 return $$result{id};
149  
150 }
151  
152  
153 sub deployRecentValidation {
154 my ($self, $id) = @_;
155  
156 chomp $id;
157  
158 return $self->_call(
159 'deployRecentValidation',
160 SOAP::Data->name(validationID => $id)
161 );
162 }
163  
164 1;
165  
166 __END__
167  
168 =pod
169  
170 =head1 NAME
171  
172 WWW::SFDC::Metadata - Interface to the Salesforce.com Metadata API
173  
174 =head1 VERSION
175  
176 version 0.22
177  
178 =head1 SYNOPSIS
179  
180 my $client = Sophos::sfdc->instance(creds => {
181 username => 'foo',
182 password => 'bar',
183 url => 'https://login.salesforce.com'
184 });
185  
186 my $manifest = $client->listMetadata(
187 {type => "CustomObject"},
188 {type => "Report", folder => "FooReports"}
189 );
190  
191 my $base64zipstring = $client->retrieveMetadata(
192 $manifest
193 );
194  
195 $client->deployMetadata(
196 $base64zipstring,
197 {checkOnly => 'true'}
198 );
199  
200 For more in-depth examples, see t/WWW/SFDC/Metadata.t
201  
202 =head1 METHODS
203  
204 =head2 listMetadata @queries
205  
206 Accepts a list of types and folders, such as
207  
208 {type => "CustomObject"},
209 {type => "Report", folder => "FooReports"}
210  
211 and generates a list of file names suitable for turning into a WWW::SFDC::Manifest.
212  
213 =head2 retrieveMetadata $manifest
214  
215 Sets up a retrieval from then checks it until done. Returns the
216 same data as checkRetrieval. Requires a manifest of the form:
217  
218 my $manifest = {
219 "ApexClass" => ["MyApexClass"],
220 "CustomObject" => ["*", "Account", "User", 'Opportunity"],
221 "Profile" => ["*"]
222 };
223  
224 =head2 deployMetadata $zipString, \%deployOptions
225  
226 Takes a base64 zip file and deploys it. Deploy options will be
227 passed verbatim into the request; see the metadata developer
228 guide for a description.
229  
230 =head2 deployRecentValidation $id
231  
232 Calls deployRecentValidation with your successfully-validated deployment.
233  
234 =head1 BUGS
235  
236 Please report any bugs or feature requests at L<https://github.com/alexander-brett/WWW-SFDC/issues>.
237  
238 =head1 SUPPORT
239  
240 You can find documentation for this module with the perldoc command.
241  
242 perldoc WWW::SFDC::Metadata
243  
244 You can also look for information at L<https://github.com/alexander-brett/WWW-SFDC>
245  
246 =head1 AUTHOR
247  
248 Alexander Brett <alexander.brett@sophos.com>
249  
250 =head1 COPYRIGHT AND LICENSE
251  
252 This software is Copyright (c) 2015 by Sophos Limited.
253  
254 This is free software, licensed under:
255  
256 The MIT (X11) License
257  
258 The full text of the license can be found in the
259 F<LICENSE> file included with this distribution.
260  
261 =cut