mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
terraform/provider: resend a service account expiration only when it changes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f283b55063
commit
53dd95db12
4 files changed
+110
-7
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(`<UpdateServiceAccountResponse></UpdateServiceAccountResponse>`))
|
||||
case "GetServiceAccount":
|
||||
_, _ = w.Write([]byte(`<GetServiceAccountResponse><GetServiceAccountResult><ServiceAccount><ServiceAccountId>sa:alice:1</ServiceAccountId><ParentUser>alice</ParentUser><Description>new</Description><AccessKeyId>AK</AccessKeyId><Status>Active</Status><Expiration>2020-01-01T00:00:00Z</Expiration></ServiceAccount></GetServiceAccountResult></GetServiceAccountResponse>`))
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user