From 00b5bed346996cfc19bacb704ae481d4320ab269 Mon Sep 17 00:00:00 2001 From: Cezar Sa Espinola Date: Mon, 13 Dec 2021 19:12:39 -0300 Subject: [PATCH] Add readiness gates support for router configuration --- internal/provider/resource_tsuru_router.go | 58 ++++++++++++------- .../provider/resource_tsuru_router_test.go | 7 ++- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/internal/provider/resource_tsuru_router.go b/internal/provider/resource_tsuru_router.go index 83bcf4f..ee72c19 100644 --- a/internal/provider/resource_tsuru_router.go +++ b/internal/provider/resource_tsuru_router.go @@ -30,6 +30,14 @@ func resourceTsuruRouter() *schema.Resource { Type: schema.TypeString, Required: true, }, + "readiness_gates": { + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Description: "List of readiness gates associated with this router", + }, "config": { Type: schema.TypeString, Description: "Configuration for router in YAML format", @@ -39,26 +47,39 @@ func resourceTsuruRouter() *schema.Resource { } } -func resourceTsuruRouterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - provider := meta.(*tsuruProvider) - name := d.Get("name").(string) - +func routerFromResourceData(d *schema.ResourceData) (tsuru.DynamicRouter, diag.Diagnostics) { config, err := parseRouterConfig(d.Get("config")) if err != nil { - return diag.Errorf("Could not decode config, err : %s", err.Error()) + return tsuru.DynamicRouter{}, diag.Errorf("Could not decode config, err : %s", err.Error()) + } + + readinessGates := []string{} + for _, item := range d.Get("readiness_gates").([]interface{}) { + readinessGates = append(readinessGates, item.(string)) } - _, err = provider.TsuruClient.RouterApi.RouterCreate(ctx, tsuru.DynamicRouter{ - Name: name, - Type: d.Get("type").(string), - Config: config, - }) + return tsuru.DynamicRouter{ + Name: d.Get("name").(string), + Type: d.Get("type").(string), + ReadinessGates: readinessGates, + Config: config, + }, nil +} + +func resourceTsuruRouterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + provider := meta.(*tsuruProvider) + + router, dg := routerFromResourceData(d) + if dg != nil { + return dg + } + _, err := provider.TsuruClient.RouterApi.RouterCreate(ctx, router) if err != nil { return diag.Errorf("Could not create tsuru router, err : %s", err.Error()) } - d.SetId(name) + d.SetId(router.Name) return nil } @@ -81,6 +102,7 @@ func resourceTsuruRouterRead(ctx context.Context, d *schema.ResourceData, meta i } d.Set("name", router.Name) d.Set("type", router.Type) + d.Set("readiness_gates", router.ReadinessGates) config, err := parseRouterConfig(d.Get("config")) if err != nil { @@ -103,17 +125,13 @@ func resourceTsuruRouterRead(ctx context.Context, d *schema.ResourceData, meta i func resourceTsuruRouterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { provider := meta.(*tsuruProvider) - config, err := parseRouterConfig(d.Get("config")) - if err != nil { - return diag.Errorf("Could not decode config, err : %s", err.Error()) - } - _, err = provider.TsuruClient.RouterApi.RouterUpdate(ctx, d.Id(), tsuru.DynamicRouter{ - Name: d.Get("name").(string), - Type: d.Get("type").(string), - Config: config, - }) + router, dg := routerFromResourceData(d) + if dg != nil { + return dg + } + _, err := provider.TsuruClient.RouterApi.RouterUpdate(ctx, d.Id(), router) if err != nil { return diag.Errorf("Could not update tsuru router: %q, err: %s", d.Id(), err.Error()) } diff --git a/internal/provider/resource_tsuru_router_test.go b/internal/provider/resource_tsuru_router_test.go index ed72646..3b3b37b 100644 --- a/internal/provider/resource_tsuru_router_test.go +++ b/internal/provider/resource_tsuru_router_test.go @@ -23,6 +23,7 @@ func TestAccTsuruRouter_basic(t *testing.T) { require.NoError(t, err) assert.Equal(t, "test_router", p.Name) assert.Equal(t, "router", p.Type) + assert.Equal(t, []string{"gate1", "gate2"}, p.ReadinessGates) assert.Equal(t, map[string]interface{}{ "url": "testing", "headers": map[string]interface{}{ @@ -35,8 +36,9 @@ func TestAccTsuruRouter_basic(t *testing.T) { fakeServer.GET("/1.3/routers", func(c echo.Context) error { return c.JSON(http.StatusOK, []*tsuru.DynamicRouter{ { - Name: "test_router", - Type: "router", + Name: "test_router", + Type: "router", + ReadinessGates: []string{"gate1", "gate2"}, Config: map[string]interface{}{ "url": "testing", "headers": map[string]interface{}{ @@ -81,6 +83,7 @@ func testAccTsuruRouterConfig_basic(fakeServer, name string) string { resource "tsuru_router" "test_router" { name = "%s" type = "router" + readiness_gates = ["gate1", "gate2"] config = <<-EOT url: "testing"