|
| 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 |
| 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 = &strSnakeDataSource{} |
| 34 | + _ datasource.DataSourceWithConfigure = &strSnakeDataSource{} |
| 35 | +) |
| 36 | + |
| 37 | +// strSnakeDataSource is the data source implementation. |
| 38 | +type ( |
| 39 | + strSnakeDataSource struct{} |
| 40 | + |
| 41 | + // strSnakeDataSourceModel maps the data source schema data. |
| 42 | + strSnakeDataSourceModel struct { |
| 43 | + ID types.Int64 `tfsdk:"id"` |
| 44 | + String types.String `tfsdk:"string"` |
| 45 | + Value types.String `tfsdk:"value"` |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +// StrSnakeDataSource is a method that exposes its paired Go function as a |
| 50 | +// Terraform Data Source. |
| 51 | +func StrSnakeDataSource() datasource.DataSource { // lint:allow_return_interface |
| 52 | + return &strSnakeDataSource{} |
| 53 | +} |
| 54 | + |
| 55 | +// Metadata returns the data source type name. |
| 56 | +func (d *strSnakeDataSource) Metadata( |
| 57 | + ctx context.Context, |
| 58 | + req datasource.MetadataRequest, |
| 59 | + resp *datasource.MetadataResponse, |
| 60 | +) { |
| 61 | + tflog.Info(ctx, "Starting StrSnake DataSource Metadata method.") |
| 62 | + |
| 63 | + resp.TypeName = req.ProviderTypeName + "_str_snake" |
| 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 StrSnake DataSource Metadata method.") |
| 69 | +} |
| 70 | + |
| 71 | +// Schema defines the schema for the data source. |
| 72 | +func (d *strSnakeDataSource) Schema( |
| 73 | + ctx context.Context, |
| 74 | + _ datasource.SchemaRequest, |
| 75 | + resp *datasource.SchemaResponse, |
| 76 | +) { |
| 77 | + tflog.Info(ctx, "Starting StrSnake DataSource Schema method.") |
| 78 | + |
| 79 | + resp.Schema = schema.Schema{ |
| 80 | + MarkdownDescription: strings.TrimSpace(dedent.Dedent(` |
| 81 | + Converts a string to ` + "`" + `snake_case` + "`" + `, removing any non-alphanumeric characters. |
| 82 | +
|
| 83 | + Maps to the [` + "`" + `caps.ToSnake()` + "`" + `](https://pkg.go.dev/github.com/chanced/caps#ToSnake) |
| 84 | + Go method, which can be used in ` + Terratest + `. |
| 85 | + `)), |
| 86 | + Attributes: map[string]schema.Attribute{ |
| 87 | + "id": schema.Int64Attribute{ |
| 88 | + Description: "Not used. Required by the " + TPF + ".", |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "string": schema.StringAttribute{ |
| 92 | + Description: "The string to convert to `snake_case`.", |
| 93 | + Required: true, |
| 94 | + }, |
| 95 | + "value": schema.StringAttribute{ |
| 96 | + Description: "The value of the environment variable, if it exists.", |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + tflog.Info(ctx, "Ending StrSnake DataSource Schema method.") |
| 103 | +} |
| 104 | + |
| 105 | +// Configure adds the provider configured client to the data source. |
| 106 | +func (d *strSnakeDataSource) Configure( |
| 107 | + ctx context.Context, |
| 108 | + req datasource.ConfigureRequest, |
| 109 | + _ *datasource.ConfigureResponse, |
| 110 | +) { |
| 111 | + tflog.Info(ctx, "Starting StrSnake DataSource Configure method.") |
| 112 | + |
| 113 | + if req.ProviderData == nil { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + tflog.Info(ctx, "Ending StrSnake DataSource Configure method.") |
| 118 | +} |
| 119 | + |
| 120 | +func (d strSnakeDataSource) Create( |
| 121 | + ctx context.Context, |
| 122 | + req resource.CreateRequest, // lint:allow_large_memory |
| 123 | + resp *resource.CreateResponse, |
| 124 | +) { |
| 125 | + tflog.Info(ctx, "Starting StrSnake DataSource Create method.") |
| 126 | + |
| 127 | + var plan strSnakeDataSourceModel |
| 128 | + |
| 129 | + diags := req.Plan.Get(ctx, &plan) |
| 130 | + resp.Diagnostics.Append(diags...) |
| 131 | + |
| 132 | + if resp.Diagnostics.HasError() { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + tflog.Info(ctx, "Ending StrSnake DataSource Create method.") |
| 137 | +} |
| 138 | + |
| 139 | +// Read refreshes the Terraform state with the latest data. |
| 140 | +func (d *strSnakeDataSource) Read( |
| 141 | + ctx context.Context, |
| 142 | + _ datasource.ReadRequest, // lint:allow_large_memory |
| 143 | + resp *datasource.ReadResponse, |
| 144 | +) { |
| 145 | + tflog.Info(ctx, "Starting StrSnake DataSource Read method.") |
| 146 | + |
| 147 | + var state strSnakeDataSourceModel |
| 148 | + diags := resp.State.Get(ctx, &state) |
| 149 | + resp.Diagnostics.Append(diags...) |
| 150 | + |
| 151 | + state.ID = types.Int64Value(1) |
| 152 | + |
| 153 | + state.Value = types.StringValue( |
| 154 | + caps.ToSnake( |
| 155 | + state.String.ValueString(), |
| 156 | + ), |
| 157 | + ) |
| 158 | + |
| 159 | + diags = resp.State.Set(ctx, &state) |
| 160 | + resp.Diagnostics.Append(diags...) |
| 161 | + |
| 162 | + if resp.Diagnostics.HasError() { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + tflog.Info(ctx, "Ending StrSnake DataSource Read method.") |
| 167 | +} |
0 commit comments