KubeBuilder

 view release on metacpan or  search on metacpan

lib/KubeBuilder.pm  view on Meta::CPAN

  has schema_file => (
    is => 'ro',
    isa => 'Str',
    required => 1,
  );

  has schema => (
    is => 'ro', 
    isa => 'Swagger::Schema::Kubernetes',
    lazy => 1,
    default => sub {
      my $self = shift;
      my $data = file($self->schema_file)->slurp;
      KubeBuilder::Error->throw("Couldn't read file " . $self->schema_file) if (not defined $data);
      $data =~ s/^\xEF\xBB\xBF//;
      return Swagger::Schema::Kubernetes->MooseX::DataModel::new_from_json($data);
    }
  );

  has log => (
    is => 'ro',
    default => sub {
      KubeBuilder::Logger->new;
    }
  );

  has objects => (
    is => 'ro',
    isa => 'HashRef[KubeBuilder::Object]',
    lazy => 1,
    traits => [ 'Hash' ],
    handles => {
      object_list => 'values',
    },
    default => sub {
      my $self = shift;
      my %objects => ();

      # Get objects from the definitions (almost everything refs out to defintions/NameOfObject
      my $definitions = (defined $self->schema->definitions) ? $self->schema->definitions : {};

      foreach my $def_name (sort keys %$definitions) {
        my $object = $self->schema->definitions->{ $def_name };

        next if (not exists $object->{ properties });

        $objects{ $def_name } = 
          KubeBuilder::Object->new(
            original_schema => $object,
            root_schema => $self,
            name => $def_name,
          );

        #$self->_get_subobjects_in(\%objects, $def_name, $objects{ $ob_name });
      }

      return \%objects;
    }
  );

  has method_groups => (is => 'ro', isa => 'HashRef[KubeBuilder::Group]', lazy => 1, default => sub {
    my $self = shift;
    my %groups = ();
    foreach my $m (@{ $self->method_list }) {
      my $group_name = $m->group;
      $groups{ $group_name } = KubeBuilder::Group->new(methods => [], name => $group_name) if (not defined $groups{ $group_name });
      push @{ $groups{ $group_name }->methods }, $m;
    }
    return \%groups;
  });

  has method_groups_list => (is => 'ro', isa => 'ArrayRef[KubeBuilder::Group]', lazy => 1, default => sub {
    my $self = shift;
    [ map { $self->method_groups->{ $_ } } sort keys %{ $self->method_groups } ];
  });

  has method_list => (is => 'ro', isa => 'ArrayRef[KubeBuilder::Method]', lazy => 1, default => sub {
    my $self = shift;
    [ map { $self->methods->{ $_ } } sort keys %{ $self->methods } ];
  });

  has methods => (
    is => 'ro',
    isa => 'HashRef[KubeBuilder::Method]',
    lazy => 1,
    default => sub {
      my $self = shift;
      my %methods => ();


      foreach my $path (sort keys %{ $self->schema->paths }) {
        my $common_parameters = $self->schema->paths->{ $path }->parameters;

        foreach my $method (qw/get post put delete options head patch/) {
          my $operation = $self->schema->paths->{ $path }->$method;
          next if (not defined $operation);

          my $method_name = $operation->operationId;
          $methods{ $method_name } = 
            KubeBuilder::Method->new(
              operation => $operation,
              root_schema => $self,
              name => $method_name,
              url => $path,
              method => uc($method),
              (defined $common_parameters) ? (common_parameters => $common_parameters) : (),
            );
        }
      }

      return \%methods;
    }
  );


  sub build {
    my $self = shift;

    $self->process_template(
      'main_module',
      { builder => $self },
    );

    $self->process_template(
      'result2object',
      { builder => $self },
    );

    foreach my $m_name (sort keys %{ $self->methods }){
      my $method = $self->methods->{ $m_name };
      $self->log->info("Generating method for definition $m_name");
      $self->process_template(
        'method',
        { method => $method },
      );
    }
  }



( run in 0.649 second using v1.01-cache-2.11-cpan-5a3173703d6 )