Skip to content

Commit

Permalink
Merge pull request #20 from d-strobel/feat/resource-local_group_member
Browse files Browse the repository at this point in the history
Feat/resource local group member
  • Loading branch information
d-strobel authored May 6, 2024
2 parents f6f785f + 06d660e commit ce3df13
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 9 deletions.
1 change: 0 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ WARN_COLOR=\033[33;01m
generate-framework:
@printf "$(OK_COLOR)==> Generate provider schema$(NO_COLOR)\n"
tfplugingen-framework generate provider --input ./internal/schema/provider_windows.json --output ./internal/generate

@printf "$(OK_COLOR)==> Generate local schema$(NO_COLOR)\n"
tfplugingen-framework generate data-sources --input ./internal/schema/local_datasources.json --output ./internal/generate
tfplugingen-framework generate resources --input ./internal/schema/local_resources.json --output ./internal/generate
Expand Down
6 changes: 6 additions & 0 deletions docs/data-sources/local_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ description: |-

<!-- data-source description generated from schema -->
Retrieve a list of all local users.
<!-- examples generated from example files -->
## Example Usage

```terraform
data "windows_local_users" "all" {}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
39 changes: 39 additions & 0 deletions docs/resources/local_group_member.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
page_title: "windows_local_group_member Resource - terraform-provider-windows"
subcategory: "Local"
description: |-
Manage group member for local security groups
---
# windows_local_group_member (Resource)

<!-- resource description generated from schema -->
Manage group member for local security groups.
<!-- examples generated from example files -->
## Example Usage

```terraform
resource "windows_local_user" "this" {
name = "test-user"
}
resource "windows_local_group" "this" {
name = "test-group"
}
resource "windows_local_group_member" "this" {
group_id = windows_local_group.this.id
member_id = windows_local_user.this.id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `group_id` (String) The ID of the local security group you want to add the member to. Changing this forces a new resource to be created.
- `member_id` (String) The ID of the principal you want to add as a member to the group. Supported object types are local users or groups. Changing this forces a new resource to be created.

### Read-Only

- `id` (String) The ID of this resource.
1 change: 1 addition & 0 deletions examples/data-sources/windows_local_users/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "windows_local_users" "all" {}
12 changes: 12 additions & 0 deletions examples/resources/windows_local_group_member/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "windows_local_user" "this" {
name = "test-user"
}

resource "windows_local_group" "this" {
name = "test-group"
}

resource "windows_local_group_member" "this" {
group_id = windows_local_group.this.id
member_id = windows_local_user.this.id
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ require (
github.com/yuin/goldmark v1.6.0 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/grpc v1.60.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 h1:EDuYyU/MkFXllv9QF9819VlI9a4tzGuCbhG0ExK9o1U=
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down Expand Up @@ -260,15 +260,15 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions internal/provider/local/local_group_member_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package local

import (
"context"
"fmt"
"terraform-provider-windows/internal/generate/resource_local_group_member"

"github.com/d-strobel/gowindows"
"github.com/d-strobel/gowindows/windows/local"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ resource.Resource = (*localGroupMemberResource)(nil)

func NewLocalGroupMemberResource() resource.Resource {
return &localGroupMemberResource{}
}

type localGroupMemberResource struct {
client *gowindows.Client
}

func (r *localGroupMemberResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_group_member"
}

func (r *localGroupMemberResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = resource_local_group_member.LocalGroupMemberResourceSchema(ctx)
resp.Schema.Description = `Manage group member for local security groups.`
}

func (r *localGroupMemberResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*gowindows.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *gowindows.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

r.client = client
}

func (r *localGroupMemberResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data resource_local_group_member.LocalGroupMemberModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Create API call logic
params := local.GroupMemberCreateParams{
SID: data.GroupId.ValueString(),
Member: data.MemberId.ValueString(),
}

if err := r.client.Local.GroupMemberCreate(ctx, params); err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create local group member, got error: %s", err))
return
}

// Create the ID for the resource
data.Id = types.StringValue(fmt.Sprintf("%s/member/%s", data.GroupId.ValueString(), data.MemberId.ValueString()))

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *localGroupMemberResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data resource_local_group_member.LocalGroupMemberModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
params := local.GroupMemberReadParams{
SID: data.GroupId.ValueString(),
Member: data.MemberId.ValueString(),
}

if _, err := r.client.Local.GroupMemberRead(ctx, params); err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete local group member, got error: %s", err))
return
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *localGroupMemberResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Update is not needed in this resource
}

func (r *localGroupMemberResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data resource_local_group_member.LocalGroupMemberModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Delete API call logic
params := local.GroupMemberDeleteParams{
SID: data.GroupId.ValueString(),
Member: data.MemberId.ValueString(),
}

if err := r.client.Local.GroupMemberDelete(ctx, params); err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete local group member, got error: %s", err))
return
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (p *WindowsProvider) Resources(ctx context.Context) []func() resource.Resou
return []func() resource.Resource{
local.NewLocalGroupResource,
local.NewLocalUserResource,
local.NewLocalGroupMemberResource,
}
}

Expand Down
64 changes: 64 additions & 0 deletions internal/schema/local_resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,70 @@
}
]
}
},
{
"name": "local_group_member",
"schema": {
"attributes": [
{
"name": "group_id",
"string": {
"computed_optional_required": "required",
"description": "The ID of the local security group you want to add the member to. Changing this forces a new resource to be created.",
"plan_modifiers": [
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
}
],
"schema_definition": "stringplanmodifier.RequiresReplace()"
}
}
]
}
},
{
"name": "member_id",
"string": {
"computed_optional_required": "required",
"description": "The ID of the principal you want to add as a member to the group. Supported object types are local users or groups. Changing this forces a new resource to be created.",
"plan_modifiers": [
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
}
],
"schema_definition": "stringplanmodifier.RequiresReplace()"
}
}
]
}
},
{
"name": "id",
"string": {
"computed_optional_required": "computed",
"description": "The ID of this resource.",
"plan_modifiers": [
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
}
],
"schema_definition": "stringplanmodifier.UseStateForUnknown()"
}
}
]
}
}
]
}
}
]
}

0 comments on commit ce3df13

Please sign in to comment.