From e2e6ac025991e58b10b0ce6dbe85c2fb93f7dbf1 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Mon, 10 Mar 2025 16:19:13 +0100 Subject: [PATCH] fix: bulk update after deploying multiple functions --- internal/functions/deploy/deploy_test.go | 4 +++ pkg/function/batch.go | 46 +++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/internal/functions/deploy/deploy_test.go b/internal/functions/deploy/deploy_test.go index 9a5b99785..9510d4c73 100644 --- a/internal/functions/deploy/deploy_test.go +++ b/internal/functions/deploy/deploy_test.go @@ -54,6 +54,10 @@ func TestDeployCommand(t *testing.T) { apitest.MockDockerStart(utils.Docker, imageUrl, containerId) require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "bundled")) } + gock.New(utils.DefaultApiHost). + Put("/v1/projects/" + flags.ProjectRef + "/functions"). + Reply(http.StatusOK). + JSON(api.BulkUpdateFunctionResponse{}) // Setup output file for _, v := range functions { outputDir := filepath.Join(utils.TempDir, fmt.Sprintf(".output_%s", v)) diff --git a/pkg/function/batch.go b/pkg/function/batch.go index e5400efbc..2d38ec63b 100644 --- a/pkg/function/batch.go +++ b/pkg/function/batch.go @@ -34,6 +34,7 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con for _, f := range result { exists[f.Slug] = struct{}{} } + toUpdate := map[string]api.BulkUpdateFunctionBody{} OUTER: for slug, function := range functionConfig { if !function.Enabled { @@ -52,27 +53,53 @@ OUTER: // Update if function already exists upsert := func() error { if _, ok := exists[slug]; ok { - if resp, err := s.client.V1UpdateAFunctionWithBodyWithResponse(ctx, s.project, slug, &api.V1UpdateAFunctionParams{ + resp, err := s.client.V1UpdateAFunctionWithBodyWithResponse(ctx, s.project, slug, &api.V1UpdateAFunctionParams{ VerifyJwt: &function.VerifyJWT, ImportMapPath: toFileURL(function.ImportMap), EntrypointPath: toFileURL(function.Entrypoint), - }, eszipContentType, bytes.NewReader(body.Bytes())); err != nil { + }, eszipContentType, bytes.NewReader(body.Bytes())) + if err != nil { return errors.Errorf("failed to update function: %w", err) } else if resp.JSON200 == nil { return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body)) } + toUpdate[slug] = api.BulkUpdateFunctionBody{ + Id: resp.JSON200.Id, + Name: resp.JSON200.Name, + Slug: resp.JSON200.Slug, + Version: resp.JSON200.Version, + EntrypointPath: resp.JSON200.EntrypointPath, + ImportMap: resp.JSON200.ImportMap, + ImportMapPath: resp.JSON200.ImportMapPath, + VerifyJwt: resp.JSON200.VerifyJwt, + Status: api.BulkUpdateFunctionBodyStatus(resp.JSON200.Status), + CreatedAt: &resp.JSON200.CreatedAt, + } } else { - if resp, err := s.client.V1CreateAFunctionWithBodyWithResponse(ctx, s.project, &api.V1CreateAFunctionParams{ + resp, err := s.client.V1CreateAFunctionWithBodyWithResponse(ctx, s.project, &api.V1CreateAFunctionParams{ Slug: &slug, Name: &slug, VerifyJwt: &function.VerifyJWT, ImportMapPath: toFileURL(function.ImportMap), EntrypointPath: toFileURL(function.Entrypoint), - }, eszipContentType, bytes.NewReader(body.Bytes())); err != nil { + }, eszipContentType, bytes.NewReader(body.Bytes())) + if err != nil { return errors.Errorf("failed to create function: %w", err) } else if resp.JSON201 == nil { return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body)) } + toUpdate[slug] = api.BulkUpdateFunctionBody{ + Id: resp.JSON201.Id, + Name: resp.JSON201.Name, + Slug: resp.JSON201.Slug, + Version: resp.JSON201.Version, + EntrypointPath: resp.JSON201.EntrypointPath, + ImportMap: resp.JSON201.ImportMap, + ImportMapPath: resp.JSON201.ImportMapPath, + VerifyJwt: resp.JSON201.VerifyJwt, + Status: api.BulkUpdateFunctionBodyStatus(resp.JSON201.Status), + CreatedAt: &resp.JSON201.CreatedAt, + } } return nil } @@ -83,6 +110,17 @@ OUTER: return err } } + if len(toUpdate) > 1 { + body := make([]api.BulkUpdateFunctionBody, len(toUpdate)) + for _, b := range toUpdate { + body = append(body, b) + } + if resp, err := s.client.V1BulkUpdateFunctionsWithResponse(ctx, s.project, body); err != nil { + return errors.Errorf("failed to bulk update: %w", err) + } else if resp.JSON200 == nil { + return errors.Errorf("unexpected bulk update status %d: %s", resp.StatusCode(), string(resp.Body)) + } + } return nil }