Skip to content

Commit fbf6f14

Browse files
committed
feat: Added str_endswith.
1 parent 7cb7301 commit fbf6f14

22 files changed

+864
-189
lines changed

acc-coverage.png

-1.64 KB
Loading

acc-coverage.svg

Lines changed: 206 additions & 180 deletions
Loading

bats/ds_str_endswith.bats.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bats
2+
# https://bats-core.readthedocs.io/en/stable/writing-tests.html
3+
4+
@test "corefunc_str_endswith: attrs" {
5+
run bash -c "tfschema data show -format=json corefunc_str_endswith | jq -Mrc '.attributes[]'"
6+
7+
[[ ${status} -eq 0 ]]
8+
[[ ${lines[0]} == '' ]]
9+
}

corefuncprovider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
140140
StrBase64GunzipDataSource,
141141
StrCamelDataSource,
142142
StrConstantDataSource,
143+
StrEndswithDataSource,
143144
StrIterativeReplaceDataSource,
144145
StrKebabDataSource,
145146
StrLeftpadDataSource,
@@ -192,6 +193,7 @@ func (p *coreFuncProvider) Functions(ctx context.Context) []func() function.Func
192193
StrBase64GunzipFunction,
193194
StrCamelFunction,
194195
StrConstantFunction,
196+
StrEndswithFunction,
195197
StrIterativeReplaceFunction,
196198
StrKebabFunction,
197199
StrLeftpadFunction,
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright 2024-2025, Northwood Labs, LLC <license@northwood-labs.com>
2+
// Copyright 2023-2025, Ryan Parman <rparman@northwood-labs.com>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package corefuncprovider // lint:no_dupe
17+
18+
import (
19+
"context"
20+
"strings"
21+
22+
"github.com/hashicorp/terraform-plugin-framework/datasource"
23+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
24+
"github.com/hashicorp/terraform-plugin-framework/resource"
25+
"github.com/hashicorp/terraform-plugin-framework/types"
26+
"github.com/hashicorp/terraform-plugin-log/tflog"
27+
"github.com/lithammer/dedent"
28+
29+
"github.com/northwood-labs/terraform-provider-corefunc/v2/corefunc"
30+
)
31+
32+
// Ensure the implementation satisfies the expected interfaces.
33+
var (
34+
_ datasource.DataSource = &strEndswithDataSource{}
35+
_ datasource.DataSourceWithConfigure = &strEndswithDataSource{}
36+
)
37+
38+
// strEndswithDataSource is the data source implementation.
39+
type (
40+
strEndswithDataSource struct{}
41+
42+
// strEndswithDataSourceModel maps the data source schema data.
43+
strEndswithDataSourceModel struct {
44+
Input types.String `tfsdk:"input"`
45+
Suffix types.String `tfsdk:"suffix"`
46+
Value types.Bool `tfsdk:"value"`
47+
}
48+
)
49+
50+
// StrEndswithDataSource is a method that exposes its paired Go function as a
51+
// Terraform Data Source.
52+
func StrEndswithDataSource() datasource.DataSource { // lint:allow_return_interface
53+
return &strEndswithDataSource{}
54+
}
55+
56+
// Metadata returns the data source type name.
57+
func (d *strEndswithDataSource) Metadata(
58+
ctx context.Context,
59+
req datasource.MetadataRequest,
60+
resp *datasource.MetadataResponse,
61+
) {
62+
tflog.Debug(ctx, "Starting StrEndswith DataSource Metadata method.")
63+
64+
resp.TypeName = req.ProviderTypeName + "_str_endswith"
65+
66+
tflog.Debug(ctx, "req.ProviderTypeName = "+req.ProviderTypeName)
67+
tflog.Debug(ctx, "resp.TypeName = "+resp.TypeName)
68+
69+
tflog.Debug(ctx, "Ending StrEndswith DataSource Metadata method.")
70+
}
71+
72+
// Schema defines the schema for the data source.
73+
func (d *strEndswithDataSource) Schema(
74+
ctx context.Context,
75+
_ datasource.SchemaRequest,
76+
resp *datasource.SchemaResponse,
77+
) {
78+
tflog.Debug(ctx, "Starting StrEndswith DataSource Schema method.")
79+
80+
resp.Schema = schema.Schema{
81+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
82+
Takes a string to check and a suffix string, and returns true if the
83+
string ends with that exact suffix.
84+
85+
-> This is identical to the built-in ` + "`endswith`" + ` function
86+
which was added in Terraform 1.3. This provides a 1:1 implementation
87+
that can be used with Terratest or other Go code, as well as with
88+
OpenTofu and Terraform going all the way back to v1.0.
89+
90+
Maps to the ` + linkPackage("StrEndsWith") + ` Go method, which can be used in ` + Terratest + `.
91+
`)),
92+
Attributes: map[string]schema.Attribute{
93+
"input": schema.StringAttribute{
94+
MarkdownDescription: " The string to check.",
95+
Required: true,
96+
},
97+
"suffix": schema.StringAttribute{
98+
MarkdownDescription: "The suffix to check for.",
99+
Required: true,
100+
},
101+
"value": schema.BoolAttribute{
102+
MarkdownDescription: "Whether or not the string ends with the specified suffix. A " +
103+
"value of `true` indicates that it does. A value of `false` indicates that it does not.",
104+
Computed: true,
105+
},
106+
},
107+
}
108+
109+
tflog.Debug(ctx, "Ending StrEndswith DataSource Schema method.")
110+
}
111+
112+
// Configure adds the provider configured client to the data source.
113+
func (d *strEndswithDataSource) Configure(
114+
ctx context.Context,
115+
req datasource.ConfigureRequest,
116+
_ *datasource.ConfigureResponse,
117+
) {
118+
tflog.Debug(ctx, "Starting StrEndswith DataSource Configure method.")
119+
120+
if req.ProviderData == nil {
121+
return
122+
}
123+
124+
tflog.Debug(ctx, "Ending StrEndswith DataSource Configure method.")
125+
}
126+
127+
func (d *strEndswithDataSource) Create(
128+
ctx context.Context,
129+
req resource.CreateRequest, // lint:allow_large_memory
130+
resp *resource.CreateResponse,
131+
) {
132+
tflog.Debug(ctx, "Starting StrEndswith DataSource Create method.")
133+
134+
var plan strEndswithDataSourceModel
135+
136+
diags := req.Plan.Get(ctx, &plan)
137+
resp.Diagnostics.Append(diags...)
138+
139+
if resp.Diagnostics.HasError() {
140+
return
141+
}
142+
143+
tflog.Debug(ctx, "Ending StrEndswith DataSource Create method.")
144+
}
145+
146+
// Read refreshes the Terraform state with the latest data.
147+
func (d *strEndswithDataSource) Read( // lint:no_dupe
148+
ctx context.Context,
149+
_ datasource.ReadRequest, // lint:allow_large_memory
150+
resp *datasource.ReadResponse,
151+
) {
152+
tflog.Debug(ctx, "Starting StrEndswith DataSource Read method.")
153+
154+
var state strEndswithDataSourceModel
155+
156+
diags := resp.State.Get(ctx, &state)
157+
resp.Diagnostics.Append(diags...)
158+
159+
value := corefunc.StrEndsWith(
160+
state.Input.ValueString(),
161+
state.Suffix.ValueString(),
162+
)
163+
164+
state.Value = types.BoolValue(value)
165+
166+
diags = resp.State.Set(ctx, &state)
167+
resp.Diagnostics.Append(diags...)
168+
169+
if resp.Diagnostics.HasError() {
170+
return
171+
}
172+
173+
tflog.Debug(ctx, "Ending StrEndswith DataSource Read method.")
174+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data "corefunc_str_endswith" "str_endswith" {
2+
input = "{{ .Input }}"
3+
suffix = "{{ .Suffix }}"
4+
}
5+
6+
#=> {{ .Expected }}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2024-2025, Northwood Labs, LLC <license@northwood-labs.com>
2+
// Copyright 2023-2025, Ryan Parman <rparman@northwood-labs.com>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package corefuncprovider // lint:no_dupe
17+
18+
import (
19+
"bytes"
20+
"fmt"
21+
"log"
22+
"os"
23+
"strconv"
24+
"strings"
25+
"testing"
26+
"text/template"
27+
28+
"github.com/northwood-labs/terraform-provider-corefunc/v2/testfixtures"
29+
30+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
31+
)
32+
33+
func TestAccStrEndswithDataSource(t *testing.T) {
34+
t.Parallel()
35+
36+
funcName := traceFuncName()
37+
38+
for name, tc := range testfixtures.StrEndsWithTestTable { // lint:no_dupe
39+
fmt.Printf(
40+
"=== RUN %s/%s\n",
41+
strings.TrimSpace(funcName),
42+
strings.TrimSpace(name),
43+
)
44+
45+
buf := &bytes.Buffer{}
46+
tmpl := template.Must(
47+
template.New("str_endswith_data_source_fixture.tftpl").
48+
Funcs(FuncMap()).
49+
ParseFiles("str_endswith_data_source_fixture.tftpl"),
50+
)
51+
52+
err := tmpl.Execute(buf, tc)
53+
if err != nil {
54+
log.Fatalln(err)
55+
}
56+
57+
if os.Getenv("PROVIDER_DEBUG") != "" {
58+
fmt.Fprintln(os.Stderr, buf.String())
59+
}
60+
61+
resource.Test(t, resource.TestCase{
62+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
63+
Steps: []resource.TestStep{
64+
{
65+
Config: providerConfig + buf.String(),
66+
Check: resource.ComposeAggregateTestCheckFunc(
67+
resource.TestCheckResourceAttr(
68+
"data.corefunc_str_endswith.str_endswith",
69+
"value",
70+
strconv.FormatBool(tc.Expected),
71+
),
72+
),
73+
},
74+
},
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)