Skip to content

Commit bedd0ee

Browse files
warroyogurav-samir
andauthored
fixing datasource schema for tanzu kubernetes clusters and adding generic helper (#479)
* fixing datasource schema for utkg cluster Signed-off-by: warroyo <warroyo7199008@gmail.com> * fix lint error --------- Signed-off-by: warroyo <warroyo7199008@gmail.com> Co-authored-by: Samir Gurav <samir.gurav@broadcom.com>
1 parent 137f846 commit bedd0ee

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Ignore any built binaries.
2-
terraform-provider-tanzu-mission-control
2+
terraform-provider-tanzu-mission-control*
33
terraform-provider-tanzu-mission-control.exe
44

55
# Ignore distribution directories.
@@ -14,3 +14,4 @@ dist/
1414

1515
# Ignore macOS desktop services store files.
1616
**/.DS_Store
17+

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ vet:
5656

5757
# Linter
5858
lint: gofmt
59-
golangci-lint run -c ./.golangci.yml ./internal/... .
59+
golangci-lint run -c ./.golangci.yml ./internal/... --timeout 5m
6060

6161
acc-test:
6262
go test $(TEST_PKGS) -tags $(BUILD_TAGS)

internal/helper/helper.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,79 @@ func GetAllMapsKeys(maps ...map[string]interface{}) map[string]bool {
227227

228228
return keys
229229
}
230+
231+
// nolint:godot
232+
// DatasourceSchemaFromResourceSchema is a recursive func that
233+
// converts an existing Resource schema to a Datasource schema.
234+
// All schema elements are copied, but certain attributes are ignored or changed:
235+
// - all attributes have Computed = true
236+
// - all attributes have ForceNew, Required = false
237+
// - Validation funcs and attributes (e.g. MaxItems) are not copied
238+
func DatasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.Schema {
239+
ds := make(map[string]*schema.Schema, len(rs))
240+
241+
for k, v := range rs {
242+
dv := &schema.Schema{
243+
Computed: true,
244+
ForceNew: false,
245+
Required: false,
246+
Description: v.Description,
247+
Type: v.Type,
248+
}
249+
250+
switch v.Type {
251+
case schema.TypeSet:
252+
dv.Set = v.Set
253+
fallthrough
254+
case schema.TypeList:
255+
// List & Set types are generally used for 2 cases:
256+
// - a list/set of simple primitive values (e.g. list of strings)
257+
// - a sub resource
258+
if elem, ok := v.Elem.(*schema.Resource); ok {
259+
// handle the case where the Element is a sub-resource
260+
dv.Elem = &schema.Resource{
261+
Schema: DatasourceSchemaFromResourceSchema(elem.Schema),
262+
}
263+
} else {
264+
// handle simple primitive case
265+
dv.Elem = v.Elem
266+
}
267+
268+
default:
269+
// Elem of all other types are copied as-is
270+
dv.Elem = v.Elem
271+
}
272+
273+
ds[k] = dv
274+
}
275+
276+
return ds
277+
}
278+
279+
// fixDatasourceSchemaFlags is a convenience func that toggles the Computed,
280+
// Optional + Required flags on a schema element. This is useful when the schema
281+
// has been generated (using `DatasourceSchemaFromResourceSchema` above for
282+
// example) and therefore the attribute flags were not set appropriately when
283+
// first added to the schema definition. Currently only supports top-level
284+
// schema elements.
285+
func FixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
286+
for _, v := range keys {
287+
schema[v].Computed = false
288+
schema[v].Optional = !required
289+
schema[v].Required = required
290+
}
291+
}
292+
293+
func AddRequiredFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
294+
FixDatasourceSchemaFlags(schema, true, keys...)
295+
}
296+
297+
func AddOptionalFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
298+
FixDatasourceSchemaFlags(schema, false, keys...)
299+
}
300+
301+
func DeleteFieldsFromSchema(schema map[string]*schema.Schema, keys ...string) {
302+
for _, key := range keys {
303+
delete(schema, key)
304+
}
305+
}

internal/resources/tanzukubernetescluster/data_source.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ import (
1818
)
1919

2020
func DataSourceTanzuKubernetesCluster() *schema.Resource {
21+
dsSchema := helper.DatasourceSchemaFromResourceSchema(tanzuKubernetesClusterSchema)
22+
23+
// Set 'Required' schema elements
24+
helper.AddRequiredFieldsToSchema(dsSchema, "name", "management_cluster_name", "provisioner_name")
25+
2126
return &schema.Resource{
2227
ReadContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
2328
return dataSourceTanzuKubernetesClusterRead(helper.GetContextWithCaller(ctx, helper.DataRead), d, m)
2429
},
25-
Schema: tanzuKubernetesClusterSchema,
30+
Schema: dsSchema,
2631
}
2732
}
2833

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424

2525
if debugMode {
2626
opts.Debug = debugMode
27-
opts.ProviderAddr = "vmware/dev/tanzu-mission-control"
27+
opts.ProviderAddr = "vmware/tanzu-mission-control"
2828
}
2929

3030
plugin.Serve(opts)

0 commit comments

Comments
 (0)