|
| 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 | +} |
0 commit comments