Skip to content

Commit

Permalink
update error diagnostic reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
kccox authored and hkantare committed Sep 26, 2024
1 parent 77b3bb0 commit 605c84e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 51 deletions.
16 changes: 10 additions & 6 deletions ibm/service/eventstreams/data_source_ibm_event_streams_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/eventstreams-go-sdk/pkg/schemaregistryv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -41,14 +42,16 @@ func DataSourceIBMEventStreamsSchema() *schema.Resource {
func dataSourceIBMEventStreamsSchemaRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession()
if err != nil {
log.Printf("[DEBUG] dataSourceIBMEventStreamsSchemaRead schemaregistryClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMEventStreamsSchemaRead schemaregistryClient: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

adminURL, instanceCRN, err := getInstanceURL(d, meta)
if err != nil {
log.Printf("[DEBUG] dataSourceIBMEventStreamsSchemaRead getInstanceURL err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMEventStreamsSchemaRead getInstanceURL: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
schemaregistryClient.SetServiceURL(adminURL)

Expand All @@ -59,8 +62,9 @@ func dataSourceIBMEventStreamsSchemaRead(context context.Context, d *schema.Reso

schema, response, err := schemaregistryClient.GetLatestSchemaWithContext(context, getLatestSchemaOptions)
if err != nil || schema == nil {
log.Printf("[DEBUG] GetLatestSchemaWithContext failed with error: %s and response:\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetLatestSchemaWithContext failed with error: %s\n and response:%s", err, response))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMEventStreamsSchemaRead GetLatestSchemaWithContext failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
uniqueID := getUniqueSchemaID(instanceCRN, schemaID)

Expand Down
17 changes: 11 additions & 6 deletions ibm/service/eventstreams/data_source_ibm_event_streams_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -54,13 +55,15 @@ func DataSourceIBMEventStreamsTopic() *schema.Resource {
func dataSourceIBMEventStreamsTopicRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
adminClient, instanceCRN, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead createSaramaAdminClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMEventStreamsTopicRead createSaramaAdminClient: %s", err), "ibm_event_streams_topic", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topics, err := adminClient.ListTopics()
if err != nil {
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead ListTopics err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("dataSourceIBMEventStreamsTopicRead ListTopics: %s", err), "ibm_event_streams_topic", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topicName := d.Get("name").(string)
for name := range topics {
Expand All @@ -72,6 +75,8 @@ func dataSourceIBMEventStreamsTopicRead(context context.Context, d *schema.Resou
return nil
}
}
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead topic %s does not exist", topicName)
return diag.FromErr(fmt.Errorf("topic %s does not exist", topicName))
tfErr := flex.TerraformErrorf(fmt.Errorf("topic %s does not exist", topicName),
fmt.Sprintf("dataSourceIBMEventStreamsTopicRead topic %s does not exist", topicName), "ibm_event_streams_topic", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
65 changes: 46 additions & 19 deletions ibm/service/eventstreams/resource_ibm_event_streams_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,15 @@ func validateName(s map[string]interface{}, t string) error {
func resourceIBMEventStreamsSchemaCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaCreate schemaregistryClient: %s", err), "ibm_event_streams_schema", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
adminURL, instanceCRN, err := getInstanceURL(d, meta)
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaCreate getInstanceURL: %s", err), "ibm_event_streams_schema", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
schemaregistryClient.SetServiceURL(adminURL)
createSchemaOptions := &schemaregistryv1.CreateSchemaOptions{}
Expand All @@ -195,8 +199,9 @@ func resourceIBMEventStreamsSchemaCreate(context context.Context, d *schema.Reso

schemaMetadata, response, err := schemaregistryClient.CreateSchemaWithContext(context, createSchemaOptions)
if err != nil || schemaMetadata == nil {
log.Printf("[DEBUG] CreateSchemaWithContext failed with error: %s and response: \n%s", err, response)
return diag.FromErr(fmt.Errorf("CreateSchemaWithContext failed with eror: %s and response: \n%s", err, response))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaCreate CreateSchemaWithContext failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
uniqueID := getUniqueSchemaID(instanceCRN, *schemaMetadata.ID)
d.SetId(uniqueID)
Expand All @@ -207,12 +212,16 @@ func resourceIBMEventStreamsSchemaCreate(context context.Context, d *schema.Reso
func resourceIBMEventStreamsSchemaRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaRead schemaregistryClient: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

adminURL, instanceCRN, err := getInstanceURL(d, meta)
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaRead getInstanceURL: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
schemaregistryClient.SetServiceURL(adminURL)

Expand All @@ -223,21 +232,27 @@ func resourceIBMEventStreamsSchemaRead(context context.Context, d *schema.Resour

avroSchema, response, err := schemaregistryClient.GetLatestSchemaWithContext(context, getSchemaOptions)
if err != nil || avroSchema == nil {
log.Printf("[DEBUG] GetSchemaWithContext failed with error: %s and response: \n%s", err, response)
if response != nil && response.StatusCode == 404 {
log.Printf("[DEBUG] GetSchemaWithContext failed with 404 Not Found error and response: \n%s", response)
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("GetSchemaWithContext failed %s\n%s", err, response))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaRead GetLatestSchemaWithContext failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

s, err := json.Marshal(avroSchema)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error marshalling the created schema: %s", err))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaRead marshalling the schema failed with error: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

if err = d.Set("schema", string(s)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting the schema: %s", err))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaRead setting the schema failed error: %s", err), "ibm_event_streams_schema", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.Set("resource_instance_id", instanceCRN)
d.Set("schema_id", schemaID)
Expand All @@ -248,12 +263,16 @@ func resourceIBMEventStreamsSchemaRead(context context.Context, d *schema.Resour
func resourceIBMEventStreamsSchemaUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaUpdate schemaregistryClient: %s", err), "ibm_event_streams_schema", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

adminURL, _, err := getInstanceURL(d, meta)
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaUpdate getInstanceURL: %s", err), "ibm_event_streams_schema", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
schemaregistryClient.SetServiceURL(adminURL)

Expand All @@ -269,8 +288,9 @@ func resourceIBMEventStreamsSchemaUpdate(context context.Context, d *schema.Reso
}
schemaMetadata, response, err := schemaregistryClient.UpdateSchemaWithContext(context, updateSchemaOptions)
if err != nil || schemaMetadata == nil {
log.Printf("[DEBUG] UpdateSchemaWithContext failed with error: %s\n and response: %s", err, response)
return diag.FromErr(fmt.Errorf("UpdateSchemaWithContext failed with error: %s and response: \n%s", err, response))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaUpdate UpdateSchema failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
}

Expand All @@ -280,12 +300,16 @@ func resourceIBMEventStreamsSchemaUpdate(context context.Context, d *schema.Reso
func resourceIBMEventStreamsSchemaDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaregistryClient, err := meta.(conns.ClientSession).ESschemaRegistrySession()
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaDelete schemaregistryClient: %s", err), "ibm_event_streams_schema", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

adminURL, _, err := getInstanceURL(d, meta)
if err != nil {
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaDelete getInstanceURL: %s", err), "ibm_event_streams_schema", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
schemaregistryClient.SetServiceURL(adminURL)

Expand All @@ -300,13 +324,16 @@ func resourceIBMEventStreamsSchemaDelete(context context.Context, d *schema.Reso
// set schema state to disabled before deleting
response, err := schemaregistryClient.SetSchemaStateWithContext(context, setSchemaOptions)
if err != nil {
log.Printf("[DEBUG] SetSchemaStateWithContext failed %s\n%s", err, response)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaDelete SetSchemaState failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

response, err = schemaregistryClient.DeleteSchemaWithContext(context, deleteSchemaOptions)
if err != nil {
log.Printf("[DEBUG] DeleteSchemaWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("DeleteSchemaWithContext failed %s\n%s", err, response))
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsSchemaDelete DeleteSchema failed with error: %s and response:\n%s", err, response), "ibm_event_streams_schema", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

d.SetId("")
Expand Down
50 changes: 30 additions & 20 deletions ibm/service/eventstreams/resource_ibm_event_streams_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ func resourceIBMEventStreamsTopicCreate(context context.Context, d *schema.Resou
log.Printf("[DEBUG] resourceIBMEventStreamsTopicCreate")
adminClient, instanceCRN, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicCreate createSaramaAdminClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicCreate createSaramaAdminClient: %s", err), "ibm_event_streams_topic", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topicName := d.Get("name").(string)
partitions := d.Get("partitions").(int)
Expand All @@ -144,17 +145,19 @@ func resourceIBMEventStreamsTopicCreate(context context.Context, d *schema.Resou
if kafkaErr != nil && kafkaErr.Err == sarama.ErrTopicAlreadyExists {
exists, err := resourceIBMEventStreamsTopicExists(context, d, meta)
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicCreate resourceIBMEventStreamsTopicExists: %s, err %s", topicName, err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicCreate resourceIBMEventStreamsTopicExists %s: %s", topicName, err), "ibm_event_streams_topic", "create")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
if exists {
d.SetId(getTopicID(instanceCRN, topicName))
return resourceIBMEventStreamsTopicRead(context, d, meta)
}
}
}
log.Printf("[ERROR] resourceIBMEventStreamsTopicCreate CreateTopic: %s, err %s", topicName, err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicCreate CreateTopic %s: %s", topicName, err), "ibm_event_streams_topic", "create")
log.Printf("[ERROR]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
log.Printf("[INFO] resourceIBMEventStreamsTopicCreate CreateTopic: topic is %s, detail is %v", topicName, topicDetail)
d.SetId(getTopicID(instanceCRN, topicName))
Expand All @@ -165,15 +168,17 @@ func resourceIBMEventStreamsTopicRead(context context.Context, d *schema.Resourc
log.Printf("[DEBUG] resourceIBMEventStreamsTopicRead")
adminClient, instanceCRN, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicRead createSaramaAdminClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicRead createSaramaAdminClient: %s", err), "ibm_event_streams_topic", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topicID := d.Id()
topicName := getTopicName(topicID)
topics, err := adminClient.ListTopics()
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicRead ListTopics err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicRead ListTopics: %s", err), "ibm_event_streams_topic", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
for name, detail := range topics {
if name == topicName {
Expand Down Expand Up @@ -201,8 +206,9 @@ func resourceIBMEventStreamsTopicUpdate(context context.Context, d *schema.Resou
log.Printf("[DEBUG] resourceIBMEventStreamsTopicUpdate")
adminClient, _, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicUpdate createSaramaAdminClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicUpdate createSaramaAdminClient: %s", err), "ibm_event_streams_topic", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topicName := d.Get("name").(string)
if d.HasChange("partitions") {
Expand All @@ -212,8 +218,9 @@ func resourceIBMEventStreamsTopicUpdate(context context.Context, d *schema.Resou
log.Printf("[INFO]resourceIBMEventStreamsTopicUpdate Updating partitions from %d to %d", oldPartitions, newPartitions)
err = adminClient.CreatePartitions(topicName, int32(newPartitions), nil, false)
if err != nil {
log.Printf("[DEBUG]resourceIBMEventStreamsTopicUpdate CreatePartitions err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicUpdate CreatePartitions: %s", err), "ibm_event_streams_topic", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.Set("partitions", int32(newPartitions))
log.Printf("[INFO]resourceIBMEventStreamsTopicUpdate partitions is set to %d", newPartitions)
Expand All @@ -223,8 +230,9 @@ func resourceIBMEventStreamsTopicUpdate(context context.Context, d *schema.Resou
configEntries := config2TopicDetail(config)
err = adminClient.AlterConfig(sarama.TopicResource, topicName, configEntries, false)
if err != nil {
log.Printf("[DEBUG]resourceIBMEventStreamsTopicUpdate AlterConfig err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicUpdate AlterConfig: %s", err), "ibm_event_streams_topic", "update")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.Set("config", topicDetail2Config(configEntries))
log.Printf("[INFO]resourceIBMEventStreamsTopicUpdate config is set to %v", topicDetail2Config(configEntries))
Expand All @@ -236,8 +244,9 @@ func resourceIBMEventStreamsTopicDelete(context context.Context, d *schema.Resou
log.Printf("[DEBUG] resourceIBMEventStreamsTopicDelete")
adminClient, _, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG] resourceIBMEventStreamsTopicDelete createSaramaAdminClient err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicDelete createSaramaAdminClient: %s", err), "ibm_event_streams_topic", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
topicName := d.Get("name").(string)
err = adminClient.DeleteTopic(topicName)
Expand All @@ -249,8 +258,9 @@ func resourceIBMEventStreamsTopicDelete(context context.Context, d *schema.Resou
return nil
}
}
log.Printf("[DEBUG] resourceIBMEventStreamsTopicDelete DeleteTopic err %s", err)
return diag.FromErr(err)
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIBMEventStreamsTopicDelete DeleteClient: %s", err), "ibm_event_streams_topic", "delete")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.SetId("")
log.Printf("[INFO]resourceIBMEventStreamsTopicDelete topic %s deleted", topicName)
Expand Down

0 comments on commit 605c84e

Please sign in to comment.