Skip to content

Commit

Permalink
docs: add list clusters sample code (#122)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <[email protected]>
  • Loading branch information
TylerGillson authored Aug 7, 2024
1 parent cd5a464 commit 67d0f68
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# palette-sdk-go

Palette SDK for the Go programming language.

See [terraform-provider-spectrocloud](https://github.com/spectrocloud/terraform-provider-spectrocloud) for example usage.
## Usage

# Usage
A project-scoped client can be instantiated as follows:

```go
Expand All @@ -15,19 +15,25 @@ pc := client.New(
```

Switch from a project-scoped client to a tenant-scoped client:

```go
client.WithScopeTenant()(pc)
```
Note that the above will only succeed if original client's credentials are associated with a user who is authorized as a tenant administrator.

Switch from a tenant-scoped client to a project-scoped client - or from one project to another:

```go
client.WithScopeProject(projectUid)(pc)
```

Refer to [client.go](client/client.go) for all possible client configuration options.
### Next Steps
- Refer to the [examples](examples/) to get started quickly.
- Refer to [client.go](client/client.go) for all possible client configuration options.
- Refer to [terraform-provider-spectrocloud](https://github.com/spectrocloud/terraform-provider-spectrocloud) for additional usage examples.

# Contributing

All contributions are welcome! Feel free to reach out on the [Spectro Cloud community Slack](https://spectrocloudcommunity.slack.com/join/shared_invite/zt-g8gfzrhf-cKavsGD_myOh30K24pImLA#/shared-invite/email).

Make sure `pre-commit` is [installed](https://pre-commit.com#install).
Expand Down
64 changes: 64 additions & 0 deletions examples/list_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"os"

"github.com/spectrocloud/palette-api-go/models"
"github.com/spectrocloud/palette-sdk-go/client"
)

func main() {
var host, apiKey, projectUid, scope string

// Parse command line arguments

numArgs := len(os.Args)
if numArgs < 3 || numArgs > 4 {
fmt.Println("Usage: main <host> <apiKey> [projectUid]")
os.Exit(1)
}
switch numArgs {
case 3:
host, apiKey = os.Args[1], os.Args[2]
scope = "tenant"
case 4:
host, apiKey, projectUid = os.Args[1], os.Args[2], os.Args[3]
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)
}
}

0 comments on commit 67d0f68

Please sign in to comment.