Skip to content

Commit

Permalink
fixing datasource schema for utkg cluster
Browse files Browse the repository at this point in the history
Signed-off-by: warroyo <[email protected]>
  • Loading branch information
warroyo authored and tenthirtyam committed Jan 14, 2025
1 parent 86f2f1a commit 0ef77df
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ignore any built binaries.
terraform-provider-tanzu-mission-control
terraform-provider-tanzu-mission-control*
terraform-provider-tanzu-mission-control.exe

# Ignore distribution directories.
Expand All @@ -14,3 +14,4 @@ dist/

# Ignore macOS desktop services store files.
**/.DS_Store

74 changes: 74 additions & 0 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,77 @@ func GetAllMapsKeys(maps ...map[string]interface{}) map[string]bool {

return keys
}

// DatasourceSchemaFromResourceSchema is a recursive func that

Check failure on line 231 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)

Check failure on line 231 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
// converts an existing Resource schema to a Datasource schema.
// All schema elements are copied, but certain attributes are ignored or changed:
// - all attributes have Computed = true
// - all attributes have ForceNew, Required = false
// - Validation funcs and attributes (e.g. MaxItems) are not copied
func DatasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*schema.Schema {
ds := make(map[string]*schema.Schema, len(rs))
for k, v := range rs {

Check failure on line 239 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

ranges should only be cuddled with assignments used in the iteration (wsl)

Check failure on line 239 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

ranges should only be cuddled with assignments used in the iteration (wsl)
dv := &schema.Schema{
Computed: true,
ForceNew: false,
Required: false,
Description: v.Description,
Type: v.Type,
}

switch v.Type {
case schema.TypeSet:
dv.Set = v.Set
fallthrough
case schema.TypeList:
// List & Set types are generally used for 2 cases:
// - a list/set of simple primitive values (e.g. list of strings)
// - a sub resource
if elem, ok := v.Elem.(*schema.Resource); ok {
// handle the case where the Element is a sub-resource
dv.Elem = &schema.Resource{
Schema: DatasourceSchemaFromResourceSchema(elem.Schema),
}
} else {
// handle simple primitive case
dv.Elem = v.Elem
}

default:
// Elem of all other types are copied as-is
dv.Elem = v.Elem

}

Check failure on line 270 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary trailing newline (whitespace)

Check failure on line 270 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary trailing newline (whitespace)
ds[k] = dv

Check failure on line 271 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

assignments should only be cuddled with other assignments (wsl)

Check failure on line 271 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

assignments should only be cuddled with other assignments (wsl)

}

Check failure on line 273 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary trailing newline (whitespace)

Check failure on line 273 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary trailing newline (whitespace)
return ds

Check failure on line 274 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

return statements should not be cuddled if block has more than two lines (wsl)

Check failure on line 274 in internal/helper/helper.go

View workflow job for this annotation

GitHub Actions / Build

return statements should not be cuddled if block has more than two lines (wsl)
}

// fixDatasourceSchemaFlags is a convenience func that toggles the Computed,
// Optional + Required flags on a schema element. This is useful when the schema
// has been generated (using `DatasourceSchemaFromResourceSchema` above for
// example) and therefore the attribute flags were not set appropriately when
// first added to the schema definition. Currently only supports top-level
// schema elements.
func FixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
for _, v := range keys {
schema[v].Computed = false
schema[v].Optional = !required
schema[v].Required = required
}
}

func AddRequiredFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
FixDatasourceSchemaFlags(schema, true, keys...)
}

func AddOptionalFieldsToSchema(schema map[string]*schema.Schema, keys ...string) {
FixDatasourceSchemaFlags(schema, false, keys...)
}

func DeleteFieldsFromSchema(schema map[string]*schema.Schema, keys ...string) {
for _, key := range keys {
delete(schema, key)
}
}
8 changes: 7 additions & 1 deletion internal/resources/tanzukubernetescluster/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import (
)

func DataSourceTanzuKubernetesCluster() *schema.Resource {

Check failure on line 20 in internal/resources/tanzukubernetescluster/data_source.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary leading newline (whitespace)

Check failure on line 20 in internal/resources/tanzukubernetescluster/data_source.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary leading newline (whitespace)

dsSchema := helper.DatasourceSchemaFromResourceSchema(tanzuKubernetesClusterSchema)

// Set 'Required' schema elements
helper.AddRequiredFieldsToSchema(dsSchema, "name", "management_cluster_name", "provisioner_name")

return &schema.Resource{
ReadContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return dataSourceTanzuKubernetesClusterRead(helper.GetContextWithCaller(ctx, helper.DataRead), d, m)
},
Schema: tanzuKubernetesClusterSchema,
Schema: dsSchema,
}
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

if debugMode {
opts.Debug = debugMode
opts.ProviderAddr = "vmware/dev/tanzu-mission-control"
opts.ProviderAddr = "vmware/tanzu-mission-control"
}

plugin.Serve(opts)
Expand Down

0 comments on commit 0ef77df

Please sign in to comment.