Skip to content

Commit b2f51f9

Browse files
authored
add import + delete support for organization users (#146)
1 parent c2971df commit b2f51f9

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

docs/resources/user_role.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ resource "openai_user_role" "example" {
2626

2727
- `role` (String) `owner` or `reader`.
2828
- `user_id` (String) The ID of the user.
29+
30+
## Import
31+
32+
Import is supported using the following syntax:
33+
34+
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
35+
36+
```shell
37+
# Import an organization user
38+
terraform import openai_user_role.example user-id
39+
40+
# Example
41+
terraform import openai_user_role.example user-000000000000000000000000
42+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Import an organization user
2+
terraform import openai_user_role.example user-id
3+
4+
# Example
5+
terraform import openai_user_role.example user-000000000000000000000000

internal/provider/resource_user_role.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
99
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/path"
1011
"github.com/hashicorp/terraform-plugin-framework/resource"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1213
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -151,5 +152,27 @@ func (r *UserRoleResource) Update(ctx context.Context, req resource.UpdateReques
151152
}
152153

153154
func (r *UserRoleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
154-
resp.Diagnostics.AddWarning("Delete not supported", "This resource does not support deletion.")
155+
var data UserRoleResourceModel
156+
157+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
158+
if resp.Diagnostics.HasError() {
159+
return
160+
}
161+
162+
httpResp, err := r.client.DeleteUserWithResponse(
163+
ctx,
164+
data.UserId.ValueString(),
165+
)
166+
167+
if err != nil {
168+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete, got error: %s", err))
169+
return
170+
} else if httpResp.StatusCode() != http.StatusOK {
171+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
172+
return
173+
}
174+
}
175+
176+
func (r *UserRoleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
177+
resource.ImportStatePassthroughID(ctx, path.Root("user_id"), req, resp)
155178
}

internal/provider/resource_user_role_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ func TestAccUserRoleResource(t *testing.T) {
1818
PreCheck: func() { acctest.PreCheck(t) },
1919
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
2020
Steps: []resource.TestStep{
21+
// Import existing user role
22+
{
23+
Config: testAccUserRoleResourceConfig(acctest.TestUserId, "owner"),
24+
ResourceName: rn,
25+
ImportState: true,
26+
ImportStateId: acctest.TestUserId,
27+
},
2128
{
2229
Config: testAccUserRoleResourceConfig(acctest.TestUserId, "owner"),
2330
ConfigStateChecks: []statecheck.StateCheck{
@@ -32,6 +39,10 @@ func TestAccUserRoleResource(t *testing.T) {
3239
statecheck.ExpectKnownValue(rn, tfjsonpath.New("role"), knownvalue.StringExact("reader")),
3340
},
3441
},
42+
{
43+
// Detach state to prevent deletion of user
44+
Config: testAccUserRoleDetachState(),
45+
},
3546
},
3647
})
3748
}
@@ -41,6 +52,22 @@ func testAccUserRoleResourceConfig(userId, role string) string {
4152
resource "openai_user_role" "test" {
4253
user_id = %[1]q
4354
role = %[2]q
55+
56+
lifecycle {
57+
prevent_destroy = true
58+
}
4459
}
4560
`, userId, role)
4661
}
62+
63+
func testAccUserRoleDetachState() string {
64+
return `
65+
removed {
66+
from = openai_user_role.test
67+
68+
lifecycle {
69+
destroy = false
70+
}
71+
}
72+
`
73+
}

0 commit comments

Comments
 (0)