Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: plural_service_context configuration field type #71

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/data-sources/service_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ A representation of a service context that can be reused during service deployme

### Read-Only

- `configuration` (Map of String)
- `configuration` (String) Configuration in JSON format. Use `jsondecode` method to decode data.
- `id` (String) Internal identifier of this service context.
2 changes: 1 addition & 1 deletion docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A representation of a cluster you can deploy to.
- `helm_repo_url` (String) Helm repository URL you'd like to use in deployment agent Helm install.
- `helm_values` (String) Additional Helm values you'd like to use in deployment agent Helm installs. This is useful for BYOK clusters that need to use custom images or other constructs.
- `kubeconfig` (Attributes) (see [below for nested schema](#nestedatt--kubeconfig))
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons). Use `jsonencode` and `jsondecode` methods to encode and decode data.
- `project_id` (String) ID of the project that this cluster belongs to.
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
- `tags` (Map of String) Key-value tags used to filter clusters.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/oidc_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ description: |-

### Optional

- `auth_method` (String)
- `description` (String) Description of this OIDC provider.
- `redirect_uris` (Set of String)

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/service_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A representation of a service context that can be reused during service deployme

### Optional

- `configuration` (Map of String)
- `configuration` (String) Configuration in JSON format. Use `jsonencode` and `jsondecode` methods to encode and decode data.
- `secrets` (Map of String, Sensitive)

### Read-Only
Expand Down
8 changes: 6 additions & 2 deletions example/servicecontext/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ provider "plural" {

resource "plural_service_context" "service_context" {
name = "service-context-test"
configuration = {
configuration = jsonencode({
"env" = "prod"
"test" = "some-value"
}
"array" = [1, 2, 3]
"nested_field" = {
"test" = "nested-value"
}
})
secrets = {
"test" = "some-secret-value"
}
Expand Down
8 changes: 3 additions & 5 deletions internal/datasource/service_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func NewServiceContextDataSource() datasource.DataSource {
Expand Down Expand Up @@ -39,11 +38,10 @@ func (d *serviceContextDataSource) Schema(_ context.Context, _ datasource.Schema
MarkdownDescription: "Human-readable name of this service context.",
Required: true,
},
"configuration": schema.MapAttribute{
Description: "",
MarkdownDescription: "",
"configuration": schema.StringAttribute{
Description: "Configuration in JSON format. Use 'jsondecode' method to decode data.",
MarkdownDescription: "Configuration in JSON format. Use `jsondecode` method to decode data.",
Computed: true,
ElementType: types.StringType,
},
},
}
Expand Down
19 changes: 11 additions & 8 deletions internal/model/service_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package model

import (
"context"

"terraform-provider-plural/internal/common"
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -13,12 +13,18 @@ import (
type ServiceContext struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Configuration types.Map `tfsdk:"configuration"`
Configuration types.String `tfsdk:"configuration"`
}

func (sc *ServiceContext) From(response *console.ServiceContextFragment, ctx context.Context, d diag.Diagnostics) {
configuration, err := json.Marshal(response.Configuration)
if err != nil {
d.AddError("Provider Error", fmt.Sprintf("Cannot marshall metadata, got error: %s", err))
return
}

sc.Id = types.StringValue(response.ID)
sc.Configuration = common.MapFrom(response.Configuration, ctx, d)
sc.Configuration = types.StringValue(string(configuration))
}

type ServiceContextExtended struct {
Expand All @@ -27,9 +33,6 @@ type ServiceContextExtended struct {
}

func (sc *ServiceContextExtended) Attributes(ctx context.Context, d diag.Diagnostics) console.ServiceContextAttributes {
configuration := make(map[string]types.String, len(sc.Configuration.Elements()))
sc.Configuration.ElementsAs(ctx, &configuration, false)

secrets := make(map[string]types.String, len(sc.Secrets.Elements()))
sc.Secrets.ElementsAs(ctx, &secrets, false)
configAttributes := make([]*console.ConfigAttributes, 0)
Expand All @@ -41,7 +44,7 @@ func (sc *ServiceContextExtended) Attributes(ctx context.Context, d diag.Diagnos
}

return console.ServiceContextAttributes{
Configuration: common.AttributesJson(configuration, d),
Configuration: sc.Configuration.ValueStringPointer(),
Secrets: configAttributes,
}
}
4 changes: 2 additions & 2 deletions internal/resource/cluster_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (r *clusterResource) schema() schema.Schema {
// },
// },
"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).",
Description: "Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons). Use 'jsonencode' and 'jsondecode' methods to encode and decode data.",
MarkdownDescription: "Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons). Use `jsonencode` and `jsondecode` methods to encode and decode data.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
Expand Down
7 changes: 3 additions & 4 deletions internal/resource/service_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ func (r *ServiceContextResource) Schema(_ context.Context, _ resource.SchemaRequ
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"configuration": schema.MapAttribute{
Description: "",
MarkdownDescription: "",
"configuration": schema.StringAttribute{
Description: "Configuration in JSON format. Use 'jsonencode' and 'jsondecode' methods to encode and decode data.",
MarkdownDescription: "Configuration in JSON format. Use `jsonencode` and `jsondecode` methods to encode and decode data.",
Optional: true,
ElementType: types.StringType,
},
"secrets": schema.MapAttribute{
Description: "",
Expand Down
Loading