diff --git a/README.md b/README.md index b4f1243c..be236fc4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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). diff --git a/examples/list_clusters.go b/examples/list_clusters.go new file mode 100644 index 00000000..e9074db4 --- /dev/null +++ b/examples/list_clusters.go @@ -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 [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) + } +}