| 1 | package Net::Kubernetes::Role::ResourceCreator; | ||||||||||
| 2 | # ABSTRACT: Role to allow creation of resources from either objects or files. | ||||||||||
| 3 | 4 | $Net::Kubernetes::Role::ResourceCreator::VERSION = '0.19'; | |||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use MooseX::Aliases; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | require YAML::XS; | |||||||||
| 7 | 4 | require Net::Kubernetes::Resource::Service; | |||||||||
| 8 | 4 | require Net::Kubernetes::Resource::Pod; | |||||||||
| 9 | 4 | require Net::Kubernetes::Resource::ReplicationController; | |||||||||
| 10 | 4 | require Net::Kubernetes::Resource::Secret; | |||||||||
| 11 | 4 | require Net::Kubernetes::Exception; | |||||||||
| 12 | 4 | ▶ | use File::Slurp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use syntax 'try'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | with 'Net::Kubernetes::Role::ResourceFactory'; | |||||||||
| 17 | |||||||||||
| 18 | 4 | requires 'ua'; | |||||||||
| 19 | 4 | requires 'create_request'; | |||||||||
| 20 | 4 | requires 'json'; | |||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | sub create_from_file { | ||||||||||
| 24 | 0 | ▶ | my($self, $file) = @_; | ||||||||
|
Subroutine
create_from_file: not called CC=3 SCAR=24.8
| |||||||||||
| 25 | 0 | ▶ | if (! -f $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | Throwable::Error->throw(message=>"Could not read file: $file"); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 0 | my $object; | |||||||||
| 30 | 0 | ▶ | if ($file =~ /\.ya?ml$/i){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | $object = YAML::XS::LoadFile($file); | |||||||||
| 32 | } | ||||||||||
| 33 | else{ | ||||||||||
| 34 | 0 | $object = $self->json->decode(scalar(read_file($file))); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 0 | return $self->create($object); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub create { | ||||||||||
| 41 | 0 | ▶ | my($self, $object) = @_; | ||||||||
|
Subroutine
create: not called CC=3 SCAR=24.8
| |||||||||||
| 42 | |||||||||||
| 43 | # This is an ugly hack and I am not proud of it. | ||||||||||
| 44 | # That being said, I have bigger fish to fry right now | ||||||||||
| 45 | # This is here because kubernetes will not accept "true" | ||||||||||
| 46 | # and "false". | ||||||||||
| 47 | # The other problem is that YAML will read a a boolean | ||||||||||
| 48 | # value as 1 or 0 which json does not switch back to | ||||||||||
| 49 | # true or false. This is not JSON's fault, but I'm not | ||||||||||
| 50 | # sure just now how I want to solve it. | ||||||||||
| 51 | 0 | my $content = $self->json->encode($object); | |||||||||
| 52 | 0 | $content =~ s/(["'])(true|false)\1/$2/g; | |||||||||
| 53 | # /EndHack | ||||||||||
| 54 | 0 | my $req = $self->create_request(POST=>$self->path.'/'.lc($object->{kind}).'s', undef, $content); | |||||||||
| 55 | 0 | my $res = $self->ua->request($req); | |||||||||
| 56 | 0 | ▶ | if ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | return $self->create_resource_object($self->json->decode($res->content)); | |||||||||
| 58 | }else{ | ||||||||||
| 59 | 0 | my $message; | |||||||||
| 60 | try{ | ||||||||||
| 61 | my $obj = $self->json->decode($res->content); | ||||||||||
| 62 | $message = $obj->{message}; | ||||||||||
| 63 | } | ||||||||||
| 64 | catch($e) { | ||||||||||
| 65 | $message = $res->message; | ||||||||||
| 66 | } | ||||||||||
| 67 | 0 | ▶ | Net::Kubernetes::Exception->throw(code=>$res->code, message=>"Error creating resource: ".$message); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | } | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 4 | return 42; | |||||||||
| 72 | |||||||||||
| 73 | __END__ | ||||||||||
| 74 | |||||||||||
| 75 | =pod | ||||||||||
| 76 | |||||||||||
| 77 | =encoding UTF-8 | ||||||||||
| 78 | |||||||||||
| 79 | =head1 NAME | ||||||||||
| 80 | |||||||||||
| 81 | Net::Kubernetes::Role::ResourceCreator - Role to allow creation of resources from either objects or files. | ||||||||||
| 82 | |||||||||||
| 83 | =head1 VERSION | ||||||||||
| 84 | |||||||||||
| 85 | version 0.19 | ||||||||||
| 86 | |||||||||||
| 87 | =head1 METHODS | ||||||||||
| 88 | |||||||||||
| 89 | =head2 create({OBJECT}) | ||||||||||
| 90 | |||||||||||
| 91 | Creates a new L<Net::Kubernetes::Resource> (subtype determined by $BNJECT->{kind}) | ||||||||||
| 92 | |||||||||||
| 93 | =head2 create_from_file(PATH_TO_FILE) (accepts either JSON or YAML files) | ||||||||||
| 94 | |||||||||||
| 95 | Create from file is really just a short cut around something like: | ||||||||||
| 96 | |||||||||||
| 97 | my $object = YAML::LoadFile(PATH_TO_FILE); | ||||||||||
| 98 | $kube->create($object); | ||||||||||
| 99 | |||||||||||
| 100 | =head1 AUTHOR | ||||||||||
| 101 | |||||||||||
| 102 | Dave Mueller <dave@perljedi.com> | ||||||||||
| 103 | |||||||||||
| 104 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 105 | |||||||||||
| 106 | This software is Copyright (c) 2015 by Dave Mueller. | ||||||||||
| 107 | |||||||||||
| 108 | This is free software, licensed under: | ||||||||||
| 109 | |||||||||||
| 110 | The MIT (X11) License | ||||||||||
| 111 | |||||||||||
| 112 | =head1 SEE ALSO | ||||||||||
| 113 | |||||||||||
| 114 | Please see those modules/websites for more information related to this module. | ||||||||||
| 115 | |||||||||||
| 116 | =over 4 | ||||||||||
| 117 | |||||||||||
| 118 | =item * | ||||||||||
| 119 | |||||||||||
| 120 | L<Net::Kubernetes|Net::Kubernetes> | ||||||||||
| 121 | |||||||||||
| 122 | =back | ||||||||||
| 123 | |||||||||||
| 124 | =head1 CONSUMES | ||||||||||
| 125 | |||||||||||
| 126 | =over 4 | ||||||||||
| 127 | |||||||||||
| 128 | =item * L<Net::Kubernetes::Role::ResourceFactory> | ||||||||||
| 129 | |||||||||||
| 130 | =back | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||