Skip to content

Commit

Permalink
clean deprecated function of cloud_project_database resources (#582)
Browse files Browse the repository at this point in the history
* clean deprecated function of cloud_project_database resources
  • Loading branch information
lpatte committed Mar 18, 2024
1 parent fe66c01 commit fe8419b
Show file tree
Hide file tree
Showing 21 changed files with 612 additions and 122 deletions.
2 changes: 1 addition & 1 deletion ovh/data_cloud_project_database_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func dataSourceCloudProjectDatabaseIntegration() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "mongodb" {
errors = append(errors, fmt.Errorf("Value %s is not a valid engine for integration", value))
errors = append(errors, fmt.Errorf("value %s is not a valid engine for integration", value))
}
return
},
Expand Down
2 changes: 1 addition & 1 deletion ovh/data_cloud_project_database_integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func dataSourceCloudProjectDatabaseIntegrations() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "mongodb" {
errors = append(errors, fmt.Errorf("Value %s is not a valid engine for integration", value))
errors = append(errors, fmt.Errorf("value %s is not a valid engine for integration", value))
}
return
},
Expand Down
14 changes: 4 additions & 10 deletions ovh/helpers/hashcode/hashcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import (

// String hashes a string to a unique hashcode.
//
// Deprecated: This will be removed in v2 without replacement. If you need
// its functionality, you can copy it, import crc32 directly, or reference the
// v1 package.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
// Copy from https://github.com/hashicorp/terraform-plugin-sdk/blob/v1.17.2/helper/hashcode/hashcode.go
// following depracation comment
func String(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
Expand All @@ -29,9 +24,8 @@ func String(s string) int {

// Strings hashes a list of strings to a unique hashcode.
//
// Deprecated: This will be removed in v2 without replacement. If you need
// its functionality, you can copy it, import crc32 directly, or reference the
// v1 package.
// Copy from https://github.com/hashicorp/terraform-plugin-sdk/blob/v1.17.2/helper/hashcode/hashcode.go
// following depracation comment
func Strings(strings []string) string {
var buf bytes.Buffer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ func TestAccCloudProjectDatabaseKafkaSchemaRegistryAcl_importBasic(t *testing.T)
ResourceName: "ovh_cloud_project_database_kafka_schemaregistryacl.schemaRegistryAcl",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccCloudProjectDatabaseKafkaAclImportId("ovh_cloud_project_database_kafka_schemaregistryacl.schemaRegistryAcl"),
ImportStateIdFunc: testAccCloudProjectDatabaseKafkaSchemaRegistryAclImportId("ovh_cloud_project_database_kafka_schemaregistryacl.schemaRegistryAcl"),
},
},
})
}

func testAccCloudProjectDatabaseKafkaSchemaRegistryAclImportId(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
testKafkaAcl, ok := s.RootModule().Resources[resourceName]
testKafkaSchemaRegistryAcl, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("ovh_cloud_project_database_kafka_schemaregistryacl not found: %s", resourceName)
}
return fmt.Sprintf(
"%s/%s/%s",
testKafkaAcl.Primary.Attributes["service_name"],
testKafkaAcl.Primary.Attributes["cluster_id"],
testKafkaAcl.Primary.Attributes["id"],
testKafkaSchemaRegistryAcl.Primary.Attributes["service_name"],
testKafkaSchemaRegistryAcl.Primary.Attributes["cluster_id"],
testKafkaSchemaRegistryAcl.Primary.Attributes["id"],
), nil
}
}
12 changes: 6 additions & 6 deletions ovh/resource_cloud_project_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func resourceCloudProjectDatabaseImportState(d *schema.ResourceData, meta interf
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/databaseId formatted")
return nil, fmt.Errorf("import Id is not service_name/engine/databaseId formatted")
}
serviceName := splitId[0]
engine := splitId[1]
Expand All @@ -248,7 +248,7 @@ func resourceCloudProjectDatabaseCreate(ctx context.Context, d *schema.ResourceD
url.PathEscape(serviceName),
url.PathEscape(engine),
)
err, params := (&CloudProjectDatabaseCreateOpts{}).FromResource(d)
params, err := (&CloudProjectDatabaseCreateOpts{}).FromResource(d)
if err != nil {
return diag.Errorf("service creation failed : %q", err)
}
Expand All @@ -261,7 +261,7 @@ func resourceCloudProjectDatabaseCreate(ctx context.Context, d *schema.ResourceD
}

log.Printf("[DEBUG] Waiting for database %s to be READY", res.Id)
err = waitForCloudProjectDatabaseReady(config.OVHClient, serviceName, engine, res.Id, d.Timeout(schema.TimeoutCreate))
err = waitForCloudProjectDatabaseReady(ctx, config.OVHClient, serviceName, engine, res.Id, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("timeout while waiting database %s to be READY: %s", res.Id, err.Error())
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func resourceCloudProjectDatabaseUpdate(ctx context.Context, d *schema.ResourceD
url.PathEscape(engine),
url.PathEscape(d.Id()),
)
err, params := (&CloudProjectDatabaseUpdateOpts{}).FromResource(d)
params, err := (&CloudProjectDatabaseUpdateOpts{}).FromResource(d)
if err != nil {
return diag.Errorf("service update failed : %q", err)
}
Expand All @@ -353,7 +353,7 @@ func resourceCloudProjectDatabaseUpdate(ctx context.Context, d *schema.ResourceD
}

log.Printf("[DEBUG] Waiting for database %s to be READY", d.Id())
err = waitForCloudProjectDatabaseReady(config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutUpdate))
err = waitForCloudProjectDatabaseReady(ctx, config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.Errorf("timeout while waiting database %s to be READY: %s", d.Id(), err.Error())
}
Expand Down Expand Up @@ -392,7 +392,7 @@ func resourceCloudProjectDatabaseDelete(ctx context.Context, d *schema.ResourceD
}

log.Printf("[DEBUG] Waiting for database %s to be DELETED", d.Id())
err = waitForCloudProjectDatabaseDeleted(config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutDelete))
err = waitForCloudProjectDatabaseDeleted(ctx, config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.Errorf("timeout while waiting database %s to be DELETED: %v", d.Id(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion ovh/resource_cloud_project_database_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func resourceCloudProjectDatabaseDatabaseImportState(d *schema.ResourceData, met
n := 4
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/engine/cluster_id/id formatted")
}
serviceName := splitId[0]
engine := splitId[1]
Expand Down
4 changes: 2 additions & 2 deletions ovh/resource_cloud_project_database_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func resourceCloudProjectDatabaseIntegration() *schema.Resource {
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "mongodb" {
errors = append(errors, fmt.Errorf("Value %s is not a valid engine for integration", value))
errors = append(errors, fmt.Errorf("value %s is not a valid engine for integration", value))
}
return
},
Expand Down Expand Up @@ -101,7 +101,7 @@ func resourceCloudProjectDatabaseIntegrationImportState(d *schema.ResourceData,
n := 4
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/engine/cluster_id/id formatted")
}
serviceName := splitId[0]
engine := splitId[1]
Expand Down
40 changes: 20 additions & 20 deletions ovh/resource_cloud_project_database_ip_restriction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
Expand Down Expand Up @@ -79,7 +79,7 @@ func resourceCloudProjectDatabaseIpRestrictionImportState(d *schema.ResourceData
n := 4
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/cluster_id/ip formatted")
return nil, fmt.Errorf("import Id is not service_name/engine/cluster_id/ip formatted")
}
serviceName := splitId[0]
engine := splitId[1]
Expand Down Expand Up @@ -111,24 +111,24 @@ func resourceCloudProjectDatabaseIpRestrictionCreate(ctx context.Context, d *sch
res := &CloudProjectDatabaseIpRestrictionResponse{}

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

log.Printf("[DEBUG] Waiting for IP restriction %s to be READY", res.Ip)
rErr = waitForCloudProjectDatabaseIpRestrictionReady(ctx, config.OVHClient, serviceName, engine, clusterId, res.Ip, d.Timeout(schema.TimeoutCreate))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", res.Ip, rErr))
return retry.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", res.Ip, rErr))
}
log.Printf("[DEBUG] IP restriction %s is READY", res.Ip)

Expand All @@ -137,7 +137,7 @@ func resourceCloudProjectDatabaseIpRestrictionCreate(ctx context.Context, d *sch
readDiags := resourceCloudProjectDatabaseIpRestrictionRead(ctx, d, meta)
rErr = diagnosticsToError(readDiags)
if rErr != nil {
return resource.NonRetryableError(rErr)
return retry.NonRetryableError(rErr)
}
return nil
},
Expand Down Expand Up @@ -190,28 +190,28 @@ func resourceCloudProjectDatabaseIpRestrictionUpdate(ctx context.Context, d *sch
params := (&CloudProjectDatabaseIpRestrictionUpdateOpts{}).FromResource(d)

return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate),
func() *resource.RetryError {
retry.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate),
func() *retry.RetryError {
log.Printf("[DEBUG] Will update IP restriction: %+v from cluster %s from project %s", params, clusterId, serviceName)
rErr := config.OVHClient.Put(endpoint, params, nil)
if rErr != nil {
if errOvh, ok := rErr.(*ovh.APIError); ok && (errOvh.Code == 409) {
return resource.RetryableError(rErr)
return retry.RetryableError(rErr)
}
return resource.NonRetryableError(fmt.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, rErr))
return retry.NonRetryableError(fmt.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, rErr))
}

log.Printf("[DEBUG] Waiting for IP restriction %s to be READY", ip)
rErr = waitForCloudProjectDatabaseIpRestrictionReady(ctx, config.OVHClient, serviceName, engine, clusterId, ip, d.Timeout(schema.TimeoutUpdate))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", ip, rErr))
return retry.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be READY: %w", ip, rErr))
}
log.Printf("[DEBUG] IP restriction %s is READY", ip)

readDiags := resourceCloudProjectDatabaseIpRestrictionRead(ctx, d, meta)
rErr = diagnosticsToError(readDiags)
if rErr != nil {
return resource.NonRetryableError(rErr)
return retry.NonRetryableError(rErr)
}
return nil
},
Expand All @@ -234,25 +234,25 @@ func resourceCloudProjectDatabaseIpRestrictionDelete(ctx context.Context, d *sch
)

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

log.Printf("[DEBUG] Waiting for IP restriction %s to be DELETED", clusterId)
rErr = waitForCloudProjectDatabaseIpRestrictionDeleted(ctx, config.OVHClient, serviceName, engine, clusterId, ip, d.Timeout(schema.TimeoutDelete))
if rErr != nil {
return resource.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be DELETED: %w", clusterId, rErr))
return retry.NonRetryableError(fmt.Errorf("timeout while waiting IP restriction %s to be DELETED: %w", clusterId, rErr))
}
log.Printf("[DEBUG] IP restriction %s is DELETED", clusterId)

Expand Down
2 changes: 1 addition & 1 deletion ovh/resource_cloud_project_database_kafka_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func resourceCloudProjectDatabaseKafkaAclImportState(d *schema.ResourceData, met
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/cluster_id/id formatted")
}
serviceName := splitId[0]
clusterId := splitId[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func resourceCloudProjectDatabaseKafkaSchemaRegistryAclImportState(d *schema.Res
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/cluster_id/id formatted")
}
serviceName := splitId[0]
clusterId := splitId[1]
Expand Down
26 changes: 13 additions & 13 deletions ovh/resource_cloud_project_database_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
Expand Down Expand Up @@ -99,7 +99,7 @@ func resourceCloudProjectDatabaseKafkaTopicImportState(d *schema.ResourceData, m
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/cluster_id/id formatted")
}
serviceName := splitId[0]
clusterId := splitId[1]
Expand All @@ -126,29 +126,29 @@ func resourceCloudProjectDatabaseKafkaTopicCreate(ctx context.Context, d *schema
res := &CloudProjectDatabaseKafkaTopicResponse{}

return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate),
func() *resource.RetryError {
retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate),
func() *retry.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 retry.RetryableError(err)
}
return resource.NonRetryableError(fmt.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err))
return retry.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()))
return retry.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 retry.NonRetryableError(err)
}
return nil
},
Expand Down Expand Up @@ -198,25 +198,25 @@ func resourceCloudProjectDatabaseKafkaTopicDelete(ctx context.Context, d *schema
)

return diag.FromErr(
resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete),
func() *resource.RetryError {
retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete),
func() *retry.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)
return retry.RetryableError(err)
}
err = helpers.CheckDeleted(d, err, endpoint)
if err != nil {
return resource.NonRetryableError(err)
return retry.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()))
return retry.NonRetryableError(fmt.Errorf("timeout while waiting topic %s to be DELETED: %s", id, err.Error()))
}
log.Printf("[DEBUG] topic %s is DELETED", id)

Expand Down
2 changes: 1 addition & 1 deletion ovh/resource_cloud_project_database_m3db_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func resourceCloudProjectDatabaseM3dbNamespaceImportState(d *schema.ResourceData
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/cluster_id/id formatted")
return nil, fmt.Errorf("import Id is not service_name/cluster_id/id formatted")
}
serviceName := splitId[0]
clusterId := splitId[1]
Expand Down
Loading

0 comments on commit fe8419b

Please sign in to comment.