-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRole.pm
359 lines (259 loc) · 10.4 KB
/
Role.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
=begin comment
Kubernetes
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
The version of the OpenAPI document: v1.13.7
Generated by: https://openapi-generator.tech
=end comment
=cut
#
# NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
# Do not edit the class manually.
# Ref: https://openapi-generator.tech
#
package Kubernetes::Role;
use utf8;
use Moose::Role;
use namespace::autoclean;
use Class::Inspector;
use Log::Any qw($log);
use Kubernetes::ApiFactory;
has base_url => (
is => 'ro',
required => 0,
isa => 'Str',
documentation => 'Root of the server that requests are sent to',
);
has api_factory => (
is => 'ro',
isa => 'Kubernetes::ApiFactory',
builder => '_build_af',
lazy => 1,
documentation => 'Builds an instance of the endpoint API class',
);
has tokens => (
is => 'ro',
isa => 'HashRef',
required => 0,
default => sub { {} },
documentation =>
'The auth tokens required by the application - basic, OAuth and/or API key(s)',
);
has _cfg => (
is => 'ro',
isa => 'Kubernetes::Configuration',
default => sub { Kubernetes::Configuration->new() },
);
has version_info => (
is => 'ro',
isa => 'HashRef',
default => sub {
{
app_name => 'Kubernetes',
app_version => 'v1.13.7',
generator_class =>
'org.openapitools.codegen.languages.PerlClientCodegen',
}
},
documentation =>
'Information about the application version and the codegen codebase version'
);
sub BUILD {
my $self = shift;
$self->_cfg->accept_tokens( $self->tokens ) if keys %{ $self->tokens };
# ignore these symbols imported into API namespaces
my %outsiders = map { $_ => 1 } qw( croak );
my %delegates;
# collect the methods callable on each API
foreach my $api_name ( $self->api_factory->apis_available ) {
my $api_class = $self->api_factory->classname_for($api_name);
my $methods = Class::Inspector->methods( $api_class, 'expanded' )
; # not Moose, so use CI instead
my @local_methods =
grep { !/^_/ }
grep { !$outsiders{$_} }
map { $_->[2] } grep { $_->[1] eq $api_class } @$methods;
push(
@{ $delegates{$_} },
{ api_name => $api_name, api_class => $api_class }
) for @local_methods;
}
# remove clashes
foreach my $method ( keys %delegates ) {
if ( @{ $delegates{$method} } > 1 ) {
my ($apis) = delete $delegates{$method};
}
}
# build the flattened API
foreach my $api_name ( $self->api_factory->apis_available ) {
my $att_name = sprintf "%s_api", lc($api_name);
my $api_class = $self->api_factory->classname_for($api_name);
my @delegated =
grep { $delegates{$_}->[0]->{api_name} eq $api_name } keys %delegates;
$log->debugf( "Adding API: '%s' handles %s",
$att_name, join ', ', @delegated );
$self->meta->add_attribute(
$att_name => (
is => 'ro',
isa => $api_class,
default => sub { $self->api_factory->get_api($api_name) },
lazy => 1,
handles => \@delegated,
)
);
}
}
sub _build_af {
my $self = shift;
my %args;
$args{base_url} = $self->base_url if $self->base_url;
return Kubernetes::ApiFactory->new(%args);
}
=head1 NAME
Kubernetes::Role - a Moose role for the Kubernetes
=head2 Kubernetes version: v1.13.7
=head1 VERSION
Automatically generated by the Perl OpenAPI Generator project:
=over 4
=item Build package: org.openapitools.codegen.languages.PerlClientCodegen
=item Codegen version:
=back
=head2 A note on Moose
This role is the only component of the library that uses Moose. See
Kubernetes::ApiFactory for non-Moosey usage.
=head1 SYNOPSIS
The Perl Generator in the OpenAPI Generator project builds a library of Perl modules to interact with
a web service defined by a OpenAPI Specification. See below for how to build the
library.
This module provides an interface to the generated library. All the classes,
objects, and methods (well, not quite *all*, see below) are flattened into this
role.
package MyApp;
use Moose;
with 'Kubernetes::Role';
package main;
my $api = MyApp->new({ tokens => $tokens });
my $pet = $api->get_pet_by_id(pet_id => $pet_id);
=head2 Structure of the library
The library consists of a set of API classes, one for each endpoint. These APIs
implement the method calls available on each endpoint.
Additionally, there is a set of "object" classes, which represent the objects
returned by and sent to the methods on the endpoints.
An API factory class is provided, which builds instances of each endpoint API.
This Moose role flattens all the methods from the endpoint APIs onto the consuming
class. It also provides methods to retrieve the endpoint API objects, and the API
factory object, should you need it.
For documentation of all these methods, see AUTOMATIC DOCUMENTATION below.
=head2 Configuring authentication
In the normal case, the OpenAPI Spec will describe what parameters are
required and where to put them. You just need to supply the tokens.
my $tokens = {
# basic
username => $username,
password => $password,
# oauth
access_token => $oauth_token,
# keys
$some_key => { token => $token,
prefix => $prefix,
in => $in, # 'head||query',
},
$another => { token => $token,
prefix => $prefix,
in => $in, # 'head||query',
},
...,
};
my $api = MyApp->new({ tokens => $tokens });
Note these are all optional, as are C<prefix> and C<in>, and depend on the API
you are accessing. Usually C<prefix> and C<in> will be determined by the code generator from
the spec and you will not need to set them at run time. If not, C<in> will
default to 'head' and C<prefix> to the empty string.
The tokens will be placed in a L<Kubernetes::Configuration> instance
as follows, but you don't need to know about this.
=over 4
=item C<$cfg-\>{username}>
String. The username for basic auth.
=item C<$cfg-\>{password}>
String. The password for basic auth.
=item C<$cfg-\>{api_key}>
Hashref. Keyed on the name of each key (there can be multiple tokens).
$cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};
=item C<$cfg->{api_key_prefix}>
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix.
$cfg->{api_key_prefix} = {
secretKey => 'string',
anotherKey => 'same or some other string',
};
=item C<$config-\>{access_token}>
String. The OAuth access token.
=back
=head1 METHODS
=head2 C<base_url>
The generated code has the C<base_url> already set as a default value. This method
returns the current value of C<base_url>.
=head2 C<api_factory>
Returns an API factory object. You probably won't need to call this directly.
$self->api_factory('Pet'); # returns a Kubernetes::PetApi instance
$self->pet_api; # the same
=head1 MISSING METHODS
Most of the methods on the API are delegated to individual endpoint API objects
(e.g. Pet API, Store API, User API etc). Where different endpoint APIs use the
same method name (e.g. C<new()>), these methods can't be delegated. So you need
to call C<$api-E<gt>pet_api-E<gt>new()>.
In principle, every API is susceptible to the presence of a few, random, undelegatable
method names. In practice, because of the way method names are constructed, it's
unlikely in general that any methods will be undelegatable, except for:
new()
class_documentation()
method_documentation()
To call these methods, you need to get a handle on the relevant object, either
by calling C<$api-E<gt>foo_api> or by retrieving an object, e.g.
C<$api-E<gt>get_pet_by_id(pet_id =E<gt> $pet_id)>. They are class methods, so
you could also call them on class names.
=head1 BUILDING YOUR LIBRARY
See the homepage C<https://openapi-generator.tech> for full details.
But briefly, clone the git repository, build the codegen codebase, set up your build
config file, then run the API build script. You will need git, Java 7 or 8 and Apache
maven 3.0.3 or better already installed.
The config file should specify the project name for the generated library:
{"moduleName":"WWW::MyProjectName"}
Your library files will be built under C<WWW::MyProjectName>.
$ git clone https://github.com/openapitools/openapi-generator
$ cd openapi-generator
$ mvn package
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i [URL or file path to JSON OpenAPI API spec] \
-g perl \
-c /path/to/config/file.json \
-o /path/to/output/folder
Bang, all done. Run the C<autodoc> script in the C<bin> directory to see the API
you just built.
=head1 AUTOMATIC DOCUMENTATION
You can print out a summary of the generated API by running the included
C<autodoc> script in the C<bin> directory of your generated library. A few
output formats are supported:
Usage: autodoc [OPTION]
-w wide format (default)
-n narrow format
-p POD format
-H HTML format
-m Markdown format
-h print this help message
-c your application class
The C<-c> option allows you to load and inspect your own application. A dummy
namespace is used if you don't supply your own class.
=head1 DOCUMENTATION FROM THE OpenAPI Spec
Additional documentation for each class and method may be provided by the OpenAPI
spec. If so, this is available via the C<class_documentation()> and
C<method_documentation()> methods on each generated object class, and the
C<method_documentation()> method on the endpoint API classes:
my $cmdoc = $api->pet_api->method_documentation->{$method_name};
my $odoc = $api->get_pet_by_id->(pet_id => $pet_id)->class_documentation;
my $omdoc = $api->get_pet_by_id->(pet_id => $pet_id)->method_documentation->{method_name};
Each of these calls returns a hashref with various useful pieces of information.
=cut
1;