| sidebar_label | title | description | hide_table_of_contents | sidebar_position | tags | ||
|---|---|---|---|---|---|---|---|
List Clusters with Palette Go SDK |
List Clusters with Palette Go SDK |
Learn how to use the Palette GO SDK to list your Palette host clusters. |
false |
10 |
|
The Palette Go Software Developer Toolkit (SDK) enables Go developers to interact with Palette services. This guide explains how to install and use the SDK to list the active host clusters in your Palette environment.
-
Go version 1.22.5 or later.
-
A Palette account with Tenant Admin access.
-
A Palette API key. Refer to the Create API Key page for instructions on how to create an API key.
-
Open a terminal window and create a folder for you Go repository.
mkdir sdk-example
-
Navigate to the created folder.
cd sdk-example -
Initialize your Go module and enable dependency tracking for your code by issuing the following command. This will create a
go.modfile in the current folder.go mod init example/list-clusters
-
Download the required Palette SDK modules. The command below retrieves the modules and records their dependencies in the
go.modfile.go get github.com/spectrocloud/palette-sdk-go/api/models go get github.com/spectrocloud/palette-sdk-go/client
-
Create a source file named
main.go.touch main.go
-
Open the
main.gofile in a text editor of your choice and paste the content below.package main import ( "fmt" "os" "github.com/spectrocloud/palette-sdk-go/api/models" "github.com/spectrocloud/palette-sdk-go/client" ) func main() { // Read environment variables host := os.Getenv("PALETTE_HOST") apiKey := os.Getenv("PALETTE_API_KEY") projectUid := os.Getenv("PALETTE_PROJECT_UID") scope := "tenant" if host == "" || apiKey == "" { fmt.Println("You must specify the PALETTE_HOST and PALETTE_API_KEY environment variables.") os.Exit(1) } if projectUid != "" { scope = "project" } // Initialize a Palette client pc := client.New( client.WithPaletteURI(host), client.WithAPIKey(apiKey), ) if projectUid != "" { client.WithScopeProject(projectUid)(pc) } else { client.WithScopeTenant()(pc) } // Search for clusters fmt.Printf("Searching for Palette clusters with %s scope...\n", scope) clusters, err := pc.SearchClusterSummaries(&models.V1SearchFilterSpec{}, []*models.V1SearchFilterSortSpec{}) if err != nil { panic(err) } // Display the results if len(clusters) == 0 { fmt.Println("\nNo clusters found.") return } fmt.Printf("\nFound %d cluster(s):\n", len(clusters)) for _, cluster := range clusters { fmt.Printf("\nName: %s\n", cluster.Metadata.Name) fmt.Printf(" Cloud Type: %s\n", cluster.SpecSummary.CloudConfig.CloudType) fmt.Printf(" Project: %s\n", cluster.SpecSummary.ProjectMeta.Name) } }
This application performs the following tasks:
-
Imports the required packages.
-
Reads the environment variables provided by the user to configure the client and define the search scope. If the user provides a project UID, the application sets the scope to project-specific and lists the active clusters within that project. If no project UID is given, the scope is set to tenant-wide, listing active clusters across all projects.
-
Initializes a Palette client using the provided host URL and Palette API key.
-
Searches for Palette clusters within the specified scope (tenant-wide or project-specific).
-
Displays the active clusters with details such as name, cloud type, and associated project.
After pasting the content, save and exit the file.
-
-
Export your Palette host URL and API key as environment variables. Replace
<your-palette-host-url>with your Palette URL, such asconsole.spectrocloud.com, and<your-palette-api-key>with your API key.export PALETTE_API_KEY=<your-palette-api-key> export PALETTE_HOST=<your-palette-host-url>
-
Issue the following command to execute the application.
go run .:::tip
Optionally, instead of exporting the variables in a separate command, you can use the
go runcommand with command-scoped environment variables.```bash PALETTE_API_KEY=<your-palette-api-key> PALETTE_HOST=<your-palette-host-url> go run . ```:::
The application will print the active clusters in your Palette environment.
Searching for Palette clusters with tenant scope... Found 1 cluster(s): Name: aws-cluster Cloud Type: aws Project: Default
-
Log in to Palette.
-
From the left Main Menu, click Clusters.
-
Verify that the clusters printed by the Go client application are also visible in the Palette UI.