diff --git a/spectrocloud/resource_role.go b/spectrocloud/resource_role.go index e7cea279..21bd31e7 100644 --- a/spectrocloud/resource_role.go +++ b/spectrocloud/resource_role.go @@ -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), @@ -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 { @@ -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 -} diff --git a/spectrocloud/resource_role_test.go b/spectrocloud/resource_role_test.go new file mode 100644 index 00000000..40cbad1b --- /dev/null +++ b/spectrocloud/resource_role_test.go @@ -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()) +}