-
Notifications
You must be signed in to change notification settings - Fork 31
/
scope_schema.go
112 lines (88 loc) · 2.85 KB
/
scope_schema.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
/*
Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0
*/
package commonscope
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)
type (
Scope int64
SchemaOption func(*SchemaConfig)
SchemaConfig struct {
Description string
Scopes []string
}
)
func WithDescription(d string) SchemaOption {
return func(config *SchemaConfig) {
config.Description = d
}
}
func WithScopes(s []string) SchemaOption {
return func(config *SchemaConfig) {
config.Scopes = s
}
}
func GetScopeSchema(opts ...SchemaOption) *schema.Schema {
cfg := &SchemaConfig{}
for _, o := range opts {
o(cfg)
}
schemaForAllowedScopes := func() map[string]*schema.Schema {
scopeSchemaMap := make(map[string]*schema.Schema)
for _, scope := range cfg.Scopes {
scopeSchemaMap[scope] = getSchemaForScope()(scope)
}
return scopeSchemaMap
}()
return &schema.Schema{
Type: schema.TypeList,
Description: cfg.Description,
Required: true,
ForceNew: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: schemaForAllowedScopes,
},
}
}
type ValidateScopeType func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error
func ValidateScope(scopesAllowed []string) ValidateScopeType {
return func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
value, ok := diff.GetOk(ScopeKey)
if !ok {
return fmt.Errorf("scope: %v is not valid: minimum one valid scope block is required", value)
}
data, _ := value.([]interface{})
if len(data) == 0 || data[0] == nil {
return fmt.Errorf("scope data: %v is not valid: minimum one valid scope block is required among: %v", data, strings.Join(scopesAllowed, `, `))
}
scopeData := data[0].(map[string]interface{})
scopesFound := make([]string, 0)
if clusterData, ok := scopeData[ClusterKey]; ok {
if clusterValue, ok := clusterData.([]interface{}); ok && len(clusterValue) != 0 {
scopesFound = append(scopesFound, ClusterKey)
}
}
if clusterGroupData, ok := scopeData[ClusterGroupKey]; ok {
if clusterGroupValue, ok := clusterGroupData.([]interface{}); ok && len(clusterGroupValue) != 0 {
scopesFound = append(scopesFound, ClusterGroupKey)
}
}
if len(scopesFound) == 0 {
return fmt.Errorf("no valid scope type block found: minimum one valid scope type block is required among: %v", strings.Join(scopesAllowed, `, `))
} else if len(scopesFound) > 1 {
return fmt.Errorf("found scopes: %v are not valid: maximum one valid scope type block is allowed", strings.Join(scopesFound, `, `))
}
if !slices.Contains(scopesAllowed, scopesFound[0]) {
return fmt.Errorf("found scope: %v is not valid: minimum one valid scope type block is required among: %v", scopesFound[0], strings.Join(scopesAllowed, `, `))
}
return nil
}
}