fix: fix new sdk builder adjustment
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / CI run build and linting (pull_request) Failing after 53s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / CI run tests (pull_request) Failing after 15m56s
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 23m35s
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / CI run build and linting (pull_request) Failing after 53s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / CI run tests (pull_request) Failing after 15m56s
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 23m35s
This commit is contained in:
parent
cc08fca97a
commit
469ed9e056
6 changed files with 223 additions and 93 deletions
|
|
@ -28,8 +28,8 @@ func InstanceDataSourceSchema(ctx context.Context) schema.Schema {
|
|||
},
|
||||
"backup_schedule": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.",
|
||||
MarkdownDescription: "The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.",
|
||||
Description: "The schedule for when the database backup will be created. Currently, ONLY daily schedules are supported (every 24 hours). The schedule is written as a cron schedule.",
|
||||
MarkdownDescription: "The schedule for when the database backup will be created. Currently, ONLY daily schedules are supported (every 24 hours). The schedule is written as a cron schedule.",
|
||||
},
|
||||
"connection_info": schema.SingleNestedAttribute{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ func InstanceResourceSchema(ctx context.Context) schema.Schema {
|
|||
},
|
||||
"backup_schedule": schema.StringAttribute{
|
||||
Required: true,
|
||||
Description: "The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.",
|
||||
MarkdownDescription: "The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.",
|
||||
Description: "The schedule for when the database backup will be created. Currently, ONLY daily schedules are supported (every 24 hours). The schedule is written as a cron schedule.",
|
||||
MarkdownDescription: "The schedule for when the database backup will be created. Currently, ONLY daily schedules are supported (every 24 hours). The schedule is written as a cron schedule.",
|
||||
},
|
||||
"connection_info": schema.SingleNestedAttribute{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
|
|
|
|||
|
|
@ -9,13 +9,18 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
postgresflexalphaPkgGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
postgresflexalphaInstance "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/postgresflexalpha"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
|
||||
// The fwresource import alias is so there is no collision
|
||||
|
|
@ -26,52 +31,6 @@ import (
|
|||
|
||||
const pfx = "stackitprivatepreview_postgresflexalpha"
|
||||
|
||||
var testInstances []string
|
||||
|
||||
func init() {
|
||||
sweeperName := fmt.Sprintf("%s_%s", pfx, "sweeper")
|
||||
resource.AddTestSweepers(
|
||||
sweeperName, &resource.Sweeper{
|
||||
Name: sweeperName,
|
||||
F: func(_ string) error { // region is passed by the testing framework
|
||||
ctx := context.Background()
|
||||
apiClientConfigOptions := []config.ConfigurationOption{}
|
||||
apiClient, err := postgresflexalphaPkgGen.NewAPIClient(apiClientConfigOptions...)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, testutils.Region).
|
||||
Size(100).
|
||||
Execute()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
for _, inst := range instances.GetInstances() {
|
||||
if strings.HasPrefix(inst.GetName(), "tf-acc-") {
|
||||
for _, item := range testInstances {
|
||||
if inst.GetName() == item {
|
||||
delErr := apiClient.DeleteInstanceRequestExecute(
|
||||
ctx,
|
||||
testutils.ProjectId,
|
||||
testutils.Region,
|
||||
inst.GetId(),
|
||||
)
|
||||
if delErr != nil {
|
||||
// TODO: maybe just warn?
|
||||
log.Fatalln(delErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestInstanceResourceSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
@ -115,27 +74,6 @@ func testAccPreCheck(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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: testutils.TestAccProtoV6ProviderFactories,
|
||||
// Steps: []resource.TestStep{
|
||||
// {
|
||||
// Config: testAccResourceEncryptionExampleConfig(exData),
|
||||
// Check: resource.TestCheckResourceAttrSet("example_resource.test", "id"),
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
//}
|
||||
|
||||
type resData struct {
|
||||
ServiceAccountFilePath string
|
||||
ProjectId string
|
||||
|
|
@ -202,28 +140,53 @@ func TestAccInstance(t *testing.T) {
|
|||
updSizeData := exData
|
||||
updSizeData.Size = 25
|
||||
|
||||
updBackupSched := exData
|
||||
updBackupSched.BackupSchedule = "0 0 1 * *"
|
||||
|
||||
/*
|
||||
{
|
||||
"backupSchedule": "6 6 * * *",
|
||||
"flavorId": "1.2",
|
||||
"name": "postgres-instance",
|
||||
"network": {
|
||||
"acl": [
|
||||
"198.51.100.0/24"
|
||||
]
|
||||
},
|
||||
"replicas": 1,
|
||||
"retentionDays": 35,
|
||||
"storage": {
|
||||
"size": 10
|
||||
},
|
||||
"version": "string"
|
||||
}
|
||||
*/
|
||||
|
||||
var testItemID string
|
||||
resource.ParallelTest(
|
||||
t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
testInstances = append(testInstances, exData.TfName)
|
||||
},
|
||||
CheckDestroy: testAccCheckPostgresFlexDestroy,
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
{
|
||||
PreConfig: func() {
|
||||
testItemID = testutils.ResStr(pfx, "instance", exData.TfName)
|
||||
},
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
exData,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(
|
||||
testutils.ResStr(pfx, "instance", exData.TfName),
|
||||
"name",
|
||||
exData.Name,
|
||||
),
|
||||
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", exData.TfName), "id"),
|
||||
// check params are set
|
||||
resource.TestCheckResourceAttrSet(testItemID, "id"),
|
||||
|
||||
// check param values
|
||||
resource.TestCheckResourceAttr(testItemID, "name", exData.Name),
|
||||
),
|
||||
},
|
||||
// Update name and verify
|
||||
|
|
@ -254,6 +217,20 @@ func TestAccInstance(t *testing.T) {
|
|||
),
|
||||
),
|
||||
},
|
||||
// Update backup schedule
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
updBackupSched,
|
||||
),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(
|
||||
testutils.ResStr(pfx, "instance", exData.TfName),
|
||||
"backup_schedule",
|
||||
updBackupSched.BackupSchedule,
|
||||
),
|
||||
),
|
||||
},
|
||||
//// Import test
|
||||
//{
|
||||
// ResourceName: "example_resource.test",
|
||||
|
|
@ -284,6 +261,7 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
|||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
testInstances = append(testInstances, data.TfName)
|
||||
},
|
||||
CheckDestroy: testAccCheckPostgresFlexDestroy,
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -336,6 +314,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
|
|||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
testInstances = append(testInstances, data.TfName)
|
||||
},
|
||||
CheckDestroy: testAccCheckPostgresFlexDestroy,
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -1015,3 +994,80 @@ func TestAccInstanceWithDatabases(t *testing.T) {
|
|||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func testAccCheckPostgresFlexDestroy(s *terraform.State) error {
|
||||
ctx := context.Background()
|
||||
var client *postgresflexalphaPkgGen.APIClient
|
||||
var err error
|
||||
|
||||
var region, projectId string
|
||||
region = testutils.Region
|
||||
if region == "" {
|
||||
region = "eu01"
|
||||
}
|
||||
|
||||
projectId = testutils.ProjectId
|
||||
if projectId == "" {
|
||||
return fmt.Errorf("projectID could not be determined in destroy function")
|
||||
}
|
||||
|
||||
apiClientConfigOptions := []config.ConfigurationOption{
|
||||
config.WithServiceAccountKeyPath(os.Getenv("TF_ACC_SERVICE_ACCOUNT_FILE")),
|
||||
config.WithRegion(region),
|
||||
}
|
||||
if testutils.PostgresFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(testutils.PostgresFlexCustomEndpoint),
|
||||
)
|
||||
}
|
||||
client, err = postgresflexalphaPkgGen.NewAPIClient(apiClientConfigOptions...)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
instancesToDestroy := []string{}
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "stackitprivatepreview_postgresflexalpha_instance" &&
|
||||
rs.Type != "stackitprivatepreview_postgresflexbeta_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, projectId, region).
|
||||
Size(100).
|
||||
Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting instancesResp: %w", err)
|
||||
}
|
||||
|
||||
items := instancesResp.GetInstances()
|
||||
for i := range items {
|
||||
if items[i].Id == nil {
|
||||
continue
|
||||
}
|
||||
if utils.Contains(instancesToDestroy, *items[i].Id) {
|
||||
err := client.DeleteInstanceRequestExecute(ctx, testutils.ProjectId, region, *items[i].Id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleting instance %s during CheckDestroy: %w", *items[i].Id, err)
|
||||
}
|
||||
err = postgresflexalpha.DeleteInstanceWaitHandler(
|
||||
ctx,
|
||||
client,
|
||||
testutils.ProjectId,
|
||||
testutils.Region,
|
||||
*items[i].Id,
|
||||
15*time.Minute,
|
||||
10*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleting instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ const (
|
|||
InstanceStateTerminating = "TERMINATING"
|
||||
InstanceStateUnknown = "UNKNOWN"
|
||||
InstanceStatePending = "PENDING"
|
||||
InstanceStateDeleted = "DELETED"
|
||||
)
|
||||
|
||||
// APIClientInstanceInterface Interface needed for tests
|
||||
|
|
@ -296,3 +297,44 @@ func GetDatabaseByIdWaitHandler(
|
|||
)
|
||||
return handler
|
||||
}
|
||||
|
||||
func DeleteInstanceWaitHandler(
|
||||
ctx context.Context,
|
||||
a APIClientInstanceInterface,
|
||||
projectId,
|
||||
region,
|
||||
instanceId string,
|
||||
timeout, sleepBeforeWait time.Duration,
|
||||
) error {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *postgresflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) // nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if !ok {
|
||||
return false, nil, fmt.Errorf("received error is no oapierror: %w", err)
|
||||
}
|
||||
if oapiErr.StatusCode == 404 {
|
||||
return true, nil, nil
|
||||
}
|
||||
return false, nil, fmt.Errorf("api returned error: %w", err)
|
||||
}
|
||||
switch *s.Status {
|
||||
case InstanceStateDeleted:
|
||||
return true, nil, nil
|
||||
case InstanceStateEmpty, InstanceStatePending, InstanceStateUnknown, InstanceStateProgressing, InstanceStateSuccess:
|
||||
return false, nil, nil
|
||||
case InstanceStateFailed:
|
||||
return true, nil, fmt.Errorf("wait handler got status FAILURE for instance: %s", instanceId)
|
||||
default:
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Status)
|
||||
}
|
||||
},
|
||||
).SetTimeout(timeout).SetSleepBeforeWait(sleepBeforeWait)
|
||||
|
||||
_, err := handler.WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue