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.
Add Monolith manifest catalog resource
- Loading branch information
Showing
7 changed files
with
374 additions
and
3 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,31 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "zentral_monolith_manifest_catalog Resource - terraform-provider-zentral" | ||
subcategory: "" | ||
description: |- | ||
The resource zentral_monolith_manifest_catalog manages Monolith manifest catalogs. | ||
--- | ||
|
||
# zentral_monolith_manifest_catalog (Resource) | ||
|
||
The resource `zentral_monolith_manifest_catalog` manages Monolith manifest catalogs. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `catalog_id` (Number) ID of the catalog. | ||
- `manifest_id` (Number) ID of the manifest. | ||
|
||
### Optional | ||
|
||
- `tag_ids` (Set of Number) The `ID`s of the tags used to scope the catalog. | ||
|
||
### Read-Only | ||
|
||
- `id` (Number) `ID` of the manifest catalog. | ||
|
||
|
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
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,41 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
type monolithManifestCatalog struct { | ||
ID types.Int64 `tfsdk:"id"` | ||
ManifestID types.Int64 `tfsdk:"manifest_id"` | ||
CatalogID types.Int64 `tfsdk:"catalog_id"` | ||
TagIDs types.Set `tfsdk:"tag_ids"` | ||
} | ||
|
||
func monolithManifestCatalogForState(mmc *goztl.MonolithManifestCatalog) monolithManifestCatalog { | ||
tagIDs := make([]attr.Value, 0) | ||
for _, tagID := range mmc.TagIDs { | ||
tagIDs = append(tagIDs, types.Int64Value(int64(tagID))) | ||
} | ||
|
||
return monolithManifestCatalog{ | ||
ID: types.Int64Value(int64(mmc.ID)), | ||
ManifestID: types.Int64Value(int64(mmc.ManifestID)), | ||
CatalogID: types.Int64Value(int64(mmc.CatalogID)), | ||
TagIDs: types.SetValueMust(types.Int64Type, tagIDs), | ||
} | ||
} | ||
|
||
func monolithManifestCatalogRequestWithState(data monolithManifestCatalog) *goztl.MonolithManifestCatalogRequest { | ||
tagIDs := make([]int, 0) | ||
for _, tagID := range data.TagIDs.Elements() { // nil if null or unknown → no iterations | ||
tagIDs = append(tagIDs, int(tagID.(types.Int64).ValueInt64())) | ||
} | ||
|
||
return &goztl.MonolithManifestCatalogRequest{ | ||
ManifestID: int(data.ManifestID.ValueInt64()), | ||
CatalogID: int(data.CatalogID.ValueInt64()), | ||
TagIDs: tagIDs, | ||
} | ||
} |
187 changes: 187 additions & 0 deletions
187
internal/provider/monolith_manifest_catalog_resource.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,187 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/zentralopensource/goztl" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces | ||
var _ resource.Resource = &MonolithManifestCatalogResource{} | ||
var _ resource.ResourceWithImportState = &MonolithManifestCatalogResource{} | ||
|
||
func NewMonolithManifestCatalogResource() resource.Resource { | ||
return &MonolithManifestCatalogResource{} | ||
} | ||
|
||
// MonolithManifestCatalogResource defines the resource implementation. | ||
type MonolithManifestCatalogResource struct { | ||
client *goztl.Client | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_monolith_manifest_catalog" | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Manages Monolith manifest catalogs.", | ||
MarkdownDescription: "The resource `zentral_monolith_manifest_catalog` manages Monolith manifest catalogs.", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.Int64Attribute{ | ||
Description: "ID of the manifest catalog.", | ||
MarkdownDescription: "`ID` of the manifest catalog.", | ||
Computed: true, | ||
PlanModifiers: []planmodifier.Int64{ | ||
int64planmodifier.UseStateForUnknown(), | ||
}, | ||
}, | ||
"manifest_id": schema.Int64Attribute{ | ||
Description: "ID of the manifest.", | ||
MarkdownDescription: "ID of the manifest.", | ||
Required: true, | ||
}, | ||
"catalog_id": schema.Int64Attribute{ | ||
Description: "ID of the catalog.", | ||
MarkdownDescription: "ID of the catalog.", | ||
Required: true, | ||
}, | ||
"tag_ids": schema.SetAttribute{ | ||
Description: "The IDs of the tags used to scope the catalog.", | ||
MarkdownDescription: "The `ID`s of the tags used to scope the catalog.", | ||
ElementType: types.Int64Type, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.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 Resource Configure Type", | ||
fmt.Sprintf("Expected *goztl.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = client | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data monolithManifestCatalog | ||
|
||
// Read Terraform plan data into the model | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
ztlMMC, _, err := r.client.MonolithManifestCatalogs.Create(ctx, monolithManifestCatalogRequestWithState(data)) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to create Monolith manifest catalog, got error: %s", err), | ||
) | ||
return | ||
} | ||
|
||
tflog.Trace(ctx, "created an Monolith manifest catalog") | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, monolithManifestCatalogForState(ztlMMC))...) | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data monolithManifestCatalog | ||
|
||
// Read Terraform prior state data into the model | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
ztlMMC, _, err := r.client.MonolithManifestCatalogs.GetByID(ctx, int(data.ID.ValueInt64())) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client error", | ||
fmt.Sprintf("Unable to read Monolith manifest catalog %d, got error: %s", data.ID.ValueInt64(), err), | ||
) | ||
return | ||
} | ||
|
||
tflog.Trace(ctx, "read an Monolith manifest catalog") | ||
|
||
// Save updated data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, monolithManifestCatalogForState(ztlMMC))...) | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data monolithManifestCatalog | ||
|
||
// Read Terraform plan data into the model | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
ztlMMC, _, err := r.client.MonolithManifestCatalogs.Update(ctx, int(data.ID.ValueInt64()), monolithManifestCatalogRequestWithState(data)) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to update Monolith manifest catalog %d, got error: %s", data.ID.ValueInt64(), err), | ||
) | ||
return | ||
} | ||
|
||
tflog.Trace(ctx, "updated an Monolith manifest catalog") | ||
|
||
// Save updated data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, monolithManifestCatalogForState(ztlMMC))...) | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var data monolithManifestCatalog | ||
|
||
// Read Terraform prior state data into the model | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := r.client.MonolithManifestCatalogs.Delete(ctx, int(data.ID.ValueInt64())) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to delete Monolith manifest catalog %d, got error: %s", data.ID.ValueInt64(), err), | ||
) | ||
return | ||
} | ||
|
||
tflog.Trace(ctx, "deleted an Monolith manifest catalog") | ||
} | ||
|
||
func (r *MonolithManifestCatalogResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resourceImportStatePassthroughZentralID(ctx, "Monolith manifest catalog", req, resp) | ||
} |
111 changes: 111 additions & 0 deletions
111
internal/provider/monolith_manifest_catalog_resource_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,111 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccMonolithManifestCatalogResource(t *testing.T) { | ||
name := acctest.RandString(12) | ||
resourceName := "zentral_monolith_manifest_catalog.test" | ||
mResourceName := "zentral_monolith_manifest.test" | ||
cResourceName := "zentral_monolith_catalog.test" | ||
tResourceName := "zentral_tag.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read | ||
{ | ||
Config: testAccMonolithManifestCatalogResourceConfigBare(name), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrPair( | ||
resourceName, "manifest_id", mResourceName, "id"), | ||
resource.TestCheckResourceAttrPair( | ||
resourceName, "catalog_id", cResourceName, "id"), | ||
resource.TestCheckResourceAttr( | ||
resourceName, "tag_ids.#", "0"), | ||
), | ||
}, | ||
// ImportState | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
// Update and Read | ||
{ | ||
Config: testAccMonolithManifestCatalogResourceConfigFull(name), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrPair( | ||
resourceName, "manifest_id", mResourceName, "id"), | ||
resource.TestCheckResourceAttrPair( | ||
resourceName, "catalog_id", cResourceName, "id"), | ||
resource.TestCheckResourceAttr( | ||
resourceName, "tag_ids.#", "1"), | ||
resource.TestCheckTypeSetElemAttrPair( | ||
resourceName, "tag_ids.*", tResourceName, "id"), | ||
), | ||
}, | ||
// ImportState | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccMonolithManifestCatalogResourceConfigBare(name string) string { | ||
return fmt.Sprintf(` | ||
resource "zentral_meta_business_unit" "test" { | ||
name = %[1]q | ||
} | ||
resource "zentral_monolith_manifest" "test" { | ||
name = %[1]q | ||
meta_business_unit_id = zentral_meta_business_unit.test.id | ||
} | ||
resource "zentral_monolith_catalog" "test" { | ||
name = %[1]q | ||
} | ||
resource "zentral_monolith_manifest_catalog" "test" { | ||
manifest_id = zentral_monolith_manifest.test.id | ||
catalog_id = zentral_monolith_catalog.test.id | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccMonolithManifestCatalogResourceConfigFull(name string) string { | ||
return fmt.Sprintf(` | ||
resource "zentral_meta_business_unit" "test" { | ||
name = %[1]q | ||
} | ||
resource "zentral_monolith_manifest" "test" { | ||
name = %[1]q | ||
meta_business_unit_id = zentral_meta_business_unit.test.id | ||
} | ||
resource "zentral_monolith_catalog" "test" { | ||
name = %[1]q | ||
} | ||
resource "zentral_tag" "test" { | ||
name = %[1]q | ||
} | ||
resource "zentral_monolith_manifest_catalog" "test" { | ||
manifest_id = zentral_monolith_manifest.test.id | ||
catalog_id = zentral_monolith_catalog.test.id | ||
tag_ids = [zentral_tag.test.id] | ||
} | ||
`, name) | ||
} |
Oops, something went wrong.