Skip to content

Commit

Permalink
feat(): add missing resources + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Skraye committed Dec 19, 2024
1 parent 6188d3b commit 6f69e3f
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/resources/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ EOT
- `description` (String) The namespace friendly description.
- `plugin_defaults` (String) The namespace plugin defaults in yaml string.
- `variables` (String) The namespace variables in yaml string.
- `allowed_namespaces` (List of Object) The allowed namespaces.
- `namespace` (String) The allowed namespace.
- `storage_type` (String) The storage type.
- `storage_configuration` (Map of String) The storage configuration.
- `secret_type` (String) The secret type.
- `secret_configuration` (Map of String) The secret configuration.

### Read-Only

Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func New(version string, tenant *string) func() *schema.Provider {
"kestra_user_api_token": resourceUserApiToken(),
"kestra_kv": resourceKv(),
"kestra_worker_group": resourceWorkerGroup(),
"kestra_app": resourceApp(),
"kestra_dashboard": resourceDashboard(),
},
}

Expand Down
109 changes: 109 additions & 0 deletions internal/provider/resource_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package provider

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/http"
)

func resourceApp() *schema.Resource {
return &schema.Resource{
Description: "Manages an App resource.",

CreateContext: resourceAppCreate,
ReadContext: resourceAppRead,
UpdateContext: resourceAppUpdate,
DeleteContext: resourceAppDelete,
Schema: map[string]*schema.Schema{
"source": {
Description: "The source text.",
Type: schema.TypeString,
Required: true,
},
"uid": {
Description: "The unique identifier.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAppCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

source := d.Get("source").(string)

req, reqErr := c.yamlRequest("POST", fmt.Sprintf("%s/apps", apiRoot(c.TenantId)), &source)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

d.SetId(req.(map[string]interface{})["uid"].(string))
return diags
}

func resourceAppRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

id := d.Id()
url := fmt.Sprintf("%s/apps/%s", apiRoot(c.TenantId), id)

req, reqErr := c.yamlRequest("GET", url, nil)
if reqErr != nil {
if reqErr.StatusCode == http.StatusNotFound {
d.SetId("")
return diags
}
return diag.FromErr(reqErr.Err)
}

response := req.(map[string]interface{})
if err := d.Set("source", response["source"].(string)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("uid", response["uid"].(string)); err != nil {
return diag.FromErr(err)
}

return diags
}

func resourceAppUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

if d.HasChanges("source") {
uid := d.Id()
source := d.Get("source").(string)
url := fmt.Sprintf("%s/apps/%s", apiRoot(c.TenantId), uid)

_, reqErr := c.yamlRequest("PUT", url, &source)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

return diags
}
return resourceAppRead(ctx, d, meta)
}

func resourceAppDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

uid := d.Id()
url := fmt.Sprintf("%s/apps/%s", apiRoot(c.TenantId), uid)

_, reqErr := c.request("DELETE", url, nil)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

d.SetId("")
return diags
}
60 changes: 60 additions & 0 deletions internal/provider/resource_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourceApp(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceApp("new"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("kestra_app.new", "id"),
),
},
},
})
}

func testAccResourceApp(resourceId string) string {
return fmt.Sprintf(
`
resource "kestra_flow" "new_flow" {
namespace = "company.team"
flow_id = "get_data"
content = <<EOT
id: get_data
namespace: company.team
tasks:
- id: hello
type: io.kestra.plugin.core.log.Log
message: Hello World! 🚀
EOT
}
resource "kestra_app" "%s" {
source = <<-EOF
id: test_tf
type: io.kestra.plugin.ee.apps.Execution
displayName: New display name
namespace: company.team
flowId: get_data
access: PRIVATE
layout:
- on: OPEN
blocks:
- type: io.kestra.plugin.ee.apps.core.blocks.Markdown
content: |
## Request data
Select the dataset you want to download.
EOF
}`,
resourceId)
}
110 changes: 110 additions & 0 deletions internal/provider/resource_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package provider

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/http"
)

func resourceDashboard() *schema.Resource {
return &schema.Resource{
Description: "Manages a Dashboard resource.",

CreateContext: resourceDashboardCreate,
ReadContext: resourceDashboardRead,
UpdateContext: resourceDashboardUpdate,
DeleteContext: resourceDashboardDelete,
Schema: map[string]*schema.Schema{
"source_code": {
Description: "The source code text.",
Type: schema.TypeString,
Required: true,
},
"id": {
Description: "The unique identifier.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceDashboardCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

sourceCode := d.Get("source_code").(string)

req, reqErr := c.yamlRequest("POST", fmt.Sprintf("%s/dashboards", apiRoot(c.TenantId)), &sourceCode)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

d.SetId(req.(map[string]interface{})["id"].(string))

return diags
}

func resourceDashboardRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

id := d.Id()
url := fmt.Sprintf("%s/dashboards/%s", apiRoot(c.TenantId), id)

req, reqErr := c.yamlRequest("GET", url, nil)
if reqErr != nil {
if reqErr.StatusCode == http.StatusNotFound {
d.SetId("")
return diags
}
return diag.FromErr(reqErr.Err)
}

response := req.(map[string]interface{})
if err := d.Set("source_code", response["sourceCode"].(string)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("id", response["id"].(string)); err != nil {
return diag.FromErr(err)
}

return diags
}

func resourceDashboardUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

if d.HasChanges("source_code") {
id := d.Id()
sourceCode := d.Get("source_code").(string)
url := fmt.Sprintf("%s/dashboards/%s", apiRoot(c.TenantId), id)

_, reqErr := c.yamlRequest("PUT", url, &sourceCode)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

return diags
}
return resourceDashboardRead(ctx, d, meta)
}

func resourceDashboardDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics

id := d.Id()
url := fmt.Sprintf("%s/dashboards/%s", apiRoot(c.TenantId), id)

_, reqErr := c.request("DELETE", url, nil)
if reqErr != nil {
return diag.FromErr(reqErr.Err)
}

d.SetId("")
return diags
}
57 changes: 57 additions & 0 deletions internal/provider/resource_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourceDashboard(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceDashboard("new"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("kestra_dashboard.new", "id"),
),
},
},
})
}

func testAccResourceDashboard(resourceId string) string {
return fmt.Sprintf(
`
resource "kestra_dashboard" "%s" {
source_code = <<-EOF
title: Overview_test
charts:
- id: executions_timeseries
type: io.kestra.plugin.core.dashboard.chart.TimeSeries
chartOptions:
displayName: Executions
description: Executions duration and count per date
legend:
enabled: true
column: date
colorByColumn: state
data:
type: io.kestra.plugin.core.dashboard.data.Executions
columns:
date:
field: START_DATE
displayName: Date
state:
field: STATE
total:
displayName: Executions
agg: COUNT
graphStyle: BARS
EOF
}`,
resourceId)
}

0 comments on commit 6f69e3f

Please sign in to comment.