diff --git a/README.md b/README.md index 195f9733..18cd1d44 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ See [CONTRIBUTORS.md](CONTRIBUTORS.md) for a list of contributors to this projec - [Radosław Szamszur](https://github.com/rszamszur) - [Ben Bouillet](https://github.com/benbouillet) -Thanks again for your support, it is much appreciated! 🙏 +Thanks again for your continuous support, it is much appreciated! 🙏 ## Acknowledgements diff --git a/fwprovider/access/resource_user_token.go b/fwprovider/access/resource_user_token.go index 331e0e5f..7813ec6d 100644 --- a/fwprovider/access/resource_user_token.go +++ b/fwprovider/access/resource_user_token.go @@ -192,7 +192,7 @@ func (r *userTokenResource) Read(ctx context.Context, req resource.ReadRequest, state.Comment = types.StringPointerValue(data.Comment) - if data.ExpirationDate != nil { + if data.ExpirationDate != nil && *data.ExpirationDate > 0 { dt := time.Unix(int64(*data.ExpirationDate), 0).UTC().Format(time.RFC3339) state.ExpirationDate = types.StringValue(dt) } @@ -214,11 +214,13 @@ func (r *userTokenResource) Update(ctx context.Context, req resource.UpdateReque } body := access.UserTokenUpdateRequestBody{ + // note: PVE API does not support resetting comment to empty string Comment: plan.Comment.ValueStringPointer(), PrivSeparate: proxmoxtypes.CustomBoolPtr(plan.PrivSeparation.ValueBoolPointer()), } if !plan.ExpirationDate.IsNull() && plan.ExpirationDate.ValueString() != "" { + // if planned value is not empty then set it expirationDate, err := time.Parse( time.RFC3339, plan.ExpirationDate.ValueString(), @@ -230,6 +232,9 @@ func (r *userTokenResource) Update(ctx context.Context, req resource.UpdateReque v := expirationDate.Unix() body.ExpirationDate = &v + } else if !state.ExpirationDate.IsNull() { + // if planned value is empty, but the current value is not then reset it + body.ExpirationDate = new(int64) } err := r.client.Access().UpdateUserToken(ctx, plan.UserID.ValueString(), plan.TokenName.ValueString(), &body) @@ -297,7 +302,7 @@ func (r *userTokenResource) ImportState( Value: types.StringNull(), } - if data.ExpirationDate != nil { + if data.ExpirationDate != nil && *data.ExpirationDate > 0 { state.ExpirationDate = types.StringValue(time.Unix(int64(*data.ExpirationDate), 0).UTC().Format(time.RFC3339)) } diff --git a/fwprovider/tests/resource_user_test.go b/fwprovider/tests/resource_user_test.go index 3a596d41..b9db975d 100644 --- a/fwprovider/tests/resource_user_test.go +++ b/fwprovider/tests/resource_user_test.go @@ -119,27 +119,25 @@ func TestAccResourceUserToken(t *testing.T) { []resource.TestStep{ { Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { - comment = "Managed by Terraform" - expiration_date = "2034-01-01T22:00:00Z" - token_name = "{{.TokenName}}" - user_id = "{{.UserID}}" - }`), + comment = "Managed by Terraform" + token_name = "{{.TokenName}}" + user_id = "{{.UserID}}" + }`), Check: testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ - "comment": "Managed by Terraform", - "expiration_date": "2034-01-01T22:00:00Z", - "id": fmt.Sprintf("%s!%s", userID, tokenName), - "user_id": userID, - "value": fmt.Sprintf("%s!%s=.*", userID, tokenName), + "comment": "Managed by Terraform", + "id": fmt.Sprintf("%s!%s", userID, tokenName), + "user_id": userID, + "value": fmt.Sprintf("%s!%s=.*", userID, tokenName), }), }, { Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { - comment = "Managed by Terraform 2" - expiration_date = "2033-01-01T01:01:01Z" - privileges_separation = false - token_name = "{{.TokenName}}" - user_id = "{{.UserID}}" - }`), + comment = "Managed by Terraform 2" + expiration_date = "2033-01-01T01:01:01Z" + privileges_separation = false + token_name = "{{.TokenName}}" + user_id = "{{.UserID}}" + }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ "comment": "Managed by Terraform 2", @@ -153,6 +151,26 @@ func TestAccResourceUserToken(t *testing.T) { }), ), }, + { + Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { + comment = "Managed by Terraform 2" + privileges_separation = false + token_name = "{{.TokenName}}" + user_id = "{{.UserID}}" + }`), + Check: resource.ComposeTestCheckFunc( + testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ + "comment": "Managed by Terraform 2", + "privileges_separation": "false", + "token_name": tokenName, + "user_id": userID, + }), + testNoResourceAttributesSet("proxmox_virtual_environment_user_token.user_token", []string{ + "expiration_date", + "value", + }), + ), + }, { ResourceName: "proxmox_virtual_environment_user_token.user_token", ImportState: true,