Skip to content

Commit

Permalink
add retry in kafka topic + clean dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatte committed Jun 2, 2023
1 parent af9b626 commit 8cfb57c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 52 deletions.
4 changes: 0 additions & 4 deletions ovh/resource_cloud_project_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func resourceCloudProjectDatabaseRead(ctx context.Context, d *schema.ResourceDat
res.AdvancedConfiguration = *advancedConfigMap
}

diags := make(diag.Diagnostics, 0)
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
Expand All @@ -322,9 +321,6 @@ func resourceCloudProjectDatabaseRead(ctx context.Context, d *schema.ResourceDat
}
}

if len(diags) > 0 {
return diags
}
return nil
}

Expand Down
4 changes: 0 additions & 4 deletions ovh/resource_cloud_project_database_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func resourceCloudProjectDatabaseIntegrationRead(ctx context.Context, d *schema.
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

diags := make(diag.Diagnostics, 0)
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
Expand All @@ -179,9 +178,6 @@ func resourceCloudProjectDatabaseIntegrationRead(ctx context.Context, d *schema.
}
}

if len(diags) > 0 {
return diags
}
return nil
}

Expand Down
96 changes: 60 additions & 36 deletions ovh/resource_cloud_project_database_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

Expand Down Expand Up @@ -123,22 +125,35 @@ func resourceCloudProjectDatabaseKafkaTopicCreate(ctx context.Context, d *schema
params := (&CloudProjectDatabaseKafkaTopicCreateOpts{}).FromResource(d)
res := &CloudProjectDatabaseKafkaTopicResponse{}

log.Printf("[DEBUG] Will create topic: %+v for cluster %s from project %s", params, clusterId, serviceName)
err := config.OVHClient.Post(endpoint, params, res)
if err != nil {
return diag.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
}

log.Printf("[DEBUG] Waiting for topic %s to be READY", res.Id)
err = waitForCloudProjectDatabaseKafkaTopicReady(ctx, config.OVHClient, serviceName, clusterId, res.Id, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("timeout while waiting topic %s to be READY: %s", res.Id, err.Error())
}
log.Printf("[DEBUG] topic %s is READY", res.Id)

d.SetId(res.Id)

return resourceCloudProjectDatabaseKafkaTopicRead(ctx, d, meta)
return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate),
func() *resource.RetryError {
log.Printf("[DEBUG] Will create topic: %+v for cluster %s from project %s", params, clusterId, serviceName)
err := config.OVHClient.Post(endpoint, params, res)
if err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(fmt.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err))
}

log.Printf("[DEBUG] Waiting for topic %s to be READY", res.Id)
err = waitForCloudProjectDatabaseKafkaTopicReady(ctx, config.OVHClient, serviceName, clusterId, res.Id, d.Timeout(schema.TimeoutCreate))
if err != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting topic %s to be READY: %s", res.Id, err.Error()))
}
log.Printf("[DEBUG] topic %s is READY", res.Id)

d.SetId(res.Id)
readDiags := resourceCloudProjectDatabaseKafkaTopicRead(ctx, d, meta)
err = diagnosticsToError(readDiags)
if err != nil {
return resource.NonRetryableError(err)
}
return nil
},
),
)
}

func resourceCloudProjectDatabaseKafkaTopicRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand All @@ -159,7 +174,6 @@ func resourceCloudProjectDatabaseKafkaTopicRead(ctx context.Context, d *schema.R
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

diags := make(diag.Diagnostics, 0)
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
Expand All @@ -168,9 +182,6 @@ func resourceCloudProjectDatabaseKafkaTopicRead(ctx context.Context, d *schema.R
}
}

if len(diags) > 0 {
return diags
}
return nil
}

Expand All @@ -186,20 +197,33 @@ func resourceCloudProjectDatabaseKafkaTopicDelete(ctx context.Context, d *schema
url.PathEscape(id),
)

log.Printf("[DEBUG] Will delete topic %s from cluster %s from project %s", id, clusterId, serviceName)
err := config.OVHClient.Delete(endpoint, nil)
if err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

log.Printf("[DEBUG] Waiting for topic %s to be DELETED", id)
err = waitForCloudProjectDatabaseKafkaTopicDeleted(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.Errorf("timeout while waiting topic %s to be DELETED: %s", id, err.Error())
}
log.Printf("[DEBUG] topic %s is DELETED", id)

d.SetId("")

return nil
return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete),
func() *resource.RetryError {
log.Printf("[DEBUG] Will delete topic %s from cluster %s from project %s", id, clusterId, serviceName)
err := config.OVHClient.Delete(endpoint, nil)
if err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(err)
}
err = helpers.CheckDeleted(d, err, endpoint)
if err != nil {
return resource.NonRetryableError(err)
}
return nil
}

log.Printf("[DEBUG] Waiting for topic %s to be DELETED", id)
err = waitForCloudProjectDatabaseKafkaTopicDeleted(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutDelete))
if err != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting topic %s to be DELETED: %s", id, err.Error()))
}
log.Printf("[DEBUG] topic %s is DELETED", id)

d.SetId("")

return nil
},
),
)
}
4 changes: 0 additions & 4 deletions ovh/resource_cloud_project_database_m3db_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schem
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

diags := make(diag.Diagnostics, 0)
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
Expand All @@ -193,9 +192,6 @@ func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schem
}
}

if len(diags) > 0 {
return diags
}
return nil
}

Expand Down
4 changes: 0 additions & 4 deletions ovh/resource_cloud_project_database_redis_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func resourceCloudProjectDatabaseRedisUserRead(ctx context.Context, d *schema.Re
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}

diags := make(diag.Diagnostics, 0)
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
Expand All @@ -141,9 +140,6 @@ func resourceCloudProjectDatabaseRedisUserRead(ctx context.Context, d *schema.Re
}
}

if len(diags) > 0 {
return diags
}
return nil
}

Expand Down

0 comments on commit 8cfb57c

Please sign in to comment.