Skip to content

Commit bcb8d20

Browse files
authored
feat: add context field management (#216)
1 parent 08d0331 commit bcb8d20

File tree

9 files changed

+663
-3
lines changed

9 files changed

+663
-3
lines changed

docs/data-sources/context_field.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "unleash_context_field Data Source - terraform-provider-unleash"
4+
subcategory: ""
5+
description: |-
6+
Fetch a context field.
7+
---
8+
9+
# unleash_context_field (Data Source)
10+
11+
Fetch a context field.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) The name of the context field.
21+
22+
### Optional
23+
24+
- `description` (String) A description of the context field.
25+
- `legal_values` (Attributes List) Legal values for this context field. If not set, then any value is available for this context field. (see [below for nested schema](#nestedatt--legal_values))
26+
- `stickiness` (Boolean) Whether this field is available for custom stickiness
27+
28+
<a id="nestedatt--legal_values"></a>
29+
### Nested Schema for `legal_values`
30+
31+
Read-Only:
32+
33+
- `description` (String) Description of the allowed value.
34+
- `value` (String) The allowed value.

docs/resources/context_field.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "unleash_context_field Resource - terraform-provider-unleash"
4+
subcategory: ""
5+
description: |-
6+
Fetch a context field.
7+
---
8+
9+
# unleash_context_field (Resource)
10+
11+
Fetch a context field.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) The name of the context field.
21+
22+
### Optional
23+
24+
- `description` (String) A description of the context field.
25+
- `legal_values` (Attributes List) Legal values for this context field. If not set, then any value is available for this context field. (see [below for nested schema](#nestedatt--legal_values))
26+
- `stickiness` (Boolean) Whether this field is available for custom stickiness. Defaults to false if not set.
27+
28+
<a id="nestedatt--legal_values"></a>
29+
### Nested Schema for `legal_values`
30+
31+
Required:
32+
33+
- `value` (String) The allowed value.
34+
35+
Optional:
36+
37+
- `description` (String) Description of the allowed value.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/Masterminds/semver v1.5.0
7-
github.com/Unleash/unleash-server-api-go v0.5.1
7+
github.com/Unleash/unleash-server-api-go v0.5.2
88
github.com/fatih/structs v1.1.0
99
github.com/hashicorp/terraform-plugin-docs v0.18.0
1010
github.com/hashicorp/terraform-plugin-framework v1.4.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBa
1313
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
1414
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX7IL/m9Y5LO+KQYv+t1CQOiFe6+SV2J7bE=
1515
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
16-
github.com/Unleash/unleash-server-api-go v0.5.1 h1:RFprAg66kfj/A7kwpiYeOqO1lpgAF8rHEODAsnYMC4U=
17-
github.com/Unleash/unleash-server-api-go v0.5.1/go.mod h1:/IvCtBfBrOVOa67elCLig45kt6NYkk79oxmRW80WOFY=
16+
github.com/Unleash/unleash-server-api-go v0.5.2 h1:jm/+AJzUmsotfyIODFFM1IjzCOT4515YMdzktRljcWk=
17+
github.com/Unleash/unleash-server-api-go v0.5.2/go.mod h1:/IvCtBfBrOVOa67elCLig45kt6NYkk79oxmRW80WOFY=
1818
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
1919
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
2020
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
unleash "github.com/Unleash/unleash-server-api-go/client"
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
11+
)
12+
13+
var (
14+
_ datasource.DataSource = &contextFieldDataSource{}
15+
_ datasource.DataSourceWithConfigure = &contextFieldDataSource{}
16+
)
17+
18+
func NewContextFieldDataSource() datasource.DataSource {
19+
return &contextFieldDataSource{}
20+
}
21+
22+
type contextFieldDataSource struct {
23+
client *unleash.APIClient
24+
}
25+
26+
type legalValueResourceModel struct {
27+
Value types.String `tfsdk:"value"`
28+
Description types.String `tfsdk:"description"`
29+
}
30+
31+
type contextFieldDataSourceModel struct {
32+
Name types.String `tfsdk:"name"`
33+
Description types.String `tfsdk:"description"`
34+
Stickiness types.Bool `tfsdk:"stickiness"`
35+
LegalValues []legalValueResourceModel `tfsdk:"legal_values"`
36+
}
37+
38+
func (d *contextFieldDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
39+
if req.ProviderData == nil {
40+
return
41+
}
42+
43+
client, ok := req.ProviderData.(*unleash.APIClient)
44+
if !ok {
45+
tflog.Error(ctx, "Unable to prepare client")
46+
return
47+
}
48+
d.client = client
49+
50+
}
51+
52+
func (d *contextFieldDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
53+
resp.TypeName = req.ProviderTypeName + "_context_field"
54+
}
55+
56+
func (d *contextFieldDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
57+
resp.Schema = schema.Schema{
58+
Description: "Fetch a context field.",
59+
Attributes: map[string]schema.Attribute{
60+
"name": schema.StringAttribute{
61+
Description: "The name of the context field.",
62+
Required: true,
63+
},
64+
"description": schema.StringAttribute{
65+
Description: "A description of the context field.",
66+
Optional: true,
67+
Computed: true,
68+
},
69+
"stickiness": schema.BoolAttribute{
70+
Description: "Whether this field is available for custom stickiness",
71+
Optional: true,
72+
Computed: true,
73+
},
74+
"legal_values": schema.ListNestedAttribute{
75+
Description: "Legal values for this context field. If not set, then any value is available for this context field.",
76+
Optional: true,
77+
Computed: true,
78+
NestedObject: schema.NestedAttributeObject{
79+
Attributes: map[string]schema.Attribute{
80+
"value": schema.StringAttribute{
81+
Description: "The allowed value.",
82+
Computed: true,
83+
},
84+
"description": schema.StringAttribute{
85+
Description: "Description of the allowed value.",
86+
Computed: true,
87+
},
88+
},
89+
},
90+
},
91+
},
92+
}
93+
}
94+
95+
func (d *contextFieldDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
96+
tflog.Debug(ctx, "Preparing to hydrate context field")
97+
var state contextFieldDataSourceModel
98+
99+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
100+
if resp.Diagnostics.HasError() {
101+
tflog.Error(ctx, "Unable to read context field data source")
102+
return
103+
}
104+
105+
var contextField, apiResponse, err = d.client.ContextAPI.GetContextField(ctx, state.Name.ValueString()).Execute()
106+
107+
if !ValidateApiResponse(apiResponse, 200, &resp.Diagnostics, err) {
108+
return
109+
}
110+
111+
legalValues := make([]legalValueResourceModel, len(contextField.LegalValues))
112+
for i, legalValue := range contextField.LegalValues {
113+
legalValues[i] = legalValueResourceModel{
114+
Value: types.StringValue(legalValue.Value),
115+
}
116+
117+
if legalValue.Description != nil {
118+
legalValues[i].Description = types.StringValue(*legalValue.Description)
119+
} else {
120+
legalValues[i].Description = types.StringNull()
121+
}
122+
}
123+
124+
state = contextFieldDataSourceModel{
125+
Name: types.StringValue(contextField.Name),
126+
LegalValues: legalValues,
127+
}
128+
129+
if contextField.Description.IsSet() {
130+
state.Description = types.StringValue(*contextField.Description.Get())
131+
} else {
132+
state.Description = types.StringNull()
133+
}
134+
135+
if contextField.Stickiness != nil {
136+
state.Stickiness = types.BoolValue(*contextField.Stickiness)
137+
} else {
138+
state.Stickiness = types.BoolNull()
139+
}
140+
141+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
142+
tflog.Debug(ctx, "Finished reading context field data source", map[string]any{"success": true})
143+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccContextFieldDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: `
16+
data "unleash_context_field" "built_in_context_field" {
17+
name = "appName"
18+
}`,
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttr("data.unleash_context_field.built_in_context_field", "name", "appName"),
21+
resource.TestCheckResourceAttr("data.unleash_context_field.built_in_context_field", "description", "Allows you to constrain on application name"),
22+
),
23+
},
24+
},
25+
})
26+
}

0 commit comments

Comments
 (0)