-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"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. | ||
// Useful to direct users to more capable resources. | ||
type AvoidJAASValidator struct { | ||
Client *juju.Client | ||
PreferredObject string | ||
} | ||
|
||
func (v AvoidJAASValidator) Description(ctx context.Context) string { | ||
return v.MarkdownDescription(ctx) | ||
} | ||
|
||
func (v AvoidJAASValidator) MarkdownDescription(_ context.Context) string { | ||
return "Enforces that this resource should not be used with JAAS" | ||
} | ||
|
||
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) | ||
} | ||
|
||
func (v AvoidJAASValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
resp.Diagnostics = v.Validate(ctx, req.Config) | ||
} | ||
|
||
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.") | ||
} | ||
} | ||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"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. | ||
type RequiresJAASValidator struct { | ||
Client *juju.Client | ||
} | ||
|
||
func (v RequiresJAASValidator) Description(ctx context.Context) string { | ||
return v.MarkdownDescription(ctx) | ||
} | ||
|
||
func (v RequiresJAASValidator) MarkdownDescription(_ context.Context) string { | ||
return "Enforces that this resource can only be used with JAAS" | ||
} | ||
|
||
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) | ||
} | ||
|
||
func (v RequiresJAASValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
resp.Diagnostics = v.Validate(ctx, req.Config) | ||
} | ||
|
||
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.") | ||
} | ||
} | ||
|
||
return diags | ||
} |