-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathschema.go
124 lines (100 loc) · 3.77 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package permissiontemplate
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
credentialsmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/credential"
)
const (
ResourceName = "tanzu-mission-control_permission_template"
// Root Keys.
CredentialsNameKey = "credentials_name"
CapabilityKey = "tanzu_capability"
ProviderKey = "tanzu_provider"
// Computed Fields.
TemplateKey = "template"
TemplateURLKey = "template_url"
TemplateValuesKey = "template_values"
UndefinedTemplateValuesKey = "undefined_template_values"
)
var capabilityProviderMap = map[string]string{
"DATA_PROTECTION": string(credentialsmodels.VmwareTanzuManageV1alpha1AccountCredentialProviderAWSEC2),
"MANAGED_K8S_PROVIDER": string(credentialsmodels.VmwareTanzuManageV1alpha1AccountCredentialProviderAWSEKS),
}
var permissionTemplateSchema = map[string]*schema.Schema{
CredentialsNameKey: credentialsNameSchema,
CapabilityKey: capabilitySchema,
ProviderKey: providerSchema,
TemplateKey: templateSchema,
TemplateURLKey: templateURLSchema,
TemplateValuesKey: templateValuesSchema,
UndefinedTemplateValuesKey: undefinedTemplateValuesSchema,
}
var credentialsNameSchema = &schema.Schema{
Type: schema.TypeString,
Description: "The name of the credentials to get permission template for.",
Required: true,
ForceNew: true,
}
var capabilitySchema = &schema.Schema{
Type: schema.TypeString,
Description: buildCapabilityProviderDescription(CapabilityKey),
Required: true,
ForceNew: true,
}
var providerSchema = &schema.Schema{
Type: schema.TypeString,
Description: buildCapabilityProviderDescription(ProviderKey),
Required: true,
ForceNew: true,
}
var templateSchema = &schema.Schema{
Type: schema.TypeString,
Description: "Base64 encoded permission template.",
Computed: true,
}
var templateURLSchema = &schema.Schema{
Type: schema.TypeString,
Description: "URL for permission template.",
Computed: true,
}
var templateValuesSchema = &schema.Schema{
Type: schema.TypeMap,
Description: "Values to be sent as parameters for the template.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
}
var undefinedTemplateValuesSchema = &schema.Schema{
Type: schema.TypeMap,
Description: "Values which are not defined in the template parameters definition.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
}
func buildCapabilityProviderDescription(schemaKey string) (description string) {
if schemaKey == CapabilityKey {
description = "The Tanzu capability of the credentials."
validValues := make([]string, 0)
for k, v := range capabilityProviderMap {
valueDescription := fmt.Sprintf("When %s is set to '%s' %s must be set to '%s'.", CapabilityKey, k, ProviderKey, v)
description = fmt.Sprintf("%s\n%s", description, valueDescription)
validValues = append(validValues, k)
}
description = fmt.Sprintf("%s\nValid values are: %v", description, validValues)
} else if schemaKey == ProviderKey {
description = "The Tanzu provider of the credentials."
validValues := make([]string, 0)
for k, v := range capabilityProviderMap {
valueDescription := fmt.Sprintf("When %s is set to '%s' %s must be set to '%s'.", ProviderKey, v, CapabilityKey, k)
description = fmt.Sprintf("%s\n%s", description, valueDescription)
validValues = append(validValues, v)
}
description = fmt.Sprintf("%s\nValid values are: %v", description, validValues)
}
return description
}