-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLT-1483: Added role management suppport in terraform.
- Loading branch information
1 parent
1cee37c
commit d015c87
Showing
9 changed files
with
296 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
page_title: "spectrocloud_role Resource - terraform-provider-spectrocloud" | ||
subcategory: "" | ||
description: |- | ||
The role resource allows you to manage roles in Palette. | ||
--- | ||
|
||
# spectrocloud_role (Resource) | ||
|
||
The role resource allows you to manage roles in Palette. | ||
|
||
You can learn more about managing roles in Palette by reviewing the [Roles](https://docs.spectrocloud.com/glossary-all/#role) guide. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
variable "roles" { | ||
type = list(string) | ||
default = ["Cluster Admin", "Cluster Profile Editor"] | ||
} | ||
# Data source loop to retrieve multiple roles | ||
data "spectrocloud_role" "roles" { | ||
for_each = toset(var.roles) | ||
name = each.key | ||
} | ||
resource "spectrocloud_role" "custom_role" { | ||
name = "Test Cluster Role" | ||
type = "project" | ||
permissions = flatten([for role in data.spectrocloud_role.roles : role.permissions]) | ||
} | ||
``` | ||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the role. | ||
- `permissions` (Set of String) The permission's assigned to the role. | ||
|
||
### Optional | ||
|
||
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) | ||
- `type` (String) The role type. Allowed values are `project` or `tenant` or `project` | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedblock--timeouts"></a> | ||
### Nested Schema for `timeouts` | ||
|
||
Optional: | ||
|
||
- `create` (String) | ||
- `delete` (String) | ||
- `update` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
variable "roles" { | ||
type = list(string) | ||
default = ["Cluster Admin", "Cluster Profile Editor"] | ||
} | ||
|
||
# Data source loop to retrieve multiple roles | ||
data "spectrocloud_role" "roles" { | ||
for_each = toset(var.roles) | ||
name = each.key | ||
} | ||
|
||
resource "spectrocloud_role" "custom_role" { | ||
name = "Test Cluster Role" | ||
type = "project" | ||
permissions = flatten([for role in data.spectrocloud_role.roles : role.permissions]) | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/resources/spectrocloud_role/terraform.template.tfvars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package spectrocloud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/spectrocloud/palette-sdk-go/api/models" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceRole() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceRoleCreate, | ||
ReadContext: resourceRoleRead, | ||
UpdateContext: resourceRoleUpdate, | ||
DeleteContext: resourceRoleDelete, | ||
Description: "The role resource allows you to manage roles in Palette.", | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Update: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
SchemaVersion: 2, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The name of the role.", | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "project", | ||
ValidateFunc: validation.StringInSlice([]string{"project", "tenant", "resource"}, false), | ||
Description: "The role type. Allowed values are `project` or `tenant` or `project`", | ||
}, | ||
"permissions": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Description: "The permission's assigned to the role.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func convertInterfaceSliceToStringSlice(input []interface{}) ([]string, error) { | ||
var output []string | ||
for _, item := range input { | ||
str, ok := item.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("item %v is not a string", item) | ||
} | ||
output = append(output, str) | ||
} | ||
return output, nil | ||
} | ||
|
||
func toRole(d *schema.ResourceData) *models.V1Role { | ||
name := d.Get("name").(string) | ||
roleType := d.Get("type").(string) | ||
permission, _ := convertInterfaceSliceToStringSlice(d.Get("permissions").(*schema.Set).List()) | ||
return &models.V1Role{ | ||
Metadata: &models.V1ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"scope": roleType, | ||
}, | ||
LastModifiedTimestamp: models.V1Time{}, | ||
Name: name, | ||
}, | ||
Spec: &models.V1RoleSpec{ | ||
Permissions: permission, | ||
Scope: models.V1Scope(roleType), | ||
Type: "user", | ||
}, | ||
Status: &models.V1RoleStatus{ | ||
IsEnabled: true, | ||
}, | ||
} | ||
} | ||
|
||
func flattenRole(d *schema.ResourceData, role *models.V1Role) error { | ||
var err error | ||
err = d.Set("name", role.Metadata.Name) | ||
if err != nil { | ||
return err | ||
} | ||
err = d.Set("type", role.Spec.Scope) | ||
if err != nil { | ||
return err | ||
} | ||
err = d.Set("permissions", role.Spec.Permissions) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceRoleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := getV1ClientWithResourceContext(m, "tenant") | ||
var diags diag.Diagnostics | ||
role := toRole(d) | ||
uid, err := c.CreateRole(role) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(uid) | ||
return diags | ||
} | ||
|
||
func resourceRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := getV1ClientWithResourceContext(m, "tenant") | ||
var diags diag.Diagnostics | ||
role, err := c.GetRoleByID(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
err = flattenRole(d, role) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} | ||
|
||
func resourceRoleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := getV1ClientWithResourceContext(m, "tenant") | ||
var diags diag.Diagnostics | ||
role := toRole(d) | ||
err := c.UpdateRole(role, d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} | ||
|
||
func resourceRoleDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := getV1ClientWithResourceContext(m, "tenant") | ||
var diags diag.Diagnostics | ||
err := c.DeleteRole(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" | ||
subcategory: "" | ||
description: |- | ||
{{ .Description | plainmarkdown | trimspace | prefixlines " " }} | ||
--- | ||
|
||
# {{.Name}} ({{.Type}}) | ||
|
||
{{ .Description | plainmarkdown | trimspace | prefixlines " " }} | ||
|
||
You can learn more about managing roles in Palette by reviewing the [Roles](https://docs.spectrocloud.com/glossary-all/#role) guide. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
variable "roles" { | ||
type = list(string) | ||
default = ["Cluster Admin", "Cluster Profile Editor"] | ||
} | ||
|
||
# Data source loop to retrieve multiple roles | ||
data "spectrocloud_role" "roles" { | ||
for_each = toset(var.roles) | ||
name = each.key | ||
} | ||
|
||
resource "spectrocloud_role" "custom_role" { | ||
name = "Test Cluster Role" | ||
type = "project" | ||
permissions = flatten([for role in data.spectrocloud_role.roles : role.permissions]) | ||
} | ||
``` | ||
|
||
|
||
{{ .SchemaMarkdown | trimspace }} |