Skip to content

Commit

Permalink
Merge pull request #59 from pluralsh/marcin/prod-2668-add-plural_serv…
Browse files Browse the repository at this point in the history
…ice_context-datasource

feat: Add service context data source
  • Loading branch information
maciaszczykm authored Sep 23, 2024
2 parents 9e3d4ab + a53450b commit a0b7900
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 10 deletions.
25 changes: 25 additions & 0 deletions docs/data-sources/service_context.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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.
2 changes: 1 addition & 1 deletion docs/resources/service_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 6 additions & 2 deletions example/servicecontext/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
plural = {
source = "pluralsh/plural"
version = "0.0.1"
version = "0.2.1"
}
}
}
Expand All @@ -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"
Expand All @@ -21,3 +21,7 @@ resource "plural_service_context" "service_context" {
"test" = "some-secret-value"
}
}

data "plural_service_context" "service_context" {
name = plural_service_context.service_context.name
}
84 changes: 84 additions & 0 deletions internal/datasource/service_context.go
Original file line number Diff line number Diff line change
@@ -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)...)
}
8 changes: 6 additions & 2 deletions internal/model/service_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ 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) {
sc.Id = types.StringValue(response.ID)
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)

Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func (p *PluralProvider) DataSources(_ context.Context) []func() datasource.Data
ds.NewConfigDataSource,
ds.NewPRAutomationDataSource,
ds.NewInfrastructureStackDataSource,
ds.NewServiceContextDataSource,
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/resource/service_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a0b7900

Please sign in to comment.