Skip to content

Commit

Permalink
fix: fixed a bug where provider_meta was not recognized because of mi…
Browse files Browse the repository at this point in the history
…ssing schema
  • Loading branch information
teneko committed Sep 22, 2022
1 parent 6d00406 commit 53ec973
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/resources/path_exists.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
page_title: "value_path_exists Resource - terraform-provider-value"
subcategory: ""
description: |-
Checks if an OS path exists and caches its computation at plan-time and won't change afterapply-time even the path may have been removed.
Checks if an OS path exists and caches its computation at plan-time and won't change after apply-time even the path may have been removed.
Provider Metadata
Each module can use providermeta. Please keep in mind that these settings only count for resources of this module! (see https://www.terraform.io/internals/provider-meta https://www.terraform.io/internals/provider-meta):
```terraform
Expand All @@ -27,7 +27,7 @@ description: |-

# value_path_exists (Resource)

Checks if an OS path exists and caches its computation at plan-time and won't change afterapply-time even the path may have been removed.
Checks if an OS path exists and caches its computation at plan-time and won't change after apply-time even the path may have been removed.
## Provider Metadata
Each module can use provider_meta. Please keep in mind that these settings only count for resources of this module! (see [https://www.terraform.io/internals/provider-meta](https://www.terraform.io/internals/provider-meta)):
```terraform
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions examples/replaced_when/file_inexistence_check/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ terraform {
version = "0.1.0"
}
}

provider_meta "value" {
guid_seed_addition = "module(file_inexistence_check)"
}
}

provider "value" {
guid_seed_addition = "project(file_inexistence_check)"
}

resource "value_unknown_proposer" "default" {}
Expand Down
2 changes: 1 addition & 1 deletion internal/fwkprovider/path_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *pathExistsResource) Configure(ctx context.Context, req resource.Configu

func (r pathExistsResource) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: "Checks if an OS path exists and caches its computation at plan-time and won't change after" +
Description: "Checks if an OS path exists and caches its computation at plan-time and won't change after " +
"apply-time even the path may have been removed." + "\n" + goproviderconfig.GetProviderMetaGuidSeedAdditionAttributeDescription(),
Attributes: map[string]tfsdk.Attribute{
"path": {
Expand Down
6 changes: 6 additions & 0 deletions internal/fwkprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type providerWithTraits interface {
fwkprovider.ProviderWithMetadata
fwkprovider.ProviderWithResources
fwkprovider.ProviderWithDataSources
fwkprovider.ProviderWithMetaSchema
}

// Provider schema struct
Expand All @@ -46,6 +47,11 @@ func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics)
return *fwkproviderconfig.GetProviderConfigSchema(), nil
}

// GetMetaSchema implements providerWithTraits
func (*provider) GetMetaSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return *fwkproviderconfig.GetProviderMetaSchema(), nil
}

func (p *provider) Configure(ctx context.Context, req fwkprovider.ConfigureRequest, resp *fwkprovider.ConfigureResponse) {
// Retrieve provider data from configuration
var config providerConfig
Expand Down
19 changes: 19 additions & 0 deletions internal/fwkproviderconfig/attribute_guid_seed_addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@ import (

"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/pseudo-dynamic/terraform-provider-value/internal/goproviderconfig"
)

const GuidSeedAdditionAttributeName string = "guid_seed_addition"

func getGuidSeedAdditionAttribute() tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.StringType,
Required: false,
Optional: true,
Computed: false,
Validators: []tfsdk.AttributeValidator{
&PlanKnownValidator{},
},
PlanModifiers: tfsdk.AttributePlanModifiers{
guidSeedAdditionDefaultEmptyModifier{},
},
Description: goproviderconfig.GetGuidSeedAdditionAttributeDescription(),
}
}

type guidSeedAdditionDefaultEmptyModifier struct{}

func (r guidSeedAdditionDefaultEmptyModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) {
Expand Down
16 changes: 1 addition & 15 deletions internal/fwkproviderconfig/provider_config_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,13 @@ package fwkproviderconfig

import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/pseudo-dynamic/terraform-provider-value/internal/goproviderconfig"
)

func GetProviderConfigSchema() *tfsdk.Schema {
return &tfsdk.Schema{
Version: 0,
Attributes: map[string]tfsdk.Attribute{
"guid_seed_addition": {
Type: types.StringType,
Required: false,
Optional: true,
Computed: false,
Validators: []tfsdk.AttributeValidator{
&PlanKnownValidator{},
},
PlanModifiers: tfsdk.AttributePlanModifiers{
guidSeedAdditionDefaultEmptyModifier{},
},
Description: goproviderconfig.GetGuidSeedAdditionAttributeDescription(),
},
GuidSeedAdditionAttributeName: getGuidSeedAdditionAttribute(),
},
}
}
14 changes: 14 additions & 0 deletions internal/fwkproviderconfig/provider_meta_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fwkproviderconfig

import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

func GetProviderMetaSchema() *tfsdk.Schema {
return &tfsdk.Schema{
Version: 0,
Attributes: map[string]tfsdk.Attribute{
GuidSeedAdditionAttributeName: getGuidSeedAdditionAttribute(),
},
}
}
6 changes: 6 additions & 0 deletions internal/goproviderconfig/attribute_guid_seed_addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ func TryUnmarshalValueThenExtractGuidSeedAddition(value *tftypes.Value) (string,
var diags []*tfprotov6.Diagnostic

if valueMap, diags, isErroneous = schema.UnmarshalValue(value); isErroneous {
diags = append(diags, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Could unmarshal value to map[string]tftypes.Value",
Detail: "Could unmarshal value to map[string]tftypes.Value to extract guid_seed_addition",
})
goto Return
}
_ = value

if seedAdditionValue, isSuccesful = valueMap["guid_seed_addition"]; !isSuccesful {
// Not having guid_seed_addition is fine.
goto Return
}
_ = seedAdditionValue
Expand Down
2 changes: 1 addition & 1 deletion internal/goproviderconfig/provider_meta_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func GetProviderMetaSchema() *tfprotov6.Schema {
Version: 0,
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
GetGuidSeedAdditionSchemaAttribute(GetProviderMetaGuidSeedAdditionAttributeDescription()),
GetGuidSeedAdditionSchemaAttribute(GetGuidSeedAdditionAttributeDescription()),
},
},
}
Expand Down

0 comments on commit 53ec973

Please sign in to comment.