|
| 1 | +// Copyright 2023, Ryan Parman |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package corefuncprovider // lint:no_dupe |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/chanced/caps" |
| 23 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 24 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 25 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 26 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 27 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 28 | + "github.com/lithammer/dedent" |
| 29 | +) |
| 30 | + |
| 31 | +// Ensure the implementation satisfies the expected interfaces. |
| 32 | +var ( |
| 33 | + _ datasource.DataSource = &strConstantDataSource{} |
| 34 | + _ datasource.DataSourceWithConfigure = &strConstantDataSource{} |
| 35 | +) |
| 36 | + |
| 37 | +// strConstantDataSource is the data source implementation. |
| 38 | +type ( |
| 39 | + strConstantDataSource struct{} |
| 40 | + |
| 41 | + // strConstantDataSourceModel maps the data source schema data. |
| 42 | + strConstantDataSourceModel struct { |
| 43 | + ID types.Int64 `tfsdk:"id"` |
| 44 | + String types.String `tfsdk:"string"` |
| 45 | + Value types.String `tfsdk:"value"` |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +// StrConstantDataSource is a method that exposes its paired Go function as a |
| 50 | +// Terraform Data Source. |
| 51 | +func StrConstantDataSource() datasource.DataSource { // lint:allow_return_interface |
| 52 | + return &strConstantDataSource{} |
| 53 | +} |
| 54 | + |
| 55 | +// Metadata returns the data source type name. |
| 56 | +func (d *strConstantDataSource) Metadata( |
| 57 | + ctx context.Context, |
| 58 | + req datasource.MetadataRequest, |
| 59 | + resp *datasource.MetadataResponse, |
| 60 | +) { |
| 61 | + tflog.Info(ctx, "Starting StrConstant DataSource Metadata method.") |
| 62 | + |
| 63 | + resp.TypeName = req.ProviderTypeName + "_str_constant" |
| 64 | + |
| 65 | + tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName)) |
| 66 | + tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName)) |
| 67 | + |
| 68 | + tflog.Info(ctx, "Ending StrConstant DataSource Metadata method.") |
| 69 | +} |
| 70 | + |
| 71 | +// Schema defines the schema for the data source. |
| 72 | +func (d *strConstantDataSource) Schema( |
| 73 | + ctx context.Context, |
| 74 | + _ datasource.SchemaRequest, |
| 75 | + resp *datasource.SchemaResponse, |
| 76 | +) { |
| 77 | + tflog.Info(ctx, "Starting StrConstant DataSource Schema method.") |
| 78 | + |
| 79 | + resp.Schema = schema.Schema{ |
| 80 | + MarkdownDescription: strings.TrimSpace(dedent.Dedent(` |
| 81 | + Converts a string to ` + "`" + `CONSTANT_CASE` + "`" + `, removing any non-alphanumeric characters. |
| 82 | + Also known as ` + "`" + `SCREAMING_SNAKE_CASE` + "`" + `. |
| 83 | +
|
| 84 | + Maps to the [` + "`" + `caps.ToScreamingSnake()` + "`" + |
| 85 | + `](https://pkg.go.dev/github.com/chanced/caps#ToScreamingSnake) |
| 86 | + Go method, which can be used in ` + Terratest + `. |
| 87 | + `)), |
| 88 | + Attributes: map[string]schema.Attribute{ |
| 89 | + "id": schema.Int64Attribute{ |
| 90 | + Description: "Not used. Required by the " + TPF + ".", |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + "string": schema.StringAttribute{ |
| 94 | + Description: "The string to convert to `CONSTANT_CASE`.", |
| 95 | + Required: true, |
| 96 | + }, |
| 97 | + "value": schema.StringAttribute{ |
| 98 | + Description: "The value of the string.", |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + tflog.Info(ctx, "Ending StrConstant DataSource Schema method.") |
| 105 | +} |
| 106 | + |
| 107 | +// Configure adds the provider configured client to the data source. |
| 108 | +func (d *strConstantDataSource) Configure( |
| 109 | + ctx context.Context, |
| 110 | + req datasource.ConfigureRequest, |
| 111 | + _ *datasource.ConfigureResponse, |
| 112 | +) { |
| 113 | + tflog.Info(ctx, "Starting StrConstant DataSource Configure method.") |
| 114 | + |
| 115 | + if req.ProviderData == nil { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + tflog.Info(ctx, "Ending StrConstant DataSource Configure method.") |
| 120 | +} |
| 121 | + |
| 122 | +func (d strConstantDataSource) Create( |
| 123 | + ctx context.Context, |
| 124 | + req resource.CreateRequest, // lint:allow_large_memory |
| 125 | + resp *resource.CreateResponse, |
| 126 | +) { |
| 127 | + tflog.Info(ctx, "Starting StrConstant DataSource Create method.") |
| 128 | + |
| 129 | + var plan strConstantDataSourceModel |
| 130 | + |
| 131 | + diags := req.Plan.Get(ctx, &plan) |
| 132 | + resp.Diagnostics.Append(diags...) |
| 133 | + |
| 134 | + if resp.Diagnostics.HasError() { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + tflog.Info(ctx, "Ending StrConstant DataSource Create method.") |
| 139 | +} |
| 140 | + |
| 141 | +// Read refreshes the Terraform state with the latest data. |
| 142 | +func (d *strConstantDataSource) Read( // lint:no_dupe |
| 143 | + ctx context.Context, |
| 144 | + _ datasource.ReadRequest, // lint:allow_large_memory |
| 145 | + resp *datasource.ReadResponse, |
| 146 | +) { |
| 147 | + tflog.Info(ctx, "Starting StrConstant DataSource Read method.") |
| 148 | + |
| 149 | + var state strConstantDataSourceModel |
| 150 | + diags := resp.State.Get(ctx, &state) |
| 151 | + resp.Diagnostics.Append(diags...) |
| 152 | + |
| 153 | + state.ID = types.Int64Value(1) |
| 154 | + |
| 155 | + state.Value = types.StringValue( |
| 156 | + caps.ToScreamingSnake( |
| 157 | + state.String.ValueString(), |
| 158 | + ), |
| 159 | + ) |
| 160 | + |
| 161 | + diags = resp.State.Set(ctx, &state) |
| 162 | + resp.Diagnostics.Append(diags...) |
| 163 | + |
| 164 | + if resp.Diagnostics.HasError() { |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + tflog.Info(ctx, "Ending StrConstant DataSource Read method.") |
| 169 | +} |
0 commit comments