Skip to content

Commit

Permalink
nil pointer check for instance (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikmota committed Apr 14, 2023
1 parent 2b9a899 commit 6928716
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions civo/datasource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,23 @@ func dataSourceInstanceRead(_ context.Context, d *schema.ResourceData, m interfa
log.Printf("[INFO] Getting the instance by id")
image, err := apiClient.FindInstance(id.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive instance: %s", err)
return diag.Errorf("[ERR] failed to retrieve instance: %s", err)
}

foundImage = image
} else if hostname, ok := d.GetOk("hostname"); ok {
log.Printf("[INFO] Getting the instance by hostname")
image, err := apiClient.FindInstance(hostname.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive instance: %s", err)
return diag.Errorf("[ERR] failed to retrieve instance: %s", err)
}

foundImage = image
}

//Check for nil pointer
if foundImage == nil {
return diag.Errorf("[ERR] failed to retrieve instance, image not found")
}
d.SetId(foundImage.ID)
d.Set("hostname", foundImage.Hostname)
d.Set("reverse_dns", foundImage.ReverseDNS)
Expand Down

0 comments on commit 6928716

Please sign in to comment.