diff --git a/terraform/provider/seaweedfs/client_iam_service_accounts.go b/terraform/provider/seaweedfs/client_iam_service_accounts.go
index 52b3d53d6d..20fe263218 100644
--- a/terraform/provider/seaweedfs/client_iam_service_accounts.go
+++ b/terraform/provider/seaweedfs/client_iam_service_accounts.go
@@ -48,14 +48,16 @@ func (c *iamClient) GetServiceAccount(ctx context.Context, id string) (iamServic
return response.Result.Account, nil
}
-func (c *iamClient) UpdateServiceAccount(ctx context.Context, id, status, description, expiration string) error {
+func (c *iamClient) UpdateServiceAccount(ctx context.Context, id, status, description string, expiration *string) error {
values := iamActionValues("UpdateServiceAccount")
values.Set("ServiceAccountId", id)
if status != "" {
values.Set("Status", status)
}
values.Set("Description", description)
- values.Set("Expiration", expiration)
+ if expiration != nil {
+ values.Set("Expiration", *expiration)
+ }
if err := c.doIAMAction(ctx, values, nil); err != nil {
return fmt.Errorf("update service account: %w", err)
}
diff --git a/terraform/provider/seaweedfs/resource_iam_service_account.go b/terraform/provider/seaweedfs/resource_iam_service_account.go
index fa9a1341ee..4fcdd793e2 100644
--- a/terraform/provider/seaweedfs/resource_iam_service_account.go
+++ b/terraform/provider/seaweedfs/resource_iam_service_account.go
@@ -135,7 +135,7 @@ func (r *iamServiceAccountResource) Create(ctx context.Context, req resource.Cre
return
}
if !plan.Status.IsNull() && plan.Status.ValueString() != "" && plan.Status.ValueString() != account.Status {
- if err := r.client.UpdateServiceAccount(ctx, account.ID, plan.Status.ValueString(), account.Description, expiration); err != nil {
+ if err := r.client.UpdateServiceAccount(ctx, account.ID, plan.Status.ValueString(), account.Description, nil); err != nil {
cleanupErr := r.client.DeleteServiceAccount(ctx, account.ID)
message := err.Error()
if cleanupErr != nil && !isNoSuchEntityError(cleanupErr) {
@@ -204,10 +204,14 @@ func (r *iamServiceAccountResource) Update(ctx context.Context, req resource.Upd
if plan.SecretAccessKey.IsUnknown() || plan.SecretAccessKey.IsNull() {
plan.SecretAccessKey = state.SecretAccessKey
}
- expiration, err := expirationToUnix(plan.Expiration.ValueString())
- if err != nil {
- resp.Diagnostics.AddError("Invalid service account expiration", err.Error())
- return
+ var expiration *string
+ if !sameExpirationInstant(plan.Expiration.ValueString(), state.Expiration.ValueString()) {
+ unix, err := expirationToUnix(plan.Expiration.ValueString())
+ if err != nil {
+ resp.Diagnostics.AddError("Invalid service account expiration", err.Error())
+ return
+ }
+ expiration = &unix
}
if plan.Status.ValueString() != "" && plan.Status.ValueString() != "Active" && plan.Status.ValueString() != "Inactive" {
resp.Diagnostics.AddError("Invalid service account status", "Status must be Active or Inactive.")
@@ -276,6 +280,15 @@ func (r *iamServiceAccountResource) setServiceAccountState(state *iamServiceAcco
}
}
+func sameExpirationInstant(a, b string) bool {
+ if a == b {
+ return true
+ }
+ ta, errA := time.Parse(time.RFC3339, a)
+ tb, errB := time.Parse(time.RFC3339, b)
+ return errA == nil && errB == nil && ta.Unix() == tb.Unix()
+}
+
func expirationToUnix(value string) (string, error) {
if value == "" {
return "", nil
diff --git a/terraform/provider/seaweedfs/resource_iam_service_account_test.go b/terraform/provider/seaweedfs/resource_iam_service_account_test.go
new file mode 100644
index 0000000000..b89c8e452b
--- /dev/null
+++ b/terraform/provider/seaweedfs/resource_iam_service_account_test.go
@@ -0,0 +1,73 @@
+package seaweedfs
+
+import (
+ "context"
+ "net/http"
+ "testing"
+
+ "github.com/hashicorp/terraform-plugin-framework/resource"
+ "github.com/hashicorp/terraform-plugin-go/tftypes"
+)
+
+func TestIAMServiceAccountUpdateOmitsUnchangedExpiration(t *testing.T) {
+ ctx := context.Background()
+ for _, tc := range []struct {
+ name string
+ planExpiration string
+ wantSent bool
+ wantValue string
+ }{
+ {name: "unchanged", planExpiration: "2020-01-01T00:00:00Z"},
+ {name: "same instant in another zone", planExpiration: "2020-01-01T01:00:00+01:00"},
+ {name: "cleared", planExpiration: "", wantSent: true, wantValue: ""},
+ {name: "changed", planExpiration: "2040-01-01T00:00:00Z", wantSent: true, wantValue: "2208988800"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ sent, sentValue := false, ""
+ client := testIAMClient(t, func(w http.ResponseWriter, r *http.Request) {
+ _ = r.ParseForm()
+ switch r.Form.Get("Action") {
+ case "UpdateServiceAccount":
+ _, sent = r.PostForm["Expiration"]
+ sentValue = r.PostForm.Get("Expiration")
+ _, _ = w.Write([]byte(``))
+ case "GetServiceAccount":
+ _, _ = w.Write([]byte(`sa:alice:1alicenewAKActive2020-01-01T00:00:00Z`))
+ default:
+ t.Errorf("unexpected action %s", r.Form.Get("Action"))
+ }
+ })
+ r := &iamServiceAccountResource{client: client, data: &providerData{}}
+ common := map[string]tftypes.Value{
+ "id": tftypes.NewValue(tftypes.String, "sa:alice:1"),
+ "service_account_id": tftypes.NewValue(tftypes.String, "sa:alice:1"),
+ "parent_user": tftypes.NewValue(tftypes.String, "alice"),
+ "status": tftypes.NewValue(tftypes.String, "Active"),
+ "access_key_id": tftypes.NewValue(tftypes.String, "AK"),
+ "secret_key": tftypes.NewValue(tftypes.String, "secret"),
+ }
+ stateValues := map[string]tftypes.Value{
+ "description": tftypes.NewValue(tftypes.String, "old"),
+ "expiration": tftypes.NewValue(tftypes.String, "2020-01-01T00:00:00Z"),
+ }
+ planValues := map[string]tftypes.Value{
+ "description": tftypes.NewValue(tftypes.String, "new"),
+ "expiration": tftypes.NewValue(tftypes.String, tc.planExpiration),
+ }
+ for k, v := range common {
+ stateValues[k] = v
+ planValues[k] = v
+ }
+ plan, _ := testResourcePlan(ctx, r, planValues)
+ state := testResourceState(ctx, r, stateValues)
+ resp := resource.UpdateResponse{State: state}
+ r.Update(ctx, resource.UpdateRequest{Plan: plan, State: state}, &resp)
+ if resp.Diagnostics.HasError() {
+ t.Fatalf("update: %v", resp.Diagnostics)
+ }
+ if sent != tc.wantSent || sentValue != tc.wantValue {
+ t.Fatalf("Expiration sent=%v value=%q, want sent=%v value=%q", sent, sentValue, tc.wantSent, tc.wantValue)
+ }
+ })
+ }
+}
diff --git a/terraform/provider/seaweedfs/resource_test_helpers_test.go b/terraform/provider/seaweedfs/resource_test_helpers_test.go
index f62c62809c..2f227bb352 100644
--- a/terraform/provider/seaweedfs/resource_test_helpers_test.go
+++ b/terraform/provider/seaweedfs/resource_test_helpers_test.go
@@ -27,6 +27,21 @@ func testResourcePlan(ctx context.Context, r resource.Resource, values map[strin
tfsdk.State{Schema: schemaResp.Schema, Raw: tftypes.NewValue(objectType, nil)}
}
+func testResourceState(ctx context.Context, r resource.Resource, values map[string]tftypes.Value) tfsdk.State {
+ var schemaResp resource.SchemaResponse
+ r.Schema(ctx, resource.SchemaRequest{}, &schemaResp)
+ objectType := schemaResp.Schema.Type().TerraformType(ctx).(tftypes.Object)
+ all := map[string]tftypes.Value{}
+ for name, attrType := range objectType.AttributeTypes {
+ if value, ok := values[name]; ok {
+ all[name] = value
+ continue
+ }
+ all[name] = tftypes.NewValue(attrType, nil)
+ }
+ return tfsdk.State{Schema: schemaResp.Schema, Raw: tftypes.NewValue(objectType, all)}
+}
+
func testIAMClient(t *testing.T, handler http.HandlerFunc) *iamClient {
t.Helper()
srv := httptest.NewServer(handler)