forked from hashicorp/terraform-provider-google
/
framework_validators.go
119 lines (94 loc) · 3.66 KB
/
framework_validators.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package fwprovider
import (
"context"
"fmt"
"os"
"time"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
googleoauth "golang.org/x/oauth2/google"
)
// Credentials Validator
var _ validator.String = credentialsValidator{}
// credentialsValidator validates that a string Attribute's is valid JSON credentials.
type credentialsValidator struct {
}
// Description describes the validation in plain text formatting.
func (v credentialsValidator) Description(_ context.Context) string {
return "value must be a path to valid JSON credentials or valid, raw, JSON credentials"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v credentialsValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateString performs the validation.
func (v credentialsValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
value := request.ConfigValue.ValueString()
// if this is a path and we can stat it, assume it's ok
if _, err := os.Stat(value); err == nil {
return
}
if _, err := googleoauth.CredentialsFromJSON(context.Background(), []byte(value)); err != nil {
response.Diagnostics.AddError("JSON credentials are not valid", err.Error())
}
}
func CredentialsValidator() validator.String {
return credentialsValidator{}
}
// Non Negative Duration Validator
type nonnegativedurationValidator struct {
}
// Description describes the validation in plain text formatting.
func (v nonnegativedurationValidator) Description(_ context.Context) string {
return "value expected to be a string representing a non-negative duration"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v nonnegativedurationValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateString performs the validation.
func (v nonnegativedurationValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
value := request.ConfigValue.ValueString()
dur, err := time.ParseDuration(value)
if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("expected %s to be a duration", value), err.Error())
return
}
if dur < 0 {
response.Diagnostics.AddError("duration must be non-negative", fmt.Sprintf("duration provided: %d", dur))
}
}
func NonNegativeDurationValidator() validator.String {
return nonnegativedurationValidator{}
}
// Non Empty String Validator
type nonEmptyStringValidator struct {
}
// Description describes the validation in plain text formatting.
func (v nonEmptyStringValidator) Description(_ context.Context) string {
return "value expected to be a string that isn't an empty string"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v nonEmptyStringValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateString performs the validation.
func (v nonEmptyStringValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
value := request.ConfigValue.ValueString()
if value == "" {
response.Diagnostics.AddError("expected a non-empty string", fmt.Sprintf("%s was set to `%s`", request.Path, value))
}
}
func NonEmptyStringValidator() validator.String {
return nonEmptyStringValidator{}
}