Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add list clusters sample code #122

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading