feat: refactor testing

This commit is contained in:
Marcel S. Henselin 2026-02-05 18:08:51 +01:00
parent c22e758b2c
commit 25c6b468b1
10 changed files with 328 additions and 2318 deletions

View file

@ -6,12 +6,15 @@ import (
"context"
_ "embed"
"fmt"
"log/slog"
"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"
"github.com/stackitcloud/stackit-sdk-go/core/config"
@ -26,13 +29,31 @@ var (
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)
}
// 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",
@ -41,75 +62,96 @@ var instanceResource = map[string]string{
"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": instanceResource["project_id"],
"project_id": testutil.ProjectId,
}
// Database resource data
var databaseResource = map[string]string{
"name": fmt.Sprintf("tfaccdb%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)),
"name": fmt.Sprintf("tfaccdb%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)),
"project_id": testutil.ProjectId,
}
func configResources(backupSchedule string, region *string) string {
var regionConfig string
if region != nil {
regionConfig = fmt.Sprintf(`region = %q`, *region)
}
func configResources(backupSchedule string, _ *string) string {
return fmt.Sprintf(
`
%s
resource "stackit_postgresflex_instance" "instance" {
project_id = "%s"
name = "%s"
acl = ["%s"]
backup_schedule = "%s"
flavor = {
cpu = %s
ram = %s
}
replicas = %s
storage = {
class = "%s"
size = %s
}
version = "%s"
%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 "stackit_postgresflex_user" "user" {
project_id = stackit_postgresflex_instance.instance.project_id
instance_id = stackit_postgresflex_instance.instance.instance_id
resource "stackitprivatepreview_postgresflexalpha_user" "user" {
project_id = "%s"
instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id
username = "%s"
roles = ["%s"]
}
resource "stackit_postgresflex_database" "database" {
project_id = stackit_postgresflex_instance.instance.project_id
instance_id = stackit_postgresflex_instance.instance.instance_id
resource "stackitprivatepreview_postgresflexalpha_database" "database" {
project_id = "%s"
instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id
name = "%s"
owner = stackit_postgresflex_user.user.username
owner = stackitprivatepreview_postgresflexalpha_user.user.username
}
`,
testutil.PostgresFlexProviderConfig(),
testutil.PostgresFlexProviderConfig(
utils.GetEnvOrDefault("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_FILE", "~/service-account.json"),
),
instanceResource["project_id"],
instanceResource["region"],
instanceResource["name"],
instanceResource["acl"],
backupSchedule,
instanceResource["flavor_cpu"],
instanceResource["flavor_ram"],
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"],
regionConfig,
userResource["project_id"],
userResource["username"],
userResource["role"],
databaseResource["project_id"],
databaseResource["name"],
)
}
@ -126,107 +168,107 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"project_id",
instanceResource["project_id"],
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"instance_id",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"name",
instanceResource["name"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.#",
"1",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.0",
instanceResource["acl"],
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.id",
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.description",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"backup_schedule",
instanceResource["backup_schedule"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.cpu",
instanceResource["flavor_cpu"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.ram",
instanceResource["flavor_ram"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"replicas",
instanceResource["replicas"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"storage.class",
instanceResource["storage_class"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"storage.size",
instanceResource["storage_size"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"version",
instanceResource["version"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"region",
testutil.Region,
),
// User
resource.TestCheckResourceAttrPair(
"stackit_postgresflex_user.user", "project_id",
"stackit_postgresflex_instance.instance", "project_id",
"stackitprivatepreview_postgresflexalpha_user.user", "project_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_postgresflex_user.user", "instance_id",
"stackit_postgresflex_instance.instance", "instance_id",
"stackitprivatepreview_postgresflexalpha_user.user", "instance_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_postgresflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_postgresflex_user.user", "password"),
resource.TestCheckResourceAttrSet("stackitprivatepreview_postgresflexalpha_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackitprivatepreview_postgresflexalpha_user.user", "password"),
// Database
resource.TestCheckResourceAttrPair(
"stackit_postgresflex_database.database", "project_id",
"stackit_postgresflex_instance.instance", "project_id",
"stackitprivatepreview_postgresflexalpha_database.database", "project_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_postgresflex_database.database", "instance_id",
"stackit_postgresflex_instance.instance", "instance_id",
"stackitprivatepreview_postgresflexalpha_database.database", "instance_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_database.database",
"stackitprivatepreview_postgresflexalpha_database.database",
"name",
databaseResource["name"],
),
resource.TestCheckResourceAttrPair(
"stackit_postgresflex_database.database", "owner",
"stackit_postgresflex_user.user", "username",
"stackitprivatepreview_postgresflexalpha_database.database", "owner",
"stackitprivatepreview_postgresflexalpha_user.user", "username",
),
),
},
@ -236,21 +278,21 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
`
%s
data "stackit_postgresflex_instance" "instance" {
project_id = stackit_postgresflex_instance.instance.project_id
instance_id = stackit_postgresflex_instance.instance.instance_id
data "stackitprivatepreview_postgresflexalpha_instance" "instance" {
project_id = stackitprivatepreview_postgresflexalpha_instance.instance.project_id
instance_id = stackitprivatepreview_postgresflexalpha_instance.instance.instance_id
}
data "stackit_postgresflex_user" "user" {
project_id = stackit_postgresflex_instance.instance.project_id
instance_id = stackit_postgresflex_instance.instance.instance_id
user_id = stackit_postgresflex_user.user.user_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 "stackit_postgresflex_database" "database" {
project_id = stackit_postgresflex_instance.instance.project_id
instance_id = stackit_postgresflex_instance.instance.instance_id
database_id = stackit_postgresflex_database.database.database_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),
@ -258,135 +300,135 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"project_id",
instanceResource["project_id"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"name",
instanceResource["name"],
),
resource.TestCheckResourceAttrPair(
"data.stackit_postgresflex_instance.instance", "project_id",
"stackit_postgresflex_instance.instance", "project_id",
"data.stackitprivatepreview_postgresflexalpha_instance.instance", "project_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_postgresflex_instance.instance", "instance_id",
"stackit_postgresflex_instance.instance", "instance_id",
"data.stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id",
"stackitprivatepreview_postgresflexalpha_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_postgresflex_user.user", "instance_id",
"stackit_postgresflex_user.user", "instance_id",
"data.stackitprivatepreview_postgresflexalpha_user.user", "instance_id",
"stackitprivatepreview_postgresflexalpha_user.user", "instance_id",
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.#",
"1",
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.0",
instanceResource["acl"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"backup_schedule",
instanceResource["backup_schedule"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.id",
instanceResource["flavor_id"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.description",
instanceResource["flavor_description"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.cpu",
instanceResource["flavor_cpu"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.ram",
instanceResource["flavor_ram"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_instance.instance",
"data.stackitprivatepreview_postgresflexalpha_instance.instance",
"replicas",
instanceResource["replicas"],
),
// User data
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"project_id",
userResource["project_id"],
),
resource.TestCheckResourceAttrSet(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"user_id",
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"username",
userResource["username"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"roles.#",
"1",
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"roles.0",
userResource["role"],
),
resource.TestCheckResourceAttrSet(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"host",
),
resource.TestCheckResourceAttrSet(
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"port",
),
// Database data
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_database.database",
"data.stackitprivatepreview_postgresflexalpha_database.database",
"project_id",
instanceResource["project_id"],
),
resource.TestCheckResourceAttr(
"data.stackit_postgresflex_database.database",
"data.stackitprivatepreview_postgresflexalpha_database.database",
"name",
databaseResource["name"],
),
resource.TestCheckResourceAttrPair(
"data.stackit_postgresflex_database.database",
"data.stackitprivatepreview_postgresflexalpha_database.database",
"instance_id",
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"instance_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_postgresflex_database.database",
"data.stackitprivatepreview_postgresflexalpha_database.database",
"owner",
"data.stackit_postgresflex_user.user",
"data.stackitprivatepreview_postgresflexalpha_user.user",
"username",
),
),
},
// Import
{
ResourceName: "stackit_postgresflex_instance.instance",
ResourceName: "stackitprivatepreview_postgresflexalpha_instance.instance",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_postgresflex_instance.instance"]
r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_instance.instance"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_postgresflex_instance.instance")
return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_instance.instance")
}
instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok {
@ -400,11 +442,11 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
ImportStateVerifyIgnore: []string{"password"},
},
{
ResourceName: "stackit_postgresflex_user.user",
ResourceName: "stackitprivatepreview_postgresflexalpha_user.user",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_postgresflex_user.user"]
r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_user.user"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_postgresflex_user.user")
return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_user.user")
}
instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok {
@ -422,11 +464,11 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
ImportStateVerifyIgnore: []string{"password", "uri"},
},
{
ResourceName: "stackit_postgresflex_database.database",
ResourceName: "stackitprivatepreview_postgresflexalpha_database.database",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_postgresflex_database.database"]
r, ok := s.RootModule().Resources["stackitprivatepreview_postgresflexalpha_database.database"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_postgresflex_database.database")
return "", fmt.Errorf("couldn't find resource stackitprivatepreview_postgresflexalpha_database.database")
}
instanceId, ok := r.Primary.Attributes["instance_id"]
if !ok {
@ -454,69 +496,69 @@ func TestAccPostgresFlexFlexResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"project_id",
instanceResource["project_id"],
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"instance_id",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"name",
instanceResource["name"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.#",
"1",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"acl.0",
instanceResource["acl"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"backup_schedule",
instanceResource["backup_schedule_updated"],
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.id",
),
resource.TestCheckResourceAttrSet(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.description",
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.cpu",
instanceResource["flavor_cpu"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"flavor.ram",
instanceResource["flavor_ram"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"replicas",
instanceResource["replicas"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"storage.class",
instanceResource["storage_class"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"storage.size",
instanceResource["storage_size"],
),
resource.TestCheckResourceAttr(
"stackit_postgresflex_instance.instance",
"stackitprivatepreview_postgresflexalpha_instance.instance",
"version",
instanceResource["version"],
),
@ -545,7 +587,7 @@ func testAccCheckPostgresFlexDestroy(s *terraform.State) error {
instancesToDestroy := []string{}
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_postgresflex_instance" {
if rs.Type != "stackitprivatepreview_postgresflexalpha_instance" {
continue
}
// instance terraform ID: = "[project_id],[region],[instance_id]"

View file

@ -1 +1,24 @@
resource "stackitprivatepreview_postgresflexalpha_instance" "msh-instance-only" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance"
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
backup_schedule = "0 0 * * *"
retention_days = 30
flavor_id = "flavor.id"
replicas = 1
storage = {
performance_class = "premium-perf2-stackit"
size = 10
}
encryption = {
kek_key_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
kek_key_ring_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
kek_key_version = 1
service_account = "service@account.email"
}
network = {
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
access_scope = "PUBLIC"
}
version = 17
}

View file

@ -1,118 +0,0 @@
package sqlserverflexbeta
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
"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/utils"
sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
sqlserverflexbetaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/flavors/datasources_gen"
)
var _ datasource.DataSource = (*flavorsDataSource)(nil)
const errorPrefix = "[Sqlserverflexbeta - Flavors]"
func NewFlavorsDataSource() datasource.DataSource {
return &flavorsDataSource{}
}
type flavorsDataSource struct {
client *sqlserverflexbetaPkg.APIClient
providerData core.ProviderData
}
func (d *flavorsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_flavors"
}
func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = sqlserverflexbetaGen.FlavorsDataSourceSchema(ctx)
}
// Configure adds the provider configured client to the data source.
func (d *flavorsDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
var ok bool
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
apiClient := sqlserverflexbetaUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix))
}
func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data sqlserverflexbetaGen.FlavorsModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString()
region := d.providerData.GetRegionWithOverride(data.Region)
flavorsId := data.FlavorsId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "flavors_id", flavorsId)
flavorsResp, err := d.client.GetFlavorsRequest(ctx, projectId, region, flavorsId).Execute()
if err != nil {
utils.LogError(
ctx,
&resp.Diagnostics,
err,
"Reading flavors",
fmt.Sprintf("flavors with ID %q does not exist in project %q.", flavorsId, projectId),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
},
)
resp.State.RemoveResource(ctx)
return
}
ctx = core.LogResponse(ctx)
// Todo: Read API call logic
// Example data value setting
// data.Id = types.StringValue("example-id")
err = mapResponseToModel(ctx, flavorsResp, &data, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
fmt.Sprintf("%s Read", errorPrefix),
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

View file

@ -7,13 +7,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
"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/utils"
sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
sqlserverflexbetaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
// sqlserverflexbetaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/user/datasources_gen"
)
@ -45,17 +44,17 @@ func (d *userDataSource) Configure(
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
var ok bool
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
apiClient := sqlserverflexbetaUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient
//var ok bool
//d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
//if !ok {
// return
//}
//
//apiClient := sqlserverflexbetaUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
//if resp.Diagnostics.HasError() {
// return
//}
//d.client = apiClient
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix))
}

View file

@ -29,7 +29,7 @@ var (
// CLI command executed to create a provider server to which the CLI can
// reattach.
TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"stackit": providerserver.NewProtocol6WithError(stackit.New("test-version")()),
"stackitprivatepreview": providerserver.NewProtocol6WithError(stackit.New("test-version")()),
}
// TestEphemeralAccProtoV6ProviderFactories is used to instantiate a provider during
@ -40,8 +40,8 @@ var (
// See the Terraform acceptance test documentation on ephemeral resources for more information:
// https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/ephemeral-resources
TestEphemeralAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"stackit": providerserver.NewProtocol6WithError(stackit.New("test-version")()),
"echo": echoprovider.NewProviderServer(),
"stackitprivatepreview": providerserver.NewProtocol6WithError(stackit.New("test-version")()),
"echo": echoprovider.NewProviderServer(),
}
// E2ETestsEnabled checks if end-to-end tests should be run.
@ -97,12 +97,12 @@ var (
func ObservabilityProviderConfig() string {
if ObservabilityCustomEndpoint == "" {
return `provider "stackit" {
return `provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
observability_custom_endpoint = "%s"
}`,
ObservabilityCustomEndpoint,
@ -111,12 +111,12 @@ func ObservabilityProviderConfig() string {
func CdnProviderConfig() string {
if CdnCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
enable_beta_resources = true
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
cdn_custom_endpoint = "%s"
enable_beta_resources = true
}`,
@ -126,10 +126,10 @@ func CdnProviderConfig() string {
func DnsProviderConfig() string {
if DnsCustomEndpoint == "" {
return `provider "stackit" {}`
return `provider "stackitprivatepreview" {}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
dns_custom_endpoint = "%s"
}`,
DnsCustomEndpoint,
@ -139,12 +139,12 @@ func DnsProviderConfig() string {
func IaaSProviderConfig() string {
if IaaSCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
iaas_custom_endpoint = "%s"
}`,
IaaSCustomEndpoint,
@ -154,13 +154,13 @@ func IaaSProviderConfig() string {
func IaaSProviderConfigWithBetaResourcesEnabled() string {
if IaaSCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
enable_beta_resources = true
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
enable_beta_resources = true
iaas_custom_endpoint = "%s"
}`,
@ -171,13 +171,13 @@ func IaaSProviderConfigWithBetaResourcesEnabled() string {
func IaaSProviderConfigWithExperiments() string {
if IaaSCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
experiments = [ "routing-tables", "network" ]
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
iaas_custom_endpoint = "%s"
experiments = [ "routing-tables", "network" ]
}`,
@ -188,12 +188,12 @@ func IaaSProviderConfigWithExperiments() string {
func KMSProviderConfig() string {
if KMSCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
kms_custom_endpoint = "%s"
}`,
KMSCustomEndpoint,
@ -203,12 +203,12 @@ func KMSProviderConfig() string {
func LoadBalancerProviderConfig() string {
if LoadBalancerCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
loadbalancer_custom_endpoint = "%s"
}`,
LoadBalancerCustomEndpoint,
@ -218,12 +218,12 @@ func LoadBalancerProviderConfig() string {
func LogMeProviderConfig() string {
if LogMeCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
logme_custom_endpoint = "%s"
}`,
LogMeCustomEndpoint,
@ -233,12 +233,12 @@ func LogMeProviderConfig() string {
func MariaDBProviderConfig() string {
if MariaDBCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
mariadb_custom_endpoint = "%s"
}`,
MariaDBCustomEndpoint,
@ -248,13 +248,13 @@ func MariaDBProviderConfig() string {
func ModelServingProviderConfig() string {
if ModelServingCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}
`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
modelserving_custom_endpoint = "%s"
}`,
ModelServingCustomEndpoint,
@ -264,12 +264,12 @@ func ModelServingProviderConfig() string {
func MongoDBFlexProviderConfig() string {
if MongoDBFlexCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
mongodbflex_custom_endpoint = "%s"
}`,
MongoDBFlexCustomEndpoint,
@ -279,12 +279,12 @@ func MongoDBFlexProviderConfig() string {
func ObjectStorageProviderConfig() string {
if ObjectStorageCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
objectstorage_custom_endpoint = "%s"
}`,
ObjectStorageCustomEndpoint,
@ -294,27 +294,28 @@ func ObjectStorageProviderConfig() string {
func OpenSearchProviderConfig() string {
if OpenSearchCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
opensearch_custom_endpoint = "%s"
}`,
OpenSearchCustomEndpoint,
)
}
func PostgresFlexProviderConfig() string {
func PostgresFlexProviderConfig(saFile string) string {
if PostgresFlexCustomEndpoint == "" {
return `
provider "stackit" {
return fmt.Sprintf(`
provider "stackitprivatepreview" {
default_region = "eu01"
}`
service_account_key_path = "%s"
}`, saFile)
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
postgresflex_custom_endpoint = "%s"
}`,
PostgresFlexCustomEndpoint,
@ -324,12 +325,12 @@ func PostgresFlexProviderConfig() string {
func RabbitMQProviderConfig() string {
if RabbitMQCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
rabbitmq_custom_endpoint = "%s"
}`,
RabbitMQCustomEndpoint,
@ -339,12 +340,12 @@ func RabbitMQProviderConfig() string {
func RedisProviderConfig() string {
if RedisCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
redis_custom_endpoint = "%s"
}`,
RedisCustomEndpoint,
@ -355,14 +356,14 @@ func ResourceManagerProviderConfig() string {
token := GetTestProjectServiceAccountToken("")
if ResourceManagerCustomEndpoint == "" || AuthorizationCustomEndpoint == "" {
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
service_account_token = "%s"
}`,
token,
)
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
resourcemanager_custom_endpoint = "%s"
authorization_custom_endpoint = "%s"
service_account_token = "%s"
@ -376,12 +377,12 @@ func ResourceManagerProviderConfig() string {
func SecretsManagerProviderConfig() string {
if SecretsManagerCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
secretsmanager_custom_endpoint = "%s"
}`,
SecretsManagerCustomEndpoint,
@ -391,12 +392,12 @@ func SecretsManagerProviderConfig() string {
func SQLServerFlexProviderConfig() string {
if SQLServerFlexCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
sqlserverflex_custom_endpoint = "%s"
}`,
SQLServerFlexCustomEndpoint,
@ -406,13 +407,13 @@ func SQLServerFlexProviderConfig() string {
func ServerBackupProviderConfig() string {
if ServerBackupCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
enable_beta_resources = true
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
server_backup_custom_endpoint = "%s"
enable_beta_resources = true
}`,
@ -423,13 +424,13 @@ func ServerBackupProviderConfig() string {
func ServerUpdateProviderConfig() string {
if ServerUpdateCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
enable_beta_resources = true
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
server_update_custom_endpoint = "%s"
enable_beta_resources = true
}`,
@ -440,12 +441,12 @@ func ServerUpdateProviderConfig() string {
func SKEProviderConfig() string {
if SKECustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
ske_custom_endpoint = "%s"
}`,
SKECustomEndpoint,
@ -455,13 +456,13 @@ func SKEProviderConfig() string {
func AuthorizationProviderConfig() string {
if AuthorizationCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
experiments = ["iam"]
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
authorization_custom_endpoint = "%s"
experiments = ["iam"]
}`,
@ -472,13 +473,13 @@ func AuthorizationProviderConfig() string {
func ServiceAccountProviderConfig() string {
if ServiceAccountCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
enable_beta_resources = true
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
service_account_custom_endpoint = "%s"
enable_beta_resources = true
}`,
@ -489,13 +490,13 @@ func ServiceAccountProviderConfig() string {
func GitProviderConfig() string {
if GitCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
enable_beta_resources = true
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
git_custom_endpoint = "%s"
enable_beta_resources = true
}`,
@ -506,12 +507,12 @@ func GitProviderConfig() string {
func ScfProviderConfig() string {
if ScfCustomEndpoint == "" {
return `
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
}`
}
return fmt.Sprintf(`
provider "stackit" {
provider "stackitprivatepreview" {
default_region = "eu01"
scf_custom_endpoint = "%s"
}`,
@ -526,6 +527,18 @@ func ResourceNameWithDateTime(name string) string {
return fmt.Sprintf("tf-acc-%s-%s", name, dateTimeTrimmed)
}
func GetTestProjectServiceAccountJson(path string) string {
var err error
token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_JSON")
if !tokenSet || token == "" {
token, err = readTestTokenFromCredentialsFile(path)
if err != nil {
return ""
}
}
return token
}
func GetTestProjectServiceAccountToken(path string) string {
var err error
token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN")

View file

@ -5,12 +5,15 @@ package stackit_test
import (
_ "embed"
"fmt"
"log/slog"
"os"
"path"
"regexp"
"runtime"
"testing"
"github.com/joho/godotenv"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@ -31,6 +34,22 @@ var testConfigProviderCredentials = config.Variables{
"name": config.StringVariable(fmt.Sprintf("tf-acc-prov%s", acctest.RandStringFromCharSet(3, acctest.CharSetAlphaNum))),
}
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)
}
// Helper function to obtain the home directory on different systems.
// Based on os.UserHomeDir().
func getHomeEnvVariableName() string {