From 6f69e3f24e84e1075fc49380257574ba2704e5df Mon Sep 17 00:00:00 2001 From: YannC Date: Thu, 19 Dec 2024 15:19:47 +0100 Subject: [PATCH] feat(): add missing resources + docs --- docs/resources/namespace.md | 6 + internal/provider/provider.go | 2 + internal/provider/resource_app.go | 109 ++++++++++++++++++ internal/provider/resource_app_test.go | 60 ++++++++++ internal/provider/resource_dashboard.go | 110 +++++++++++++++++++ internal/provider/resource_dashboard_test.go | 57 ++++++++++ 6 files changed, 344 insertions(+) create mode 100644 internal/provider/resource_app.go create mode 100644 internal/provider/resource_app_test.go create mode 100644 internal/provider/resource_dashboard.go create mode 100644 internal/provider/resource_dashboard_test.go diff --git a/docs/resources/namespace.md b/docs/resources/namespace.md index 77a617d..e20e866 100644 --- a/docs/resources/namespace.md +++ b/docs/resources/namespace.md @@ -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 diff --git a/internal/provider/provider.go b/internal/provider/provider.go index f8320c4..755665c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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(), }, } diff --git a/internal/provider/resource_app.go b/internal/provider/resource_app.go new file mode 100644 index 0000000..0985c67 --- /dev/null +++ b/internal/provider/resource_app.go @@ -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 +} diff --git a/internal/provider/resource_app_test.go b/internal/provider/resource_app_test.go new file mode 100644 index 0000000..cca4e5e --- /dev/null +++ b/internal/provider/resource_app_test.go @@ -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 = <