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