Skip to content

Commit

Permalink
feat: implement import state for resource windows_local_group_member
Browse files Browse the repository at this point in the history
  • Loading branch information
d-strobel committed Oct 17, 2024
1 parent 7454b7e commit 1aa3bed
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions internal/provider/local/local_group_member_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package local
import (
"context"
"fmt"
"strings"

"github.com/d-strobel/terraform-provider-windows/internal/generate/resource_local_group_member"

"github.com/d-strobel/gowindows"
"github.com/d-strobel/gowindows/windows/local/accounts"
"github.com/d-strobel/gowindows/winerror"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -86,24 +89,39 @@ func (r *localGroupMemberResource) Read(ctx context.Context, req resource.ReadRe
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

// Split the ID into SID and Member
resourceId := data.Id.ValueString()
resourceIdParts := strings.Split(resourceId, "/member/")
if len(resourceIdParts) != 2 {
resp.Diagnostics.AddError(
"Invalid Import ID",
fmt.Sprintf("Expected import ID format: '<SID>/member/<Member>', got: %s", resourceId),
)
}

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
params := accounts.GroupMemberReadParams{
SID: data.GroupId.ValueString(),
Member: data.MemberId.ValueString(),
SID: resourceIdParts[0],
Member: resourceIdParts[1],
}

if _, err := r.client.LocalAccounts.GroupMemberRead(ctx, params); err != nil {
winResp, err := r.client.LocalAccounts.GroupMemberRead(ctx, params)
if err != nil {
tflog.Error(ctx, "Received unexpected error from remote windows client", map[string]interface{}{
"command": winerror.UnwrapCommand(err),
})
resp.Diagnostics.AddError("Windows Client Error", fmt.Sprintf("Unable to delete local group member:\n%s", err.Error()))
return
}

// Set read values
data.GroupId = types.StringValue(params.SID)
data.MemberId = types.StringValue(winResp.SID.Value)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -136,3 +154,7 @@ func (r *localGroupMemberResource) Delete(ctx context.Context, req resource.Dele
return
}
}

func (r *localGroupMemberResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

0 comments on commit 1aa3bed

Please sign in to comment.