generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
580 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
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: "zentral_monolith_sub_manifest Data Source - terraform-provider-zentral" | ||
subcategory: "" | ||
description: |- | ||
The data source zentral_monolith_sub_manifest allows details of a Monolith sub manifest to be retrieved by its ID or its name. | ||
--- | ||
|
||
# zentral_monolith_sub_manifest (Data Source) | ||
|
||
The data source `zentral_monolith_sub_manifest` allows details of a Monolith sub manifest to be retrieved by its `ID` or its `name`. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `id` (Number) `ID` of the sub manifest. | ||
- `name` (String) Name of the sub manifest. | ||
|
||
### Read-Only | ||
|
||
- `description` (String) Description of the sub manifest. | ||
- `meta_business_unit_id` (Number) The `ID` of the meta business unit this manifest is restricted to. | ||
|
||
|
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,31 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "zentral_monolith_sub_manifest Resource - terraform-provider-zentral" | ||
subcategory: "" | ||
description: |- | ||
The resource zentral_monolith_sub_manifest manages Monolith sub manifests. | ||
--- | ||
|
||
# zentral_monolith_sub_manifest (Resource) | ||
|
||
The resource `zentral_monolith_sub_manifest` manages Monolith sub manifests. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Name of the sub manifest. | ||
|
||
### Optional | ||
|
||
- `description` (String) Description of the sub manifest. | ||
- `meta_business_unit_id` (Number) The `ID` of the meta business unit this sub manifest is restricted to. | ||
|
||
### Read-Only | ||
|
||
- `id` (Number) `ID` of the sub manifest. | ||
|
||
|
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,42 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
type monolithSubManifest struct { | ||
ID types.Int64 `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Description types.String `tfsdk:"description"` | ||
MetaBusinessUnitID types.Int64 `tfsdk:"meta_business_unit_id"` | ||
} | ||
|
||
func monolithSubManifestForState(msm *goztl.MonolithSubManifest) monolithSubManifest { | ||
var mbu types.Int64 | ||
if msm.MetaBusinessUnitID != nil { | ||
mbu = types.Int64Value(int64(*msm.MetaBusinessUnitID)) | ||
} else { | ||
mbu = types.Int64Null() | ||
} | ||
|
||
return monolithSubManifest{ | ||
ID: types.Int64Value(int64(msm.ID)), | ||
Name: types.StringValue(msm.Name), | ||
Description: types.StringValue(msm.Description), | ||
MetaBusinessUnitID: mbu, | ||
} | ||
} | ||
|
||
func monolithSubManifestRequestWithState(data monolithSubManifest) *goztl.MonolithSubManifestRequest { | ||
var mbu *int | ||
if !data.MetaBusinessUnitID.IsNull() { | ||
mbu = goztl.Int(int(data.MetaBusinessUnitID.ValueInt64())) | ||
} | ||
|
||
return &goztl.MonolithSubManifestRequest{ | ||
Name: data.Name.ValueString(), | ||
Description: data.Description.ValueString(), | ||
MetaBusinessUnitID: mbu, | ||
} | ||
} |
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,132 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces | ||
var _ datasource.DataSource = &MonolithSubManifestDataSource{} | ||
|
||
func NewMonolithSubManifestDataSource() datasource.DataSource { | ||
return &MonolithSubManifestDataSource{} | ||
} | ||
|
||
// MonolithSubManifestDataSource defines the data source implementation. | ||
type MonolithSubManifestDataSource struct { | ||
client *goztl.Client | ||
} | ||
|
||
func (d *MonolithSubManifestDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_monolith_sub_manifest" | ||
} | ||
|
||
func (d *MonolithSubManifestDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Allows details of a Monolith sub manifest to be retrieved by its ID or its name.", | ||
MarkdownDescription: "The data source `zentral_monolith_sub_manifest` allows details of a Monolith sub manifest to be retrieved by its `ID` or its `name`.", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.Int64Attribute{ | ||
Description: "ID of the sub manifest.", | ||
MarkdownDescription: "`ID` of the sub manifest.", | ||
Optional: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Description: "Name of the sub manifest.", | ||
MarkdownDescription: "Name of the sub manifest.", | ||
Optional: true, | ||
}, | ||
"description": schema.StringAttribute{ | ||
Description: "Description of the sub manifest.", | ||
MarkdownDescription: "Description of the sub manifest.", | ||
Computed: true, | ||
}, | ||
"meta_business_unit_id": schema.Int64Attribute{ | ||
Description: "The ID of the meta business unit this manifest is restricted to.", | ||
MarkdownDescription: "The `ID` of the meta business unit this manifest is restricted to.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *MonolithSubManifestDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*goztl.Client) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *goztl.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *MonolithSubManifestDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { | ||
var data monolithSubManifest | ||
diags := req.Config.Get(ctx, &data) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if data.ID.IsNull() && data.Name.IsNull() { | ||
resp.Diagnostics.AddError( | ||
"Invalid `zentral_monolith_sub_manifest` data source", | ||
"`id` or `name` missing", | ||
) | ||
} else if !data.ID.IsNull() && !data.Name.IsNull() { | ||
resp.Diagnostics.AddError( | ||
"Invalid `zentral_monolith_sub_manifest` data source", | ||
"`id` and `name` cannot be both set", | ||
) | ||
} | ||
} | ||
|
||
func (d *MonolithSubManifestDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data monolithSubManifest | ||
|
||
// Read Terraform manifest data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var ztlMSM *goztl.MonolithSubManifest | ||
var err error | ||
if data.ID.ValueInt64() > 0 { | ||
ztlMSM, _, err = d.client.MonolithSubManifests.GetByID(ctx, int(data.ID.ValueInt64())) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to get Monolith sub manifest '%d' by ID, got error: %s", data.ID.ValueInt64(), err), | ||
) | ||
} | ||
} else { | ||
ztlMSM, _, err = d.client.MonolithSubManifests.GetByName(ctx, data.Name.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to get Monolith sub manifest '%s' by name, got error: %s", data.Name.ValueString(), err), | ||
) | ||
} | ||
} | ||
|
||
if ztlMSM != nil { | ||
resp.Diagnostics.Append(resp.State.Set(ctx, monolithSubManifestForState(ztlMSM))...) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
internal/provider/monolith_sub_manifest_data_source_test.go
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,75 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccMonolithSubManifestDataSource(t *testing.T) { | ||
sm1Name := acctest.RandString(12) | ||
sm2Name := acctest.RandString(12) | ||
sm1ResourceName := "zentral_monolith_sub_manifest.test1" | ||
sm2ResourceName := "zentral_monolith_sub_manifest.test2" | ||
mbuResourceName := "zentral_meta_business_unit.test" | ||
ds1ResourceName := "data.zentral_monolith_sub_manifest.test_by_id" | ||
ds2ResourceName := "data.zentral_monolith_sub_manifest.test_by_name" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMonolithSubManifestDataSourceConfig(sm1Name, sm2Name), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Read by id | ||
resource.TestCheckResourceAttrPair( | ||
ds1ResourceName, "id", sm1ResourceName, "id"), | ||
resource.TestCheckResourceAttr( | ||
ds1ResourceName, "name", sm1Name), | ||
resource.TestCheckResourceAttr( | ||
ds1ResourceName, "description", ""), | ||
resource.TestCheckNoResourceAttr( | ||
ds1ResourceName, "meta_business_unit_id"), | ||
// Read by name | ||
resource.TestCheckResourceAttrPair( | ||
ds2ResourceName, "id", sm2ResourceName, "id"), | ||
resource.TestCheckResourceAttr( | ||
ds2ResourceName, "name", sm2Name), | ||
resource.TestCheckResourceAttr( | ||
ds2ResourceName, "description", "Description"), | ||
resource.TestCheckResourceAttrPair( | ||
ds2ResourceName, "meta_business_unit_id", mbuResourceName, "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccMonolithSubManifestDataSourceConfig(sm1Name string, sm2Name string) string { | ||
return fmt.Sprintf(` | ||
resource "zentral_meta_business_unit" "test" { | ||
name = %[2]q | ||
} | ||
resource "zentral_monolith_sub_manifest" "test1" { | ||
name = %[1]q | ||
} | ||
resource "zentral_monolith_sub_manifest" "test2" { | ||
name = %[2]q | ||
description = "Description" | ||
meta_business_unit_id = zentral_meta_business_unit.test.id | ||
} | ||
data "zentral_monolith_sub_manifest" "test_by_id" { | ||
id = zentral_monolith_sub_manifest.test1.id | ||
} | ||
data "zentral_monolith_sub_manifest" "test_by_name" { | ||
name = zentral_monolith_sub_manifest.test2.name | ||
} | ||
`, sm1Name, sm2Name) | ||
} |
Oops, something went wrong.