Skip to content

Commit

Permalink
add metadata field
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Apr 17, 2024
1 parent 520f166 commit 5b66eb1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A representation of a cluster you can deploy to.

### Required

- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
- `name` (String) Human-readable name of this cluster, that also translates to cloud resource name.

### Optional
Expand Down
16 changes: 15 additions & 1 deletion example/gcp/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ resource "plural_cluster" "gcp_workload_cluster" {
tags = {
"managed-by" = "terraform-provider-plural"
}

metadata = jsonencode({
test1 = "string"
test2 = false
test3 = jsonencode({
abc = false
})
})
# Alternative method:
# metadata = <<EOF
#{
# "test1": "string",
# "test2": false,
# "test3": {"abc": false},
#}
#EOF
depends_on = [plural_provider.gcp_provider]
}
11 changes: 11 additions & 0 deletions internal/resource/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package resource

import (
"context"
"encoding/json"
"fmt"

"terraform-provider-plural/internal/common"

Expand All @@ -22,6 +24,7 @@ type cluster struct {
Cloud types.String `tfsdk:"cloud"`
Protect types.Bool `tfsdk:"protect"`
Tags types.Map `tfsdk:"tags"`
Metadata types.String `tfsdk:"name"`
Bindings *common.ClusterBindings `tfsdk:"bindings"`
NodePools types.Map `tfsdk:"node_pools"`
CloudSettings *ClusterCloudSettings `tfsdk:"cloud_settings"`
Expand Down Expand Up @@ -76,6 +79,7 @@ func (c *cluster) Attributes(ctx context.Context, d diag.Diagnostics) console.Cl
WriteBindings: c.Bindings.WriteAttributes(),
Tags: c.TagsAttribute(ctx, d),
NodePools: c.NodePoolsAttribute(ctx, d),
Metadata: c.Metadata.ValueStringPointer(),
}
}

Expand All @@ -89,6 +93,12 @@ func (c *cluster) UpdateAttributes(ctx context.Context, d diag.Diagnostics) cons
}

func (c *cluster) From(cl *console.ClusterFragment, ctx context.Context, d diag.Diagnostics) {
metadata, err := json.Marshal(cl.Metadata)
if err != nil {
d.AddError("Provider Error", fmt.Sprintf("Cannot marshall metadata, got error: %s", err))
return
}

c.Id = types.StringValue(cl.ID)
c.InsertedAt = types.StringPointerValue(cl.InsertedAt)
c.Name = types.StringValue(cl.Name)
Expand All @@ -98,6 +108,7 @@ func (c *cluster) From(cl *console.ClusterFragment, ctx context.Context, d diag.
c.Tags = common.ClusterTagsFrom(cl.Tags, d)
c.ProviderId = common.ClusterProviderIdFrom(cl.Provider)
c.NodePools = common.ClusterNodePoolsFrom(cl.NodePools, c.NodePools, ctx, d)
c.Metadata = types.StringValue(string(metadata))
}

func (c *cluster) FromCreate(cc *console.CreateCluster, ctx context.Context, d diag.Diagnostics) {
Expand Down
6 changes: 6 additions & 0 deletions internal/resource/cluster_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (r *clusterResource) schema() schema.Schema {
path.MatchRoot("cloud")),
},
},
"metadata": schema.StringAttribute{
Description: "Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).",
MarkdownDescription: "Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"cloud": schema.StringAttribute{
Description: "The cloud provider used to create this cluster.",
MarkdownDescription: "The cloud provider used to create this cluster.",
Expand Down

0 comments on commit 5b66eb1

Please sign in to comment.