Skip to content

Commit c2e3158

Browse files
committed
[Resource Discovery] Fix issue where singular datasources result in success even though nothing was found during discovery.
This issue was caused by the `handleMissingResourceError` that is used to nullify missing resource errors (404). This resulted in resource discovery assuming that discovery succeeded for singular datasources and proceeding to import a non-existent resource. This fix checks that singular data source results are not voided before proceeding to add it to discovered resource list. Voided singular data source results are considered a case of resource not found.
1 parent 19c025c commit c2e3158

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- Support for `mount_type_details`, `mount_type`, `nfs_server` and `nfs_server_export` attributes in `oci_database_backup_destination` resource
1010
- Support resource discovery for `ons` resources
1111

12+
### Fixed
13+
- Fix issue where discovering object storage buckets without lifecycle policies, results in an error
14+
1215
### Notes
1316
- `mount_type_details` attribute needs to be set when `type` attribute is set to `NFS` in `oci_database_backup_destination` resource
1417

oci/crud_helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ func handleMissingResourceError(sync ResourceVoider, err *error) {
125125
(strings.Contains((*err).Error(), "Load balancer") && strings.Contains((*err).Error(), " has no ")) ||
126126
strings.Contains(strings.ToLower((*err).Error()), "status code: 404") { // status code: 404 is not enough because the load balancer error responses don't include it for some reason
127127
log.Println("[DEBUG] Object does not exist, voiding resource and nullifying error")
128-
sync.VoidState()
128+
if sync != nil {
129+
sync.VoidState()
130+
}
129131
*err = nil
130132
}
131133
}

oci/export_compartment.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ func findResourcesGeneric(clients *OracleClients, tfMeta *TerraformResourceAssoc
916916
return results, err
917917
}
918918

919-
if tfMeta.datasourceItemsAttr != "" {
919+
if !tfMeta.DiscoversWithSingularDatasource() {
920920
// Results are from a plural datasource
921921
itemSchema := datasource.Schema[tfMeta.datasourceItemsAttr]
922922
elemResource, ok := itemSchema.Elem.(*schema.Resource)
@@ -986,8 +986,8 @@ func findResourcesGeneric(clients *OracleClients, tfMeta *TerraformResourceAssoc
986986

987987
results = append(results, resource)
988988
}
989-
} else {
990-
// Result is from a singular datasource
989+
} else if d.Id() != "" {
990+
// Result is from a singular datasource that hasn't had its state voided (hence d.Id() is non-empty)
991991
resource, err := generateOciResourceFromResourceData(d, d, datasource.Schema, "", tfMeta, parent)
992992
if err != nil {
993993
return results, err
@@ -1011,6 +1011,8 @@ func findResourcesGeneric(clients *OracleClients, tfMeta *TerraformResourceAssoc
10111011
if discoverable {
10121012
results = append(results, resource)
10131013
}
1014+
} else {
1015+
log.Printf("[DEBUG] singular data source not able to find resource")
10141016
}
10151017

10161018
return results, nil

oci/export_resource_helpers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ type TerraformResourceHints struct {
5050
ignorableRequiredMissingAttributes map[string]bool
5151
}
5252

53+
func (h *TerraformResourceHints) DiscoversWithSingularDatasource() bool {
54+
return h.datasourceItemsAttr == ""
55+
}
56+
5357
type TerraformResourceAssociation struct {
5458
*TerraformResourceHints
5559
datasourceQueryParams map[string]string // Mapping of data source inputs and the source attribute from a parent resource
@@ -132,7 +136,9 @@ func (ctx *resourceDiscoveryContext) printErrors() {
132136
if resourceDiscoveryError.resourceGraph != nil {
133137
var notFoundChildren []string
134138
getNotFoundChildren(resourceDiscoveryError.resourceType, resourceDiscoveryError.resourceGraph, &notFoundChildren)
135-
color.Red(fmt.Sprintf("\tFollowing child resources were not discovered due to parent error: %v", strings.Join(notFoundChildren, ", ")))
139+
if len(notFoundChildren) > 0 {
140+
color.Red(fmt.Sprintf("\tFollowing child resources were also not discovered due to parent error: %v", strings.Join(notFoundChildren, ", ")))
141+
}
136142
}
137143
}
138144
}

0 commit comments

Comments
 (0)