diff --git a/internal/provider/validator_avoid_jaas.go b/internal/provider/validator_avoid_jaas.go index 2492f287..01ec4843 100644 --- a/internal/provider/validator_avoid_jaas.go +++ b/internal/provider/validator_avoid_jaas.go @@ -8,14 +8,12 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/juju/terraform-provider-juju/internal/juju" ) var _ datasource.ConfigValidator = &AvoidJAASValidator{} -var _ provider.ConfigValidator = &AvoidJAASValidator{} var _ resource.ConfigValidator = &AvoidJAASValidator{} // AvoidJAASValidator enforces that the resource is not used with JAAS. @@ -25,41 +23,40 @@ type AvoidJAASValidator struct { PreferredObject string } +// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact. func (v AvoidJAASValidator) Description(ctx context.Context) string { return v.MarkdownDescription(ctx) } +// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact. func (v AvoidJAASValidator) MarkdownDescription(_ context.Context) string { return "Enforces that this resource should not be used with JAAS" } +// ValidateResource performs the validation on the data source. func (v AvoidJAASValidator) ValidateDataSource(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { resp.Diagnostics = v.Validate(ctx, req.Config) } -func (v AvoidJAASValidator) ValidateProvider(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) { - resp.Diagnostics = v.Validate(ctx, req.Config) -} - +// ValidateResource performs the validation on the resource. func (v AvoidJAASValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { resp.Diagnostics = v.Validate(ctx, req.Config) } +// Validate runs the main validation logic of the validator, reading configuration data out of `config` and returning with diagnostics. func (v AvoidJAASValidator) Validate(ctx context.Context, config tfsdk.Config) diag.Diagnostics { var diags diag.Diagnostics - if v.Client != nil { - if v.Client.IsJAAS() { - hint := "" - if v.PreferredObject != "" { - hint = "Try the " + v.PreferredObject + " resource instead." - } - diags.AddWarning("Invalid use of resource with JAAS.", - "It is not supported to use this resource with a JAAS setup. "+ - hint+ - "JAAS offers additional enterprise features through the use of dedicated resources. "+ - "See the provider documentation for more details.") + if v.Client != nil && v.Client.IsJAAS() { + hint := "" + if v.PreferredObject != "" { + hint = "Try the " + v.PreferredObject + " resource instead." } + diags.AddError("Invalid use of resource with JAAS.", + "It is not supported to use this resource with a JAAS setup. "+ + hint+ + "JAAS offers additional enterprise features through the use of dedicated resources. "+ + "See the provider documentation for more details.") } return diags } diff --git a/internal/provider/validator_require_jaas.go b/internal/provider/validator_require_jaas.go index 25af7ee9..6fcf238f 100644 --- a/internal/provider/validator_require_jaas.go +++ b/internal/provider/validator_require_jaas.go @@ -8,14 +8,12 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/juju/terraform-provider-juju/internal/juju" ) var _ datasource.ConfigValidator = &RequiresJAASValidator{} -var _ provider.ConfigValidator = &RequiresJAASValidator{} var _ resource.ConfigValidator = &RequiresJAASValidator{} // RequiresJAASValidator enforces that the resource can only be used with JAAS. @@ -23,34 +21,33 @@ type RequiresJAASValidator struct { Client *juju.Client } +// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact. func (v RequiresJAASValidator) Description(ctx context.Context) string { return v.MarkdownDescription(ctx) } +// // MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact. func (v RequiresJAASValidator) MarkdownDescription(_ context.Context) string { return "Enforces that this resource can only be used with JAAS" } +// ValidateResource performs the validation on the data source. func (v RequiresJAASValidator) ValidateDataSource(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { resp.Diagnostics = v.Validate(ctx, req.Config) } -func (v RequiresJAASValidator) ValidateProvider(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) { - resp.Diagnostics = v.Validate(ctx, req.Config) -} - +// ValidateResource performs the validation on the resource. func (v RequiresJAASValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { resp.Diagnostics = v.Validate(ctx, req.Config) } +// Validate runs the main validation logic of the validator, reading configuration data out of `config` and returning with diagnostics. func (v RequiresJAASValidator) Validate(ctx context.Context, config tfsdk.Config) diag.Diagnostics { var diags diag.Diagnostics - if v.Client != nil { - if v.Client.IsJAAS() { - diags.AddError("Attempted use of resource without JAAS.", - "This resource can only be used with a JAAS setup offering additional enterprise features - see https://jaas.ai/ for more details.") - } + if v.Client != nil && v.Client.IsJAAS() { + diags.AddError("Attempted use of resource without JAAS.", + "This resource can only be used with a JAAS setup offering additional enterprise features - see https://jaas.ai/ for more details.") } return diags