Skip to content

Commit

Permalink
Add support to check error of deploy event
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 25, 2023
1 parent df44c6b commit b23b8f4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/resources/app_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 36 additions & 1 deletion internal/provider/resource_tsuru_app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
58 changes: 56 additions & 2 deletions internal/provider/resource_tsuru_app_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -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",
Expand All @@ -66,14 +71,63 @@ 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"),
),
},
},
})
}

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(`
Expand Down

0 comments on commit b23b8f4

Please sign in to comment.