Skip to content

fix(cis): update error toolchain changes for cis #6300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ibm/service/cis/data_source_ibm_cis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -125,7 +124,7 @@ func dataSourceIBMCISInstanceRead(d *schema.ResourceData, meta interface{}) erro

serviceOff, err := rsCatRepo.FindByName(service.(string), true)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving service offering: %s", err)
return flex.FmtErrorf("[ERROR] Error retrieving service offering: %s", err)
}

rsInstQuery.ServiceID = serviceOff[0].ID
Expand All @@ -152,13 +151,13 @@ func dataSourceIBMCISInstanceRead(d *schema.ResourceData, meta interface{}) erro
}

if len(filteredInstances) == 0 {
return fmt.Errorf("[ERROR] No resource instance found with name [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
return flex.FmtErrorf("[ERROR] No resource instance found with name [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
}

var instance models.ServiceInstanceV2

if len(filteredInstances) > 1 {
return fmt.Errorf("[ERROR] More than one resource instance found with name matching [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
return flex.FmtErrorf("[ERROR] More than one resource instance found with name matching [%s]\nIf not specified please specify more filters like resource_group_id if instance doesn't exists in default group, location or service", name)
}
instance = filteredInstances[0]

Expand All @@ -169,14 +168,14 @@ func dataSourceIBMCISInstanceRead(d *schema.ResourceData, meta interface{}) erro
d.Set("guid", instance.Guid)
serviceOff, err := rsCatRepo.GetServiceName(instance.ServiceID)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving service offering: %s", err)
return flex.FmtErrorf("[ERROR] Error retrieving service offering: %s", err)
}

d.Set("service", serviceOff)

servicePlan, err := rsCatRepo.GetServicePlanName(instance.ResourcePlanID)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving plan: %s", err)
return flex.FmtErrorf("[ERROR] Error retrieving plan: %s", err)
}
d.Set("plan", servicePlan)

Expand Down
6 changes: 3 additions & 3 deletions ibm/service/cis/data_source_ibm_cis_alert_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -80,15 +80,15 @@ func DataSourceIBMCISAlertWebhooksValidator() *validate.ResourceValidator {
func dataIBMCISWebhookRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).CisWebhookSession()
if err != nil {
return fmt.Errorf("[ERROR] Error while getting the cisWebhookession %s", err)
return flex.FmtErrorf("[ERROR] Error while getting the cisWebhookession %s", err)
}
crn := d.Get(cisID).(string)
sess.Crn = core.StringPtr(crn)
opt := sess.NewListWebhooksOptions()

result, resp, err := sess.ListWebhooks(opt)
if err != nil || result == nil {
return fmt.Errorf("[ERROR] Error Listing all Webhooks %q: %s %s", d.Id(), err, resp)
return flex.FmtErrorf("[ERROR] Error Listing all Webhooks %q: %s %s", d.Id(), err, resp)
}

webhooks := make([]map[string]interface{}, 0)
Expand Down
6 changes: 3 additions & 3 deletions ibm/service/cis/data_source_ibm_cis_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package cis

import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/networking-go-sdk/alertsv1"
Expand Down Expand Up @@ -146,12 +146,12 @@ func dataIBMCISAlertPolicyRead(d *schema.ResourceData, meta interface{}) error {
alertOutput[cisAlertType] = *alertObj.AlertType
filterOpt, err := json.Marshal(alertObj.Filters)
if err != nil {
return fmt.Errorf("[ERROR] Error marshalling the created filters: %s", err)
return flex.FmtErrorf("[ERROR] Error marshalling the created filters: %s", err)
}
alertOutput[cisAlertFilters] = string(filterOpt)
conditionsOpt, err := json.Marshal(alertObj.Conditions)
if err != nil {
return fmt.Errorf("[ERROR] Error marshalling the created Conditions: %s", err)
return flex.FmtErrorf("[ERROR] Error marshalling the created Conditions: %s", err)
}
alertOutput[cisAlertConditions] = string(conditionsOpt)
alertOutput[cisAlertMechanisms] = dataflattenCISMechanism(*alertObj.Mechanisms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -133,7 +132,7 @@ func dataSourceIBMCISCustomCertificatesRead(d *schema.ResourceData, meta interfa
opt := cisClient.NewListCustomCertificatesOptions()
result, resp, err := cisClient.ListCustomCertificates(opt)
if err != nil {
return fmt.Errorf("[ERROR] Failed to list custom certificates: %v", resp)
return flex.FmtErrorf("[ERROR] Failed to list custom certificates: %v", resp)
}
certsList := make([]map[string]interface{}, 0)
for _, r := range result.Result {
Expand Down
3 changes: 1 addition & 2 deletions ibm/service/cis/data_source_ibm_cis_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -131,7 +130,7 @@ func dataSourceIBMCISDomainRead(d *schema.ResourceData, meta interface{}) error
}

if !zoneFound {
return fmt.Errorf("[ERROR] Given zone does not exist. Please specify correct domain")
return flex.FmtErrorf("[ERROR] Given zone does not exist. Please specify correct domain")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -139,7 +138,7 @@ func dataSourceIBMCISEdgeFunctionsActionsRead(d *schema.ResourceData, meta inter
opt := cisClient.NewListEdgeFunctionsActionsOptions()
result, _, err := cisClient.ListEdgeFunctionsActions(opt)
if err != nil {
return fmt.Errorf("[ERROR] Error: %v", err)
return flex.FmtErrorf("[ERROR] Error: %v", err)
}
scriptInfo := make([]map[string]interface{}, 0)
for _, script := range result.Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -104,7 +103,7 @@ func dataSourceIBMCISEdgeFunctionsTriggerRead(d *schema.ResourceData, meta inter
opt := cisClient.NewListEdgeFunctionsTriggersOptions()
result, _, err := cisClient.ListEdgeFunctionsTriggers(opt)
if err != nil {
return fmt.Errorf("[ERROR] Error listing edge functions triggers: %v", err)
return flex.FmtErrorf("[ERROR] Error listing edge functions triggers: %v", err)
}
triggerInfo := make([]map[string]interface{}, 0)
for _, trigger := range result.Result {
Expand Down
7 changes: 3 additions & 4 deletions ibm/service/cis/data_source_ibm_cis_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -87,20 +86,20 @@ func dataIBMCISFiltersRead(d *schema.ResourceData, meta interface{}) error {

sess, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return fmt.Errorf("[ERROR] Error while Getting IAM Access Token using BluemixSession %s", err)
return flex.FmtErrorf("[ERROR] Error while Getting IAM Access Token using BluemixSession %s", err)
}
xAuthtoken := sess.Config.IAMAccessToken

cisClient, err := meta.(conns.ClientSession).CisFiltersSession()
if err != nil {
return fmt.Errorf("[ERROR] Error while getting the CisFiltersSession %s", err)
return flex.FmtErrorf("[ERROR] Error while getting the CisFiltersSession %s", err)
}
crn := d.Get(cisID).(string)
zoneID, _, _ := flex.ConvertTftoCisTwoVar(d.Get(cisDomainID).(string))

result, resp, err := cisClient.ListAllFilters(cisClient.NewListAllFiltersOptions(xAuthtoken, crn, zoneID))
if err != nil || result == nil {
return fmt.Errorf("[ERROR] Error Listing all filters %q: %s %s", d.Id(), err, resp)
return flex.FmtErrorf("[ERROR] Error Listing all filters %q: %s %s", d.Id(), err, resp)
}

filtersList := make([]map[string]interface{}, 0)
Expand Down
15 changes: 12 additions & 3 deletions ibm/service/cis/data_source_ibm_cis_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,29 @@ func DataSourceIBMCISFirewallRulesValidator() *validate.ResourceValidator {
func dataSourceIBMCISFirewallRulesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataSourceIBMCISFirewallRulesRead BluemixSession initialization failed: %s", err.Error()),
"ibm_cis_firewall_rule", "read")
return tfErr.GetDiag()
}
xAuthtoken := sess.Config.IAMAccessToken

cisClient, err := meta.(conns.ClientSession).CisFirewallRulesSession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataSourceIBMCISFirewallRulesRead CisFirewallRulesSession initialization failed: %s", err.Error()),
"ibm_cis_firewall_rule", "read")
return tfErr.GetDiag()
}
crn := d.Get(cisID).(string)
zoneID, _, _ := flex.ConvertTftoCisTwoVar(d.Get(cisDomainID).(string))

result, resp, err := cisClient.ListAllFirewallRules(cisClient.NewListAllFirewallRulesOptions(xAuthtoken, crn, zoneID))
if err != nil || result == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error listing the firewall rules %s:%s", err, resp))
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataSourceIBMCISFirewallRulesRead ListAllFirewallRules failed with error: %s and response:\n%s", err, resp),
"ibm_cis_firewall_rule", "read")
return tfErr.GetDiag()
}

fwrList := make([]map[string]interface{}, 0)
Expand Down
12 changes: 9 additions & 3 deletions ibm/service/cis/data_source_ibm_cis_mtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ func dataIBMCISMtlsRead(context context.Context, d *schema.ResourceData, meta in
sess, err := meta.(conns.ClientSession).CisMtlsSession()

if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error while getting the CisMtlsSession() %s %v", err, sess))
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataIBMCISMtlsRead CisMtlsSession initialization failed: %s", err.Error()),
"ibm_cis_mtls", "read")
return tfErr.GetDiag()
}

zoneID, crn, _ := flex.ConvertTftoCisTwoVar(d.Id())
Expand All @@ -128,8 +131,11 @@ func dataIBMCISMtlsRead(context context.Context, d *schema.ResourceData, meta in

result, resp, err := sess.ListAccessCertificates(opt)
if err != nil {
log.Printf("[WARN] List all certificates failed: %v\n", resp)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataIBMCISMtlsRead ListAccessCertificates failed: %s", err.Error()),
"ibm_cis_mtls", "read")
log.Printf("[WARN] List all certificates failed: %v\n%s\n", resp, tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
mtlsCertLists := make([]map[string]interface{}, 0)
for _, certObj := range result.Result {
Expand Down
19 changes: 14 additions & 5 deletions ibm/service/cis/data_source_ibm_cis_mtls_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ func dataIBMCISMtlsAppRead(context context.Context, d *schema.ResourceData, meta
sess, err := meta.(conns.ClientSession).CisMtlsSession()

if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error while getting the CisMtlsSession() %s %v", err, sess))
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataIBMCISMtlsAppRead CisMtlsSession initialization failed: %s", err.Error()),
"ibm_cis_mtls_app", "read")
return tfErr.GetDiag()
}

zoneID, crn, _ := flex.ConvertTftoCisTwoVar(d.Id())
Expand All @@ -179,8 +182,11 @@ func dataIBMCISMtlsAppRead(context context.Context, d *schema.ResourceData, meta
opt := sess.NewListAccessApplicationsOptions(zoneID)
result, resp, err := sess.ListAccessApplications(opt)
if err != nil {
log.Printf("[WARN] List all Applications failed: %v\n", resp)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataIBMCISMtlsAppRead ListAccessApplications failed: %s", err.Error()),
"ibm_cis_mtls_app", "read")
log.Printf("[WARN] List all Applications failed: %v\n%s\n", resp, tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

mtlsAppLists := make([]map[string]interface{}, 0)
Expand All @@ -201,8 +207,11 @@ func dataIBMCISMtlsAppRead(context context.Context, d *schema.ResourceData, meta
PolicyOpt := sess.NewListAccessPoliciesOptions(zoneID, *appObj.ID)
PolicyResult, PolicyResp, PolicyErr := sess.ListAccessPolicies(PolicyOpt)
if PolicyErr != nil {
log.Printf("[WARN] List all Policies failed: %v\n", PolicyResp)
return diag.FromErr(PolicyErr)
tfErr := flex.TerraformErrorf(PolicyErr,
fmt.Sprintf("dataIBMCISMtlsAppRead ListAccessPolicies failed: %s", PolicyErr.Error()),
"ibm_cis_mtls_app", "read")
log.Printf("[WARN] List all Policies failed: %v\n%s\n", PolicyResp, tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

for _, PolicyObj := range PolicyResult.Result {
Expand Down
20 changes: 16 additions & 4 deletions ibm/service/cis/data_source_ibm_cis_origin_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ func DataSourceIBMCISOriginAuthPullValidator() *validate.ResourceValidator {
func dataIBMCISOriginAuthRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).CisOrigAuthSession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err,
fmt.Sprintf("dataIBMCISOriginAuthRead CisOrigAuthSession initialization failed: %s", err.Error()),
"ibm_cis_origin_auth", "read")
return tfErr.GetDiag()
}

crn := d.Get(cisID).(string)
Expand All @@ -150,7 +153,10 @@ func dataIBMCISOriginAuthRead(context context.Context, d *schema.ResourceData, m
zoneSettingsResult, zoneSettingsResponse, zoneSettingsErr := sess.GetZoneOriginPullSettings(zoneSettingsOpt)

if zoneSettingsErr != nil || zoneSettingsResponse == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Getting Zone Level Origin Pull Settings: %s, Response: %s", zoneSettingsErr, zoneSettingsResponse))
tfErr := flex.TerraformErrorf(zoneSettingsErr,
fmt.Sprintf("dataIBMCISOriginAuthRead GetZoneOriginPullSettings failed: %s \nResponse: %s", zoneSettingsErr.Error(), zoneSettingsResponse),
"ibm_cis_origin_auth", "read")
return tfErr.GetDiag()
}

zoneSettings := zoneSettingsResult.Result.Enabled
Expand All @@ -161,7 +167,10 @@ func dataIBMCISOriginAuthRead(context context.Context, d *schema.ResourceData, m
zoneCertListResult, zoneCertListResponse, zoneCertListErr := sess.ListZoneOriginPullCertificates(zoneCertListOpt)

if zoneCertListErr != nil || zoneCertListResponse == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Getting Zone Level Origin Pull Settings: %s, Response: %s", zoneCertListErr, zoneCertListResponse))
tfErr := flex.TerraformErrorf(zoneCertListErr,
fmt.Sprintf("dataIBMCISOriginAuthRead ListZoneOriginPullCertificates failed: %s \nResponse: %s", zoneCertListErr.Error(), zoneCertListResponse),
"ibm_cis_origin_auth", "read")
return tfErr.GetDiag()
}

zoneCertLists := make([]map[string]interface{}, 0)
Expand Down Expand Up @@ -191,7 +200,10 @@ func dataIBMCISOriginAuthRead(context context.Context, d *schema.ResourceData, m
hostnameSettingsResult, hostnameSettingsResponse, hostnameSettingsErr := sess.GetHostnameOriginPullSettings(hostnameSettingsOpt)

if hostnameSettingsErr != nil || hostnameSettingsResponse == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Getting Zone Level Origin Pull Settings: %s", hostnameSettingsErr))
tfErr := flex.TerraformErrorf(hostnameSettingsErr,
fmt.Sprintf("dataIBMCISOriginAuthRead GetHostnameOriginPullSettings failed: %s \nResponse: %s", hostnameSettingsErr.Error(), hostnameSettingsResponse),
"ibm_cis_origin_auth", "read")
return tfErr.GetDiag()
}

hostnameSettings := hostnameSettingsResult.Result.Enabled
Expand Down
3 changes: 1 addition & 2 deletions ibm/service/cis/data_source_ibm_cis_range_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -158,7 +157,7 @@ func dataSourceIBMCISRangeAppsRead(d *schema.ResourceData, meta interface{}) err
opt := cisClient.NewListRangeAppsOptions()
result, resp, err := cisClient.ListRangeApps(opt)
if err != nil {
return fmt.Errorf("[ERROR] Failed to list range applications: %v", resp)
return flex.FmtErrorf("[ERROR] Failed to list range applications: %v", resp)
}
apps := make([]map[string]interface{}, 0)
for _, i := range result.Result {
Expand Down
3 changes: 1 addition & 2 deletions ibm/service/cis/data_source_ibm_cis_rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cis

import (
"fmt"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
Expand Down Expand Up @@ -217,7 +216,7 @@ func dataSourceIBMCISRateLimitRead(d *schema.ResourceData, meta interface{}) err
opt := cisClient.NewListAllZoneRateLimitsOptions()
rateLimitRecord, resp, err := cisClient.ListAllZoneRateLimits(opt)
if err != nil {
return fmt.Errorf("[ERROR] Failed to read RateLimit: %v", resp)
return flex.FmtErrorf("[ERROR] Failed to read RateLimit: %v", resp)
}
rules := make([]map[string]interface{}, 0)
for _, r := range rateLimitRecord.Result {
Expand Down
Loading