mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-01 19:12:59 +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:
parent
73d01e7543
commit
a436c24923
@ -141,7 +141,7 @@ See [CONTRIBUTORS.md](CONTRIBUTORS.md) for a list of contributors to this projec
|
|||||||
- [Radosław Szamszur](https://github.com/rszamszur)
|
- [Radosław Szamszur](https://github.com/rszamszur)
|
||||||
- [Ben Bouillet](https://github.com/benbouillet)
|
- [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
|
## Acknowledgements
|
||||||
|
@ -192,7 +192,7 @@ func (r *userTokenResource) Read(ctx context.Context, req resource.ReadRequest,
|
|||||||
|
|
||||||
state.Comment = types.StringPointerValue(data.Comment)
|
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)
|
dt := time.Unix(int64(*data.ExpirationDate), 0).UTC().Format(time.RFC3339)
|
||||||
state.ExpirationDate = types.StringValue(dt)
|
state.ExpirationDate = types.StringValue(dt)
|
||||||
}
|
}
|
||||||
@ -214,11 +214,13 @@ func (r *userTokenResource) Update(ctx context.Context, req resource.UpdateReque
|
|||||||
}
|
}
|
||||||
|
|
||||||
body := access.UserTokenUpdateRequestBody{
|
body := access.UserTokenUpdateRequestBody{
|
||||||
|
// note: PVE API does not support resetting comment to empty string
|
||||||
Comment: plan.Comment.ValueStringPointer(),
|
Comment: plan.Comment.ValueStringPointer(),
|
||||||
PrivSeparate: proxmoxtypes.CustomBoolPtr(plan.PrivSeparation.ValueBoolPointer()),
|
PrivSeparate: proxmoxtypes.CustomBoolPtr(plan.PrivSeparation.ValueBoolPointer()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if !plan.ExpirationDate.IsNull() && plan.ExpirationDate.ValueString() != "" {
|
if !plan.ExpirationDate.IsNull() && plan.ExpirationDate.ValueString() != "" {
|
||||||
|
// if planned value is not empty then set it
|
||||||
expirationDate, err := time.Parse(
|
expirationDate, err := time.Parse(
|
||||||
time.RFC3339,
|
time.RFC3339,
|
||||||
plan.ExpirationDate.ValueString(),
|
plan.ExpirationDate.ValueString(),
|
||||||
@ -230,6 +232,9 @@ func (r *userTokenResource) Update(ctx context.Context, req resource.UpdateReque
|
|||||||
|
|
||||||
v := expirationDate.Unix()
|
v := expirationDate.Unix()
|
||||||
body.ExpirationDate = &v
|
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)
|
err := r.client.Access().UpdateUserToken(ctx, plan.UserID.ValueString(), plan.TokenName.ValueString(), &body)
|
||||||
@ -297,7 +302,7 @@ func (r *userTokenResource) ImportState(
|
|||||||
Value: types.StringNull(),
|
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))
|
state.ExpirationDate = types.StringValue(time.Unix(int64(*data.ExpirationDate), 0).UTC().Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,27 +119,25 @@ func TestAccResourceUserToken(t *testing.T) {
|
|||||||
[]resource.TestStep{
|
[]resource.TestStep{
|
||||||
{
|
{
|
||||||
Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" {
|
Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" {
|
||||||
comment = "Managed by Terraform"
|
comment = "Managed by Terraform"
|
||||||
expiration_date = "2034-01-01T22:00:00Z"
|
token_name = "{{.TokenName}}"
|
||||||
token_name = "{{.TokenName}}"
|
user_id = "{{.UserID}}"
|
||||||
user_id = "{{.UserID}}"
|
}`),
|
||||||
}`),
|
|
||||||
Check: testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{
|
Check: testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{
|
||||||
"comment": "Managed by Terraform",
|
"comment": "Managed by Terraform",
|
||||||
"expiration_date": "2034-01-01T22:00:00Z",
|
"id": fmt.Sprintf("%s!%s", userID, tokenName),
|
||||||
"id": fmt.Sprintf("%s!%s", userID, tokenName),
|
"user_id": userID,
|
||||||
"user_id": userID,
|
"value": fmt.Sprintf("%s!%s=.*", userID, tokenName),
|
||||||
"value": fmt.Sprintf("%s!%s=.*", userID, tokenName),
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" {
|
Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" {
|
||||||
comment = "Managed by Terraform 2"
|
comment = "Managed by Terraform 2"
|
||||||
expiration_date = "2033-01-01T01:01:01Z"
|
expiration_date = "2033-01-01T01:01:01Z"
|
||||||
privileges_separation = false
|
privileges_separation = false
|
||||||
token_name = "{{.TokenName}}"
|
token_name = "{{.TokenName}}"
|
||||||
user_id = "{{.UserID}}"
|
user_id = "{{.UserID}}"
|
||||||
}`),
|
}`),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{
|
testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{
|
||||||
"comment": "Managed by Terraform 2",
|
"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",
|
ResourceName: "proxmox_virtual_environment_user_token.user_token",
|
||||||
ImportState: true,
|
ImportState: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user