0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00

fix(access): user token expiration_date handling (#1293)

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2024-05-14 18:24:13 -04:00 committed by GitHub
parent 73d01e7543
commit a436c24923
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 19 deletions

View File

@ -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

View File

@ -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))
}

View File

@ -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,