Skip to content

Commit f2d3baf

Browse files
fix(dnsservices): update error toolchain changes for dns services (IBM-Cloud#6289)
* updated according to sme * minor fixes * removed older error logs * minor fixes * removed changes on test files
1 parent 863aeff commit f2d3baf

19 files changed

+250
-136
lines changed

ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
)
@@ -101,14 +102,16 @@ func DataSourceIBMPrivateDNSCustomResolver() *schema.Resource {
101102
func dataSourceIBMDNSCustomResolverRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
102103
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
103104
if err != nil {
104-
return diag.FromErr(err)
105+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMDNSCustomResolverRead Client initialization failed: %s", err.Error()), "ibm_dns_custom_resolver", "read")
106+
return tfErr.GetDiag()
105107
}
106108
instanceID := d.Get(pdnsInstanceID).(string)
107109

108110
opt := sess.NewListCustomResolversOptions(instanceID)
109111
result, resp, err := sess.ListCustomResolversWithContext(context, opt)
110112
if err != nil || result == nil {
111-
return diag.FromErr(fmt.Errorf("[ERROR] Error listing the custom resolvers %s:%s", err, resp))
113+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMDNSCustomResolverRead ListCustomResolversWithContext failed with error: %s and response:\n%s", err, resp), "ibm_dns_custom_resolver", "read")
114+
return tfErr.GetDiag()
112115
}
113116

114117
customResolvers := make([]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_custom_resolver_forwarding_rules.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
)
@@ -101,7 +102,8 @@ func DataSourceIBMPrivateDNSForwardingRules() *schema.Resource {
101102
func dataSourceIbmDnsCrForwardingRulesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
102103
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
103104
if err != nil {
104-
return diag.FromErr(err)
105+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIbmDnsCrForwardingRulesRead Client initialization failed: %s", err.Error()), "ibm_dns_custom_resolver_forwarding_rule", "read")
106+
return tfErr.GetDiag()
105107
}
106108
instanceID := d.Get(pdnsInstanceID).(string)
107109
resolverID := d.Get(pdnsCRFRResolverID).(string)
@@ -110,7 +112,8 @@ func dataSourceIbmDnsCrForwardingRulesRead(context context.Context, d *schema.Re
110112

111113
result, resp, err := sess.ListForwardingRulesWithContext(context, opt)
112114
if err != nil || result == nil {
113-
return diag.FromErr(fmt.Errorf("[ERROR] Error listing the forwarding rules %s:%s", err, resp))
115+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIbmDnsCustomResolverForwardingRulesRead ListForwardingRulesWithContext failed with error: %s and response:\n%s", err, resp), "ibm_dns_custom_resolver_forwarding_rule", "read")
116+
return tfErr.GetDiag()
114117
}
115118

116119
forwardRules := make([]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_glb_monitors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dnsservices
55

66
import (
7-
"fmt"
87
"time"
98

109
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

@@ -139,7 +139,7 @@ func dataSourceIBMPrivateDNSGLBMonitorsRead(d *schema.ResourceData, meta interfa
139139
listDNSGLBMonitorions := sess.NewListMonitorsOptions(instanceID)
140140
availableGLBMonitors, detail, err := sess.ListMonitors(listDNSGLBMonitorions)
141141
if err != nil {
142-
return fmt.Errorf("[ERROR] Error reading list of pdns GLB monitors:%s\n%s", err, detail)
142+
return flex.FmtErrorf("[ERROR] Error reading list of dns services GLB monitors:%s\n%s", err, detail)
143143
}
144144

145145
dnsMonitors := make([]map[string]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_glb_pools.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dnsservices
55

66
import (
7-
"fmt"
87
"time"
98

109
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

@@ -149,7 +149,7 @@ func dataSourceIBMPrivateDNSGLBPoolsRead(d *schema.ResourceData, meta interface{
149149
listDNSGLBPooloptions := sess.NewListPoolsOptions(instanceID)
150150
availableGLBPools, detail, err := sess.ListPools(listDNSGLBPooloptions)
151151
if err != nil {
152-
return fmt.Errorf("[ERROR] Error reading list of pdns GLB pools:%s\n%s", err, detail)
152+
return flex.FmtErrorf("[ERROR] Error reading list of dns services GLB pools:%s\n%s", err, detail)
153153
}
154154
d.Set(pdnsInstanceID, instanceID)
155155
dnsPools := make([]map[string]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_glbs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dnsservices
55

66
import (
7-
"fmt"
87
"time"
98

109
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

@@ -131,7 +131,7 @@ func dataSourceIBMPrivateDNSGLBsRead(d *schema.ResourceData, meta interface{}) e
131131
listDNSGLBs := sess.NewListLoadBalancersOptions(instanceID, zoneID)
132132
availableGLBs, detail, err := sess.ListLoadBalancers(listDNSGLBs)
133133
if err != nil {
134-
return fmt.Errorf("[ERROR] Error reading list of pdns GLB load balancers:%s\n%s", err, detail)
134+
return flex.FmtErrorf("[ERROR] Error reading list of dns services GLB load balancers:%s\n%s", err, detail)
135135
}
136136

137137
dnslbs := make([]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_permitted_network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dnsservices
55

66
import (
7-
"fmt"
87
"time"
98

109
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

@@ -107,7 +107,7 @@ func dataSourceIBMPrivateDNSPermittedNetworksRead(d *schema.ResourceData, meta i
107107
listPermittedNetworkOptions := sess.NewListPermittedNetworksOptions(instanceID, dnsZoneID)
108108
availablePermittedNetworks, detail, err := sess.ListPermittedNetworks(listPermittedNetworkOptions)
109109
if err != nil {
110-
return fmt.Errorf("[ERROR] Error reading list of pdns permitted networks:%s\n%s", err, detail)
110+
return flex.FmtErrorf("[ERROR] Error reading list of dns services permitted networks:%s\n%s", err, detail)
111111
}
112112

113113
permittedNetworks := make([]map[string]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_resource_records.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package dnsservices
55

66
import (
77
"encoding/json"
8-
"fmt"
98
"time"
109

1110
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
11+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
)
1414

@@ -79,7 +79,7 @@ func dataSourceIBMPrivateDNSResourceRecordsRead(d *schema.ResourceData, meta int
7979
listDNSResRecOptions := sess.NewListResourceRecordsOptions(instanceID, DnszoneID)
8080
availableDNSResRecs, detail, err := sess.ListResourceRecords(listDNSResRecOptions)
8181
if err != nil {
82-
return fmt.Errorf("[ERROR] Error reading list of pdns resource records:%s\n%s", err, detail)
82+
return flex.FmtErrorf("[ERROR] Error reading list of dns services resource records:%s\n%s", err, detail)
8383
}
8484
dnsResRecs := make([]map[string]interface{}, 0)
8585
for _, instance := range availableDNSResRecs.ResourceRecords {
@@ -90,7 +90,7 @@ func dataSourceIBMPrivateDNSResourceRecordsRead(d *schema.ResourceData, meta int
9090
// Marshal the rdata map into a JSON string
9191
rData, err := json.Marshal(instance.Rdata)
9292
if err != nil {
93-
return fmt.Errorf("[ERROR] Error reading rdata map of dns resource records:%s", err)
93+
return flex.FmtErrorf("[ERROR] Error reading rdata map of dns resource records:%s", err)
9494
}
9595
jsonStr := string(rData)
9696
dnsRecord[pdnsRdata] = jsonStr

ibm/service/dnsservices/data_source_ibm_private_dns_secondary_zones.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
)
@@ -97,7 +98,8 @@ func DataSourceIBMPrivateDNSSecondaryZones() *schema.Resource {
9798
func dataSourceIBMDNSSecondaryZonesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
9899
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
99100
if err != nil {
100-
return diag.FromErr(err)
101+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMDNSSecondaryZonesRead Client initialization failed: %s", err.Error()), "ibm_dns_custom_resolver_secondary_zone", "read")
102+
return tfErr.GetDiag()
101103
}
102104
instanceID := d.Get(pdnsInstanceID).(string)
103105
resolverID := d.Get(pdnsSZResolverID).(string)
@@ -106,7 +108,8 @@ func dataSourceIBMDNSSecondaryZonesRead(context context.Context, d *schema.Resou
106108

107109
result, resp, err := sess.ListSecondaryZonesWithContext(context, opt)
108110
if err != nil || result == nil {
109-
return diag.FromErr(fmt.Errorf("[ERROR] Error listing the Secondary Zones %s:%s", err, resp))
111+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMDNSSecondaryZonesRead ListSecondaryZonesWithContext failed with error: %s and response:\n%s", err, resp), "ibm_dns_custom_resolver_secondary_zone", "read")
112+
return tfErr.GetDiag()
110113
}
111114

112115
secondaryZones := make([]interface{}, 0)

ibm/service/dnsservices/data_source_ibm_private_dns_zones.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dnsservices
55

66
import (
7-
"fmt"
87
"time"
98

109
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

@@ -87,7 +87,7 @@ func dataSourceIBMPrivateDNSZonesRead(d *schema.ResourceData, meta interface{})
8787
listDNSZonesOptions := sess.NewListDnszonesOptions(instanceID)
8888
availableDNSZones, detail, err := sess.ListDnszones(listDNSZonesOptions)
8989
if err != nil {
90-
return fmt.Errorf("[ERROR] Error reading list of dns zones:%s\n%s", err, detail)
90+
return flex.FmtErrorf("[ERROR] Error reading list of dns zones:%s\n%s", err, detail)
9191
}
9292
dnsZones := make([]map[string]interface{}, 0)
9393
for _, instance := range availableDNSZones.Dnszones {

ibm/service/dnsservices/resource_ibm_dns_linked_zone.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12-
//"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
13+
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
)
@@ -103,7 +104,8 @@ func ResourceIBMDNSLinkedZone() *schema.Resource {
103104
func resourceIBMDNSLinkedZoneCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
104105
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
105106
if err != nil {
106-
return diag.FromErr(err)
107+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneCreate Client initialization failed: %s", err.Error()), "ibm_dns_linked_zone", "create")
108+
return tfErr.GetDiag()
107109
}
108110

109111
var (
@@ -130,7 +132,8 @@ func resourceIBMDNSLinkedZoneCreate(ctx context.Context, d *schema.ResourceData,
130132

131133
resource, response, err := sess.CreateLinkedZone(createLinkedZoneOptions)
132134
if err != nil {
133-
return diag.FromErr(fmt.Errorf("[ERROR] Error creating DNS Linked zone:%s\n%s", err, response))
135+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("CreateLinkedZone failed with error: %s and response:\n%s", err, response), "ibm_dns_linked_zone", "create")
136+
return tfErr.GetDiag()
134137
}
135138

136139
d.SetId(fmt.Sprintf("%s/%s", instanceID, *resource.ID))
@@ -140,11 +143,14 @@ func resourceIBMDNSLinkedZoneCreate(ctx context.Context, d *schema.ResourceData,
140143
func resourceIBMDNSLinkedZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
141144
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
142145
if err != nil {
143-
return diag.FromErr(err)
146+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneRead Client initialization failed: %s", err.Error()), "ibm_dns_linked_zone", "read")
147+
return tfErr.GetDiag()
144148
}
145149
idSet := strings.Split(d.Id(), "/")
146150
if len(idSet) < 2 {
147-
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id()))
151+
err := fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id())
152+
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_dns_linked_zone", "read")
153+
return tfErr.GetDiag()
148154
}
149155
instanceID := idSet[0]
150156
linkedDnsZoneID := idSet[1]
@@ -156,7 +162,8 @@ func resourceIBMDNSLinkedZoneRead(ctx context.Context, d *schema.ResourceData, m
156162
d.SetId("")
157163
return nil
158164
}
159-
return diag.FromErr(fmt.Errorf("[ERROR] Error reading DNS Linked zone:%s\n%s", err, response))
165+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetLinkedZone failed with error: %s and response:\n%s", err, response), "ibm_dns_linked_zone", "read")
166+
return tfErr.GetDiag()
160167
}
161168

162169
d.Set(DnsLinkedZoneInstanceID, idSet[0])
@@ -171,12 +178,15 @@ func resourceIBMDNSLinkedZoneRead(ctx context.Context, d *schema.ResourceData, m
171178
func resourceIBMDNSLinkedZoneUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
172179
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
173180
if err != nil {
174-
return diag.FromErr(err)
181+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneUpdate Client initialization failed: %s", err.Error()), "ibm_dns_linked_zone", "update")
182+
return tfErr.GetDiag()
175183
}
176184

177185
idSet := strings.Split(d.Id(), "/")
178186
if len(idSet) < 2 {
179-
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/resolverID/secondaryZoneID", d.Id()))
187+
err := fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/resolverID/secondaryZoneID", d.Id())
188+
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_dns_linked_zone", "update")
189+
return tfErr.GetDiag()
180190
}
181191
instanceID := idSet[0]
182192
linkedDnsZoneID := idSet[1]
@@ -185,7 +195,8 @@ func resourceIBMDNSLinkedZoneUpdate(ctx context.Context, d *schema.ResourceData,
185195
getLinkedZoneOptions := sess.NewGetLinkedZoneOptions(instanceID, linkedDnsZoneID)
186196
_, response, err := sess.GetLinkedZone(getLinkedZoneOptions)
187197
if err != nil {
188-
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching secondary zone:%s\n%s", err, response))
198+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneUpdate GetLinkedZone failed with error: %s and response:\n%s", err, response), "ibm_dns_linked_zone", "update")
199+
return tfErr.GetDiag()
189200
}
190201

191202
// Update DNS Linked zone if attributes has any change
@@ -204,7 +215,8 @@ func resourceIBMDNSLinkedZoneUpdate(ctx context.Context, d *schema.ResourceData,
204215
_, response, err := sess.UpdateLinkedZone(updateLinkedZoneOptions)
205216

206217
if err != nil {
207-
return diag.FromErr(fmt.Errorf("[ERROR] Error updating DNS Services zone:%s\n%s", err, response))
218+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("UpdateLinkedZone failed with error: %s and response:\n%s", err, response), "ibm_dns_linked_zone", "update")
219+
return tfErr.GetDiag()
208220
}
209221
}
210222

@@ -213,11 +225,14 @@ func resourceIBMDNSLinkedZoneUpdate(ctx context.Context, d *schema.ResourceData,
213225
func resourceIBMDNSLinkedZoneDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
214226
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
215227
if err != nil {
216-
return diag.FromErr(err)
228+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneDelete Client initialization failed: %s", err.Error()), "ibm_dns_linked_zone", "delete")
229+
return tfErr.GetDiag()
217230
}
218231
idSet := strings.Split(d.Id(), "/")
219232
if len(idSet) < 2 {
220-
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id()))
233+
err := fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/linkedDnsZoneID", d.Id())
234+
tfErr := flex.TerraformErrorf(err, err.Error(), "ibm_dns_linked_zone", "delete")
235+
return tfErr.GetDiag()
221236
}
222237
instanceID := idSet[0]
223238
linkedDnsZoneID := idSet[1]
@@ -232,7 +247,8 @@ func resourceIBMDNSLinkedZoneDelete(ctx context.Context, d *schema.ResourceData,
232247
if response != nil && response.StatusCode == 404 {
233248
return nil
234249
}
235-
return diag.FromErr(fmt.Errorf("[ERROR] Error reading DNS Services secondary zone:%s\n%s", err, response))
250+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMDNSLinkedZoneDelete failed with error: %s and response:\n%s", err, response), "ibm_dns_linked_zone", "delete")
251+
return tfErr.GetDiag()
236252
}
237253

238254
d.SetId("")

0 commit comments

Comments
 (0)