|
| 1 | +package fwkprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/pseudo-dynamic/terraform-provider-value/internal/fwkproviderconfig" |
| 9 | + "github.com/pseudo-dynamic/terraform-provider-value/internal/goproviderconfig" |
| 10 | + "github.com/pseudo-dynamic/terraform-provider-value/internal/guid" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | +) |
| 17 | + |
| 18 | +const pathExistsResourceSuffix string = "_path_exists" |
| 19 | +const pathExistsResourceName string = providerName + pathExistsResourceSuffix |
| 20 | + |
| 21 | +type pathExistsResource struct { |
| 22 | + ProviderGuidSeedAddition *string |
| 23 | +} |
| 24 | + |
| 25 | +type pathExistsResourceWithTraits interface { |
| 26 | + resource.ResourceWithMetadata |
| 27 | + resource.ResourceWithGetSchema |
| 28 | + resource.ResourceWithModifyPlan |
| 29 | + resource.ResourceWithConfigure |
| 30 | +} |
| 31 | + |
| 32 | +func NewPathExistsResource() pathExistsResourceWithTraits { |
| 33 | + return &pathExistsResource{} |
| 34 | +} |
| 35 | + |
| 36 | +// Configure implements pathExistsResourceWithTraits |
| 37 | +func (r *pathExistsResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 38 | + if req.ProviderData == nil { |
| 39 | + // For whatever reason Configure gets called with nil ProviderData. |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + provderData := req.ProviderData.(*providerData) |
| 44 | + r.ProviderGuidSeedAddition = provderData.GuidSeedAddition |
| 45 | +} |
| 46 | + |
| 47 | +func (r pathExistsResource) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { |
| 48 | + return tfsdk.Schema{ |
| 49 | + Description: "Checks if an OS path exists and caches its computation at plan-time and won't change after" + |
| 50 | + "apply-time even the path may have been removed." + "\n" + goproviderconfig.GetProviderMetaGuidSeedAdditionAttributeDescription(), |
| 51 | + Attributes: map[string]tfsdk.Attribute{ |
| 52 | + "path": { |
| 53 | + Type: types.StringType, |
| 54 | + Required: true, |
| 55 | + Description: "A path to a file or directory.", |
| 56 | + }, |
| 57 | + "proposed_unknown": { |
| 58 | + Type: types.BoolType, |
| 59 | + Required: true, |
| 60 | + Description: goproviderconfig.GetProposedUnknownAttributeDescription(), |
| 61 | + }, |
| 62 | + "guid_seed": { |
| 63 | + Type: types.StringType, |
| 64 | + Required: true, |
| 65 | + Description: goproviderconfig.GetGuidSeedAttributeDescription(pathExistsResourceName), |
| 66 | + Validators: []tfsdk.AttributeValidator{ |
| 67 | + &fwkproviderconfig.PlanKnownValidator{}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "exists": { |
| 71 | + Type: types.BoolType, |
| 72 | + Computed: true, |
| 73 | + Description: "The computation whether the path exists or not.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, nil |
| 77 | +} |
| 78 | + |
| 79 | +type pathExistsState struct { |
| 80 | + Path types.String `tfsdk:"path"` |
| 81 | + GuidSeed types.String `tfsdk:"guid_seed"` |
| 82 | + ProposedUnknown types.Bool `tfsdk:"proposed_unknown"` |
| 83 | + Exists types.Bool `tfsdk:"exists"` |
| 84 | +} |
| 85 | + |
| 86 | +func (r *pathExistsResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 87 | + resp.TypeName = req.ProviderTypeName + pathExistsResourceSuffix |
| 88 | +} |
| 89 | + |
| 90 | +// ModifyPlan implements PathExistsResourceWithTraits |
| 91 | +func (r *pathExistsResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { |
| 92 | + if r.ProviderGuidSeedAddition == nil { |
| 93 | + resp.Diagnostics.AddError("Bad provider guid seed", "Provider guid seed is null but was expected to be empty") |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Get current config |
| 98 | + var config pathExistsState |
| 99 | + diags := req.Config.Get(ctx, &config) |
| 100 | + resp.Diagnostics.Append(diags...) |
| 101 | + |
| 102 | + if resp.Diagnostics.HasError() { |
| 103 | + return |
| 104 | + } |
| 105 | + suppliedGuidSeed := config.GuidSeed.Value |
| 106 | + isPlanPhase := config.ProposedUnknown.IsUnknown() |
| 107 | + |
| 108 | + var providerMetaSeedAddition string |
| 109 | + var isSuccessful bool |
| 110 | + if providerMetaSeedAddition, _, isSuccessful = goproviderconfig.TryUnmarshalValueThenExtractGuidSeedAddition(&req.ProviderMeta.Raw); !isSuccessful { |
| 111 | + resp.Diagnostics.AddError("Extraction failed", "Could not extract provider meta guid seed addition") |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + composedGuidSeed := guid.ComposeGuidSeed(r.ProviderGuidSeedAddition, |
| 116 | + &providerMetaSeedAddition, |
| 117 | + pathExistsResourceName, |
| 118 | + "exists", |
| 119 | + &suppliedGuidSeed) |
| 120 | + |
| 121 | + checkPathExistence := func() types.Bool { |
| 122 | + if config.Path.Unknown { |
| 123 | + return types.Bool{Unknown: true} |
| 124 | + } |
| 125 | + |
| 126 | + _, err := os.Stat(config.Path.Value) |
| 127 | + pathExists := err == nil |
| 128 | + return types.Bool{Value: pathExists} |
| 129 | + } |
| 130 | + |
| 131 | + cachedExists, err := guid.GetPlanCachedBoolean( |
| 132 | + isPlanPhase, |
| 133 | + composedGuidSeed, |
| 134 | + pathExistsResourceName, |
| 135 | + checkPathExistence) |
| 136 | + |
| 137 | + if err != nil { |
| 138 | + resp.Diagnostics.AddError("Plan cache mechanism failed for exists attribute", err.Error()) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + resp.Diagnostics.AddWarning("Exists (cached) is unknown: "+strconv.FormatBool(cachedExists.Unknown), "") |
| 143 | + |
| 144 | + config.Exists = cachedExists |
| 145 | + resp.Plan.Set(ctx, &config) |
| 146 | +} |
| 147 | + |
| 148 | +// Create a new resource |
| 149 | +func (r pathExistsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 150 | + // Retrieve values from plan |
| 151 | + var plan pathExistsState |
| 152 | + diags := req.Plan.Get(ctx, &plan) |
| 153 | + resp.Diagnostics.Append(diags...) |
| 154 | + |
| 155 | + if resp.Diagnostics.HasError() { |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + diags = resp.State.Set(ctx, &plan) |
| 160 | + resp.Diagnostics.Append(diags...) |
| 161 | + |
| 162 | + if resp.Diagnostics.HasError() { |
| 163 | + return |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Read resource information |
| 168 | +func (r pathExistsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 169 | + // Get current state |
| 170 | + var state pathExistsState |
| 171 | + diags := req.State.Get(ctx, &state) |
| 172 | + resp.Diagnostics.Append(diags...) |
| 173 | + |
| 174 | + if resp.Diagnostics.HasError() { |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + diags = resp.State.Set(ctx, &state) |
| 179 | + resp.Diagnostics.Append(diags...) |
| 180 | + |
| 181 | + if resp.Diagnostics.HasError() { |
| 182 | + return |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// Update resource |
| 187 | +func (r pathExistsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 188 | + // Get plan |
| 189 | + var plan pathExistsState |
| 190 | + diags := req.Plan.Get(ctx, &plan) |
| 191 | + resp.Diagnostics.Append(diags...) |
| 192 | + |
| 193 | + if resp.Diagnostics.HasError() { |
| 194 | + return |
| 195 | + } |
| 196 | + |
| 197 | + // Set new state |
| 198 | + diags = resp.State.Set(ctx, &plan) |
| 199 | + resp.Diagnostics.Append(diags...) |
| 200 | + |
| 201 | + if resp.Diagnostics.HasError() { |
| 202 | + return |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +// Delete resource |
| 207 | +func (r pathExistsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 208 | + var state pathExistsState |
| 209 | + diags := req.State.Get(ctx, &state) |
| 210 | + resp.Diagnostics.Append(diags...) |
| 211 | + |
| 212 | + if resp.Diagnostics.HasError() { |
| 213 | + return |
| 214 | + } |
| 215 | + |
| 216 | + // Remove resource from state |
| 217 | + resp.State.RemoveResource(ctx) |
| 218 | +} |
0 commit comments