// Copyright (c) STACKIT package postgresflexalpha_test import ( "context" _ "embed" "encoding/json" "fmt" "log" "log/slog" "net/http" "net/http/httptest" "os" "strings" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/joho/godotenv" "github.com/stackitcloud/stackit-sdk-go/core/utils" postgresflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance" "github.com/stackitcloud/stackit-sdk-go/core/config" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/testutil" postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" // The fwresource import alias is so there is no collision // with the more typical acceptance testing import: // "github.com/hashicorp/terraform-plugin-testing/helper/resource" fwresource "github.com/hashicorp/terraform-plugin-framework/resource" ) func TestInstanceResourceSchema(t *testing.T) { t.Parallel() ctx := context.Background() schemaRequest := fwresource.SchemaRequest{} schemaResponse := &fwresource.SchemaResponse{} // Instantiate the resource.Resource and call its Schema method postgresflexalpha.NewInstanceResource().Schema(ctx, schemaRequest, schemaResponse) if schemaResponse.Diagnostics.HasError() { t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics) } // Validate the schema diagnostics := schemaResponse.Schema.ValidateImplementation(ctx) if diagnostics.HasError() { t.Fatalf("Schema validation diagnostics: %+v", diagnostics) } } var ( //go:embed testdata/resource-complete.tf resourceSecurityGroupMinConfig string //nolint:unused // needs implementation ) func setup() { err := godotenv.Load() if err != nil { slog.Info("could not find .env file - not loading .env") return } slog.Info("loaded .env file") } func TestMain(m *testing.M) { setup() code := m.Run() // shutdown() os.Exit(code) } //var ( // validFlavor = "2.4" // kekKeyRingId = "" // kekKeyVersion = "" // kekKeySA = "" //) func testAccPreCheck(t *testing.T) { // TODO: if needed ... } //func TestAccResourceExample_parallel(t *testing.T) { // t.Parallel() // // exData := resData{ // Region: "eu01", // ServiceAccountFilePath: sa_file, // ProjectID: project_id, // Name: acctest.RandomWithPrefix("tf-acc"), // } // // resource.Test(t, resource.TestCase{ // ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, // Steps: []resource.TestStep{ // { // Config: testAccResourceEncryptionExampleConfig(exData), // Check: resource.TestCheckResourceAttrSet("example_resource.test", "id"), // }, // }, // }) //} type resData struct { ServiceAccountFilePath string ProjectID string Region string Name string Flavor string BackupSchedule string RetentionDays uint32 } func getExample() resData { return resData{ Region: testutil.Region, ServiceAccountFilePath: testutil.ServiceAccountFile, ProjectID: testutil.ProjectId, Name: acctest.RandomWithPrefix("tf-acc"), Flavor: "2.4", BackupSchedule: "0 0 * * *", RetentionDays: 33, } } func TestAccResourceExample_basic(t *testing.T) { exData := getExample() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { Config: testAccResourceNoEncryptionExampleConfig(exData), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name), resource.TestCheckResourceAttrSet("example_resource.test", "id"), ), }, //// Create and verify //{ // Config: testAccResourceNoEncryptionExampleConfig(exData), // Check: resource.ComposeTestCheckFunc( // resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name), // ), //}, //// Update and verify //{ // Config: testAccResourceNoEncryptionExampleConfig(exData), // Check: resource.ComposeTestCheckFunc( // resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name), // ), //}, // Import test { ResourceName: "example_resource.test", ImportState: true, ImportStateVerify: true, }, }, }) } func testAccResourceNoEncryptionExampleConfig(data resData) string { return fmt.Sprintf(` %[1]s resource "stackitprivatepreview_postgresflexalpha_instance" "test" { project_id = %[2]q name = %[3]q backup_schedule = %[4]q retention_days = %[5]d flavor_id = %[6]q replicas = 1 storage = { performance_class = "premium-perf2-stackit" size = 10 } network = { acl = ["0.0.0.0/0"] access_scope = "PUBLIC" } version = 17 } `, testutil.PostgresFlexProviderConfig(data.ServiceAccountFilePath), data.ProjectID, data.Name, data.BackupSchedule, data.RetentionDays, data.Flavor, data.Name, ) } //func testAccResourceEncryptionExampleConfig(data resData) string { // return fmt.Sprintf(` // //%[1]s // //resource "stackitprivatepreview_postgresflexalpha_instance" "test" { // project_id = %[2]q // name = %[3]q // backup_schedule = "0 0 * * *" // retention_days = 45 // flavor_id = "2.1" // replicas = 1 // storage = { // performance_class = "premium-perf2-stackit" // size = 10 // } // encryption = { // kek_key_id = "key01" // kek_key_ring_id = "key_ring_01" // kek_key_version = 1 // service_account = "service@account.email" // } // network = { // acl = ["0.0.0.0/0"] // access_scope = "PUBLIC" // } // version = 14 //} // //`, // testutil.PostgresFlexProviderConfig(data.ServiceAccountFilePath), // data.ProjectID, // data.Name, // ) //} //func testCheckResourceExists(resourceName string) resource.TestCheckFunc { // return func(s *terraform.State) error { // rs, ok := s.RootModule().Resources[resourceName] // if !ok { // return fmt.Errorf("resource not found: %s", resourceName) // } // // if rs.Primary.ID == "" { // return fmt.Errorf("resource ID not set") // } // // // Verify resource exists in the API // //client := testAccProvider.Meta().(*APIClient) // //_, err := client.GetResource(rs.Primary.ID) // //if err != nil { // // return fmt.Errorf("error fetching resource: %w", err) // //} // // return nil // } //} func setupMockServer() *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/api/resources", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: w.WriteHeader(http.StatusCreated) err := json.NewEncoder(w).Encode(map[string]string{ "id": "mock-id-123", "name": "test-resource", }) if err != nil { log.Fatalln(err) } case http.MethodGet: w.WriteHeader(http.StatusOK) err := json.NewEncoder(w).Encode([]map[string]string{}) if err != nil { log.Fatalln(err) } } }) return httptest.NewServer(mux) } func TestUnitResourceCreate(t *testing.T) { server := setupMockServer() defer server.Close() // Configure provider to use mock server URL err := os.Setenv("API_ENDPOINT", server.URL) if err != nil { log.Fatalln(err) } // Run unit tests against mock } // type postgresFlexClientMocked struct { // returnError bool // getFlavorsResp *postgresflex.GetFlavorsResponse // } // // func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*postgresflex.GetFlavorsResponse, error) { // if c.returnError { // return nil, fmt.Errorf("get flavors failed") // } // // return c.getFlavorsResp, nil // } //func TestNewInstanceResource(t *testing.T) { // exData := resData{ // Region: "eu01", // ServiceAccountFilePath: sa_file, // ProjectID: project_id, // Name: "testRes", // } // resource.Test(t, resource.TestCase{ // ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, // Steps: []resource.TestStep{ // { // Config: testAccResourceEncryptionExampleConfig(exData), // Check: resource.ComposeAggregateTestCheckFunc( // resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name), // resource.TestCheckResourceAttrSet("example_resource.test", "id"), // ), // }, // }, // }) // // //tests := []struct { // // name string // // want resource.Resource // //}{ // // { // // name: "create empty instance resource", // // want: &instanceResource{}, // // }, // //} // //for _, tt := range tests { // // t.Run(tt.name, func(t *testing.T) { // // if got := NewInstanceResource(); !reflect.DeepEqual(got, tt.want) { // // t.Errorf("NewInstanceResource() = %v, want %v", got, tt.want) // // } // // }) // //} //} // Instance resource data var instanceResource = map[string]string{ "project_id": testutil.ProjectId, "region": "eu01", "name": fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(7, acctest.CharSetAlphaNum)), "acl": "192.168.0.0/16", "backup_schedule": "00 16 * * *", "backup_schedule_updated": "00 12 * * *", "retention_days": "33", "flavor_cpu": "2", "flavor_ram": "4", "flavor_description": "Small, Compute optimized", "replicas": "1", "storage_class": "premium-perf12-stackit", "storage_size": "5", "version": "14", "flavor_id": "2.4", "kek_key_id": "UUID1", "kek_key_ring_id": "UUID2", "kek_key_version": "1", "service_account": "service@account.com", "access_scope": "SNA", } // User resource data var userResource = map[string]string{ "username": fmt.Sprintf("tfaccuser%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)), "role": "createdb", "project_id": testutil.ProjectId, } // Database resource data var databaseResource = map[string]string{ "name": fmt.Sprintf("tfaccdb%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)), "project_id": testutil.ProjectId, } func configResources(backupSchedule string, _ *string) string { return fmt.Sprintf( ` %s resource "stackitprivatepreview_postgresflexalpha_instance" "instance" { project_id = "%s" region = "%s" name = "%s" backup_schedule = "%s" retention_days = %s flavor_id = %s replicas = %s storage = { performance_class = "%s" size = %s } encryption = { kek_key_id = "%s" kek_key_ring_id = "%s" kek_key_version = "%s" service_account = "%s" } network = { acl = ["%s"] access_scope = "%s" } version = %s } resource "stackitprivatepreview_postgresflexalpha_user" "user" { project_id = "%s" instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id username = "%s" roles = ["%s"] } resource "stackitprivatepreview_postgresflexalpha_database" "database" { project_id = "%s" instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id name = "%s" owner = stackitprivatepreview_postgresflexalpha_user.user.username } `, testutil.PostgresFlexProviderConfig( utils.GetEnvOrDefault("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_FILE", "~/service-account.json"), ), instanceResource["project_id"], instanceResource["region"], instanceResource["name"], backupSchedule, instanceResource["retention_days"], instanceResource["flavor_id"], instanceResource["replicas"], instanceResource["storage_class"], instanceResource["storage_size"], instanceResource["kek_key_id"], instanceResource["kek_key_ring_id"], instanceResource["kek_key_version"], instanceResource["service_account"], instanceResource["acl"], instanceResource["access_scope"], instanceResource["version"], userResource["project_id"], userResource["username"], userResource["role"], databaseResource["project_id"], databaseResource["name"], ) } func TestAccPostgresFlexFlexResource(t *testing.T) { resource.Test( t, resource.TestCase{ ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, CheckDestroy: testAccCheckPostgresFlexDestroy, Steps: []resource.TestStep{ // Creation { // testdata/ // ConfigDirectory: config.TestNameDirectory(), // testdata// // ConfigDirectory: config.TestStepDirectory(), Config: configResources(instanceResource["backup_schedule"], &testutil.Region), Check: resource.ComposeAggregateTestCheckFunc( // Instance resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", instanceResource["project_id"], ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "name", instanceResource["name"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "acl.#", "1", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "acl.0", instanceResource["acl"], ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.id", ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.description", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "backup_schedule", instanceResource["backup_schedule"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.cpu", instanceResource["flavor_cpu"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.ram", instanceResource["flavor_ram"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "replicas", instanceResource["replicas"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "storage.class", instanceResource["storage_class"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "storage.size", instanceResource["storage_size"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "version", instanceResource["version"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "region", testutil.Region, ), // User resource.TestCheckResourceAttrPair( "stackitprivatepreview_postgresflexalpha_user.user", "project_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", ), resource.TestCheckResourceAttrPair( "stackitprivatepreview_postgresflexalpha_user.user", "instance_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttrSet("stackitprivatepreview_postgresflexalpha_user.user", "user_id"), resource.TestCheckResourceAttrSet("stackitprivatepreview_postgresflexalpha_user.user", "password"), // Database resource.TestCheckResourceAttrPair( "stackitprivatepreview_postgresflexalpha_database.database", "project_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", ), resource.TestCheckResourceAttrPair( "stackitprivatepreview_postgresflexalpha_database.database", "instance_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_database.database", "name", databaseResource["name"], ), resource.TestCheckResourceAttrPair( "stackitprivatepreview_postgresflexalpha_database.database", "owner", "stackitprivatepreview_postgresflexalpha_user.user", "username", ), ), }, // data source { Config: fmt.Sprintf( ` %s data "stackitprivatepreview_postgresflexalpha_instance" "instance" { project_id = stackitprivatepreview_postgresflexalpha_instance.instance.project_id instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id } data "stackitprivatepreview_postgresflexalpha_user" "user" { project_id = stackitprivatepreview_postgresflexalpha_instance.instance.project_id instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id user_id = stackitprivatepreview_postgresflexalpha_user.user.user_id } data "stackitprivatepreview_postgresflexalpha_database" "database" { project_id = stackitprivatepreview_postgresflexalpha_instance.instance.project_id instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id database_id = stackitprivatepreview_postgresflexalpha_database.database.database_id } `, configResources(instanceResource["backup_schedule"], nil), ), Check: resource.ComposeAggregateTestCheckFunc( // Instance data resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", instanceResource["project_id"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "name", instanceResource["name"], ), resource.TestCheckResourceAttrPair( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", ), resource.TestCheckResourceAttrPair( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttrPair( "data.stackitprivatepreview_postgresflexalpha_user.user", "instance_id", "stackitprivatepreview_postgresflexalpha_user.user", "instance_id", ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "acl.#", "1", ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "acl.0", instanceResource["acl"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "backup_schedule", instanceResource["backup_schedule"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.id", instanceResource["flavor_id"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.description", instanceResource["flavor_description"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.cpu", instanceResource["flavor_cpu"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.ram", instanceResource["flavor_ram"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_instance.instance", "replicas", instanceResource["replicas"], ), // User data resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_user.user", "project_id", userResource["project_id"], ), resource.TestCheckResourceAttrSet( "data.stackitprivatepreview_postgresflexalpha_user.user", "user_id", ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_user.user", "username", userResource["username"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_user.user", "roles.#", "1", ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_user.user", "roles.0", userResource["role"], ), resource.TestCheckResourceAttrSet( "data.stackitprivatepreview_postgresflexalpha_user.user", "host", ), resource.TestCheckResourceAttrSet( "data.stackitprivatepreview_postgresflexalpha_user.user", "port", ), // Database data resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_database.database", "project_id", instanceResource["project_id"], ), resource.TestCheckResourceAttr( "data.stackitprivatepreview_postgresflexalpha_database.database", "name", databaseResource["name"], ), resource.TestCheckResourceAttrPair( "data.stackitprivatepreview_postgresflexalpha_database.database", "instance_id", "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttrPair( "data.stackitprivatepreview_postgresflexalpha_database.database", "owner", "data.stackitprivatepreview_postgresflexalpha_user.user", "username", ), ), }, // Import { ResourceName: "stackitprivatepreview_postgresflexalpha_instance.instance", ImportStateIdFunc: func(s *terraform.State) (string, error) { r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_instance.instance"] if !ok { return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_instance.instance") } instanceId, ok := r.Primary.Attributes["instance_id"] if !ok { return "", fmt.Errorf("couldn't find attribute instance_id") } return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId), nil }, ImportState: true, ImportStateVerify: true, ImportStateVerifyIgnore: []string{"password"}, }, { ResourceName: "stackitprivatepreview_postgresflexalpha_user.user", ImportStateIdFunc: func(s *terraform.State) (string, error) { r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_user.user"] if !ok { return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_user.user") } instanceId, ok := r.Primary.Attributes["instance_id"] if !ok { return "", fmt.Errorf("couldn't find attribute instance_id") } userId, ok := r.Primary.Attributes["user_id"] if !ok { return "", fmt.Errorf("couldn't find attribute user_id") } return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId, userId), nil }, ImportState: true, ImportStateVerify: true, ImportStateVerifyIgnore: []string{"password", "uri"}, }, { ResourceName: "stackitprivatepreview_postgresflexalpha_database.database", ImportStateIdFunc: func(s *terraform.State) (string, error) { r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_database.database"] if !ok { return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_database.database") } instanceId, ok := r.Primary.Attributes["instance_id"] if !ok { return "", fmt.Errorf("couldn't find attribute instance_id") } databaseId, ok := r.Primary.Attributes["database_id"] if !ok { return "", fmt.Errorf("couldn't find attribute database_id") } return fmt.Sprintf( "%s,%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId, databaseId, ), nil }, ImportState: true, ImportStateVerify: true, }, // Update { Config: configResources(instanceResource["backup_schedule_updated"], nil), Check: resource.ComposeAggregateTestCheckFunc( // Instance data resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "project_id", instanceResource["project_id"], ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "name", instanceResource["name"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "acl.#", "1", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "acl.0", instanceResource["acl"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "backup_schedule", instanceResource["backup_schedule_updated"], ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.id", ), resource.TestCheckResourceAttrSet( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.description", ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.cpu", instanceResource["flavor_cpu"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "flavor.ram", instanceResource["flavor_ram"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "replicas", instanceResource["replicas"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "storage.class", instanceResource["storage_class"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "storage.size", instanceResource["storage_size"], ), resource.TestCheckResourceAttr( "stackitprivatepreview_postgresflexalpha_instance.instance", "version", instanceResource["version"], ), ), }, // Deletion is done by the framework implicitly }, }, ) } func testAccCheckPostgresFlexDestroy(s *terraform.State) error { ctx := context.Background() var client *postgresflex.APIClient var err error if testutil.PostgresFlexCustomEndpoint == "" { client, err = postgresflex.NewAPIClient() } else { client, err = postgresflex.NewAPIClient( config.WithEndpoint(testutil.PostgresFlexCustomEndpoint), ) } if err != nil { return fmt.Errorf("creating client: %w", err) } instancesToDestroy := []string{} for _, rs := range s.RootModule().Resources { if rs.Type != "stackitprivatepreview_postgresflexalpha_instance" { continue } // instance terraform ID: = "[project_id],[region],[instance_id]" instanceId := strings.Split(rs.Primary.ID, core.Separator)[2] instancesToDestroy = append(instancesToDestroy, instanceId) } instancesResp, err := client.ListInstancesRequest(ctx, testutil.ProjectId, testutil.Region).Execute() if err != nil { return fmt.Errorf("getting instancesResp: %w", err) } items := *instancesResp.Instances for i := range items { if items[i].Id == nil { continue } if utils.Contains(instancesToDestroy, *items[i].Id) { // TODO @mhenselin - does force still exist? err := client.DeleteInstanceRequestExecute(ctx, testutil.ProjectId, testutil.Region, *items[i].Id) if err != nil { return fmt.Errorf("deleting instance %s during CheckDestroy: %w", *items[i].Id, err) } } } return nil }