Skip to content

Commit 4d4d655

Browse files
ronaldgrnjianyuan
authored andcommitted
add import + delete support for organization users
1 parent b081bcd commit 4d4d655

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-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
}

0 commit comments

Comments
 (0)