-
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.
Merge pull request #447 from Aflynn50/channel-validator
Validate channel in application resource
- Loading branch information
Showing
10 changed files
with
136 additions
and
8 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
terraform { | ||
required_providers { | ||
juju = { | ||
version = "~> 0.10.0" | ||
version = "~> 0.11.0" | ||
source = "juju/juju" | ||
} | ||
} | ||
|
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
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
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,47 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/juju/charm/v11" | ||
) | ||
|
||
type StringIsChannelValidator struct{} | ||
|
||
// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact. | ||
func (v StringIsChannelValidator) Description(context.Context) string { | ||
return "string must conform to track/risk or track/risk/branch e.g. latest/stable" | ||
} | ||
|
||
// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact. | ||
func (v StringIsChannelValidator) MarkdownDescription(context.Context) string { | ||
return "string must conform to track/risk or track/risk/branch e.g. latest/stable" | ||
} | ||
|
||
// Validate runs the main validation logic of the validator, reading configuration data out of `req` and updating `resp` with diagnostics. | ||
func (v StringIsChannelValidator) ValidateString(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) { | ||
// If the value is unknown or null, there is nothing to validate. | ||
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
if channel, err := charm.ParseChannel(req.ConfigValue.ValueString()); err != nil { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path, | ||
"Invalid Channel", | ||
err.Error(), | ||
) | ||
return | ||
} else if channel.Track == "" || channel.Risk == "" { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path, | ||
"Invalid Channel", | ||
"String must conform to track/risk or track/risk/branch, e.g. latest/stable", | ||
) | ||
return | ||
} | ||
} |
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,73 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package provider_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/juju/terraform-provider-juju/internal/provider" | ||
) | ||
|
||
func TestChannelValidatorValid(t *testing.T) { | ||
validChannels := []types.String{ | ||
types.StringValue("track/stable"), | ||
types.StringValue("track/edge/branch"), | ||
types.StringNull(), | ||
types.StringUnknown(), | ||
} | ||
|
||
channelValidator := provider.StringIsChannelValidator{} | ||
for _, channel := range validChannels { | ||
req := validator.StringRequest{ | ||
ConfigValue: channel, | ||
} | ||
var resp validator.StringResponse | ||
channelValidator.ValidateString(context.Background(), req, &resp) | ||
|
||
if resp.Diagnostics.HasError() { | ||
t.Errorf("errors %v", resp.Diagnostics.Errors()) | ||
} | ||
} | ||
} | ||
|
||
func TestChannelValidatorInvalid(t *testing.T) { | ||
invalidChannels := []struct { | ||
str types.String | ||
err string | ||
}{{ | ||
str: types.StringValue("track"), | ||
err: "String must conform to track/risk or track/risk/branch, e.g. latest/stable", | ||
}, { | ||
str: types.StringValue("edge"), | ||
err: "String must conform to track/risk or track/risk/branch, e.g. latest/stable", | ||
}, { | ||
str: types.StringValue(`track\risk`), | ||
err: "String must conform to track/risk or track/risk/branch, e.g. latest/stable", | ||
}, { | ||
str: types.StringValue(`track/invalidrisk`), | ||
err: `risk in channel "track/invalidrisk" not valid`, | ||
}, { | ||
str: types.StringValue(`track/invalidrisk/branch`), | ||
err: `risk in channel "track/invalidrisk/branch" not valid`, | ||
}} | ||
|
||
channelValidator := provider.StringIsChannelValidator{} | ||
for _, test := range invalidChannels { | ||
req := validator.StringRequest{ | ||
ConfigValue: test.str, | ||
} | ||
var resp validator.StringResponse | ||
channelValidator.ValidateString(context.Background(), req, &resp) | ||
|
||
if c := resp.Diagnostics.ErrorsCount(); c != 1 { | ||
t.Errorf("expected one error, got %d", c) | ||
} | ||
if deets := resp.Diagnostics.Errors()[0].Detail(); deets != test.err { | ||
t.Errorf("expected error %q, got %q", test.err, deets) | ||
} | ||
} | ||
} |