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.
Showing
9 changed files
with
345 additions
and
0 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,25 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "kestra_tenant Data Source - terraform-provider-kestra" | ||
subcategory: "" | ||
description: |- | ||
Use this data source to access information about an existing Kestra Tenant. | ||
--- | ||
|
||
# kestra_tenant (Data Source) | ||
|
||
Use this data source to access information about an existing Kestra Tenant. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `tenant_id` (String) The tenant id. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `name` (String) The tenant name. |
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,28 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "kestra_tenant Resource - terraform-provider-kestra" | ||
subcategory: "" | ||
description: |- | ||
Manages a Kestra Tenant. | ||
--- | ||
|
||
# kestra_tenant (Resource) | ||
|
||
Manages a Kestra Tenant. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `tenant_id` (String) The tenant id. | ||
|
||
### Optional | ||
|
||
- `name` (String) The tenant name. | ||
|
||
### 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,48 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceTenant() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Use this data source to access information about an existing Kestra Tenant.", | ||
|
||
ReadContext: dataSourceTenantRead, | ||
Schema: map[string]*schema.Schema{ | ||
"tenant_id": { | ||
Description: "The tenant id.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Description: "The tenant name.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceTenantRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
tenantId := d.Get("tenant_id").(string) | ||
|
||
r, reqErr := c.request("GET", fmt.Sprintf("%s/tenants/%s", apiRoot(""), tenantId), nil) | ||
if reqErr != nil { | ||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
errs := tenantApiToSchema(r.(map[string]interface{}), d) | ||
if errs != nil { | ||
return errs | ||
} | ||
|
||
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,36 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceTenant(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceTenant("admin"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr( | ||
"data.kestra_tenant.new", "id", regexp.MustCompile("admin"), | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceTenant(id string) string { | ||
return fmt.Sprintf( | ||
` | ||
data "kestra_tenant" "new" { | ||
tenant_id = "%s" | ||
}`, | ||
id, | ||
) | ||
} |
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,126 @@ | ||
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 resourceTenant() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Manages a Kestra Tenant.", | ||
|
||
CreateContext: resourceTenantCreate, | ||
ReadContext: resourceTenantRead, | ||
UpdateContext: resourceTenantUpdate, | ||
DeleteContext: resourceTenantDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"tenant_id": { | ||
Description: "The tenant id.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Description: "The tenant name.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTenantCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
body, err := tenantSchemaToApi(d) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
r, reqErr := c.request("POST", fmt.Sprintf("%s/tenants", apiRoot("")), body) | ||
if reqErr != nil { | ||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
errs := tenantApiToSchema(r.(map[string]interface{}), d) | ||
if errs != nil { | ||
return errs | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func resourceTenantRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
tenantId := d.Id() | ||
|
||
r, reqErr := c.request("GET", fmt.Sprintf("%s/tenants/%s", apiRoot(""), tenantId), nil) | ||
if reqErr != nil { | ||
if reqErr.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return diags | ||
} | ||
|
||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
errs := tenantApiToSchema(r.(map[string]interface{}), d) | ||
if errs != nil { | ||
return errs | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func resourceTenantUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
if d.HasChanges("name") { | ||
body, err := tenantSchemaToApi(d) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
tenantId := d.Id() | ||
|
||
r, reqErr := c.request("PUT", fmt.Sprintf("%s/tenants/%s", apiRoot(""), tenantId), body) | ||
if err != nil { | ||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
errs := tenantApiToSchema(r.(map[string]interface{}), d) | ||
if errs != nil { | ||
return errs | ||
} | ||
|
||
return diags | ||
} else { | ||
return resourceTenantRead(ctx, d, meta) | ||
} | ||
} | ||
|
||
func resourceTenantDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*Client) | ||
var diags diag.Diagnostics | ||
|
||
tenantId := d.Id() | ||
|
||
_, reqErr := c.request("DELETE", fmt.Sprintf("%s/tenants/%s", apiRoot(""), tenantId), nil) | ||
if reqErr != nil { | ||
return diag.FromErr(reqErr.Err) | ||
} | ||
|
||
d.SetId("") | ||
|
||
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,48 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccTenant(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccResourceTenant( | ||
"custom", | ||
"My custom tenant", | ||
), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"kestra_tenant.new", "tenant_id", "custom", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"kestra_tenant.new", "name", "My custom tenant", | ||
), | ||
), | ||
}, | ||
{ | ||
ResourceName: "kestra_tenant.new", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccResourceTenant(id, name string) string { | ||
return fmt.Sprintf( | ||
` | ||
resource "kestra_tenant" "new" { | ||
tenant_id = "%s" | ||
name = "%s" | ||
}`, | ||
id, | ||
name, | ||
) | ||
} |
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,30 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func tenantSchemaToApi(d *schema.ResourceData) (map[string]interface{}, error) { | ||
body := make(map[string]interface{}, 0) | ||
|
||
body["id"] = d.Get("tenant_id").(string) | ||
body["name"] = d.Get("name").(string) | ||
|
||
return body, nil | ||
} | ||
|
||
func tenantApiToSchema(r map[string]interface{}, d *schema.ResourceData) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
d.SetId(r["id"].(string)) | ||
|
||
if err := d.Set("tenant_id", r["id"].(string)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("name", r["name"].(string)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return diags | ||
} |