Skip to content

Commit

Permalink
fix(access): fix user and role import (#1346)
Browse files Browse the repository at this point in the history
* fix(access): fix `user` and `role` import

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
bpg authored Jun 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 9bbbf8d commit bdd9225
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 18 additions & 1 deletion proxmoxtf/resource/role.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ package resource
import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -46,13 +47,23 @@ func Role() *schema.Resource {
UpdateContext: roleUpdate,
DeleteContext: roleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
roleID := d.Id()

err := d.Set(mkResourceVirtualEnvironmentRoleRoleID, roleID)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

return []*schema.ResourceData{d}, nil
},
},
}
}

func roleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)

client, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
@@ -83,19 +94,22 @@ func roleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag

func roleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)

client, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
}

roleID := d.Id()

role, err := client.Access().GetRole(ctx, roleID)
if err != nil {
if errors.Is(err, api.ErrResourceDoesNotExist) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

@@ -108,11 +122,13 @@ func roleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
}

err = d.Set(mkResourceVirtualEnvironmentRolePrivileges, privileges)

return diag.FromErr(err)
}

func roleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)

client, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
@@ -140,6 +156,7 @@ func roleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag

func roleDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)

client, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
12 changes: 11 additions & 1 deletion proxmoxtf/resource/user.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ package resource
import (
"context"
"errors"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -147,7 +148,16 @@ func User() *schema.Resource {
UpdateContext: userUpdate,
DeleteContext: userDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
roleID := d.Id()

err := d.Set(mkResourceVirtualEnvironmentUserUserID, roleID)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

return []*schema.ResourceData{d}, nil
},
},
}
}

0 comments on commit bdd9225

Please sign in to comment.