From b23b8f4f191c06943e326ecf52c13fef03f464c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Wed, 25 Jan 2023 13:32:11 -0300 Subject: [PATCH] Add support to check error of deploy event --- docs/resources/app_deploy.md | 4 +- .../provider/resource_tsuru_app_deploy.go | 37 +++++++++++- .../resource_tsuru_app_deploy_test.go | 58 ++++++++++++++++++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/docs/resources/app_deploy.md b/docs/resources/app_deploy.md index ecb5047..6d369d7 100644 --- a/docs/resources/app_deploy.md +++ b/docs/resources/app_deploy.md @@ -3,12 +3,12 @@ page_title: "tsuru_app_deploy Resource - terraform-provider-tsuru" subcategory: "" description: |- - Do a deployment for an application, that currently only supports deploys via prebuilt docker images, to do deploys via tsuru platforms please use tsuru-client + Perform an application deploy. Currently, only supporting deploys via prebuilt container images; in order to deploy via tsuru platforms please use tsuru-client --- # tsuru_app_deploy (Resource) -Do a deployment for an application, that currently only supports deploys via prebuilt docker images, to do deploys via tsuru platforms please use tsuru-client +Perform an application deploy. Currently, only supporting deploys via prebuilt container images; in order to deploy via tsuru platforms please use tsuru-client ## Example Usage diff --git a/internal/provider/resource_tsuru_app_deploy.go b/internal/provider/resource_tsuru_app_deploy.go index 26b1843..e970164 100644 --- a/internal/provider/resource_tsuru_app_deploy.go +++ b/internal/provider/resource_tsuru_app_deploy.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "encoding/base64" + "errors" "fmt" "io" "log" @@ -26,7 +27,7 @@ import ( func resourceTsuruApplicationDeploy() *schema.Resource { return &schema.Resource{ - Description: "Do a deployment for an application, that currently only supports deploys via prebuilt docker images, to do deploys via tsuru platforms please use tsuru-client", + Description: "Perform an application deploy. Currently, only supporting deploys via prebuilt container images; in order to deploy via tsuru platforms please use tsuru-client", CreateContext: resourceTsuruApplicationDeployDo, UpdateContext: resourceTsuruApplicationDeployDo, ReadContext: resourceTsuruApplicationDeployRead, @@ -146,11 +147,45 @@ func resourceTsuruApplicationDeployDo(ctx context.Context, d *schema.ResourceDat if err := scanner.Err(); err != nil { log.Fatal("[ERROR]", err) } + + err = waitForEventComplete(ctx, provider, eventID) + if err != nil { + return diag.FromErr(err) + } } return resourceTsuruApplicationDeployRead(ctx, d, meta) } +func waitForEventComplete(ctx context.Context, provider *tsuruProvider, eventID string) error { + deadline := time.Now().UTC().Add(time.Minute * 2) + + for { + e, _, err := provider.TsuruClient.EventApi.EventInfo(ctx, eventID) + + if err != nil { + return err + } + + if e.Running { + log.Println("[DEBUG] event is still running, pooling in the next 20 seconds") + time.Sleep(time.Second * 20) + continue + } + + if e.Error != "" { + return errors.New(e.Error + ", see details of event ID: " + eventID) + } + + if time.Now().UTC().After(deadline) { + return errors.New("event is still running after deploy") + } + + return nil + } + +} + func resourceTsuruApplicationDeployRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { provider := meta.(*tsuruProvider) diff --git a/internal/provider/resource_tsuru_app_deploy_test.go b/internal/provider/resource_tsuru_app_deploy_test.go index c334512..3f2db8b 100644 --- a/internal/provider/resource_tsuru_app_deploy_test.go +++ b/internal/provider/resource_tsuru_app_deploy_test.go @@ -10,6 +10,7 @@ import ( "net/http/httptest" "net/url" "os" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -38,9 +39,13 @@ func TestAccResourceTsuruAppDeploy(t *testing.T) { return c.String(http.StatusOK, "OK") }) + iterationCount := 0 fakeServer.GET("/1.1/events/:eventID", func(c echo.Context) error { + iterationCount++ + return c.JSON(http.StatusOK, map[string]interface{}{ - "Running": true, + "Running": iterationCount < 2, + "EndTime": "2023-01-04T19:26:20.946Z", "EndCustomData": map[string]interface{}{ "Kind": 3, "Data": "GwAAAAJpbWFnZQALAAAAdGVzdDoxLjIuMwAA", @@ -66,7 +71,7 @@ func TestAccResourceTsuruAppDeploy(t *testing.T) { testAccResourceExists(resourceName), resource.TestCheckResourceAttr(resourceName, "app", "app01"), resource.TestCheckResourceAttr(resourceName, "image", "myrepo/app01:0.1.0"), - resource.TestCheckResourceAttr(resourceName, "status", "running"), + resource.TestCheckResourceAttr(resourceName, "status", "finished"), resource.TestCheckResourceAttr(resourceName, "output_image", "test:1.2.3"), ), }, @@ -74,6 +79,55 @@ func TestAccResourceTsuruAppDeploy(t *testing.T) { }) } +func TestAccResourceTsuruAppDeployFailed(t *testing.T) { + fakeServer := echo.New() + + fakeServer.POST("/1.0/apps/:app/deploy", func(c echo.Context) error { + c.Response().Header().Set("X-Tsuru-Eventid", "abc-123") + + formParams, err := c.FormParams() + if err != nil { + return err + } + assert.Equal(t, url.Values{ + "image": {"myrepo/app01:0.1.0"}, + "message": {"deploy via terraform"}, + "new-version": {"false"}, + "origin": {"image"}, + "override-versions": {"false"}}, + formParams) + + return c.String(http.StatusOK, "OK") + }) + + fakeServer.GET("/1.1/events/:eventID", func(c echo.Context) error { + + return c.JSON(http.StatusOK, map[string]interface{}{ + "Running": false, + "Error": "deploy failed", + "EndTime": "2023-01-04T19:26:20.946Z", + }) + }) + + fakeServer.HTTPErrorHandler = func(err error, c echo.Context) { + t.Errorf("methods=%s, path=%s, err=%s", c.Request().Method, c.Path(), err.Error()) + } + server := httptest.NewServer(fakeServer) + os.Setenv("TSURU_TARGET", server.URL) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + Config: testAccResourceTsuruAppDeploy_basic(server.URL), + ExpectError: regexp.MustCompile("deploy failed, see details of event ID: abc-123"), + }, + }, + }) +} + func testAccResourceTsuruAppDeploy_basic(serverURL string) string { return fmt.Sprintf(`