Rancher Stacks List Using Rest API

This is another tutorials of rancher rest API uses.This rancher API tutorial help to read all stacks from rancher using Rancher RestAPI.I have already shared How to get Containers tutorial.The tutorial help to find all stacks List based on project.

We will send project id which was uniquely generated by rancher and get it from project details rest api json data.We will use rancher version 2 beta rest api to get all stacks.I am using php client to create rest request to rancher api server and find stacks information.

Rancher provides infrastructure services such as multi-host networking, global and local load balancing, and volume snapshots.

Get Rancher Stacks Using Rancher RestApi

We will use PHP client to access rancher containers stacks,There is 'Projects/project_id/stacks' end points to get rancher stacks using rest api. Lets create HTTP Client using Guzzle.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$token = base64_encode('username': 'password');
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'restapi url', //https://api.domain.com/v2-beta/
    // You can set any number of default request options.
    'timeout'  => 2.0,
	'headers' => ['Authorization' => "Basic " . $token, 'Content-Type' => 'application/json', "Accept" => "application/json"],
    //ssl false
    'verify' => false
]);

Please change token string as per your REST API credentials.The parameters are:

$token : This variable will contains combination of username and password string of rancher rest api.
base_uri : The Rancher API host api url path.
headers : This will contains request header parameters.
timeout : Request timeout in sec.
verify : SSL verification false.

Above step has been created Rancher rest client using authentication string, we will use Rancher Rest API end point to get stacks list of particular project, Project could be an env like Dev,Int etc.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$response = $client->get('projects/${PROJECT_ID}/stacks');
echo "
";
echo $response->getBody()->getContents();

Where as : ${PROJECT_ID} is the rancher project id.