Skip to content

Commit

Permalink
Add readiness gates support for router configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Dec 13, 2021
1 parent a923332 commit 00b5bed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
58 changes: 38 additions & 20 deletions internal/provider/resource_tsuru_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand Down
7 changes: 5 additions & 2 deletions internal/provider/resource_tsuru_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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{}{
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 00b5bed

Please sign in to comment.