diff --git a/docs/data-sources/namespace_file.md b/docs/data-sources/namespace_file.md new file mode 100644 index 0000000..7ad2c43 --- /dev/null +++ b/docs/data-sources/namespace_file.md @@ -0,0 +1,39 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "kestra_namespace_file Data Source - terraform-provider-kestra" +subcategory: "" +description: |- + Use this data source to access information about an existing Namespace File +--- + +# kestra_namespace_file (Data Source) + +Use this data source to access information about an existing Namespace File + +## Example Usage + +```terraform +data "kestra_namespace_file" "example" { + namespace_ = "io.kestra.mynamespace" + filename = "my-file.yml" +} +``` + + +## Schema + +### Required + +- `filename` (String) The path to the namespace file that will be created. +Missing parent directories will be created. +If the file already exists, it will be overridden with the given content. +- `namespace` (String) The Namespace file namespace. + +### Optional + +- `content` (String) Content to store in the file, expected to be a UTF-8 encoded string. +- `tenant_id` (String) The tenant id. + +### Read-Only + +- `id` (String) The ID of this resource. diff --git a/docs/resources/namespace_file.md b/docs/resources/namespace_file.md new file mode 100644 index 0000000..2e1b4c2 --- /dev/null +++ b/docs/resources/namespace_file.md @@ -0,0 +1,62 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "kestra_namespace_file Resource - terraform-provider-kestra" +subcategory: "" +description: |- + Manages a Kestra Namespace File. +--- + +# kestra_namespace_file (Resource) + +Manages a Kestra Namespace File. + +## Example Usage + +```terraform +resource "kestra_namespace_file" "example" { + namespace = "io.kestra.mynamespace" + filename = "/path/my-file.sh" + content = < +## Schema + +### Required + +- `filename` (String) The path to the namespace file that will be created. +Missing parent directories will be created. +If the file already exists, it will be overridden with the given content. +- `namespace` (String) The Namespace file namespace. + +### Optional + +- `content` (String) Content to store in the file, expected to be a UTF-8 encoded string. + Conflicts with `source`. + Exactly one of these four arguments must be specified. +- `source` (String) Path to file to use as source for the one we are creating. + Conflicts with `content`. + Exactly one of these four arguments must be specified. +- `tenant_id` (String) The tenant id. + +### Read-Only + +- `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import kestra_template.example {{namespace}}/{{template_id}} +``` diff --git a/examples/data-sources/kestra_namespace_file/data-source.tf b/examples/data-sources/kestra_namespace_file/data-source.tf new file mode 100644 index 0000000..82b1351 --- /dev/null +++ b/examples/data-sources/kestra_namespace_file/data-source.tf @@ -0,0 +1,4 @@ +data "kestra_namespace_file" "example" { + namespace_ = "io.kestra.mynamespace" + filename = "my-file.yml" +} diff --git a/examples/resources/kestra_namespace_file/import.sh b/examples/resources/kestra_namespace_file/import.sh new file mode 100644 index 0000000..9f9846b --- /dev/null +++ b/examples/resources/kestra_namespace_file/import.sh @@ -0,0 +1 @@ +terraform import kestra_template.example {{namespace}}/{{filename}} \ No newline at end of file diff --git a/examples/resources/kestra_namespace_file/resource.tf b/examples/resources/kestra_namespace_file/resource.tf new file mode 100644 index 0000000..13eed63 --- /dev/null +++ b/examples/resources/kestra_namespace_file/resource.tf @@ -0,0 +1,14 @@ +resource "kestra_namespace_file" "example" { + namespace = "io.kestra.mynamespace" + filename = "/path/my-file.sh" + content = < 0 { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + r = file + } else { + r = strings.NewReader(content) + } + + fw, err := w.CreateFormFile("fileContent", "file.txt") + if err != nil { + return nil, err + } + + if _, err := io.Copy(fw, r); err != nil { + return nil, err + } + + w.Close() + + req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf(url), &buf) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", w.FormDataContentType()) + return req, nil +} + +func namespaceFileConvertId(id string) (string, string) { + splits := strings.Split(id, "/") + + return splits[0], strings.Join(splits[1:], "/") +} diff --git a/internal/provider/resource_namespace_file_test.go b/internal/provider/resource_namespace_file_test.go new file mode 100644 index 0000000..c052b01 --- /dev/null +++ b/internal/provider/resource_namespace_file_test.go @@ -0,0 +1,81 @@ +package provider + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccNamespaceFile(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccResourceNamespaceFile( + "io.kestra.terraform", + "simple.yml", + concat( + "tasks:", + " - id: t1", + " type: io.kestra.core.tasks.debugs.Echo", + " format: first {{task.id}}", + " level: TRACE", + ), + ), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "kestra_namespace_file.new", "id", "io.kestra.terraform/simple.yml", + ), + resource.TestCheckResourceAttr( + "kestra_namespace_file.new", "namespace", "io.kestra.terraform", + ), + resource.TestMatchResourceAttr( + "kestra_namespace_file.new", "content", regexp.MustCompile(".*id: t1\n.*"), + ), + ), + }, + { + Config: testAccResourceNamespaceFile( + "io.kestra.terraform", + "simple", + concat( + "tasks:", + " - id: t2", + " type: io.kestra.core.tasks.debugs.Echo", + " format: first {{task.id}}", + " level: TRACE", + ), + ), + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "kestra_namespace_file.new", "content", regexp.MustCompile(".*id: t2\n.*"), + ), + ), + }, + { + ResourceName: "kestra_namespace_file.new", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccResourceNamespaceFile(namespace, fileName, content string) string { + return fmt.Sprintf( + ` + resource "kestra_namespace_file" "new" { + namespace = "%s" + filename = "%s" + content = <