Skip to content

Commit

Permalink
Add extra check for dependency resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
HemanthDogiparthi12 committed May 24, 2024
1 parent 2e2847b commit 1b8c8f5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
25 changes: 18 additions & 7 deletions genesyscloud/tfexporter/genesyscloud_resource_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func NewGenesysCloudResourceExporter(ctx context.Context, d *schema.ResourceData
exportAsHCL: d.Get("export_as_hcl").(bool),
splitFilesByResource: d.Get("split_files_by_resource").(bool),
logPermissionErrors: d.Get("log_permission_errors").(bool),
addDependsOn: d.Get("enable_dependency_resolution").(bool),
addDependsOn: computeDependsOn(d),
filterType: filterType,
includeStateFile: d.Get("include_state_file").(bool),
ignoreCyclicDeps: d.Get("ignore_cyclic_deps").(bool),
Expand All @@ -159,6 +159,19 @@ func NewGenesysCloudResourceExporter(ctx context.Context, d *schema.ResourceData
return gre, nil
}

func computeDependsOn(d *schema.ResourceData) bool {
addDependsOn := d.Get("enable_dependency_resolution").(bool)
if addDependsOn {
if exportableResourceTypes, ok := d.GetOk("include_filter_resources"); ok {
filter := lists.InterfaceListToStrings(exportableResourceTypes.([]interface{}))
addDependsOn = len(filter) > 0
} else {
addDependsOn = false
}
}
return addDependsOn
}

func (g *GenesysCloudResourceExporter) Export() (diagErr diag.Diagnostics) {
// Step #1 Retrieve the exporters we are have registered and have been requested by the user
diagErr = g.retrieveExporters()
Expand Down Expand Up @@ -1452,12 +1465,10 @@ func (g *GenesysCloudResourceExporter) populateConfigExcluded(exporters map[stri
}

if !matchFound {
if dependsOn, ok := g.d.GetOk("enable_dependency_resolution"); ok {
if dependsOn == true {
excludedAttr := excluded[resourceIdx+1:]
log.Printf("Ignoring exclude attribute %s on %s resources. Since exporter is not retrieved", excludedAttr, resourceName)
continue
}
if g.addDependsOn {
excludedAttr := excluded[resourceIdx+1:]
log.Printf("Ignoring exclude attribute %s on %s resources. Since exporter is not retrieved", excludedAttr, resourceName)
continue
} else {
return diag.Errorf("Resource %s in excluded_attributes is not being exported.", resourceName)
}
Expand Down
45 changes: 45 additions & 0 deletions genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,51 @@ func TestUnitTfExportRemoveZeroValuesFunc(t *testing.T) {
}
}

// TestUnitComputeDependsOn will test computeDependsOn function
func TestUnitComputeDependsOn(t *testing.T) {

createResourceData := func(enableDependencyResolution bool, includeFilterResources []interface{}) *schema.ResourceData {

resourceSchema := map[string]*schema.Schema{
"enable_dependency_resolution": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"include_filter_resources": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
}

data := schema.TestResourceDataRaw(t, resourceSchema, map[string]interface{}{
"enable_dependency_resolution": enableDependencyResolution,
"include_filter_resources": includeFilterResources,
})
return data
}

tests := []struct {
enableDependencyResolution bool
includeFilterResources []interface{}
expected bool
}{
{true, []interface{}{"resource1", "resource2"}, true},
{true, []interface{}{}, false},
{false, []interface{}{"resource1"}, false},
{false, []interface{}{}, false},
}

for _, test := range tests {
data := createResourceData(test.enableDependencyResolution, test.includeFilterResources)
result := computeDependsOn(data)
if result != test.expected {
t.Errorf("computeDependsOn(%v, %v) = %v; want %v", test.enableDependencyResolution, test.includeFilterResources, result, test.expected)
}
}
}

// TestUnitTfExportAllowEmptyArray will test if fields included in the exporter property `AllowEmptyArrays`
// will retain empty arrays in the configMap when their state values are null or [].
// Empty array fields not included in `AllowEmptyArrays` will be sanitized to nil by default,
Expand Down

0 comments on commit 1b8c8f5

Please sign in to comment.