Code Monkey home page Code Monkey logo

rancher-api's Introduction

rancher-api

rancher API v3 for php

I found easy implementations for Rancher API v3 lacking, so here this is.

Currrent state

There still is a lot of API to implement, pulls are welcome. Given the correct config you can import projects from Rancher.

Example importing rancher "projects"

RancherConfig contains:

  • name
  • host
  • token
  • secret
<?php

namespace App\Command;

use App\Entity\Project;
use App\Repository\ProjectRepository;
use App\Repository\RancherConfigRepository;
use Doctrine\ORM\EntityManagerInterface;
use Rootshell\RancherApi\Rancher;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class RancherGetProjectsCommand extends Command
{
    protected static $defaultName = 'rancher:populate:projects';
    /**
     * @var RancherConfigRepository
     */
    private $rancherConfigRepository;
    /**
     * @var ProjectRepository
     */
    private $projectRepository;
    /**
     * @var EntityManager
     */
    private $entityManager;

    public function __construct(RancherConfigRepository $rancherConfigRepository, ProjectRepository $projectRepository, EntityManagerInterface $entityManager)
    {
        $this->rancherConfigRepository = $rancherConfigRepository;
        $this->projectRepository = $projectRepository;
        $this->entityManager = $entityManager;
        Command::__construct();
    }

    protected function configure()
    {
        $this
            ->setDescription('Add a short description for your command')
            ->addArgument('name', InputArgument::OPTIONAL, 'RancherConfig Name')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $name = $input->getArgument('name');
        $rancherOption = $this->rancherConfigRepository->findAll();

        if(empty($name)) {
            $rancherOption[] = 'all';
            $name = $io->choice('Select config to check', $rancherOption);
        }

        $entityList = [];
        if($name != 'all') {
            $entityList = [$name];
        } else {
            $key = array_search($name, $rancherOption);
            if($key){
                unset($rancherOption[$key]);
            }
            $entityList = $rancherOption;
        }

        foreach ($entityList as $rancherEntity) {
            if(is_string($rancherEntity)) {
                $rancherEntity = $this->rancherConfigRepository->findOneBy(['name' => $rancherEntity]);
            }
            $io->writeln("getting projects for: {$rancherEntity->getName()}");
            $rancher = new Rancher($rancherEntity->getHost(), $rancherEntity->getAccessToken(), $rancherEntity->getSecret());
            $projects = $rancher->projects()->getAll();
            foreach ($projects as $project) {
                $entity = $this->projectRepository->findOneBy(['rancher_id' => $project->id]);
                if(!$entity) {
                    $entity = new Project();
                    $entity->setRancherId($project->id);
                    $io->writeln("Don't have an project with the id: {$project->id} which has the name: {$project->name}");
                }
                $entity->setName($project->name);
                $entity->setUuid($project->uuid);

                $this->entityManager->persist($entity);
            }

        }
        $this->entityManager->flush();

        $io->success('Entities updated!');
        return 0;
    }
}

rancher-api's People

Contributors

1338 avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.