Skip to content

Commit 84b38da

Browse files
committed
Added - Support for Discovering all resources irrespective of their lifecycle state
1 parent 6e783ad commit 84b38da

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

internal/commonexport/commonexport_functions.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,16 @@ func FindResourcesGeneric(ctx *ResourceDiscoveryContext, tfMeta *TerraformResour
489489
if itemMap, ok := item.(map[string]interface{}); ok {
490490
if state, exists := itemMap["state"].(string); exists && len(tfMeta.DiscoverableLifecycleStates) > 0 {
491491
discoverable := false
492-
for _, val := range tfMeta.DiscoverableLifecycleStates {
493-
if strings.EqualFold(state, val) {
494-
discoverable = true
495-
break
492+
493+
if utils.GetEnvSettingWithBlankDefault(globalvar.DiscoverAllStatesEnv) == "1" {
494+
utils.Logf("Skipping Lifecycle State Check as TF_DISCOVER_ALL_STATES is set")
495+
discoverable = true
496+
} else {
497+
for _, val := range tfMeta.DiscoverableLifecycleStates {
498+
if strings.EqualFold(state, val) {
499+
discoverable = true
500+
break
501+
}
496502
}
497503
}
498504

@@ -609,10 +615,16 @@ func FindResourcesGeneric(ctx *ResourceDiscoveryContext, tfMeta *TerraformResour
609615
discoverable := true
610616
if state, ok := resource.SourceAttributes["state"]; ok && len(tfMeta.DiscoverableLifecycleStates) > 0 {
611617
discoverable = false
612-
for _, val := range tfMeta.DiscoverableLifecycleStates {
613-
if strings.EqualFold(state.(string), val) {
614-
discoverable = true
615-
break
618+
619+
if utils.GetEnvSettingWithBlankDefault(globalvar.DiscoverAllStatesEnv) == "1" {
620+
utils.Logf("Skipping Lifecycle State Check as TF_DISCOVER_ALL_STATES is set")
621+
discoverable = true
622+
} else {
623+
for _, val := range tfMeta.DiscoverableLifecycleStates {
624+
if strings.EqualFold(state.(string), val) {
625+
discoverable = true
626+
break
627+
}
616628
}
617629
}
618630
}

internal/globalvar/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ These missing attributes are also added to the lifecycle ignore_changes.
7171
EnvOCITFLogFile = "OCI_TF_LOG_PATH" // Log path for Custom TF logger - TFProviderLogger
7272
TerraformBinPathName = "terraform_bin_path"
7373
MaxInt64 = 1<<63 - 1 // TODO : Fix needed for GoLang SDK v1.17.2
74+
DiscoverAllStatesEnv = "TF_DISCOVER_ALL_STATES"
7475
)
7576

7677
const (

internal/resourcediscovery/export_compartment_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ var exportChildDefinition = &tf_export.TerraformResourceHints{
6969
RequireResourceRefresh: true,
7070
}
7171

72+
var exportChildDefinitionInactive = &tf_export.TerraformResourceHints{
73+
ResourceClass: "oci_test_child_inactive",
74+
DatasourceClass: "oci_test_children",
75+
ResourceAbbreviation: "child",
76+
DatasourceItemsAttr: "item_summaries",
77+
DiscoverableLifecycleStates: []string{resourceDiscoveryTestInactiveLifecycle},
78+
}
79+
7280
var exportParentDefinitionWithFaultyDatasource = &tf_export.TerraformResourceHints{
7381
ResourceClass: "oci_test_parent",
7482
DatasourceClass: "oci_test_error_parents",
@@ -132,6 +140,24 @@ var compartmentTestingResourceGraph = tf_export.TerraformResourceGraph{
132140
},
133141
}
134142

143+
var compartmentTestingResourceInactiveLifeCycleGraphGraph = tf_export.TerraformResourceGraph{
144+
"oci_identity_compartment": {
145+
{
146+
TerraformResourceHints: exportParentDefinition,
147+
},
148+
},
149+
"oci_test_parent": {
150+
{
151+
TerraformResourceHints: exportChildDefinitionInactive,
152+
DatasourceQueryParams: map[string]string{"parent_id": "id"},
153+
},
154+
{
155+
TerraformResourceHints: exportChildDefinition,
156+
DatasourceQueryParams: map[string]string{"parent_id": "id"},
157+
},
158+
},
159+
}
160+
135161
var compartmentTestingResourceGraphWithFaultyParentResource = tf_export.TerraformResourceGraph{
136162
"oci_identity_compartment": {
137163
{
@@ -1216,6 +1242,60 @@ func TestUnitFindResources_basic(t *testing.T) {
12161242
}
12171243
}
12181244

1245+
func TestUnitFindResourcesInActiveLifeCycle_basic(t *testing.T) {
1246+
initResourceDiscoveryTests()
1247+
defer cleanupResourceDiscoveryTests()
1248+
rootResource := getRootCompartmentResource()
1249+
1250+
ctx := &tf_export.ResourceDiscoveryContext{
1251+
ErrorList: tf_export.ErrorList{},
1252+
}
1253+
os.Setenv(globalvar.DiscoverAllStatesEnv, "1")
1254+
1255+
results, err := findResources(ctx, rootResource, compartmentTestingResourceInactiveLifeCycleGraphGraph, true)
1256+
if err != nil {
1257+
t.Logf("got error from findResources: %v", err)
1258+
t.Fail()
1259+
}
1260+
1261+
foundInactiveResource := false
1262+
for _, foundResource := range results {
1263+
if foundResource.TerraformClass == "oci_test_child_inactive" {
1264+
foundInactiveResource = true
1265+
break
1266+
}
1267+
}
1268+
if foundInactiveResource == false {
1269+
t.Logf("Inactive Resources not found")
1270+
t.Fail()
1271+
}
1272+
}
1273+
1274+
func TestUnitFindResourcesInActiveLifeCycleWithGlobalVariableNotSet_basic(t *testing.T) {
1275+
initResourceDiscoveryTests()
1276+
defer cleanupResourceDiscoveryTests()
1277+
rootResource := getRootCompartmentResource()
1278+
1279+
ctx := &tf_export.ResourceDiscoveryContext{
1280+
ErrorList: tf_export.ErrorList{},
1281+
}
1282+
1283+
os.Unsetenv(globalvar.DiscoverAllStatesEnv)
1284+
1285+
results, err := findResources(ctx, rootResource, compartmentTestingResourceInactiveLifeCycleGraphGraph, true)
1286+
if err != nil {
1287+
t.Logf("got error from findResources: %v", err)
1288+
t.Fail()
1289+
}
1290+
1291+
for _, foundResource := range results {
1292+
if foundResource.TerraformClass == "oci_test_child_inactive" {
1293+
t.Logf("Inactive resource found even when export variable TF_DISCOVER_ALL_STATES is not set to 1: %v", err)
1294+
t.Fail()
1295+
}
1296+
}
1297+
}
1298+
12191299
// Test that resources can be found using a resource dependency graph
12201300
// issue-routing-tag: terraform/default
12211301
func TestUnitFindResources_filter(t *testing.T) {
@@ -1232,7 +1312,7 @@ func TestUnitFindResources_filter(t *testing.T) {
12321312
}},
12331313
},
12341314
}
1235-
results, err := findResources(ctx, rootResource, compartmentTestingResourceGraph, true)
1315+
results, err := findResources(ctx, rootResource, compartmentTestingResourceGraph, false)
12361316
if err != nil {
12371317
t.Logf("got error from findResources: %v", err)
12381318
t.Fail()

website/docs/guides/resource_discovery.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ Example for specifying multiple filters
333333
```
334334
--filter="Type=oci_core_vcn" --filter="AttrName=dns_label;Value=test" // discover resources of type oci_core_vcn such that they have dns_label attribute value as test
335335
```
336+
### Discover all resources
337+
338+
If TF_DISCOVER_ALL_STATES is set to 1, then Resource Discovery will try to discover all resources irrespective of
339+
their lifecycle state.
340+
341+
```
342+
export TF_DISCOVER_ALL_STATES=1
343+
```
344+
336345

337346
### Supported Resources
338347
As of this writing, the list of Terraform services and resources that can be discovered by the command is as follows.

0 commit comments

Comments
 (0)