-
Notifications
You must be signed in to change notification settings - Fork 31
/
validator_channel.go
47 lines (39 loc) · 1.66 KB
/
validator_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
}
}