-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathprovider.go
More file actions
149 lines (140 loc) · 4.48 KB
/
provider.go
File metadata and controls
149 lines (140 loc) · 4.48 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package provider
import (
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// GetObjectTypeFromSchema returns a tftypes.Type that can wholy represent the schema input
func GetObjectTypeFromSchema(schema *tfprotov5.Schema) tftypes.Type {
bm := map[string]tftypes.Type{}
for _, att := range schema.Block.Attributes {
bm[att.Name] = att.Type
}
for _, b := range schema.Block.BlockTypes {
attrs := map[string]tftypes.Type{}
for _, att := range b.Block.Attributes {
attrs[att.Name] = att.Type
}
bm[b.TypeName] = tftypes.List{
ElementType: tftypes.Object{AttributeTypes: attrs},
}
// TODO handle repeated blocks
}
return tftypes.Object{AttributeTypes: bm}
}
// GetResourceType returns the tftypes.Type of a resource of type 'name'
func GetResourceType(name string) (tftypes.Type, error) {
sch := GetProviderResourceSchema()
rsch, ok := sch[name]
if !ok {
return tftypes.DynamicPseudoType, fmt.Errorf("unknown resource %s - cannot find schema", name)
}
return GetObjectTypeFromSchema(rsch), nil
}
// GetProviderResourceSchema contains the definitions of all supported resources
func GetProviderResourceSchema() map[string]*tfprotov5.Schema {
waitForType := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"fields": tftypes.Map{
ElementType: tftypes.String,
},
},
}
return map[string]*tfprotov5.Schema{
"kubernetes_manifest": {
Version: 1,
Block: &tfprotov5.SchemaBlock{
BlockTypes: []*tfprotov5.SchemaNestedBlock{
{
TypeName: "timeouts",
Nesting: tfprotov5.SchemaNestedBlockNestingModeList,
MinItems: 0,
MaxItems: 1,
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "create",
Type: tftypes.String,
Description: "Timeout for the create operation.",
Optional: true,
},
{
Name: "update",
Type: tftypes.String,
Description: "Timeout for the update operation.",
Optional: true,
},
{
Name: "delete",
Type: tftypes.String,
Description: "Timeout for the delete operation.",
Optional: true,
},
},
},
},
{
TypeName: "field_manager",
Nesting: tfprotov5.SchemaNestedBlockNestingModeList,
MinItems: 0,
MaxItems: 1,
Block: &tfprotov5.SchemaBlock{
Description: "Configure field manager options.",
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "name",
Type: tftypes.String,
Required: false,
Optional: true,
Computed: false,
Sensitive: false,
Description: "The name to use for the field manager when creating and updating the resource.",
DescriptionKind: 0,
Deprecated: false,
},
{
Name: "force_conflicts",
Type: tftypes.Bool,
Required: false,
Optional: true,
Computed: false,
Sensitive: false,
Description: "Force changes against conflicts.",
DescriptionKind: 0,
Deprecated: false,
},
},
},
},
},
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "manifest",
Type: tftypes.DynamicPseudoType,
Required: true,
Description: "A Kubernetes manifest describing the desired state of the resource in HCL format.",
},
{
Name: "object",
Type: tftypes.DynamicPseudoType,
Optional: true,
Computed: true,
Description: "The resulting resource state, as returned by the API server after applying the desired state from `manifest`.",
},
{
Name: "wait_for",
Type: waitForType,
Optional: true,
Description: "A map of attribute paths and desired patterns to be matched. After each apply the provider will wait for all attributes listed here to reach a value that matches the desired pattern.",
},
{
Name: "computed_fields",
Type: tftypes.List{ElementType: tftypes.String},
Description: "List of manifest fields whose values can be altered by the API server during 'apply'. Defaults to: [\"metadata.annotations\", \"metadata.labels\"]",
Optional: true,
},
},
},
},
}
}