Skip to content

Commit b2d2151

Browse files
committed
Update error handling on route data source
1 parent a4aadb5 commit b2d2151

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

ibm/service/power/data_source_ibm_pi_route.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package power
55

66
import (
77
"context"
8+
"fmt"
89
"log"
910

1011
"github.com/IBM-Cloud/power-go-client/clients/instance"
@@ -95,10 +96,12 @@ func DataSourceIBMPIRoute() *schema.Resource {
9596
}
9697
}
9798

98-
func dataSourceIBMPIRouteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
99+
func dataSourceIBMPIRouteRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
99100
sess, err := meta.(conns.ClientSession).IBMPISession()
100101
if err != nil {
101-
return diag.FromErr(err)
102+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("IBMPISession failed: %s", err.Error()), "(Data) ibm_pi_route", "read")
103+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
104+
return tfErr.GetDiag()
102105
}
103106

104107
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
@@ -107,7 +110,9 @@ func dataSourceIBMPIRouteRead(ctx context.Context, d *schema.ResourceData, meta
107110

108111
route, err := client.Get(routeID)
109112
if err != nil {
110-
return diag.FromErr(err)
113+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Get failed: %s", err.Error()), "(Data) ibm_pi_route", "read")
114+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
115+
return tfErr.GetDiag()
111116
}
112117

113118
d.Set(Attr_Action, route.Action)

ibm/service/power/data_source_ibm_pi_route_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestAccIBMPIRouteDataSource_basic(t *testing.T) {
3131
func testAccCheckIBMPIRouteDataSourceConfig() string {
3232
return fmt.Sprintf(`
3333
data "ibm_pi_route" "route_data" {
34-
pi_cloud_instance_id = "%[2]s"
35-
pi_route_id = "%[1]s"
36-
}`, acc.Pi_route_id, acc.Pi_cloud_instance_id)
34+
pi_cloud_instance_id = "%[1]s"
35+
pi_route_id = "%[2]s"
36+
}`, acc.Pi_cloud_instance_id, acc.Pi_route_id)
3737
}

ibm/service/power/data_source_ibm_pi_routes.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package power
55

66
import (
77
"context"
8+
"fmt"
89
"log"
910

1011
"github.com/IBM-Cloud/power-go-client/clients/instance"
@@ -105,18 +106,22 @@ func DataSourceIBMPIRoutes() *schema.Resource {
105106
}
106107
}
107108

108-
func dataSourceIBMPIRoutesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
109+
func dataSourceIBMPIRoutesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
109110
sess, err := meta.(conns.ClientSession).IBMPISession()
110111
if err != nil {
111-
return diag.FromErr(err)
112+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("IBMPISession failed: %s", err.Error()), "(Data) ibm_pi_routes", "read")
113+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
114+
return tfErr.GetDiag()
112115
}
113116

114117
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
115118
client := instance.NewIBMPIRouteClient(ctx, sess, cloudInstanceID)
116119

117120
routes, err := client.GetAll()
118121
if err != nil {
119-
return diag.FromErr(err)
122+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetAll failed: %s", err.Error()), "(Data) ibm_pi_routes", "read")
123+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
124+
return tfErr.GetDiag()
120125
}
121126
var clientgenU, _ = uuid.GenerateUUID()
122127
d.SetId(clientgenU)
@@ -125,10 +130,10 @@ func dataSourceIBMPIRoutesRead(ctx context.Context, d *schema.ResourceData, meta
125130
return nil
126131
}
127132

128-
func flattenRoutes(routes []*models.Route, meta interface{}) []map[string]interface{} {
129-
result := make([]map[string]interface{}, len(routes))
133+
func flattenRoutes(routes []*models.Route, meta any) []map[string]any {
134+
result := make([]map[string]any, len(routes))
130135
for _, r := range routes {
131-
route := map[string]interface{}{
136+
route := map[string]any{
132137
Attr_RouteID: r.ID,
133138
Attr_Action: r.Action,
134139
Attr_Advertise: r.Advertise,

0 commit comments

Comments
 (0)