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

PLT-1482:Added import support for resource role #541

Merged
merged 2 commits into from
Nov 13, 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
16 changes: 16 additions & 0 deletions docs/resources/role.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ resource "spectrocloud_role" "custom_role" {
}
```

```
### Importing existing role state & config

```hcl
# import existing user example
import {
to = spectrocloud_role.test_role
id = "{roleUID}"
}

# To generate TF configuration.
terraform plan -generate-config-out=test_role.tf

# To import State file
terraform import spectrocloud_role.test_role {roleUID}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
113 changes: 65 additions & 48 deletions spectrocloud/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func resourceRole() *schema.Resource {
ReadContext: resourceRoleRead,
UpdateContext: resourceRoleUpdate,
DeleteContext: resourceRoleDelete,
Description: "The role resource allows you to manage roles in Palette.",
Importer: &schema.ResourceImporter{
StateContext: resourceRoleImport,
},
Description: "The role resource allows you to manage roles in Palette.",

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -48,6 +51,67 @@ func resourceRole() *schema.Resource {
}
}

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
}

func resourceRoleImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
c := getV1ClientWithResourceContext(m, "tenant")
_, err := c.GetRoleByID(d.Id())
if err != nil {
return nil, err
}

diags := resourceRoleRead(ctx, d, m)
if diags.HasError() {
return nil, fmt.Errorf("could not read role for import: %v", diags)
}
return []*schema.ResourceData{d}, nil
}

func convertInterfaceSliceToStringSlice(input []interface{}) ([]string, error) {
var output []string
for _, item := range input {
Expand Down Expand Up @@ -99,50 +163,3 @@ func flattenRole(d *schema.ResourceData, role *models.V1Role) error {
}
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
}
80 changes: 80 additions & 0 deletions spectrocloud/resource_role_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package spectrocloud

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/palette-sdk-go/api/models"
"github.com/stretchr/testify/assert"
"testing"
)

func TestToRole(t *testing.T) {

d := resourceRole().TestResourceData()
err := d.Set("name", "test-role")
if err != nil {
return
}
err = d.Set("type", "project")
if err != nil {
return
}
err = d.Set("permissions", []interface{}{"bbb"})
if err != nil {
return
}

role := toRole(d)

expected := &models.V1Role{
Metadata: &models.V1ObjectMeta{
Annotations: map[string]string{
"scope": "project",
},
Name: "test-role",
},
Spec: &models.V1RoleSpec{
Permissions: []string{"bbb"},
Scope: models.V1Scope("project"),
Type: "user",
},
Status: &models.V1RoleStatus{
IsEnabled: true,
},
}

assert.Equal(t, expected, role)
}

func TestFlattenRole(t *testing.T) {
d := schema.TestResourceDataRaw(t, map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"permissions": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}, map[string]interface{}{})

role := &models.V1Role{
Metadata: &models.V1ObjectMeta{
Name: "test-role",
},
Spec: &models.V1RoleSpec{
Permissions: []string{"read", "write"},
Scope: models.V1Scope("admin"),
},
}

err := flattenRole(d, role)
assert.NoError(t, err)
assert.Equal(t, "test-role", d.Get("name"))
assert.Equal(t, "admin", d.Get("type"))
assert.ElementsMatch(t, []interface{}{"read", "write"}, d.Get("permissions").(*schema.Set).List())
}
16 changes: 16 additions & 0 deletions templates/resources/role.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,21 @@ resource "spectrocloud_role" "custom_role" {
}
```

```
### Importing existing role state & config

```hcl
# import existing user example
import {
to = spectrocloud_role.test_role
id = "{roleUID}"
}

# To generate TF configuration.
terraform plan -generate-config-out=test_role.tf

# To import State file
terraform import spectrocloud_role.test_role {roleUID}
```

{{ .SchemaMarkdown | trimspace }}
Loading