diff --git a/.golangci.yml b/.golangci.yml index c2e4758a9..565def48f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - unconvert - unused - stylecheck + - errorlint issues: exclude: diff --git a/internal/schemautil/service.go b/internal/schemautil/service.go index cd2f2d011..8e5cf607e 100644 --- a/internal/schemautil/service.go +++ b/internal/schemautil/service.go @@ -2,6 +2,7 @@ package schemautil import ( "context" + "errors" "fmt" "strconv" "strings" @@ -586,7 +587,7 @@ func getDefaultDiskSpaceIfNotSet(ctx context.Context, d *schema.ResourceData, cl if aiven.IsNotFound(err) { return 0, nil } - return 0, fmt.Errorf("unable to get service plan parameters: %s", err) + return 0, fmt.Errorf("unable to get service plan parameters: %w", err) } if ads, ok := d.GetOk("additional_disk_space"); ok { @@ -743,7 +744,7 @@ func copyServicePropertiesFromAPIResponseToTerraform( } if err := d.Set(serviceType+"_user_config", newUserConfig); err != nil { - return fmt.Errorf("cannot set `%s_user_config` : %s; Please make sure that all Aiven services have unique s names", serviceType, err) + return fmt.Errorf("cannot set `%s_user_config` : %w; Please make sure that all Aiven services have unique s names", serviceType, err) } params := s.URIParams @@ -784,7 +785,7 @@ func copyServicePropertiesFromAPIResponseToTerraform( } if err := d.Set("components", FlattenServiceComponents(s)); err != nil { - return fmt.Errorf("cannot set `components` : %s", err) + return fmt.Errorf("cannot set `components` : %w", err) } return copyConnectionInfoFromAPIResponseToTerraform(d, serviceType, s.ConnectionInfo, s.Metadata) @@ -872,11 +873,8 @@ func copyConnectionInfoFromAPIResponseToTerraform( // IsUnknownRole checks if the database returned an error because of an unknown role // to make deletions idempotent func IsUnknownRole(err error) bool { - aivenError, ok := err.(aiven.Error) - if !ok { - return false - } - return strings.Contains(aivenError.Message, "Code: 511") + var e *aiven.Error + return errors.As(err, &e) && strings.Contains(e.Message, "Code: 511") } // IsUnknownResource is a function to handle errors that we want to treat as "Not Found" diff --git a/internal/sdkprovider/kafkatopicrepository/create.go b/internal/sdkprovider/kafkatopicrepository/create.go index 0b9210f28..e778557da 100644 --- a/internal/sdkprovider/kafkatopicrepository/create.go +++ b/internal/sdkprovider/kafkatopicrepository/create.go @@ -2,6 +2,7 @@ package kafkatopicrepository import ( "context" + "errors" "fmt" "strings" @@ -26,7 +27,7 @@ func (rep *repository) Create(ctx context.Context, project, service string, req } // If this is not errNotFound, then something happened - if err != errNotFound { + if !errors.Is(err, errNotFound) { return err } @@ -59,7 +60,7 @@ func (rep *repository) Create(ctx context.Context, project, service string, req // Retry lib returns a custom error object // we can't compare in tests with if err != nil { - return fmt.Errorf("topic create error: %s", err) + return fmt.Errorf("topic create error: %w", err) } return err } diff --git a/internal/sdkprovider/kafkatopicrepository/create_test.go b/internal/sdkprovider/kafkatopicrepository/create_test.go index 5d3e6cd3c..d095f42b5 100644 --- a/internal/sdkprovider/kafkatopicrepository/create_test.go +++ b/internal/sdkprovider/kafkatopicrepository/create_test.go @@ -2,6 +2,7 @@ package kafkatopicrepository import ( "context" + "errors" "fmt" "sync" "sync/atomic" @@ -24,7 +25,7 @@ func TestCreateConflict(t *testing.T) { wg.Add(1) go func() { err := rep.Create(ctx, "a", "b", aiven.CreateKafkaTopicRequest{TopicName: "c"}) - if err == errAlreadyExists { + if errors.Is(err, errAlreadyExists) { atomic.AddInt32(&conflictErr, 1) } wg.Done() @@ -113,7 +114,12 @@ func TestCreateRetries(t *testing.T) { TopicName: "my-topic", } err := rep.Create(ctx, "foo", "bar", req) - assert.Equal(t, opt.expectErr, err) + // Check the error message using EqualError because the error is wrapped + if opt.expectErr == nil { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, opt.expectErr.Error()) + } assert.Equal(t, opt.expectCalled, client.createCalled) }) } diff --git a/internal/sdkprovider/kafkatopicrepository/read.go b/internal/sdkprovider/kafkatopicrepository/read.go index ecce54b63..1deb3b9de 100644 --- a/internal/sdkprovider/kafkatopicrepository/read.go +++ b/internal/sdkprovider/kafkatopicrepository/read.go @@ -147,7 +147,7 @@ func (rep *repository) fetch(ctx context.Context, queue map[string]*request) { if err != nil { // Send errors // Flattens error to a string, because it might go really completed for testing - err = fmt.Errorf("topic read error: %s", err) + err = fmt.Errorf("topic read error: %w", err) for _, r := range reqs { r.send(nil, err) } diff --git a/internal/sdkprovider/kafkatopicrepository/repository_test.go b/internal/sdkprovider/kafkatopicrepository/repository_test.go index bbf409396..7de2630d7 100644 --- a/internal/sdkprovider/kafkatopicrepository/repository_test.go +++ b/internal/sdkprovider/kafkatopicrepository/repository_test.go @@ -235,7 +235,12 @@ func TestRepositoryRead(t *testing.T) { defer wg.Done() topic, err := rep.Read(ctx, opt.requests[i].project, opt.requests[i].service, opt.requests[i].topic) assert.Equal(t, opt.responses[i].topic, topic) - assert.Equal(t, opt.responses[i].err, err) + // Check the error message using EqualError because the error is wrapped + if opt.responses[i].err == nil { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, opt.responses[i].err.Error()) + } }(i) } diff --git a/internal/sdkprovider/service/account/account_team_member_test.go b/internal/sdkprovider/service/account/account_team_member_test.go index d9c820da7..d462e6e0c 100644 --- a/internal/sdkprovider/service/account/account_team_member_test.go +++ b/internal/sdkprovider/service/account/account_team_member_test.go @@ -2,6 +2,7 @@ package account_test import ( "context" + "errors" "fmt" "log" "os" @@ -87,7 +88,8 @@ func testAccCheckAivenAccountTeamMemberResourceDestroy(s *terraform.State) error r, err := c.Accounts.List(ctx) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } @@ -98,7 +100,8 @@ func testAccCheckAivenAccountTeamMemberResourceDestroy(s *terraform.State) error if a.Id == accountID { ri, err := c.AccountTeamInvites.List(ctx, accountID, teamID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/account/account_team_project_test.go b/internal/sdkprovider/service/account/account_team_project_test.go index f0ef22f27..023fcf8fe 100644 --- a/internal/sdkprovider/service/account/account_team_project_test.go +++ b/internal/sdkprovider/service/account/account_team_project_test.go @@ -2,6 +2,7 @@ package account_test import ( "context" + "errors" "fmt" "log" "os" @@ -93,7 +94,8 @@ func testAccCheckAivenAccountTeamProjectResourceDestroy(s *terraform.State) erro r, err := c.Accounts.List(ctx) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } @@ -104,7 +106,8 @@ func testAccCheckAivenAccountTeamProjectResourceDestroy(s *terraform.State) erro if a.Id == accountID { rp, err := c.AccountTeamProjects.List(ctx, accountID, teamID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/account/account_team_test.go b/internal/sdkprovider/service/account/account_team_test.go index 0e7ae8f54..65730b105 100644 --- a/internal/sdkprovider/service/account/account_team_test.go +++ b/internal/sdkprovider/service/account/account_team_test.go @@ -2,6 +2,7 @@ package account_test import ( "context" + "errors" "fmt" "log" "os" @@ -79,7 +80,8 @@ func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error { r, err := c.Accounts.List(ctx) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } @@ -90,7 +92,8 @@ func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error { if ac.Id == accountID { rl, err := c.AccountTeams.List(ctx, accountID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/account/account_test.go b/internal/sdkprovider/service/account/account_test.go index c183e0abe..c82dfa5f4 100644 --- a/internal/sdkprovider/service/account/account_test.go +++ b/internal/sdkprovider/service/account/account_test.go @@ -2,6 +2,7 @@ package account_test import ( "context" + "errors" "fmt" "log" "testing" @@ -87,7 +88,8 @@ func testAccCheckAivenAccountResourceDestroy(s *terraform.State) error { r, err := c.Accounts.List(ctx) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/account/sweep.go b/internal/sdkprovider/service/account/sweep.go index fac9317bb..a4624512f 100644 --- a/internal/sdkprovider/service/account/sweep.go +++ b/internal/sdkprovider/service/account/sweep.go @@ -2,6 +2,7 @@ package account import ( "context" + "errors" "fmt" "strings" @@ -119,7 +120,8 @@ func sweepAccounts(ctx context.Context) func(region string) error { for _, a := range accounts { if err := client.Accounts.Delete(ctx, a.Id); err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { continue } @@ -172,13 +174,13 @@ func sweepAccountTeamMembers(ctx context.Context) func(region string) error { accounts, err := listTestAccounts(ctx) if err != nil { - return fmt.Errorf("error retrieving a list of accounts : %s", err) + return fmt.Errorf("error retrieving a list of accounts : %w", err) } for _, a := range accounts { tr, err := client.AccountTeams.List(ctx, a.Id) if err != nil { - return fmt.Errorf("error retrieving a list of account teams : %s", err) + return fmt.Errorf("error retrieving a list of account teams : %w", err) } for _, t := range tr.Teams { @@ -186,26 +188,26 @@ func sweepAccountTeamMembers(ctx context.Context) func(region string) error { // delete all account team invitations mi, err := client.AccountTeamInvites.List(ctx, t.AccountId, t.Id) if err != nil { - return fmt.Errorf("error retrieving a list of account team invitations : %s", err) + return fmt.Errorf("error retrieving a list of account team invitations : %w", err) } for _, i := range mi.Invites { err := client.AccountTeamInvites.Delete(ctx, i.AccountId, i.TeamId, i.UserEmail) if err != nil { - return fmt.Errorf("cannot delete account team invitation : %s", err) + return fmt.Errorf("cannot delete account team invitation : %w", err) } } // delete all account team members mr, err := client.AccountTeamMembers.List(ctx, t.AccountId, t.Id) if err != nil { - return fmt.Errorf("error retrieving a list of account team members : %s", err) + return fmt.Errorf("error retrieving a list of account team members : %w", err) } for _, m := range mr.Members { err := client.AccountTeamMembers.Delete(ctx, t.AccountId, t.Id, m.UserId) if err != nil { - return fmt.Errorf("cannot delete account team member : %s", err) + return fmt.Errorf("cannot delete account team member : %w", err) } } } @@ -226,26 +228,26 @@ func sweepAccountTeamProjects(ctx context.Context) func(region string) error { accounts, err := listTestAccounts(ctx) if err != nil { - return fmt.Errorf("error retrieving a list of accounts : %s", err) + return fmt.Errorf("error retrieving a list of accounts : %w", err) } for _, a := range accounts { tr, err := client.AccountTeams.List(ctx, a.Id) if err != nil { - return fmt.Errorf("error retrieving a list of account teams : %s", err) + return fmt.Errorf("error retrieving a list of account teams : %w", err) } for _, t := range tr.Teams { if strings.Contains(t.Name, "test-acc-team-") { pr, err := client.AccountTeamProjects.List(ctx, t.AccountId, t.Id) if err != nil { - return fmt.Errorf("error retrieving a list of account team projects : %s", err) + return fmt.Errorf("error retrieving a list of account team projects : %w", err) } for _, p := range pr.Projects { err := client.AccountTeamProjects.Delete(ctx, t.AccountId, t.Id, p.ProjectName) if err != nil { - return fmt.Errorf("cannot delete account team project : %s", err) + return fmt.Errorf("cannot delete account team project : %w", err) } } } diff --git a/internal/sdkprovider/service/cassandra/cassandra_user_test.go b/internal/sdkprovider/service/cassandra/cassandra_user_test.go index 9bbaeefb3..873015d67 100644 --- a/internal/sdkprovider/service/cassandra/cassandra_user_test.go +++ b/internal/sdkprovider/service/cassandra/cassandra_user_test.go @@ -2,6 +2,7 @@ package cassandra_test import ( "context" + "errors" "fmt" "os" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenCassandraUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/clickhouse/clickhouse_user_test.go b/internal/sdkprovider/service/clickhouse/clickhouse_user_test.go index 7ac61cf23..9fe0348be 100644 --- a/internal/sdkprovider/service/clickhouse/clickhouse_user_test.go +++ b/internal/sdkprovider/service/clickhouse/clickhouse_user_test.go @@ -2,6 +2,7 @@ package clickhouse_test import ( "context" + "errors" "fmt" "log" "os" @@ -59,7 +60,8 @@ func testAccCheckAivenClickhouseUserResourceDestroy(s *terraform.State) error { p, err := c.ClickhouseUser.Get(ctx, projectName, serviceName, uuid) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/clickhouse/role.go b/internal/sdkprovider/service/clickhouse/role.go index 85980c4ad..4c91b13c1 100644 --- a/internal/sdkprovider/service/clickhouse/role.go +++ b/internal/sdkprovider/service/clickhouse/role.go @@ -2,6 +2,7 @@ package clickhouse import ( "context" + "errors" "fmt" "log" "strings" @@ -10,11 +11,8 @@ import ( ) func isUnknownRole(err error) bool { - aivenError, ok := err.(aiven.Error) - if !ok { - return false - } - return strings.Contains(aivenError.Message, "Code: 511") + var e *aiven.Error + return errors.As(err, &e) && strings.Contains(e.Message, "Code: 511") } func CreateRole(ctx context.Context, client *aiven.Client, projectName, serviceName, roleName string) error { diff --git a/internal/sdkprovider/service/connectionpool/connection_pool_test.go b/internal/sdkprovider/service/connectionpool/connection_pool_test.go index 08de235ac..6997e1b36 100644 --- a/internal/sdkprovider/service/connectionpool/connection_pool_test.go +++ b/internal/sdkprovider/service/connectionpool/connection_pool_test.go @@ -2,6 +2,7 @@ package connectionpool_test import ( "context" + "errors" "fmt" "os" "testing" @@ -197,7 +198,8 @@ func testAccCheckAivenConnectionPoolResourceDestroy(s *terraform.State) error { pool, err := c.ConnectionPools.Get(ctx, projectName, serviceName, databaseName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/influxdb/influxdb_database_test.go b/internal/sdkprovider/service/influxdb/influxdb_database_test.go index d453d9416..fc4a6cbdb 100644 --- a/internal/sdkprovider/service/influxdb/influxdb_database_test.go +++ b/internal/sdkprovider/service/influxdb/influxdb_database_test.go @@ -105,7 +105,8 @@ func testAccCheckAivenInfluxDBDatabaseResourceDestroy(s *terraform.State) error db, err := c.Databases.Get(ctx, projectName, serviceName, databaseName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/influxdb/influxdb_user_test.go b/internal/sdkprovider/service/influxdb/influxdb_user_test.go index 2d56b9c76..8f3f24d04 100644 --- a/internal/sdkprovider/service/influxdb/influxdb_user_test.go +++ b/internal/sdkprovider/service/influxdb/influxdb_user_test.go @@ -2,6 +2,7 @@ package influxdb_test import ( "context" + "errors" "fmt" "os" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenInfluxDBUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/kafka/kafka_acl_test.go b/internal/sdkprovider/service/kafka/kafka_acl_test.go index fae35f77c..2bf154e60 100644 --- a/internal/sdkprovider/service/kafka/kafka_acl_test.go +++ b/internal/sdkprovider/service/kafka/kafka_acl_test.go @@ -2,6 +2,7 @@ package kafka_test import ( "context" + "errors" "fmt" "log" "os" @@ -259,7 +260,8 @@ func testAccCheckAivenKafkaACLResourceDestroy(s *terraform.State) error { p, err := c.KafkaACLs.Get(ctx, project, serviceName, aclID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/kafka/kafka_connector_test.go b/internal/sdkprovider/service/kafka/kafka_connector_test.go index 286571b08..ff6b3df14 100644 --- a/internal/sdkprovider/service/kafka/kafka_connector_test.go +++ b/internal/sdkprovider/service/kafka/kafka_connector_test.go @@ -2,6 +2,7 @@ package kafka_test import ( "context" + "errors" "fmt" "os" "regexp" @@ -86,7 +87,8 @@ func testAccCheckAivenKafkaConnectorResourceDestroy(s *terraform.State) error { _, err = c.Services.Get(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } @@ -95,7 +97,8 @@ func testAccCheckAivenKafkaConnectorResourceDestroy(s *terraform.State) error { list, err := c.KafkaConnectors.List(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } @@ -105,7 +108,8 @@ func testAccCheckAivenKafkaConnectorResourceDestroy(s *terraform.State) error { for _, connector := range list.Connectors { res, err := c.KafkaConnectors.GetByName(ctx, projectName, serviceName, connector.Name) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } diff --git a/internal/sdkprovider/service/kafka/kafka_test.go b/internal/sdkprovider/service/kafka/kafka_test.go index e4722e08d..822fbd7e0 100644 --- a/internal/sdkprovider/service/kafka/kafka_test.go +++ b/internal/sdkprovider/service/kafka/kafka_test.go @@ -83,7 +83,7 @@ func TestAccAiven_kafka(t *testing.T) { a, err := c.KafkaACLs.List(ctx, os.Getenv("AIVEN_PROJECT_NAME"), rName2) if common.IsCritical(err) { - return fmt.Errorf("cannot get a list of kafka ACLs: %s", err) + return fmt.Errorf("cannot get a list of kafka ACLs: %w", err) } if len(a) > 0 { @@ -92,7 +92,7 @@ func TestAccAiven_kafka(t *testing.T) { s, err := c.KafkaSchemaRegistryACLs.List(ctx, os.Getenv("AIVEN_PROJECT_NAME"), rName2) if common.IsCritical(err) { - return fmt.Errorf("cannot get a list of Kafka Schema ACLs: %s", err) + return fmt.Errorf("cannot get a list of Kafka Schema ACLs: %w", err) } if len(s) > 0 { diff --git a/internal/sdkprovider/service/kafka/kafka_user_test.go b/internal/sdkprovider/service/kafka/kafka_user_test.go index 820976731..f85292ad7 100644 --- a/internal/sdkprovider/service/kafka/kafka_user_test.go +++ b/internal/sdkprovider/service/kafka/kafka_user_test.go @@ -2,6 +2,7 @@ package kafka_test import ( "context" + "errors" "fmt" "os" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenKafkaUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/kafka/mirrormaker_replication_flow_test.go b/internal/sdkprovider/service/kafka/mirrormaker_replication_flow_test.go index 6c501bcf2..c53542684 100644 --- a/internal/sdkprovider/service/kafka/mirrormaker_replication_flow_test.go +++ b/internal/sdkprovider/service/kafka/mirrormaker_replication_flow_test.go @@ -2,6 +2,7 @@ package kafka_test import ( "context" + "errors" "fmt" "os" "regexp" @@ -60,7 +61,8 @@ func testAccCheckAivenMirrorMakerReplicationFlowResourceDestroy(s *terraform.Sta s, err := c.Services.Get(ctx, project, serviceName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } return nil @@ -69,7 +71,8 @@ func testAccCheckAivenMirrorMakerReplicationFlowResourceDestroy(s *terraform.Sta if s.Type == "kafka_mirrormaker" { f, err := c.KafkaMirrorMakerReplicationFlow.Get(ctx, project, serviceName, sourceCluster, targetCluster) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/kafkaschema/kafka_schema_registry_acl_test.go b/internal/sdkprovider/service/kafkaschema/kafka_schema_registry_acl_test.go index 0145c0b7c..5b9bd1352 100644 --- a/internal/sdkprovider/service/kafkaschema/kafka_schema_registry_acl_test.go +++ b/internal/sdkprovider/service/kafkaschema/kafka_schema_registry_acl_test.go @@ -2,6 +2,7 @@ package kafkaschema_test import ( "context" + "errors" "fmt" "log" "os" @@ -259,7 +260,8 @@ func testAccCheckAivenKafkaSchemaRegistryACLResourceDestroy(s *terraform.State) p, err := c.KafkaSchemaRegistryACLs.Get(ctx, project, serviceName, aclID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/kafkaschema/kafka_schema_test.go b/internal/sdkprovider/service/kafkaschema/kafka_schema_test.go index b0d850448..cb42d4c3d 100644 --- a/internal/sdkprovider/service/kafkaschema/kafka_schema_test.go +++ b/internal/sdkprovider/service/kafkaschema/kafka_schema_test.go @@ -2,6 +2,7 @@ package kafkaschema_test import ( "context" + "errors" "fmt" "os" "regexp" @@ -190,7 +191,8 @@ func testAccCheckAivenKafkaSchemaResourceDestroy(s *terraform.State) error { _, err = c.Services.Get(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } @@ -199,7 +201,8 @@ func testAccCheckAivenKafkaSchemaResourceDestroy(s *terraform.State) error { schemaList, err := c.KafkaSubjectSchemas.List(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } @@ -209,7 +212,8 @@ func testAccCheckAivenKafkaSchemaResourceDestroy(s *terraform.State) error { for _, s := range schemaList.KafkaSchemaSubjects.Subjects { versions, err := c.KafkaSubjectSchemas.GetVersions(ctx, projectName, serviceName, s) if err != nil { - if err.(aiven.Error).Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { return nil } diff --git a/internal/sdkprovider/service/m3db/m3db_user_test.go b/internal/sdkprovider/service/m3db/m3db_user_test.go index 00ed62793..245e9ee38 100644 --- a/internal/sdkprovider/service/m3db/m3db_user_test.go +++ b/internal/sdkprovider/service/m3db/m3db_user_test.go @@ -2,6 +2,7 @@ package m3db_test import ( "context" + "errors" "fmt" "os" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenM3DBUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/mysql/mysql_database_test.go b/internal/sdkprovider/service/mysql/mysql_database_test.go index e6d4360e7..edcffd3f0 100644 --- a/internal/sdkprovider/service/mysql/mysql_database_test.go +++ b/internal/sdkprovider/service/mysql/mysql_database_test.go @@ -105,7 +105,8 @@ func testAccCheckAivenMySQLDatabaseResourceDestroy(s *terraform.State) error { db, err := c.Databases.Get(ctx, projectName, serviceName, databaseName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/mysql/mysql_user_test.go b/internal/sdkprovider/service/mysql/mysql_user_test.go index e1fdd68ab..1da65dc64 100644 --- a/internal/sdkprovider/service/mysql/mysql_user_test.go +++ b/internal/sdkprovider/service/mysql/mysql_user_test.go @@ -2,6 +2,7 @@ package mysql_test import ( "context" + "errors" "fmt" "os" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenMySQLUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/opensearch/opensearch_acl_config_test.go b/internal/sdkprovider/service/opensearch/opensearch_acl_config_test.go index 9bd4b5981..b08d64a9d 100644 --- a/internal/sdkprovider/service/opensearch/opensearch_acl_config_test.go +++ b/internal/sdkprovider/service/opensearch/opensearch_acl_config_test.go @@ -2,6 +2,7 @@ package opensearch_test import ( "context" + "errors" "fmt" "os" "testing" @@ -84,7 +85,8 @@ func testAccCheckAivenOpenSearchACLConfigResourceDestroy(s *terraform.State) err r, err := c.OpenSearchACLs.Get(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/opensearch/opensearch_acl_rule_test.go b/internal/sdkprovider/service/opensearch/opensearch_acl_rule_test.go index f18209aaa..995175dca 100644 --- a/internal/sdkprovider/service/opensearch/opensearch_acl_rule_test.go +++ b/internal/sdkprovider/service/opensearch/opensearch_acl_rule_test.go @@ -2,6 +2,7 @@ package opensearch_test import ( "context" + "errors" "fmt" "os" "testing" @@ -93,7 +94,8 @@ func testAccCheckAivenOpenSearchACLRuleResourceDestroy(s *terraform.State) error r, err := c.OpenSearchACLs.Get(ctx, projectName, serviceName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/opensearch/opensearch_user.go b/internal/sdkprovider/service/opensearch/opensearch_user.go index cde74e4e0..15b63810b 100644 --- a/internal/sdkprovider/service/opensearch/opensearch_user.go +++ b/internal/sdkprovider/service/opensearch/opensearch_user.go @@ -140,7 +140,7 @@ func resourceOpenSearchUserDelete(ctx context.Context, d *schema.ResourceData, m err = client.ServiceUsers.Delete(ctx, projectName, serviceName, username) if common.IsCritical(err) { - var e aiven.Error + var e *aiven.Error // This is a special case where the user is not managed by Aiven, but by the OpenSearch Security plugin. // We don't want to fail on destroy operations if the OpenSearch Security Plugin is enabled, diff --git a/internal/sdkprovider/service/opensearch/opensearch_user_test.go b/internal/sdkprovider/service/opensearch/opensearch_user_test.go index bcad62731..5ece944bd 100644 --- a/internal/sdkprovider/service/opensearch/opensearch_user_test.go +++ b/internal/sdkprovider/service/opensearch/opensearch_user_test.go @@ -2,6 +2,7 @@ package opensearch_test import ( "context" + "errors" "fmt" "os" "testing" @@ -59,7 +60,8 @@ func testAccCheckAivenOpenSearchUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/organization/organization_user_group_test.go b/internal/sdkprovider/service/organization/organization_user_group_test.go index 9e87927a5..39be1be67 100644 --- a/internal/sdkprovider/service/organization/organization_user_group_test.go +++ b/internal/sdkprovider/service/organization/organization_user_group_test.go @@ -2,6 +2,7 @@ package organization_test import ( "context" + "errors" "fmt" "log" "testing" @@ -76,7 +77,8 @@ func testAccCheckAivenOrganizationUserGroupResourceDestroy(s *terraform.State) e r, err := c.OrganizationUserGroups.Get(ctx, orgID, userGroupID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/organization/organization_user_test.go b/internal/sdkprovider/service/organization/organization_user_test.go index 97075ac7e..5ecebed40 100644 --- a/internal/sdkprovider/service/organization/organization_user_test.go +++ b/internal/sdkprovider/service/organization/organization_user_test.go @@ -2,6 +2,7 @@ package organization_test import ( "context" + "errors" "fmt" "regexp" "testing" @@ -69,7 +70,8 @@ func testAccCheckAivenOrganizationUserResourceDestroy(s *terraform.State) error r, err := c.Organization.Get(ctx, organizationID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } @@ -79,7 +81,8 @@ func testAccCheckAivenOrganizationUserResourceDestroy(s *terraform.State) error if r.ID == organizationID { ri, err := c.OrganizationUserInvitations.List(ctx, organizationID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } diff --git a/internal/sdkprovider/service/pg/pg_database_test.go b/internal/sdkprovider/service/pg/pg_database_test.go index 379893c4b..59654d073 100644 --- a/internal/sdkprovider/service/pg/pg_database_test.go +++ b/internal/sdkprovider/service/pg/pg_database_test.go @@ -113,7 +113,8 @@ func testAccCheckAivenPGDatabaseResourceDestroy(s *terraform.State) error { db, err := c.Databases.Get(ctx, projectName, serviceName, databaseName) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/pg/pg_test.go b/internal/sdkprovider/service/pg/pg_test.go index 6c966aae3..82efccf07 100644 --- a/internal/sdkprovider/service/pg/pg_test.go +++ b/internal/sdkprovider/service/pg/pg_test.go @@ -954,7 +954,7 @@ func testAccCheckAivenServiceTerminationProtection(n string) resource.TestCheckF service, err := c.Services.Get(ctx, projectName, serviceName) if err != nil { - return fmt.Errorf("cannot get service %s err: %s", serviceName, err) + return fmt.Errorf("cannot get service %s err: %w", serviceName, err) } if service.TerminationProtection == false { @@ -985,7 +985,7 @@ func testAccCheckAivenServiceTerminationProtection(n string) resource.TestCheckF ) if err != nil { - return fmt.Errorf("unable to update Aiven service to set termination_protection=false err: %s", err) + return fmt.Errorf("unable to update Aiven service to set termination_protection=false err: %w", err) } return nil diff --git a/internal/sdkprovider/service/pg/pg_user_test.go b/internal/sdkprovider/service/pg/pg_user_test.go index 5207f2a00..cebfa9878 100644 --- a/internal/sdkprovider/service/pg/pg_user_test.go +++ b/internal/sdkprovider/service/pg/pg_user_test.go @@ -2,6 +2,7 @@ package pg_test import ( "context" + "errors" "fmt" "os" "testing" @@ -123,7 +124,8 @@ func testAccCheckAivenPGUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/project/billing_group_test.go b/internal/sdkprovider/service/project/billing_group_test.go index f746f50f1..639f05be6 100644 --- a/internal/sdkprovider/service/project/billing_group_test.go +++ b/internal/sdkprovider/service/project/billing_group_test.go @@ -2,6 +2,7 @@ package project_test import ( "context" + "errors" "fmt" "testing" @@ -56,7 +57,8 @@ func testAccCheckAivenBillingGroupResourceDestroy(s *terraform.State) error { } db, err := c.BillingGroup.Get(ctx, rs.Primary.ID) - if common.IsCritical(err) && err.(aiven.Error).Status != 500 { + var e *aiven.Error + if common.IsCritical(err) && errors.As(err, &e) && e.Status != 500 { return fmt.Errorf("error getting a billing group by id: %w", err) } diff --git a/internal/sdkprovider/service/project/project.go b/internal/sdkprovider/service/project/project.go index e7404ca14..f1cbece4a 100644 --- a/internal/sdkprovider/service/project/project.go +++ b/internal/sdkprovider/service/project/project.go @@ -2,6 +2,7 @@ package project import ( "context" + "errors" "log" "net/http" "os" @@ -349,7 +350,8 @@ func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interf // to make long acceptance tests pass which generate some balance re := regexp.MustCompile("Project with open balance cannot be deleted") if err != nil && os.Getenv("TF_ACC") != "" { - if re.MatchString(err.Error()) && err.(aiven.Error).Status == 403 { + var e *aiven.Error + if re.MatchString(err.Error()) && errors.As(err, &e) && e.Status == 403 { return nil } } @@ -490,9 +492,6 @@ func setProjectTerraformProperties( // isNotProjectMember This happens right after project created func isNotProjectMember(err error) bool { - if err == nil { - return false - } - e, ok := err.(aiven.Error) - return ok && e.Status == http.StatusForbidden && strings.Contains(e.Message, "Not a project member") + var e *aiven.Error + return errors.As(err, &e) && e.Status == http.StatusForbidden && strings.Contains(e.Message, "Not a project member") } diff --git a/internal/sdkprovider/service/project/project_test.go b/internal/sdkprovider/service/project/project_test.go index c2f5675b4..30f23f546 100644 --- a/internal/sdkprovider/service/project/project_test.go +++ b/internal/sdkprovider/service/project/project_test.go @@ -2,6 +2,7 @@ package project_test import ( "context" + "errors" "fmt" "log" "regexp" @@ -255,8 +256,8 @@ func testAccCheckAivenProjectResourceDestroy(s *terraform.State) error { p, err := c.Projects.Get(ctx, rs.Primary.ID) if err != nil { - errStatus := err.(aiven.Error).Status - if errStatus != 404 && errStatus != 403 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 && e.Status != 403 { return err } } diff --git a/internal/sdkprovider/service/project/project_user.go b/internal/sdkprovider/service/project/project_user.go index 1cc3eae2f..74d64efa2 100644 --- a/internal/sdkprovider/service/project/project_user.go +++ b/internal/sdkprovider/service/project/project_user.go @@ -2,6 +2,7 @@ package project import ( "context" + "errors" "strings" "github.com/aiven/aiven-go-client/v2" @@ -53,7 +54,8 @@ func ResourceProjectUser() *schema.Resource { // isProjectUserAlreadyInvited return true if user already been invited to the project func isProjectUserAlreadyInvited(err error) bool { - if e, ok := err.(aiven.Error); ok { + var e *aiven.Error + if errors.As(err, &e) { if strings.Contains(e.Message, "already been invited to this project") && e.Status == 409 { return true } @@ -166,9 +168,10 @@ func resourceProjectUserDelete(ctx context.Context, d *schema.ResourceData, m in if user != nil { err := client.ProjectUsers.DeleteUser(ctx, projectName, email) if err != nil { - if err.(aiven.Error).Status != 404 || - !strings.Contains(err.(aiven.Error).Message, "User does not exist") || - !strings.Contains(err.(aiven.Error).Message, "User not found") { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 || + !strings.Contains(e.Message, "User does not exist") || + !strings.Contains(e.Message, "User not found") { return diag.FromErr(err) } diff --git a/internal/sdkprovider/service/project/project_user_test.go b/internal/sdkprovider/service/project/project_user_test.go index de5d70d97..c67c5acb4 100644 --- a/internal/sdkprovider/service/project/project_user_test.go +++ b/internal/sdkprovider/service/project/project_user_test.go @@ -2,6 +2,7 @@ package project_test import ( "context" + "errors" "fmt" "testing" @@ -63,8 +64,8 @@ func testAccCheckAivenProjectUserResourceDestroy(s *terraform.State) error { p, i, err := c.ProjectUsers.Get(ctx, projectName, email) if err != nil { - errStatus := err.(aiven.Error).Status - if errStatus != 404 && errStatus != 403 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 && e.Status != 403 { return err } } diff --git a/internal/sdkprovider/service/project/sweep.go b/internal/sdkprovider/service/project/sweep.go index 7e5833d4a..79c36b77b 100644 --- a/internal/sdkprovider/service/project/sweep.go +++ b/internal/sdkprovider/service/project/sweep.go @@ -2,6 +2,7 @@ package project import ( "context" + "errors" "fmt" "log" "strings" @@ -54,16 +55,16 @@ func sweepProjects(ctx context.Context) func(region string) error { projects, err := client.Projects.List(ctx) if err != nil { - return fmt.Errorf("error retrieving a list of projects : %s", err) + return fmt.Errorf("error retrieving a list of projects : %w", err) } for _, project := range projects { if strings.Contains(project.Name, "test-acc-") { if err := client.Projects.Delete(ctx, project.Name); err != nil { - e := err.(aiven.Error) + var e *aiven.Error // project not found - if e.Status == 404 { + if errors.As(err, &e) && e.Status == 404 { continue } @@ -73,7 +74,7 @@ func sweepProjects(ctx context.Context) func(region string) error { continue } - return fmt.Errorf("error destroying project %s during sweep: %s", project.Name, err) + return fmt.Errorf("error destroying project %s during sweep: %w", project.Name, err) } } } @@ -91,21 +92,20 @@ func sweepBillingGroups(ctx context.Context) func(region string) error { billingGroups, err := client.BillingGroup.ListAll(ctx) if err != nil { - return fmt.Errorf("error retrieving a list of billing groups : %s", err) + return fmt.Errorf("error retrieving a list of billing groups : %w", err) } for _, billingGroup := range billingGroups { if strings.Contains(billingGroup.BillingGroupName, "test-acc-") { if err := client.BillingGroup.Delete(ctx, billingGroup.Id); err != nil { - e := err.(aiven.Error) - // billing group not found - if e.Status == 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status == 404 { continue } return fmt.Errorf( - "error destroying billing group %s during sweep: %s", + "error destroying billing group %s during sweep: %w", billingGroup.BillingGroupName, err, ) diff --git a/internal/sdkprovider/service/redis/redis_user_test.go b/internal/sdkprovider/service/redis/redis_user_test.go index 96f887672..d0f9ad114 100644 --- a/internal/sdkprovider/service/redis/redis_user_test.go +++ b/internal/sdkprovider/service/redis/redis_user_test.go @@ -2,6 +2,7 @@ package redis_test import ( "context" + "errors" "fmt" "os" "testing" @@ -55,7 +56,8 @@ func testAccCheckAivenRedisUserResourceDestroy(s *terraform.State) error { p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/serviceintegration/service_integration.go b/internal/sdkprovider/service/serviceintegration/service_integration.go index 24e1e9f21..0045a7dc1 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration.go @@ -274,7 +274,7 @@ func resourceServiceIntegrationCheckForPreexistingResource(ctx context.Context, integrations, err := client.ServiceIntegrations.List(ctx, projectName, sourceServiceName) if common.IsCritical(err) { - return nil, fmt.Errorf("unable to get list of service integrations: %s", err) + return nil, fmt.Errorf("unable to get list of service integrations: %w", err) } for i := range integrations { diff --git a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go index a01195be0..271bcb7d8 100644 --- a/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go +++ b/internal/sdkprovider/service/serviceintegration/service_integration_endpoint_test.go @@ -2,6 +2,7 @@ package serviceintegration_test import ( "context" + "errors" "fmt" "os" "testing" @@ -248,7 +249,8 @@ func testAccCheckAivenServiceIntegraitonEndpointResourceDestroy(s *terraform.Sta i, err := c.ServiceIntegrationEndpoints.Get(ctx, projectName, endpointID) if err != nil { - if err.(aiven.Error).Status != 404 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 { return err } } diff --git a/internal/sdkprovider/service/serviceintegration/sweep.go b/internal/sdkprovider/service/serviceintegration/sweep.go index 74f745607..d12eecd5a 100644 --- a/internal/sdkprovider/service/serviceintegration/sweep.go +++ b/internal/sdkprovider/service/serviceintegration/sweep.go @@ -36,7 +36,7 @@ func sweepServiceIntegrations(ctx context.Context) func(region string) error { services, err := client.Services.List(ctx, projectName) if common.IsCritical(err) { - return fmt.Errorf("error retrieving a list of service for a project `%s`: %s", projectName, err) + return fmt.Errorf("error retrieving a list of service for a project `%s`: %w", projectName, err) } for _, service := range services { @@ -46,7 +46,7 @@ func sweepServiceIntegrations(ctx context.Context) func(region string) error { serviceIntegrations, err := client.ServiceIntegrations.List(ctx, projectName, service.Name) if err != nil { - return fmt.Errorf("error retrieving a list of service integration for service `%s`: %s", service.Name, err) + return fmt.Errorf("error retrieving a list of service integration for service `%s`: %w", service.Name, err) } for _, serviceIntegration := range serviceIntegrations { if err := client.ServiceIntegrations.Delete( @@ -56,7 +56,7 @@ func sweepServiceIntegrations(ctx context.Context) func(region string) error { ); err != nil { if !aiven.IsNotFound(err) { return fmt.Errorf( - "unable to delete service integration `%s`: %s", + "unable to delete service integration `%s`: %w", serviceIntegration.ServiceIntegrationID, err, ) diff --git a/internal/sdkprovider/service/staticip/static_ip.go b/internal/sdkprovider/service/staticip/static_ip.go index 48bf51f6e..f00d6f396 100644 --- a/internal/sdkprovider/service/staticip/static_ip.go +++ b/internal/sdkprovider/service/staticip/static_ip.go @@ -176,7 +176,7 @@ func resourceStaticIPWait(ctx context.Context, d *schema.ResourceData, m interfa } if _, err := conf.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("error waiting for static ip to be created: %s", err) + return fmt.Errorf("error waiting for static ip to be created: %w", err) } return nil @@ -184,22 +184,22 @@ func resourceStaticIPWait(ctx context.Context, d *schema.ResourceData, m interfa func setStaticIPState(d *schema.ResourceData, project string, staticIP *aiven.StaticIP) error { if err := d.Set("project", project); err != nil { - return fmt.Errorf("error setting static ips `project` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `project` for resource %s: %w", d.Id(), err) } if err := d.Set("cloud_name", staticIP.CloudName); err != nil { - return fmt.Errorf("error setting static ips `cloud_name` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `cloud_name` for resource %s: %w", d.Id(), err) } if err := d.Set("ip_address", staticIP.IPAddress); err != nil { - return fmt.Errorf("error setting static ips `ip_address` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `ip_address` for resource %s: %w", d.Id(), err) } if err := d.Set("service_name", staticIP.ServiceName); err != nil { - return fmt.Errorf("error setting static ips `service_name` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `service_name` for resource %s: %w", d.Id(), err) } if err := d.Set("state", staticIP.State); err != nil { - return fmt.Errorf("error setting static ips `state` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `state` for resource %s: %w", d.Id(), err) } if err := d.Set("static_ip_address_id", staticIP.StaticIPAddressID); err != nil { - return fmt.Errorf("error setting static ips `static_ip_address_id` for resource %s: %s", d.Id(), err) + return fmt.Errorf("error setting static ips `static_ip_address_id` for resource %s: %w", d.Id(), err) } return nil } diff --git a/internal/sdkprovider/service/vpc/azure_privatelink_test.go b/internal/sdkprovider/service/vpc/azure_privatelink_test.go index fb849dae8..46513e0fd 100644 --- a/internal/sdkprovider/service/vpc/azure_privatelink_test.go +++ b/internal/sdkprovider/service/vpc/azure_privatelink_test.go @@ -2,6 +2,7 @@ package vpc_test import ( "context" + "errors" "fmt" "os" "testing" @@ -60,7 +61,8 @@ func testAccCheckAivenAzurePrivatelinkResourceDestroy(s *terraform.State) error } pv, err := c.AzurePrivatelink.Get(ctx, project, serviceName) - if common.IsCritical(err) && err.(aiven.Error).Status != 500 { + var e *aiven.Error + if common.IsCritical(err) && errors.As(err, &e) && e.Status != 500 { return fmt.Errorf("error getting a Azure Privatelink: %w", err) } diff --git a/internal/sdkprovider/service/vpc/gcp_privatelink_test.go b/internal/sdkprovider/service/vpc/gcp_privatelink_test.go index f44541929..89dd4465a 100644 --- a/internal/sdkprovider/service/vpc/gcp_privatelink_test.go +++ b/internal/sdkprovider/service/vpc/gcp_privatelink_test.go @@ -2,6 +2,7 @@ package vpc_test import ( "context" + "errors" "fmt" "os" "testing" @@ -54,7 +55,8 @@ func testAccCheckAivenGCPPrivatelinkResourceDestroy(s *terraform.State) error { } pv, err := c.GCPPrivatelink.Get(ctx, project, serviceName) - if common.IsCritical(err) && err.(aiven.Error).Status != 500 { + var e *aiven.Error + if common.IsCritical(err) && errors.As(err, &e) && e.Status != 500 { return fmt.Errorf("error getting a GCP Privatelink: %w", err) } diff --git a/internal/sdkprovider/service/vpc/project_vpc.go b/internal/sdkprovider/service/vpc/project_vpc.go index b9ce1546f..46ab1fabf 100644 --- a/internal/sdkprovider/service/vpc/project_vpc.go +++ b/internal/sdkprovider/service/vpc/project_vpc.go @@ -2,6 +2,7 @@ package vpc import ( "context" + "errors" "log" "time" @@ -218,7 +219,8 @@ func (w *ProjectVPCDeleteWaiter) RefreshFunc() resource.StateRefreshFunc { // VPC cannot be deleted while there are services migrating from // it or service deletion is still in progress - if err.(aiven.Error).Status != 409 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 409 { return nil, "", err } } diff --git a/internal/sdkprovider/service/vpc/project_vpc_test.go b/internal/sdkprovider/service/vpc/project_vpc_test.go index 22b5dc43b..d812187e7 100644 --- a/internal/sdkprovider/service/vpc/project_vpc_test.go +++ b/internal/sdkprovider/service/vpc/project_vpc_test.go @@ -2,6 +2,7 @@ package vpc_test import ( "context" + "errors" "fmt" "os" "regexp" @@ -124,8 +125,8 @@ func testAccCheckAivenProjectVPCResourceDestroy(s *terraform.State) error { vpc, err := c.VPCs.Get(ctx, projectName, vpcID) if err != nil { - errStatus := err.(aiven.Error).Status - if errStatus != 404 && errStatus != 403 { + var e *aiven.Error + if errors.As(err, &e) && e.Status != 404 && e.Status != 403 { return err } } diff --git a/internal/sdkprovider/service/vpc/sweep.go b/internal/sdkprovider/service/vpc/sweep.go index f5078ade5..5ad3dd105 100644 --- a/internal/sdkprovider/service/vpc/sweep.go +++ b/internal/sdkprovider/service/vpc/sweep.go @@ -101,13 +101,13 @@ func sweepVPCs(ctx context.Context) func(string) error { vpcs, err := client.VPCs.List(ctx, projectName) if err != nil { - return fmt.Errorf("error retrieving a list of vpcs for a project : %s", err) + return fmt.Errorf("error retrieving a list of vpcs for a project : %w", err) } for _, vpc := range vpcs { err := client.VPCs.Delete(ctx, projectName, vpc.ProjectVPCID) if common.IsCritical(err) { - return fmt.Errorf("error deleting vpc %s: %s", vpc.ProjectVPCID, err) + return fmt.Errorf("error deleting vpc %s: %w", vpc.ProjectVPCID, err) } } @@ -125,13 +125,13 @@ func sweepVPCPeeringCons(ctx context.Context) func(string) error { vpcs, err := client.VPCs.List(ctx, projectName) if err != nil { - return fmt.Errorf("error retrieving a list of vpcs for a project : %s", err) + return fmt.Errorf("error retrieving a list of vpcs for a project : %w", err) } for _, vpc := range vpcs { peeringCons, err := client.VPCPeeringConnections.List(ctx, projectName, vpc.ProjectVPCID) if err != nil { - return fmt.Errorf("error retrieving a list of vpc peering connections for a project : %s", err) + return fmt.Errorf("error retrieving a list of vpc peering connections for a project : %w", err) } for _, peeringCon := range peeringCons { @@ -188,19 +188,19 @@ func sweepAWSPrivatelinks(ctx context.Context) func(string) error { serviceList, err := client.Services.List(ctx, projectName) if err != nil { - return fmt.Errorf("error retrieving a list of services for a project : %s", err) + return fmt.Errorf("error retrieving a list of services for a project : %w", err) } for _, service := range serviceList { awsPrivetelink, err := client.AWSPrivatelink.Get(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error retrieving a list of aws privatelinks for a project : %s", err) + return fmt.Errorf("error retrieving a list of aws privatelinks for a project : %w", err) } if awsPrivetelink != nil { err := client.AWSPrivatelink.Delete(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error deleting aws privatelink %s/%s: %s", + return fmt.Errorf("error deleting aws privatelink %s/%s: %w", projectName, service.Name, err) @@ -222,19 +222,19 @@ func sweepAzurePrivatelinks(ctx context.Context) func(string) error { serviceList, err := client.Services.List(ctx, projectName) if err != nil { - return fmt.Errorf("error retrieving a list of services for a project : %s", err) + return fmt.Errorf("error retrieving a list of services for a project : %w", err) } for _, service := range serviceList { azurePrivetelink, err := client.AzurePrivatelink.Get(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error retrieving a list of azure privatelinks for a project : %s", err) + return fmt.Errorf("error retrieving a list of azure privatelinks for a project : %w", err) } if azurePrivetelink != nil { err := client.AzurePrivatelink.Delete(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error deleting azure privatelink %s/%s: %s", + return fmt.Errorf("error deleting azure privatelink %s/%s: %w", projectName, service.Name, err) @@ -256,19 +256,19 @@ func sweepGCPPrivatelinks(ctx context.Context) func(string) error { serviceList, err := client.Services.List(ctx, projectName) if err != nil { - return fmt.Errorf("error retrieving a list of services for a project : %s", err) + return fmt.Errorf("error retrieving a list of services for a project : %w", err) } for _, service := range serviceList { gcpPrivetelink, err := client.GCPPrivatelink.Get(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error retrieving a list of gcp privatelinks for a project : %s", err) + return fmt.Errorf("error retrieving a list of gcp privatelinks for a project : %w", err) } if gcpPrivetelink != nil { err := client.GCPPrivatelink.Delete(ctx, projectName, service.Name) if common.IsCritical(err) { - return fmt.Errorf("error deleting gcp privatelink %s/%s: %s", + return fmt.Errorf("error deleting gcp privatelink %s/%s: %w", projectName, service.Name, err) diff --git a/internal/sdkprovider/service/vpc/vpc_peering_connection.go b/internal/sdkprovider/service/vpc/vpc_peering_connection.go index 928fea862..659b55364 100644 --- a/internal/sdkprovider/service/vpc/vpc_peering_connection.go +++ b/internal/sdkprovider/service/vpc/vpc_peering_connection.go @@ -465,7 +465,7 @@ func ConvertStateInfoToMap(s *map[string]interface{}) map[string]string { func isAzureVPCPeeringConnection(ctx context.Context, d *schema.ResourceData, c *aiven.Client) (bool, error) { p, err := parsePeerVPCID(d.Id()) if err != nil { - return false, fmt.Errorf("error parsing Azure peering VPC ID: %s", err) + return false, fmt.Errorf("error parsing Azure peering VPC ID: %w", err) } // If peerRegion is nil the peered VPC is assumed to be in the same region and diff --git a/internal/sweep/sweep.go b/internal/sweep/sweep.go index f18dbe5b8..c322128fe 100644 --- a/internal/sweep/sweep.go +++ b/internal/sweep/sweep.go @@ -41,7 +41,7 @@ func SharedClient() (*aiven.Client, error) { func SweepServices(ctx context.Context, t string) error { client, err := SharedClient() if err != nil { - return fmt.Errorf("error getting client: %s", err) + return fmt.Errorf("error getting client: %w", err) } projectName := os.Getenv("AIVEN_PROJECT_NAME") @@ -74,13 +74,13 @@ func SweepServices(ctx context.Context, t string) error { }) if err != nil { - return fmt.Errorf("error disabling `termination_protection` for service '%s' during sweep: %s", s.Name, err) + return fmt.Errorf("error disabling `termination_protection` for service '%s' during sweep: %w", s.Name, err) } } if err := client.Services.Delete(ctx, projectName, s.Name); err != nil { if common.IsCritical(err) { - return fmt.Errorf("error destroying service %s during sweep: %s", s.Name, err) + return fmt.Errorf("error destroying service %s during sweep: %w", s.Name, err) } } }