generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for the namespace file resource and datasource (#95)
closes #91
- Loading branch information
Showing
12 changed files
with
542 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
# 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 = "myscript.py" | ||
content = file("myscript.py") | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `content` (String) Content to store in the file, expected to be a UTF-8 encoded string. | ||
- `filename` (String) The filename to the namespace file. | ||
- `namespace` (String) The namespace of the namespace file resource. | ||
|
||
### Optional | ||
|
||
- `tenant_id` (String) The tenant id. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
# 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 = <<EOT | ||
#!/bin/bash | ||
echo "Hello World" | ||
EOT | ||
} | ||
resource "kestra_namespace_file" "withsource" { | ||
namespace = "io.kestra.mynamespace" | ||
filename = "/path/my-file.sh" | ||
content = file("./kestra/file.sh") | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## 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 of the namespace file resource. | ||
|
||
### 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. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import kestra_namespace_file.example {{namespace}}/{{filename}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
data "kestra_namespace_file" "example" { | ||
namespace = "io.kestra.mynamespace" | ||
filename = "myscript.py" | ||
content = file("myscript.py") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform import kestra_namespace_file.example {{namespace}}/{{filename}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
resource "kestra_namespace_file" "example" { | ||
namespace = "io.kestra.mynamespace" | ||
filename = "/path/my-file.sh" | ||
content = <<EOT | ||
#!/bin/bash | ||
echo "Hello World" | ||
EOT | ||
} | ||
|
||
resource "kestra_namespace_file" "withsource" { | ||
namespace = "io.kestra.mynamespace" | ||
filename = "/path/my-file.sh" | ||
content = file("./kestra/file.sh") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceNamespaceFile() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Use this data source to access information about an existing Namespace File", | ||
|
||
ReadContext: dataSourceNamespaceFileRead, | ||
Schema: map[string]*schema.Schema{ | ||
"tenant_id": { | ||
Description: "The tenant id.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"namespace": { | ||
Description: "The namespace of the namespace file resource.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"filename": { | ||
Description: "The filename to the namespace file.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"content": { | ||
Description: "Content to store in the file, expected to be a UTF-8 encoded string.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNamespaceFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
namespace := d.Get("namespace").(string) | ||
filename := d.Get("filename").(string) | ||
tenantId := c.TenantId | ||
|
||
url := c.Url + fmt.Sprintf("%s/namespaces/%s/files?path=%s", apiRoot(tenantId), namespace, filename) | ||
|
||
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf(url), nil) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
statusCode, body, reqErr := c.rawResponseRequest("GET", req) | ||
if reqErr != nil { | ||
if statusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return diags | ||
} | ||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s", namespace, filename)) | ||
if err := d.Set("namespace", namespace); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("filename", filename); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("content", string(body)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceNamespaceFile(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceNamespaceFile("io.kestra.terraform.data", "/flow.py"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.kestra_namespace_file.new", "filename", "/flow.py", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceNamespaceFile(namespace, filename string) string { | ||
return fmt.Sprintf( | ||
` | ||
data "kestra_namespace_file" "new" { | ||
namespace = "%s" | ||
filename = "%s" | ||
}`, | ||
namespace, | ||
filename, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.