From 8ef7f1e294d9fb601154897d1867f3c7ed3403e6 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Wed, 6 Nov 2024 13:24:11 +0530 Subject: [PATCH] PLT-1470 (#536) * PLT-1470: Added data source support for team. --- docs/data-sources/team.md | 42 ++++++++++++ .../spectrocloud_team/data-source.tf | 14 ++++ .../spectrocloud_team/providers.tf | 28 ++++++++ .../terraform.template.tfvars | 4 ++ go.mod | 2 +- go.sum | 4 +- spectrocloud/data_source_team.go | 66 +++++++++++++++++++ spectrocloud/provider.go | 2 + 8 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 docs/data-sources/team.md create mode 100644 examples/data-sources/spectrocloud_team/data-source.tf create mode 100644 examples/data-sources/spectrocloud_team/providers.tf create mode 100644 examples/data-sources/spectrocloud_team/terraform.template.tfvars create mode 100644 spectrocloud/data_source_team.go diff --git a/docs/data-sources/team.md b/docs/data-sources/team.md new file mode 100644 index 00000000..bdeb3e36 --- /dev/null +++ b/docs/data-sources/team.md @@ -0,0 +1,42 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "spectrocloud_team Data Source - terraform-provider-spectrocloud" +subcategory: "" +description: |- + +--- + +# spectrocloud_team (Data Source) + + + +## Example Usage + +```terraform +data "spectrocloud_team" "team1" { + name = "team2" + + # (alternatively) + # id = "5fd0ca727c411c71b55a359c" +} + +output "team-id" { + value = data.spectrocloud_team.team1.id +} + +output "team-role-ids" { + value = data.spectrocloud_team.team1.role_ids +} +``` + + +## Schema + +### Optional + +- `id` (String) The unique ID of the team. If provided, `name` cannot be used. +- `name` (String) The name of the team. If provided, `id` cannot be used. + +### Read-Only + +- `role_ids` (List of String) The roles id's assigned to the team. diff --git a/examples/data-sources/spectrocloud_team/data-source.tf b/examples/data-sources/spectrocloud_team/data-source.tf new file mode 100644 index 00000000..e208a80f --- /dev/null +++ b/examples/data-sources/spectrocloud_team/data-source.tf @@ -0,0 +1,14 @@ +data "spectrocloud_team" "team1" { + name = "team2" + + # (alternatively) + # id = "5fd0ca727c411c71b55a359c" +} + +output "team-id" { + value = data.spectrocloud_team.team1.id +} + +output "team-role-ids" { + value = data.spectrocloud_team.team1.role_ids +} \ No newline at end of file diff --git a/examples/data-sources/spectrocloud_team/providers.tf b/examples/data-sources/spectrocloud_team/providers.tf new file mode 100644 index 00000000..f3bdb2e0 --- /dev/null +++ b/examples/data-sources/spectrocloud_team/providers.tf @@ -0,0 +1,28 @@ +terraform { + required_providers { + spectrocloud = { + version = ">= 0.1" + source = "spectrocloud/spectrocloud" + } + } +} + +variable "sc_host" { + description = "Spectro Cloud Endpoint" + default = "api.spectrocloud.com" +} + +variable "sc_api_key" { + description = "Spectro Cloud API key" +} + +variable "sc_project_name" { + description = "Spectro Cloud Project (e.g: Default)" + default = "Default" +} + +provider "spectrocloud" { + host = var.sc_host + api_key = var.sc_api_key + project_name = var.sc_project_name +} diff --git a/examples/data-sources/spectrocloud_team/terraform.template.tfvars b/examples/data-sources/spectrocloud_team/terraform.template.tfvars new file mode 100644 index 00000000..c7e9d50b --- /dev/null +++ b/examples/data-sources/spectrocloud_team/terraform.template.tfvars @@ -0,0 +1,4 @@ +# Spectro Cloud credentials +sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS) +sc_api_key = "{Enter Spectro Cloud API Key}" +sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default \ No newline at end of file diff --git a/go.mod b/go.mod index 24e2cff1..e8e0a70c 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/robfig/cron v1.2.0 github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368 github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d - github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb + github.com/spectrocloud/palette-sdk-go v0.0.0-20241106055848-76d563f78474 github.com/stretchr/testify v1.9.0 gotest.tools v2.2.0+incompatible k8s.io/api v0.23.5 diff --git a/go.sum b/go.sum index f234f5f9..24701380 100644 --- a/go.sum +++ b/go.sum @@ -600,8 +600,8 @@ github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368 h1:eY0BOyEbGu github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368/go.mod h1:LlZ9We4kDaELYi7Is0SVmnySuDhwphJLS6ZT4wXxFIk= github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d h1:OMRbHxMJ1a+G1BYzvUYuMM0wLkYJPdnEOFx16faQ/UY= github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d/go.mod h1:MktpRPnSXDTHsQrFSD+daJFQ1zMLSR+1gWOL31jVvWE= -github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb h1:LVeVFAMVdZRhtn1VY3DnDi32ts90r8/RXP5+1RZBZEA= -github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U= +github.com/spectrocloud/palette-sdk-go v0.0.0-20241106055848-76d563f78474 h1:NRbW7hfLXSarRadga47u/bKl83gY8dYZ3MNv30xxetM= +github.com/spectrocloud/palette-sdk-go v0.0.0-20241106055848-76d563f78474/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= diff --git a/spectrocloud/data_source_team.go b/spectrocloud/data_source_team.go new file mode 100644 index 00000000..a62c408d --- /dev/null +++ b/spectrocloud/data_source_team.go @@ -0,0 +1,66 @@ +package spectrocloud + +import ( + "context" + "github.com/spectrocloud/palette-sdk-go/api/models" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceTeam() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceTeamRead, + + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ConflictsWith: []string{"name"}, + Description: "The unique ID of the team. If provided, `name` cannot be used.", + }, + "name": { + Type: schema.TypeString, + Optional: true, + Description: "The name of the team. If provided, `id` cannot be used.", + }, + "role_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: "The roles id's assigned to the team.", + }, + }, + } +} + +func dataSourceTeamRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := getV1ClientWithResourceContext(m, "") + var diags diag.Diagnostics + var team *models.V1Team + var err error + if v, ok := d.GetOk("name"); ok { + team, err = c.GetTeamWithName(v.(string)) + if err != nil { + return diag.FromErr(err) + } + } else { + if val, okay := d.GetOk("id"); okay && val != "" { + team, err = c.GetTeam(val.(string)) + if err != nil { + return diag.FromErr(err) + } + } + } + if team != nil { + d.SetId(team.Metadata.UID) + if err := d.Set("name", team.Metadata.Name); err != nil { + return diag.FromErr(err) + } + if err := d.Set("role_ids", team.Spec.Roles); err != nil { + return diag.FromErr(err) + } + } + return diags +} diff --git a/spectrocloud/provider.go b/spectrocloud/provider.go index 2433011e..d8e265e8 100644 --- a/spectrocloud/provider.go +++ b/spectrocloud/provider.go @@ -142,6 +142,8 @@ func New(_ string) func() *schema.Provider { "spectrocloud_ssh_key": resourceSSHKey(), }, DataSourcesMap: map[string]*schema.Resource{ + "spectrocloud_team": dataSourceTeam(), + "spectrocloud_user": dataSourceUser(), "spectrocloud_project": dataSourceProject(),