Skip to content

Commit b69b67b

Browse files
committed
feat: Added the Terraform wrapper and docs for EnvEnsure().
1 parent 2f0a4ae commit b69b67b

File tree

9 files changed

+271
-7
lines changed

9 files changed

+271
-7
lines changed
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package corefuncprovider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/northwood-labs/terraform-provider-corefunc/corefunc"
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/resource"
14+
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-log/tflog"
16+
"github.com/lithammer/dedent"
17+
)
18+
19+
// Ensure the implementation satisfies the expected interfaces.
20+
var (
21+
_ datasource.DataSource = &envEnsureDataSource{}
22+
_ datasource.DataSourceWithConfigure = &envEnsureDataSource{}
23+
)
24+
25+
// envEnsureDataSource is the data source implementation.
26+
type (
27+
envEnsureDataSource struct{}
28+
29+
// envEnsureDataSourceModel maps the data source schema data.
30+
envEnsureDataSourceModel struct {
31+
ID types.Int64 `tfsdk:"id"`
32+
Name types.String `tfsdk:"name"`
33+
Value types.String `tfsdk:"value"`
34+
}
35+
)
36+
37+
// EnvEnsureDataSource is a method that exposes its paired Go function as a
38+
// Terraform Data Source.
39+
func EnvEnsureDataSource() datasource.DataSource { // lint:allow_return_interface
40+
return &envEnsureDataSource{}
41+
}
42+
43+
// Metadata returns the data source type name.
44+
func (d *envEnsureDataSource) Metadata(
45+
ctx context.Context,
46+
req datasource.MetadataRequest,
47+
resp *datasource.MetadataResponse,
48+
) {
49+
tflog.Info(ctx, "Starting EnvEnsure DataSource Metadata method.")
50+
51+
resp.TypeName = req.ProviderTypeName + "_env_ensure"
52+
53+
tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName))
54+
tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName))
55+
56+
tflog.Info(ctx, "Ending EnvEnsure DataSource Metadata method.")
57+
}
58+
59+
// Schema defines the schema for the data source.
60+
func (d *envEnsureDataSource) Schema(
61+
ctx context.Context,
62+
_ datasource.SchemaRequest,
63+
resp *datasource.SchemaResponse,
64+
) {
65+
tflog.Info(ctx, "Starting EnvEnsure DataSource Schema method.")
66+
67+
resp.Schema = schema.Schema{
68+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
69+
Ensures that a given environment variable is set to a non-empty value.
70+
If the environment variable is unset or if it is set to an empty string,
71+
it will trigger a Terraform-level error.
72+
73+
Maps to the ` + linkPackage("EnvEnsure") + ` Go method, which can be used in
74+
` + Terratest + `.
75+
`)),
76+
Attributes: map[string]schema.Attribute{
77+
"id": schema.Int64Attribute{
78+
Description: "Not used. Required by the " + TPF + ".",
79+
Computed: true,
80+
},
81+
"name": schema.StringAttribute{
82+
Description: "The name of the environment variable to check.",
83+
Required: true,
84+
},
85+
"value": schema.StringAttribute{
86+
Description: "The value of the environment variable, if it exists.",
87+
Computed: true,
88+
},
89+
},
90+
}
91+
92+
tflog.Info(ctx, "Ending EnvEnsure DataSource Schema method.")
93+
}
94+
95+
// Configure adds the provider configured client to the data source.
96+
func (d *envEnsureDataSource) Configure(
97+
ctx context.Context,
98+
req datasource.ConfigureRequest,
99+
_ *datasource.ConfigureResponse,
100+
) {
101+
tflog.Info(ctx, "Starting EnvEnsure DataSource Configure method.")
102+
103+
if req.ProviderData == nil {
104+
return
105+
}
106+
107+
tflog.Info(ctx, "Ending EnvEnsure DataSource Configure method.")
108+
}
109+
110+
func (d envEnsureDataSource) Create(
111+
ctx context.Context,
112+
req resource.CreateRequest, // lint:allow_large_memory
113+
resp *resource.CreateResponse,
114+
) {
115+
tflog.Info(ctx, "Starting EnvEnsure DataSource Create method.")
116+
117+
var plan envEnsureDataSourceModel
118+
119+
diags := req.Plan.Get(ctx, &plan)
120+
resp.Diagnostics.Append(diags...)
121+
122+
if resp.Diagnostics.HasError() {
123+
return
124+
}
125+
126+
tflog.Info(ctx, "Ending EnvEnsure DataSource Create method.")
127+
}
128+
129+
// Read refreshes the Terraform state with the latest data.
130+
func (d *envEnsureDataSource) Read(
131+
ctx context.Context,
132+
_ datasource.ReadRequest, // lint:allow_large_memory
133+
resp *datasource.ReadResponse,
134+
) {
135+
tflog.Info(ctx, "Starting EnvEnsure DataSource Read method.")
136+
137+
var state envEnsureDataSourceModel
138+
diags := resp.State.Get(ctx, &state)
139+
resp.Diagnostics.Append(diags...)
140+
141+
state.ID = types.Int64Value(1)
142+
143+
err := corefunc.EnvEnsure(state.Name.ValueString())
144+
if err != nil {
145+
resp.Diagnostics.AddError(
146+
"Undefined Environment Variable",
147+
err.Error(),
148+
)
149+
150+
return
151+
}
152+
153+
// Slightly differ from the Go function, which returns true if the
154+
// environment variable is defined. In this case, we want to return the
155+
// value of the environment variable.
156+
state.Value = types.StringValue(
157+
os.Getenv(
158+
state.Name.ValueString(),
159+
),
160+
)
161+
162+
diags = resp.State.Set(ctx, &state)
163+
resp.Diagnostics.Append(diags...)
164+
165+
if resp.Diagnostics.HasError() {
166+
return
167+
}
168+
169+
tflog.Info(ctx, "Ending EnvEnsure DataSource Read method.")
170+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package corefuncprovider
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccEnvEnsureDataSource(t *testing.T) {
15+
funcName := traceFuncName()
16+
17+
for name, tc := range testfixtures.EnvEnsureTestTable {
18+
fmt.Printf(
19+
"=== RUN %s/%s\n",
20+
strings.TrimSpace(funcName),
21+
strings.TrimSpace(name),
22+
)
23+
24+
_ = os.Setenv(tc.EnvVarName, tc.SetValue)
25+
26+
resource.Test(t, resource.TestCase{
27+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
28+
Steps: []resource.TestStep{
29+
{
30+
Config: providerConfig + `
31+
data "corefunc_env_ensure" "env" {
32+
name = "` + tc.EnvVarName + `"
33+
}
34+
`,
35+
Check: resource.ComposeAggregateTestCheckFunc(
36+
resource.TestCheckResourceAttr(
37+
"data.corefunc_env_ensure.env",
38+
"value",
39+
tc.SetValue,
40+
),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
}

corefuncprovider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
8888

8989
return []func() datasource.DataSource{
9090
TruncateLabelDataSource,
91+
EnvEnsureDataSource,
9192
}
9293
}
9394

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -------------------------------------------------------------------------
2+
# AWS_DEFAULT_REGION="us-east-1"
3+
4+
data "corefunc_env_ensure" "aws_default_region" {
5+
name = "AWS_DEFAULT_REGION"
6+
}
7+
8+
# `aws_default_region_value` is the read-only attribute containing the
9+
# value of the environment variable
10+
output "aws_default_region_value" {
11+
value = data.corefunc_env_ensure.aws_default_region.value
12+
}
13+
14+
# => us-east-1
15+
16+
# -------------------------------------------------------------------------
17+
# AWS_PAGER=""
18+
19+
data "corefunc_env_ensure" "aws_pager" {
20+
name = "AWS_PAGER"
21+
}
22+
23+
# `aws_pager_value` is the read-only attribute containing the value of the
24+
# environment variable
25+
output "aws_pager_value" {
26+
value = data.corefunc_env_ensure.aws_pager.value
27+
}
28+
29+
# => This will trigger an error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = "~> 1.1"
3+
4+
required_providers {
5+
corefunc = {
6+
source = "northwood-labs/corefunc"
7+
version = "~> 1.0"
8+
}
9+
}
10+
}
11+
12+
# There are no configuration options
13+
14+
provider "corefunc" {}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# Pass the prefix and the label, then limit to 64 characters.
1+
# Pass the prefix and the label, then limit to 64 characters
2+
23
data "corefunc_str_truncate_label" "label" {
34
prefix = "NW-ZZZ-CLOUD-TEST-APP-CLOUD-PROD-CRIT"
45
label = "K8S Pods Not Running Deployment Check"
56
max_length = 64
67
}
78

8-
# `value` is the read-only attribute containing the truncated label.
9+
# `value` is the read-only attribute containing the truncated label
10+
911
output "value" {
1012
value = data.corefunc_str_truncate_label.label.value
1113
}
1214

13-
#=> NW-ZZZ-CLOUD-TEST-APP-CLOUD-PR…: K8S Pods Not Running Deploymen…
15+
# => NW-ZZZ-CLOUD-TEST-APP-CLOUD-PR…: K8S Pods Not Running Deploymen…

examples/data-sources/corefunc_str_truncate_label/versions.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ terraform {
99
}
1010
}
1111

12-
# There are no configuration options.
12+
# There are no configuration options
13+
1314
provider "corefunc" {}

examples/provider/provider.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ terraform {
99
}
1010
}
1111

12-
# There are no configuration options.
12+
# There are no configuration options
13+
1314
provider "corefunc" {}

templates/data-sources.md.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ description: |-
1111

1212
{{ .Description | trimspace }}
1313

14-
## Example Usage
14+
{{ if .HasExample -}}## Example Usage
1515

16-
{{ tffile (printf "examples/data-sources/%s/data-source.tf" .Name)}}
16+
{{ tffile (printf "examples/data-sources/%s/data-source.tf" .Name)}}{{- end }}
1717

1818
{{ .SchemaMarkdown | trimspace }}
1919

0 commit comments

Comments
 (0)