Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing datasource schema for tanzu kubernetes clusters and adding generic helper #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@ -17,12 +17,18 @@
tanzukubernetesclustermodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/tanzukubernetescluster"
)

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
Loading