Skip to content

Commit

Permalink
PLT-1470 (#536)
Browse files Browse the repository at this point in the history
* PLT-1470: Added data source support for team.
  • Loading branch information
SivaanandM authored Nov 6, 2024
1 parent f5d27fc commit 8ef7f1e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 3 deletions.
42 changes: 42 additions & 0 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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.
14 changes: 14 additions & 0 deletions examples/data-sources/spectrocloud_team/data-source.tf
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions examples/data-sources/spectrocloud_team/providers.tf
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
66 changes: 66 additions & 0 deletions spectrocloud/data_source_team.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions spectrocloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down

0 comments on commit 8ef7f1e

Please sign in to comment.