Skip to content

Commit

Permalink
Add Monolith manifest catalog resource
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Mar 6, 2023
1 parent fcda098 commit 56d3f69
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 3 deletions.
31 changes: 31 additions & 0 deletions docs/resources/monolith_manifest_catalog.md
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.


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
github.com/zentralopensource/goztl v0.1.21
github.com/zentralopensource/goztl v0.1.23
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uU
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
github.com/zentralopensource/goztl v0.1.21 h1:sHrFMPM+d5eWOze13/BocSpZ6l+qGHTgPnb4yEuqwUU=
github.com/zentralopensource/goztl v0.1.21/go.mod h1:zakEe4QjAUvawFpzehwryMM5f2RI61a8m1wcNxQ8G/A=
github.com/zentralopensource/goztl v0.1.23 h1:5A53ucGqjb0XVvZza9NUTNqCRbvxjHan+1eaprqWEj8=
github.com/zentralopensource/goztl v0.1.23/go.mod h1:zakEe4QjAUvawFpzehwryMM5f2RI61a8m1wcNxQ8G/A=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
41 changes: 41 additions & 0 deletions internal/provider/monolith_manifest_catalog.go
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 internal/provider/monolith_manifest_catalog_resource.go
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 internal/provider/monolith_manifest_catalog_resource_test.go
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)
}
Loading

0 comments on commit 56d3f69

Please sign in to comment.