Skip to content

Commit e130268

Browse files
author
Uddipaan Hazarika
committed
nil pointer fixes in error handling
1 parent 1dd12d6 commit e130268

File tree

5 files changed

+142
-104
lines changed

5 files changed

+142
-104
lines changed

internal/provider/resource_appdata_dsource.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func resourceDsourceRead(ctx context.Context, d *schema.ResourceData, meta inter
591591
d.Set("current_timeflow_id", result.GetCurrentTimeflowId())
592592
d.Set("is_appdata", result.GetIsAppdata())
593593
d.Set("sync_policy_id", result.GetSyncPolicyId())
594-
d.Set("retention_policy_id", result.GetReplicaRetentionPolicyId())
594+
d.Set("retention_policy_id", result.GetRetentionPolicyId())
595595
d.Set("ops_pre_sync", flattenDSourceHooks(result.GetHooks().OpsPreSync, oldOpsPreSync))
596596
d.Set("ops_post_sync", flattenDSourceHooks(result.GetHooks().OpsPostSync, oldOpsPostSync))
597597

@@ -706,15 +706,16 @@ func resourceDsourceUpdate(ctx context.Context, d *schema.ResourceData, meta int
706706
return diags
707707
}
708708

709-
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
710-
if job_err != "" {
711-
tflog.Warn(ctx, DLPX+WARN+"Appdata Dsource Update Job Polling failed but continuing with update. Error: "+job_err)
712-
}
713-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
714-
if isJobTerminalFailure(job_status) {
715-
return diag.Errorf("[NOT OK] Appdata Dsource Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
709+
if res != nil {
710+
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
711+
if job_err != "" {
712+
tflog.Warn(ctx, DLPX+WARN+"Appdata Dsource Update Job Polling failed but continuing with update. Error: "+job_err)
713+
}
714+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
715+
if isJobTerminalFailure(job_status) {
716+
return diag.Errorf("[NOT OK] Appdata Dsource Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
717+
}
716718
}
717-
718719
if d.HasChanges(
719720
"tags",
720721
) { // tags update
@@ -761,15 +762,16 @@ func resourceDsourceDelete(ctx context.Context, d *schema.ResourceData, meta int
761762
return diags
762763
}
763764

764-
job_status, job_err := PollJobStatus(res.GetId(), ctx, client)
765-
if job_err != "" {
766-
tflog.Warn(ctx, DLPX+WARN+"Job Polling failed but continuing with deletion. Error :"+job_err)
767-
}
768-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
769-
if isJobTerminalFailure(job_status) {
770-
return diag.Errorf("[NOT OK] dSource-Delete %s. JobId: %s / Error: %s", job_status, res.GetId(), job_err)
765+
if res != nil {
766+
job_status, job_err := PollJobStatus(res.GetId(), ctx, client)
767+
if job_err != "" {
768+
tflog.Warn(ctx, DLPX+WARN+"Job Polling failed but continuing with deletion. Error :"+job_err)
769+
}
770+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
771+
if isJobTerminalFailure(job_status) {
772+
return diag.Errorf("[NOT OK] dSource-Delete %s. JobId: %s / Error: %s", job_status, res.GetId(), job_err)
773+
}
771774
}
772-
773775
_, diags := PollForObjectDeletion(ctx, func() (interface{}, *http.Response, error) {
774776
return client.DSourcesAPI.GetDsourceById(ctx, dsourceId).Execute()
775777
})

internal/provider/resource_environment.go

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ func resourceEnvironment() *schema.Resource {
360360
Computed: true,
361361
},
362362
},
363+
Importer: &schema.ResourceImporter{
364+
StateContext: schema.ImportStatePassthroughContext,
365+
},
363366
}
364367
}
365368

@@ -734,20 +737,27 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
734737
if diags := apiErrorResponseHelper(ctx, res, httpRes, err); diags != nil {
735738
revertChanges(d, changedKeys)
736739
updateFailure = true
737-
failureEvents = append(failureEvents, httpRes.Body.Close().Error())
740+
if len(diags) > 0 {
741+
failureEvents = append(failureEvents, diags[0].Summary)
742+
} else {
743+
tflog.Warn(ctx, "UpdateEnvironment Diagnostics is empty or nil; skipping appending to failureEvents")
744+
}
738745
}
739746

740-
job_res, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
741-
if job_err != "" {
742-
tflog.Warn(ctx, DLPX+WARN+"Env Host Update Job Polling failed but continuing with update. Error: "+job_err)
743-
}
744-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
745-
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
746-
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+res.Job.GetId()+"!")
747-
revertChanges(d, changedKeys)
748-
updateFailure = true
749-
failureEvents = append(failureEvents, job_err)
750-
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *res.Job.Id, job_res, job_err)
747+
// if the above api call fails, no point in polling as res will be nil
748+
if res != nil {
749+
job_res, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
750+
if job_err != "" {
751+
tflog.Warn(ctx, DLPX+WARN+"Env Host Update Job Polling failed but continuing with update. Error: "+job_err)
752+
}
753+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
754+
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
755+
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+res.Job.GetId()+"!")
756+
revertChanges(d, changedKeys)
757+
updateFailure = true
758+
failureEvents = append(failureEvents, job_err)
759+
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *res.Job.Id, job_res, job_err)
760+
}
751761
}
752762
}
753763
if d.HasChanges(
@@ -798,21 +808,28 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
798808
if diags := apiErrorResponseHelper(ctx, resEnvUser, httpResEnvUser, errEnvUser); diags != nil {
799809
revertChanges(d, changedKeys)
800810
updateFailure = true
801-
failureEvents = append(failureEvents, httpResEnvUser.Body.Close().Error())
811+
if len(diags) > 0 {
812+
failureEvents = append(failureEvents, diags[0].Summary)
813+
} else {
814+
tflog.Warn(ctx, "UpdateEnvironmentUser Diagnostics is empty or nil; skipping appending to failureEvents")
815+
}
802816
}
803817

804-
job_res, job_err := PollJobStatus(resEnvUser.Job.GetId(), ctx, client)
805-
if job_err != "" {
806-
tflog.Warn(ctx, DLPX+WARN+"Env User Update Job Polling failed but continuing with update. Error: "+job_err)
807-
}
808-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
809-
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
810-
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+resEnvUser.Job.GetId()+"!")
811-
revertChanges(d, changedKeys)
812-
updateFailure = true
813-
failureEvents = append(failureEvents, job_err)
814-
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *resEnvUser.Job.Id, job_res, job_err)
818+
if resEnvUser != nil {
819+
job_res, job_err := PollJobStatus(resEnvUser.Job.GetId(), ctx, client)
820+
if job_err != "" {
821+
tflog.Warn(ctx, DLPX+WARN+"Env User Update Job Polling failed but continuing with update. Error: "+job_err)
822+
}
823+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
824+
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
825+
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+resEnvUser.Job.GetId()+"!")
826+
revertChanges(d, changedKeys)
827+
updateFailure = true
828+
failureEvents = append(failureEvents, job_err)
829+
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *resEnvUser.Job.Id, job_res, job_err)
830+
}
815831
}
832+
816833
}
817834
if d.HasChanges(
818835
"hosts",
@@ -888,20 +905,26 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
888905
if diags := apiErrorResponseHelper(ctx, hostUpdateRes, hostHttpRes, hostUpdateErr); diags != nil {
889906
revertChanges(d, changedKeys)
890907
updateFailure = true
891-
failureEvents = append(failureEvents, hostHttpRes.Body.Close().Error())
908+
if len(diags) > 0 {
909+
failureEvents = append(failureEvents, diags[0].Summary)
910+
} else {
911+
tflog.Warn(ctx, "UpdateHost Diagnostics is empty or nil; skipping appending to failureEvents")
912+
}
892913
}
893914

894-
job_res, job_err := PollJobStatus(hostUpdateRes.Job.GetId(), ctx, client)
895-
if job_err != "" {
896-
tflog.Warn(ctx, DLPX+WARN+"Env Host Update Job Polling failed but continuing with update. Error: "+job_err)
897-
}
898-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
899-
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
900-
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+hostUpdateRes.Job.GetId()+"!")
901-
revertChanges(d, changedKeys)
902-
updateFailure = true
903-
failureEvents = append(failureEvents, job_err)
904-
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *hostUpdateRes.Job.Id, job_res, job_err)
915+
if hostUpdateRes != nil {
916+
job_res, job_err := PollJobStatus(hostUpdateRes.Job.GetId(), ctx, client)
917+
if job_err != "" {
918+
tflog.Warn(ctx, DLPX+WARN+"Env Host Update Job Polling failed but continuing with update. Error: "+job_err)
919+
}
920+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
921+
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
922+
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+hostUpdateRes.Job.GetId()+"!")
923+
revertChanges(d, changedKeys)
924+
updateFailure = true
925+
failureEvents = append(failureEvents, job_err)
926+
// return diag.Errorf("[NOT OK] Job %s %s with error %s", *hostUpdateRes.Job.Id, job_res, job_err)
927+
}
905928
}
906929
}
907930

@@ -922,7 +945,11 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
922945
if diags := apiErrorResponseHelper(ctx, nil, tagDelResp, tagDelErr); diags != nil {
923946
revertChanges(d, changedKeys)
924947
updateFailure = true
925-
failureEvents = append(failureEvents, tagDelResp.Body.Close().Error())
948+
if len(diags) > 0 {
949+
failureEvents = append(failureEvents, diags[0].Summary)
950+
} else {
951+
tflog.Warn(ctx, "DeleteEnvironmentTags Diagnostics is empty or nil; skipping appending to failureEvents")
952+
}
926953
}
927954
}
928955
// create tag

internal/provider/resource_oracle_dsource.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ func resourceOracleDsourceRead(ctx context.Context, d *schema.ResourceData, meta
923923
d.Set("current_timeflow_id", result.GetCurrentTimeflowId())
924924
d.Set("is_appdata", result.GetIsAppdata())
925925
d.Set("sync_policy_id", result.GetSyncPolicyId())
926-
d.Set("retention_policy_id", result.GetReplicaRetentionPolicyId())
926+
d.Set("retention_policy_id", result.GetRetentionPolicyId())
927927
d.Set("log_sync_enabled", result.GetLogsyncEnabled())
928928
d.Set("exported_data_directory", result.GetExportedDataDirectory())
929929
d.Set("ops_pre_sync", flattenDSourceHooks(result.GetHooks().OpsPreSync, oldOpsPreSync))
@@ -1073,13 +1073,15 @@ func resourceOracleDsourceUpdate(ctx context.Context, d *schema.ResourceData, me
10731073
return diags
10741074
}
10751075

1076-
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
1077-
if job_err != "" {
1078-
tflog.Warn(ctx, DLPX+WARN+"Oracle Dsource Update Job Polling failed but continuing with update. Error: "+job_err)
1079-
}
1080-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1081-
if isJobTerminalFailure(job_status) {
1082-
return diag.Errorf("[NOT OK] Oracle Dsource-Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
1076+
if res != nil {
1077+
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
1078+
if job_err != "" {
1079+
tflog.Warn(ctx, DLPX+WARN+"Oracle Dsource Update Job Polling failed but continuing with update. Error: "+job_err)
1080+
}
1081+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1082+
if isJobTerminalFailure(job_status) {
1083+
return diag.Errorf("[NOT OK] Oracle Dsource-Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
1084+
}
10831085
}
10841086

10851087
if d.HasChanges(
@@ -1128,15 +1130,16 @@ func resourceOracleDsourceDelete(ctx context.Context, d *schema.ResourceData, me
11281130
return diags
11291131
}
11301132

1131-
job_status, job_err := PollJobStatus(res.GetId(), ctx, client)
1132-
if job_err != "" {
1133-
tflog.Warn(ctx, DLPX+WARN+"Job Polling failed but continuing with deletion. Error :"+job_err)
1134-
}
1135-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1136-
if isJobTerminalFailure(job_status) {
1137-
return diag.Errorf("[NOT OK] dSource-Delete %s. JobId: %s / Error: %s", job_status, res.GetId(), job_err)
1133+
if res != nil {
1134+
job_status, job_err := PollJobStatus(res.GetId(), ctx, client)
1135+
if job_err != "" {
1136+
tflog.Warn(ctx, DLPX+WARN+"Job Polling failed but continuing with deletion. Error :"+job_err)
1137+
}
1138+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1139+
if isJobTerminalFailure(job_status) {
1140+
return diag.Errorf("[NOT OK] dSource-Delete %s. JobId: %s / Error: %s", job_status, res.GetId(), job_err)
1141+
}
11381142
}
1139-
11401143
_, diags := PollForObjectDeletion(ctx, func() (interface{}, *http.Response, error) {
11411144
return client.DSourcesAPI.GetDsourceById(ctx, dsourceId).Execute()
11421145
})

internal/provider/resource_vdb.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,17 @@ func helper_provision_by_snapshot(ctx context.Context, d *schema.ResourceData, m
10201020

10211021
d.SetId(apiRes.GetVdbId())
10221022

1023-
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1024-
if job_err != "" {
1025-
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1026-
}
1027-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1028-
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
1029-
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+apiRes.Job.GetId()+"!")
1030-
return diag.Errorf("[NOT OK] Job %s %s with error %s", apiRes.Job.GetId(), job_res, job_err)
1023+
if apiRes != nil {
1024+
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1025+
if job_err != "" {
1026+
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1027+
}
1028+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1029+
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
1030+
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+" "+apiRes.Job.GetId()+"!")
1031+
return diag.Errorf("[NOT OK] Job %s %s with error %s", apiRes.Job.GetId(), job_res, job_err)
1032+
}
10311033
}
1032-
10331034
readDiags := resourceVdbRead(ctx, d, meta)
10341035

10351036
if readDiags.HasError() {
@@ -1268,16 +1269,17 @@ func helper_provision_by_timestamp(ctx context.Context, d *schema.ResourceData,
12681269

12691270
d.SetId(apiRes.GetVdbId())
12701271

1271-
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1272-
if job_err != "" {
1273-
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1274-
}
1275-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1276-
if job_res == "FAILED" {
1277-
tflog.Error(ctx, DLPX+ERROR+"Job "+apiRes.Job.GetId()+" Failed!")
1278-
return diag.Errorf("[NOT OK] Job %s Failed with error %s", apiRes.Job.GetId(), job_err)
1272+
if apiRes != nil {
1273+
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1274+
if job_err != "" {
1275+
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1276+
}
1277+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1278+
if job_res == "FAILED" {
1279+
tflog.Error(ctx, DLPX+ERROR+"Job "+apiRes.Job.GetId()+" Failed!")
1280+
return diag.Errorf("[NOT OK] Job %s Failed with error %s", apiRes.Job.GetId(), job_err)
1281+
}
12791282
}
1280-
12811283
readDiags := resourceVdbRead(ctx, d, meta)
12821284

12831285
if readDiags.HasError() {
@@ -1501,16 +1503,17 @@ func helper_provision_by_bookmark(ctx context.Context, d *schema.ResourceData, m
15011503

15021504
d.SetId(apiRes.GetVdbId())
15031505

1504-
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1505-
if job_err != "" {
1506-
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1507-
}
1508-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1509-
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
1510-
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+apiRes.Job.GetId()+"!")
1511-
return diag.Errorf("[NOT OK] Job %s %s with error %s", apiRes.Job.GetId(), job_res, job_err)
1506+
if apiRes != nil {
1507+
job_res, job_err := PollJobStatus(apiRes.Job.GetId(), ctx, client)
1508+
if job_err != "" {
1509+
tflog.Error(ctx, DLPX+ERROR+"Job Polling failed but continuing with provisioning. Error: "+job_err)
1510+
}
1511+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_res)
1512+
if job_res == Failed || job_res == Canceled || job_res == Abandoned {
1513+
tflog.Error(ctx, DLPX+ERROR+"Job "+job_res+apiRes.Job.GetId()+"!")
1514+
return diag.Errorf("[NOT OK] Job %s %s with error %s", apiRes.Job.GetId(), job_res, job_err)
1515+
}
15121516
}
1513-
15141517
readDiags := resourceVdbRead(ctx, d, meta)
15151518

15161519
if readDiags.HasError() {
@@ -1933,13 +1936,15 @@ func resourceVdbUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
19331936
return diags
19341937
}
19351938

1936-
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
1937-
if job_err != "" {
1938-
tflog.Warn(ctx, DLPX+WARN+"VDB Update Job Polling failed but continuing with update. Error: "+job_err)
1939-
}
1940-
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1941-
if isJobTerminalFailure(job_status) {
1942-
return diag.Errorf("[NOT OK] VDB-Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
1939+
if res != nil {
1940+
job_status, job_err := PollJobStatus(res.Job.GetId(), ctx, client)
1941+
if job_err != "" {
1942+
tflog.Warn(ctx, DLPX+WARN+"VDB Update Job Polling failed but continuing with update. Error: "+job_err)
1943+
}
1944+
tflog.Info(ctx, DLPX+INFO+"Job result is "+job_status)
1945+
if isJobTerminalFailure(job_status) {
1946+
return diag.Errorf("[NOT OK] VDB-Update %s. JobId: %s / Error: %s", job_status, res.Job.GetId(), job_err)
1947+
}
19431948
}
19441949

19451950
if d.HasChanges(

internal/provider/utility.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func apiErrorResponseHelper(ctx context.Context, res interface{}, httpRes *http.
248248
tflog.Error(ctx, DLPX+ERROR+"An error occurred: "+nerr.Error())
249249
diags = diag.FromErr(nerr)
250250
} else {
251+
tflog.Info(ctx, DLPX+INFO+"Error: "+resBody)
251252
diags = diag.Errorf(resBody)
252253
}
253254
return diags

0 commit comments

Comments
 (0)