From ad6570d712adef9ffe0253d63dda5f33842e023c Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Mon, 23 Sep 2024 10:04:29 +0200 Subject: [PATCH 1/3] add service context data source --- example/servicecontext/main.tf | 6 +- internal/datasource/service_context.go | 84 ++++++++++++++++++++++++++ internal/model/service_context.go | 8 ++- internal/provider/provider.go | 1 + internal/resource/service_context.go | 10 +-- 5 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 internal/datasource/service_context.go diff --git a/example/servicecontext/main.tf b/example/servicecontext/main.tf index c9645f89..9d409f8f 100644 --- a/example/servicecontext/main.tf +++ b/example/servicecontext/main.tf @@ -2,7 +2,7 @@ terraform { required_providers { plural = { source = "pluralsh/plural" - version = "0.0.1" + version = "0.2.1" } } } @@ -21,3 +21,7 @@ resource "plural_service_context" "service_context" { "test" = "some-secret-value" } } + +data "plural_service_context" "service_context" { + name = "service-context-tf" +} diff --git a/internal/datasource/service_context.go b/internal/datasource/service_context.go new file mode 100644 index 00000000..a03a8c6a --- /dev/null +++ b/internal/datasource/service_context.go @@ -0,0 +1,84 @@ +package datasource + +import ( + "context" + "fmt" + + "terraform-provider-plural/internal/client" + "terraform-provider-plural/internal/common" + "terraform-provider-plural/internal/model" + + "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 { + return &serviceContextDataSource{} +} + +type serviceContextDataSource struct { + client *client.Client +} + +func (d *serviceContextDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_service_context" +} + +func (d *serviceContextDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "A representation of a service context that can be reused during service deployment templating.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + Description: "Internal identifier of this service context.", + MarkdownDescription: "Internal identifier of this service context.", + }, + "name": schema.StringAttribute{ + Description: "Human-readable name of this service context.", + MarkdownDescription: "Human-readable name of this service context.", + Required: true, + }, + "configuration": schema.MapAttribute{ + Description: "", + MarkdownDescription: "", + Computed: true, + ElementType: types.StringType, + }, + }, + } +} + +func (d *serviceContextDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + data, ok := req.ProviderData.(*common.ProviderData) + if !ok { + resp.Diagnostics.AddError( + "Unexpected Service Context Data Source Configure Type", + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + return + } + + d.client = data.Client +} + +func (d *serviceContextDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + data := new(model.ServiceContext) + resp.Diagnostics.Append(req.Config.Get(ctx, data)...) + if resp.Diagnostics.HasError() { + return + } + + response, err := d.client.GetServiceContext(ctx, data.Name.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read service context, got error: %s", err)) + return + } + + data.From(response.ServiceContext, ctx, resp.Diagnostics) + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) +} diff --git a/internal/model/service_context.go b/internal/model/service_context.go index 00a42bd8..5d52e584 100644 --- a/internal/model/service_context.go +++ b/internal/model/service_context.go @@ -14,7 +14,6 @@ type ServiceContext struct { Id types.String `tfsdk:"id"` Name types.String `tfsdk:"name"` Configuration types.Map `tfsdk:"configuration"` - Secrets types.Map `tfsdk:"secrets"` } func (sc *ServiceContext) From(response *console.ServiceContextFragment, ctx context.Context, d diag.Diagnostics) { @@ -22,7 +21,12 @@ func (sc *ServiceContext) From(response *console.ServiceContextFragment, ctx con sc.Configuration = common.MapFrom(response.Configuration, ctx, d) } -func (sc *ServiceContext) Attributes(ctx context.Context, d diag.Diagnostics) console.ServiceContextAttributes { +type ServiceContextExtended struct { + ServiceContext + Secrets types.Map `tfsdk:"secrets"` +} + +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) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 22c2dc26..f582a259 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -208,6 +208,7 @@ func (p *PluralProvider) DataSources(_ context.Context) []func() datasource.Data ds.NewConfigDataSource, ds.NewPRAutomationDataSource, ds.NewInfrastructureStackDataSource, + ds.NewServiceContextDataSource, } } diff --git a/internal/resource/service_context.go b/internal/resource/service_context.go index 4a0444f4..00fc13f1 100644 --- a/internal/resource/service_context.go +++ b/internal/resource/service_context.go @@ -40,7 +40,7 @@ func (r *ServiceContextResource) Schema(_ context.Context, _ resource.SchemaRequ "id": schema.StringAttribute{ Computed: true, Description: "Internal identifier of this service context.", - MarkdownDescription: "Internal identifier of this provider.", + MarkdownDescription: "Internal identifier of this service context.", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, }, "name": schema.StringAttribute{ @@ -85,7 +85,7 @@ func (r *ServiceContextResource) Configure(_ context.Context, req resource.Confi } func (r *ServiceContextResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - data := new(model.ServiceContext) + data := new(model.ServiceContextExtended) resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) if resp.Diagnostics.HasError() { return @@ -102,7 +102,7 @@ func (r *ServiceContextResource) Create(ctx context.Context, req resource.Create } func (r *ServiceContextResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - data := new(model.ServiceContext) + data := new(model.ServiceContextExtended) resp.Diagnostics.Append(req.State.Get(ctx, data)...) if resp.Diagnostics.HasError() { return @@ -119,7 +119,7 @@ func (r *ServiceContextResource) Read(ctx context.Context, req resource.ReadRequ } func (r *ServiceContextResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - data := new(model.ServiceContext) + data := new(model.ServiceContextExtended) resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) if resp.Diagnostics.HasError() { return @@ -135,7 +135,7 @@ func (r *ServiceContextResource) Update(ctx context.Context, req resource.Update } func (r *ServiceContextResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - data := new(model.ServiceContext) + data := new(model.ServiceContextExtended) resp.Diagnostics.Append(req.State.Get(ctx, data)...) if resp.Diagnostics.HasError() { return From 89abc76f22828d28a4bbc8b0bdda1240245d020f Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Mon, 23 Sep 2024 10:12:03 +0200 Subject: [PATCH 2/3] generate docs --- docs/data-sources/service_context.md | 25 +++++++++++++++++++++++++ docs/resources/service_context.md | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 docs/data-sources/service_context.md diff --git a/docs/data-sources/service_context.md b/docs/data-sources/service_context.md new file mode 100644 index 00000000..167a7fb6 --- /dev/null +++ b/docs/data-sources/service_context.md @@ -0,0 +1,25 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "plural_service_context Data Source - terraform-provider-plural" +subcategory: "" +description: |- + A representation of a service context that can be reused during service deployment templating. +--- + +# plural_service_context (Data Source) + +A representation of a service context that can be reused during service deployment templating. + + + + +## Schema + +### Required + +- `name` (String) Human-readable name of this service context. + +### Read-Only + +- `configuration` (Map of String) +- `id` (String) Internal identifier of this service context. diff --git a/docs/resources/service_context.md b/docs/resources/service_context.md index 807b08a8..0f66c0c0 100644 --- a/docs/resources/service_context.md +++ b/docs/resources/service_context.md @@ -26,4 +26,4 @@ A representation of a service context that can be reused during service deployme ### Read-Only -- `id` (String) Internal identifier of this provider. +- `id` (String) Internal identifier of this service context. From a53450b119664f02b5935017754abf72db0642c5 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Mon, 23 Sep 2024 10:28:15 +0200 Subject: [PATCH 3/3] update example --- example/servicecontext/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/servicecontext/main.tf b/example/servicecontext/main.tf index 9d409f8f..a29400fe 100644 --- a/example/servicecontext/main.tf +++ b/example/servicecontext/main.tf @@ -12,7 +12,7 @@ provider "plural" { } resource "plural_service_context" "service_context" { - name = "service-context-tf" + name = "service-context-test" configuration = { "env" = "prod" "test" = "some-value" @@ -23,5 +23,5 @@ resource "plural_service_context" "service_context" { } data "plural_service_context" "service_context" { - name = "service-context-tf" + name = plural_service_context.service_context.name }