Skip to content

Commit a0b7900

Browse files
authored
Merge pull request #59 from pluralsh/marcin/prod-2668-add-plural_service_context-datasource
feat: Add service context data source
2 parents 9e3d4ab + a53450b commit a0b7900

File tree

7 files changed

+128
-10
lines changed

7 files changed

+128
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "plural_service_context Data Source - terraform-provider-plural"
4+
subcategory: ""
5+
description: |-
6+
A representation of a service context that can be reused during service deployment templating.
7+
---
8+
9+
# plural_service_context (Data Source)
10+
11+
A representation of a service context that can be reused during service deployment templating.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Human-readable name of this service context.
21+
22+
### Read-Only
23+
24+
- `configuration` (Map of String)
25+
- `id` (String) Internal identifier of this service context.

docs/resources/service_context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ A representation of a service context that can be reused during service deployme
2626

2727
### Read-Only
2828

29-
- `id` (String) Internal identifier of this provider.
29+
- `id` (String) Internal identifier of this service context.

example/servicecontext/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
plural = {
44
source = "pluralsh/plural"
5-
version = "0.0.1"
5+
version = "0.2.1"
66
}
77
}
88
}
@@ -12,7 +12,7 @@ provider "plural" {
1212
}
1313

1414
resource "plural_service_context" "service_context" {
15-
name = "service-context-tf"
15+
name = "service-context-test"
1616
configuration = {
1717
"env" = "prod"
1818
"test" = "some-value"
@@ -21,3 +21,7 @@ resource "plural_service_context" "service_context" {
2121
"test" = "some-secret-value"
2222
}
2323
}
24+
25+
data "plural_service_context" "service_context" {
26+
name = plural_service_context.service_context.name
27+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package datasource
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"terraform-provider-plural/internal/client"
8+
"terraform-provider-plural/internal/common"
9+
"terraform-provider-plural/internal/model"
10+
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
)
15+
16+
func NewServiceContextDataSource() datasource.DataSource {
17+
return &serviceContextDataSource{}
18+
}
19+
20+
type serviceContextDataSource struct {
21+
client *client.Client
22+
}
23+
24+
func (d *serviceContextDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
25+
resp.TypeName = req.ProviderTypeName + "_service_context"
26+
}
27+
28+
func (d *serviceContextDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
29+
resp.Schema = schema.Schema{
30+
MarkdownDescription: "A representation of a service context that can be reused during service deployment templating.",
31+
Attributes: map[string]schema.Attribute{
32+
"id": schema.StringAttribute{
33+
Computed: true,
34+
Description: "Internal identifier of this service context.",
35+
MarkdownDescription: "Internal identifier of this service context.",
36+
},
37+
"name": schema.StringAttribute{
38+
Description: "Human-readable name of this service context.",
39+
MarkdownDescription: "Human-readable name of this service context.",
40+
Required: true,
41+
},
42+
"configuration": schema.MapAttribute{
43+
Description: "",
44+
MarkdownDescription: "",
45+
Computed: true,
46+
ElementType: types.StringType,
47+
},
48+
},
49+
}
50+
}
51+
52+
func (d *serviceContextDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
53+
if req.ProviderData == nil {
54+
return
55+
}
56+
57+
data, ok := req.ProviderData.(*common.ProviderData)
58+
if !ok {
59+
resp.Diagnostics.AddError(
60+
"Unexpected Service Context Data Source Configure Type",
61+
fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
62+
)
63+
return
64+
}
65+
66+
d.client = data.Client
67+
}
68+
69+
func (d *serviceContextDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
70+
data := new(model.ServiceContext)
71+
resp.Diagnostics.Append(req.Config.Get(ctx, data)...)
72+
if resp.Diagnostics.HasError() {
73+
return
74+
}
75+
76+
response, err := d.client.GetServiceContext(ctx, data.Name.ValueString())
77+
if err != nil {
78+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read service context, got error: %s", err))
79+
return
80+
}
81+
82+
data.From(response.ServiceContext, ctx, resp.Diagnostics)
83+
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
84+
}

internal/model/service_context.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ type ServiceContext struct {
1414
Id types.String `tfsdk:"id"`
1515
Name types.String `tfsdk:"name"`
1616
Configuration types.Map `tfsdk:"configuration"`
17-
Secrets types.Map `tfsdk:"secrets"`
1817
}
1918

2019
func (sc *ServiceContext) From(response *console.ServiceContextFragment, ctx context.Context, d diag.Diagnostics) {
2120
sc.Id = types.StringValue(response.ID)
2221
sc.Configuration = common.MapFrom(response.Configuration, ctx, d)
2322
}
2423

25-
func (sc *ServiceContext) Attributes(ctx context.Context, d diag.Diagnostics) console.ServiceContextAttributes {
24+
type ServiceContextExtended struct {
25+
ServiceContext
26+
Secrets types.Map `tfsdk:"secrets"`
27+
}
28+
29+
func (sc *ServiceContextExtended) Attributes(ctx context.Context, d diag.Diagnostics) console.ServiceContextAttributes {
2630
configuration := make(map[string]types.String, len(sc.Configuration.Elements()))
2731
sc.Configuration.ElementsAs(ctx, &configuration, false)
2832

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func (p *PluralProvider) DataSources(_ context.Context) []func() datasource.Data
208208
ds.NewConfigDataSource,
209209
ds.NewPRAutomationDataSource,
210210
ds.NewInfrastructureStackDataSource,
211+
ds.NewServiceContextDataSource,
211212
}
212213
}
213214

internal/resource/service_context.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *ServiceContextResource) Schema(_ context.Context, _ resource.SchemaRequ
4040
"id": schema.StringAttribute{
4141
Computed: true,
4242
Description: "Internal identifier of this service context.",
43-
MarkdownDescription: "Internal identifier of this provider.",
43+
MarkdownDescription: "Internal identifier of this service context.",
4444
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
4545
},
4646
"name": schema.StringAttribute{
@@ -85,7 +85,7 @@ func (r *ServiceContextResource) Configure(_ context.Context, req resource.Confi
8585
}
8686

8787
func (r *ServiceContextResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
88-
data := new(model.ServiceContext)
88+
data := new(model.ServiceContextExtended)
8989
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
9090
if resp.Diagnostics.HasError() {
9191
return
@@ -102,7 +102,7 @@ func (r *ServiceContextResource) Create(ctx context.Context, req resource.Create
102102
}
103103

104104
func (r *ServiceContextResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
105-
data := new(model.ServiceContext)
105+
data := new(model.ServiceContextExtended)
106106
resp.Diagnostics.Append(req.State.Get(ctx, data)...)
107107
if resp.Diagnostics.HasError() {
108108
return
@@ -119,7 +119,7 @@ func (r *ServiceContextResource) Read(ctx context.Context, req resource.ReadRequ
119119
}
120120

121121
func (r *ServiceContextResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
122-
data := new(model.ServiceContext)
122+
data := new(model.ServiceContextExtended)
123123
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
124124
if resp.Diagnostics.HasError() {
125125
return
@@ -135,7 +135,7 @@ func (r *ServiceContextResource) Update(ctx context.Context, req resource.Update
135135
}
136136

137137
func (r *ServiceContextResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
138-
data := new(model.ServiceContext)
138+
data := new(model.ServiceContextExtended)
139139
resp.Diagnostics.Append(req.State.Get(ctx, data)...)
140140
if resp.Diagnostics.HasError() {
141141
return

0 commit comments

Comments
 (0)