Chef-REST-Client

 view release on metacpan or  search on metacpan

lib/Chef/REST/Client.pm  view on Meta::CPAN

use vars qw { $AUTOLOAD };

sub new {
  my $class = shift;
  my $param = {@_};
  my $self  = $class->SUPER::new(@_);
     $self->name($param->{'chef_client_name'}) if defined $param->{'chef_client_name'};
     $self->private_key($param->{'chef_client_private_key'}) if defined $param->{'chef_client_private_key'};     
  bless $self, $class;
  return $self;
}

sub name {
  my ($self,$client_name) = (@_);
         $self->{ 'CHEF_CLIENT' } = $client_name if defined $client_name;
  return $self->{ 'CHEF_CLIENT' };
}

#----------------------------------#
# Class : Chef::REST::Client::Role #
#----------------------------------#
sub role_ 
{
  my $self = shift;
  my $param = {@_};


}

#-----------------------------------#
# Class : Chef::REST::Client::Roles #
#-----------------------------------#
sub roles_ 
{
  my $self = shift;
  
  package Chef::REST::Client::roles;

  use parent qw { Chef::REST::Client };

  bless $self, 'Chef::REST::Client::roles';
        $self->api_end_point('roles');
  return $self;
  
  sub api_end_point 
  {
    my ($self,$api_end_point) = (@_);
           $self->{ 'API_END_POINT' } = $api_end_point if defined $api_end_point;
    return $self->{ 'API_END_POINT' };
  }
    
  sub list 
  {
    my $self = shift;
    my $end_point = $self->api_end_point;
    my $mojo_json = new Mojo::JSON();
    my $list_of_roles = 
       $mojo_json->decode(
             $self->ua(  'client_name'   => $self->name )
                  ->get( 'api_end_point' => $end_point  )
                  ->decoded_content
          );
    my @_roles;      
    foreach my $r ( keys(%$list_of_roles) ){
      my $role = Chef::REST::Client::role( 'name' => $r,  
                                           'url'  => $list_of_roles->{$r} 
                                         );
       push @_roles , $role;
    }
    return \@_roles;
  }

}

sub AUTOLOAD {
   my $self = shift;
   my $hash_param = pop @_;
   my ($method, $data) = ( 'GET');
   my $request_url = undef;
   
   if( ref $hash_param ne 'HASH')
   {
      push @_ , $hash_param if defined $hash_param;
      undef $hash_param;
   }
   else {
   	$method = 'POST' if $hash_param->{'method'} =~ m/post/i;
   	$data   = $hash_param->{'data'};
   }  

   my @param = @_;

	my $module =  (split ('::', $AUTOLOAD))[-1];
	load $AUTOLOAD;	
	my $obj = $AUTOLOAD->new();

	my (@api_end_point, @q );
	   @api_end_point = ( $module , @param );
	   
	foreach my $k ( keys(%$hash_param))
	{		
		push @q , join '=' , ($k , $hash_param->{$k});
	}
	
	$request_url = join '/', @api_end_point;
	$request_url = join '?' , $request_url , (join '&', @q ) if defined $q[0];
   
	my $result;
	
	 if ($method eq 'GET' )
	 { 
	   $result = $self->get( 'api_end_point' =>  $request_url )->decoded_content;
	 }
	 elsif( $method eq 'POST')
	 {
	   $result = $self->post( 
	                    'api_end_point' =>  $request_url ,
	                    'data'          => $data
	                 );
	 }         
   $obj->populate( $result ); 
	return $obj;
}

1;

=head1 KNOWN BUGS

=head1 SUPPORT

open a github ticket or email comments to Bhavin Patel <bpatel10@nyit.edu>

=head1 COPYRIGHT AND LICENSE

This Software is free to use , licensed under : The Artisic License 2.0 (GPL Compatible)

=cut



( run in 0.329 second using v1.01-cache-2.11-cpan-8644d7adfcd )