Compare commits

..

No commits in common. "alpha" and "v0.0.14-alpha" have entirely different histories.

109 changed files with 5127 additions and 7173 deletions

View file

@ -22,39 +22,6 @@ env:
CODE_COVERAGE_ARTIFACT_NAME: "code-coverage" CODE_COVERAGE_ARTIFACT_NAME: "code-coverage"
jobs: jobs:
runner_test:
name: "Test STACKIT runner"
runs-on: stackit-docker
steps:
- name: Install needed tools
run: |
apt-get -y -qq update
apt-get -y -qq install jq python3 python3-pip python-is-python3 s3cmd git make wget
- name: Setup Go
uses: actions/setup-go@v6
with:
go-version: ${{ env.GO_VERSION }}
- name: Install go tools
run: |
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest
go install github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest
- name: Setup JAVA
uses: actions/setup-java@v5
with:
distribution: 'temurin' # See 'Supported distributions' for available options
java-version: '21'
- name: Checkout
uses: actions/checkout@v6
- name: Run build pkg directory
run: |
go run cmd/main.go build
publish_test: publish_test:
name: "Test readiness for publishing provider" name: "Test readiness for publishing provider"
needs: config needs: config

View file

@ -1,29 +0,0 @@
name: Runner stats
on:
workflow_dispatch:
jobs:
stats-own:
name: "Get own runner stats"
runs-on: ubuntu-latest
steps:
- name: Install needed tools
run: |
apt-get -y -qq update
apt-get -y -qq install inxi
- name: Show stats
run: inxi -c 0
stats-stackit:
name: "Get STACKIT runner stats"
runs-on: stackit-docker
steps:
- name: Install needed tools
run: |
apt-get -y -qq update
apt-get -y -qq install inxi
- name: Show stats
run: inxi -c 0

2
.gitignore vendored
View file

@ -46,5 +46,3 @@ dist
pkg_gen pkg_gen
/release/ /release/
.env
**/.env

View file

@ -12,10 +12,9 @@ project-tools:
# LINT # LINT
lint-golangci-lint: lint-golangci-lint:
@echo "Linting with golangci-lint" @echo "Linting with golangci-lint"
@go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint run --fix --config golang-ci.yaml @$(SCRIPTS_BASE)/lint-golangci-lint.sh
lint-tf:
lint-tf:
@echo "Linting terraform files" @echo "Linting terraform files"
@terraform fmt -check -diff -recursive @terraform fmt -check -diff -recursive
@ -24,7 +23,6 @@ lint: lint-golangci-lint lint-tf
# DOCUMENTATION GENERATION # DOCUMENTATION GENERATION
generate-docs: generate-docs:
@echo "Generating documentation with tfplugindocs" @echo "Generating documentation with tfplugindocs"
@$(SCRIPTS_BASE)/tfplugindocs.sh @$(SCRIPTS_BASE)/tfplugindocs.sh
build: build:

View file

@ -60,7 +60,7 @@ func (b *Builder) Build() error {
if !b.PackagesOnly { if !b.PackagesOnly {
slog.Info(" ... Checking needed commands available") slog.Info(" ... Checking needed commands available")
err := checkCommands([]string{}) err := checkCommands([]string{"tfplugingen-framework", "tfplugingen-openapi"})
if err != nil { if err != nil {
return err return err
} }
@ -111,7 +111,7 @@ func (b *Builder) Build() error {
} }
slog.Info("Creating OAS dir") slog.Info("Creating OAS dir")
err = os.MkdirAll(path.Join(genDir, "oas"), 0o755) //nolint:gosec // this dir is not sensitive, so we can use 0755 err = os.MkdirAll(path.Join(genDir, "oas"), 0755)
if err != nil { if err != nil {
return err return err
} }
@ -158,17 +158,7 @@ func (b *Builder) Build() error {
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
slog.Error( slog.Error("cmd.Wait", "code", exitErr.ExitCode(), "error", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"cmd.Wait",
"code",
exitErr.ExitCode(),
"error",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return fmt.Errorf("%s", stdErr.String()) return fmt.Errorf("%s", stdErr.String())
} }
if err != nil { if err != nil {
@ -202,11 +192,7 @@ func (b *Builder) Build() error {
} }
slog.Info("Rearranging package directories") slog.Info("Rearranging package directories")
//nolint:gosec // this dir is not sensitive, so we can use 0755 err = os.MkdirAll(path.Join(*root, "pkg_gen"), 0755) // noqa:gosec
err = os.MkdirAll(
path.Join(*root, "pkg_gen"),
0o755,
)
if err != nil { if err != nil {
return err return err
} }
@ -216,20 +202,19 @@ func (b *Builder) Build() error {
return err return err
} }
for _, item := range items { for _, item := range items {
if !item.IsDir() { if item.IsDir() {
continue slog.Info(" -> package", "name", item.Name())
} tgtDir := path.Join(*root, "pkg_gen", item.Name())
slog.Info(" -> package", "name", item.Name()) if fileExists(tgtDir) {
tgtDir := path.Join(*root, "pkg_gen", item.Name()) delErr := os.RemoveAll(tgtDir)
if fileExists(tgtDir) { if delErr != nil {
delErr := os.RemoveAll(tgtDir) return delErr
if delErr != nil { }
return delErr }
err = os.Rename(path.Join(srcDir, item.Name()), tgtDir)
if err != nil {
return err
} }
}
err = os.Rename(path.Join(srcDir, item.Name()), tgtDir)
if err != nil {
return err
} }
} }
@ -290,8 +275,8 @@ type templateData struct {
Fields []string Fields []string
} }
func fileExists(pathValue string) bool { func fileExists(path string) bool {
_, err := os.Stat(pathValue) _, err := os.Stat(path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false return false
} }
@ -327,22 +312,10 @@ func createBoilerplate(rootFolder, folder string) error {
resourceName := res.Name() resourceName := res.Name()
dsFile := path.Join( dsFile := path.Join(folder, svc.Name(), res.Name(), "datasources_gen", fmt.Sprintf("%s_data_source_gen.go", res.Name()))
folder,
svc.Name(),
res.Name(),
"datasources_gen",
fmt.Sprintf("%s_data_source_gen.go", res.Name()),
)
handleDS = fileExists(dsFile) handleDS = fileExists(dsFile)
resFile := path.Join( resFile := path.Join(folder, svc.Name(), res.Name(), "resources_gen", fmt.Sprintf("%s_resource_gen.go", res.Name()))
folder,
svc.Name(),
res.Name(),
"resources_gen",
fmt.Sprintf("%s_resource_gen.go", res.Name()),
)
handleRes = fileExists(resFile) handleRes = fileExists(resFile)
dsGoFile := path.Join(folder, svc.Name(), res.Name(), "datasource.go") dsGoFile := path.Join(folder, svc.Name(), res.Name(), "datasource.go")
@ -434,6 +407,7 @@ func createBoilerplate(rootFolder, folder string) error {
if err != nil { if err != nil {
return err return err
} }
} }
} }
} }
@ -442,7 +416,7 @@ func createBoilerplate(rootFolder, folder string) error {
} }
func ucfirst(s string) string { func ucfirst(s string) string {
if s == "" { if len(s) == 0 {
return "" return ""
} }
return strings.ToUpper(s[:1]) + s[1:] return strings.ToUpper(s[:1]) + s[1:]
@ -477,8 +451,8 @@ func writeTemplateToFile(tplName, tplFile, outFile string, data *templateData) e
} }
func generateServiceFiles(rootDir, generatorDir string) error { func generateServiceFiles(rootDir, generatorDir string) error {
//nolint:gosec // this file is not sensitive, so we can use 0755 // slog.Info("Generating specs folder")
err := os.MkdirAll(path.Join(rootDir, "generated", "specs"), 0o755) err := os.MkdirAll(path.Join(rootDir, "generated", "specs"), 0755)
if err != nil { if err != nil {
return err return err
} }
@ -516,6 +490,7 @@ func generateServiceFiles(rootDir, generatorDir string) error {
continue continue
} }
// slog.Info("Checking spec", "name", spec.Name())
r := regexp.MustCompile(`^(.*)_config.yml$`) r := regexp.MustCompile(`^(.*)_config.yml$`)
matches := r.FindAllStringSubmatch(specFile.Name(), -1) matches := r.FindAllStringSubmatch(specFile.Name(), -1)
if matches != nil { if matches != nil {
@ -531,44 +506,27 @@ func generateServiceFiles(rootDir, generatorDir string) error {
resource, resource,
) )
oasFile := path.Join( oasFile := path.Join(generatorDir, "oas", fmt.Sprintf("%s%s.json", service.Name(), svcVersion.Name()))
generatorDir,
"oas",
fmt.Sprintf("%s%s.json", service.Name(), svcVersion.Name()),
)
if _, oasErr := os.Stat(oasFile); os.IsNotExist(oasErr) { if _, oasErr := os.Stat(oasFile); os.IsNotExist(oasErr) {
slog.Warn( slog.Warn(" could not find matching oas", "svc", service.Name(), "version", svcVersion.Name())
" could not find matching oas",
"svc",
service.Name(),
"version",
svcVersion.Name(),
)
continue continue
} }
scName := fmt.Sprintf("%s%s", service.Name(), svcVersion.Name()) scName := fmt.Sprintf("%s%s", service.Name(), svcVersion.Name())
scName = strings.ReplaceAll(scName, "-", "") scName = strings.ReplaceAll(scName, "-", "")
//nolint:gosec // this file is not sensitive, so we can use 0755 err = os.MkdirAll(path.Join(rootDir, "generated", "internal", "services", scName, resource), 0755)
err = os.MkdirAll(path.Join(rootDir, "generated", "internal", "services", scName, resource), 0o755)
if err != nil { if err != nil {
return err return err
} }
specJsonFile := path.Join( // slog.Info("Generating openapi spec json")
rootDir, specJsonFile := path.Join(rootDir, "generated", "specs", fmt.Sprintf("%s_%s_spec.json", scName, resource))
"generated",
"specs",
fmt.Sprintf("%s_%s_spec.json", scName, resource),
)
var stdOut, stdErr bytes.Buffer var stdOut, stdErr bytes.Buffer
// nolint:gosec // #nosec this command is not using any untrusted input, so we can ignore gosec warning // noqa:gosec
cmd := exec.Command( cmd := exec.Command(
"go", "tfplugingen-openapi",
"run",
"github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi",
"generate", "generate",
"--config", "--config",
path.Join(rootDir, "service_specs", service.Name(), svcVersion.Name(), fileName), path.Join(rootDir, "service_specs", service.Name(), svcVersion.Name(), fileName),
@ -595,29 +553,11 @@ func generateServiceFiles(rootDir, generatorDir string) error {
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
slog.Error( slog.Error("tfplugingen-openapi generate", "code", exitErr.ExitCode(), "error", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-openapi generate",
"code",
exitErr.ExitCode(),
"error",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return fmt.Errorf("%s", stdErr.String()) return fmt.Errorf("%s", stdErr.String())
} }
if err != nil { if err != nil {
slog.Error( slog.Error("tfplugingen-openapi generate", "err", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-openapi generate",
"err",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return err return err
} }
} }
@ -625,26 +565,18 @@ func generateServiceFiles(rootDir, generatorDir string) error {
slog.Warn(" command output", "stdout", stdOut.String(), "stderr", stdErr.String()) slog.Warn(" command output", "stdout", stdOut.String(), "stderr", stdErr.String())
} }
tgtFolder := path.Join( // slog.Info("Creating terraform svc resource files folder")
rootDir, tgtFolder := path.Join(rootDir, "generated", "internal", "services", scName, resource, "resources_gen")
"generated", err = os.MkdirAll(tgtFolder, 0755)
"internal",
"services",
scName,
resource,
"resources_gen",
)
//nolint:gosec // this file is not sensitive, so we can use 0755
err = os.MkdirAll(tgtFolder, 0o755)
if err != nil { if err != nil {
return err return err
} }
// nolint:gosec // #nosec this command is not using any untrusted input, so we can ignore gosec warning // slog.Info("Generating terraform svc resource files")
// noqa:gosec
cmd2 := exec.Command( cmd2 := exec.Command(
"go", "tfplugingen-framework",
"run",
"github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework",
"generate", "generate",
"resources", "resources",
"--input", "--input",
@ -665,53 +597,27 @@ func generateServiceFiles(rootDir, generatorDir string) error {
if err = cmd2.Wait(); err != nil { if err = cmd2.Wait(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
slog.Error( slog.Error("tfplugingen-framework generate resources", "code", exitErr.ExitCode(), "error", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-framework generate resources",
"code",
exitErr.ExitCode(),
"error",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return fmt.Errorf("%s", stdErr.String()) return fmt.Errorf("%s", stdErr.String())
} }
if err != nil { if err != nil {
slog.Error( slog.Error("tfplugingen-framework generate resources", "err", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-framework generate resources",
"err",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return err return err
} }
} }
tgtFolder = path.Join( // slog.Info("Creating terraform svc datasource files folder")
rootDir, tgtFolder = path.Join(rootDir, "generated", "internal", "services", scName, resource, "datasources_gen")
"generated", err = os.MkdirAll(tgtFolder, 0755)
"internal",
"services",
scName,
resource,
"datasources_gen",
)
//nolint:gosec // this directory is not sensitive, so we can use 0755
err = os.MkdirAll(tgtFolder, 0o755)
if err != nil { if err != nil {
return err return err
} }
// nolint:gosec // #nosec this command is not using any untrusted input, so we can ignore gosec warning // slog.Info("Generating terraform svc resource files")
// noqa:gosec
cmd3 := exec.Command( cmd3 := exec.Command(
"go", "tfplugingen-framework",
"run",
"github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework",
"generate", "generate",
"data-sources", "data-sources",
"--input", "--input",
@ -733,29 +639,11 @@ func generateServiceFiles(rootDir, generatorDir string) error {
if err = cmd3.Wait(); err != nil { if err = cmd3.Wait(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
slog.Error( slog.Error("tfplugingen-framework generate data-sources", "code", exitErr.ExitCode(), "error", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-framework generate data-sources",
"code",
exitErr.ExitCode(),
"error",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return fmt.Errorf("%s", stdErr.String()) return fmt.Errorf("%s", stdErr.String())
} }
if err != nil { if err != nil {
slog.Error( slog.Error("tfplugingen-framework generate data-sources", "err", err, "stdout", stdOut.String(), "stderr", stdErr.String())
"tfplugingen-framework generate data-sources",
"err",
err,
"stdout",
stdOut.String(),
"stderr",
stdErr.String(),
)
return err return err
} }
} }
@ -786,10 +674,10 @@ func handleTfTagForDatasourceFile(filePath, service, resource string) error {
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
root, err := getRoot() root, err := getRoot()
if err != nil { if err != nil {
//nolint:gocritic // in this case, we want to log the error and exit, as we cannot proceed without the root directory
log.Fatal(err) log.Fatal(err)
} }
@ -797,6 +685,7 @@ func handleTfTagForDatasourceFile(filePath, service, resource string) error {
if err != nil { if err != nil {
return err return err
} }
defer tmp.Close()
sc := bufio.NewScanner(f) sc := bufio.NewScanner(f)
for sc.Scan() { for sc.Scan() {
@ -820,7 +709,6 @@ func handleTfTagForDatasourceFile(filePath, service, resource string) error {
return err return err
} }
//nolint:gosec // path traversal is not a concern here
if err := os.Rename(tmp.Name(), filePath); err != nil { if err := os.Rename(tmp.Name(), filePath); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -885,23 +773,13 @@ func copyFile(src, dst string) (int64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func(source *os.File) { defer source.Close()
err := source.Close()
if err != nil {
slog.Error("copyFile", "err", err)
}
}(source)
destination, err := os.Create(dst) destination, err := os.Create(dst)
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func(destination *os.File) { defer destination.Close()
err := destination.Close()
if err != nil {
slog.Error("copyFile", "err", err)
}
}(destination)
nBytes, err := io.Copy(destination, source) nBytes, err := io.Copy(destination, source)
return nBytes, err return nBytes, err
} }
@ -912,8 +790,10 @@ func getOnlyLatest(m map[string]version) (map[string]version, error) {
item, ok := tmpMap[k] item, ok := tmpMap[k]
if !ok { if !ok {
tmpMap[k] = v tmpMap[k] = v
} else if item.major == v.major && item.minor < v.minor { } else {
tmpMap[k] = v if item.major == v.major && item.minor < v.minor {
tmpMap[k] = v
}
} }
} }
return tmpMap, nil return tmpMap, nil
@ -927,19 +807,18 @@ func getVersions(dir string) (map[string]version, error) {
} }
for _, entry := range children { for _, entry := range children {
if !entry.IsDir() { if entry.IsDir() {
continue versions, err := os.ReadDir(path.Join(dir, "services", entry.Name()))
} if err != nil {
versions, err := os.ReadDir(path.Join(dir, "services", entry.Name())) return nil, err
if err != nil { }
return nil, err m, err2 := extractVersions(entry.Name(), versions)
} if err2 != nil {
m, err2 := extractVersions(entry.Name(), versions) return m, err2
if err2 != nil { }
return m, err2 for k, v := range m {
} res[k] = v
for k, v := range m { }
res[k] = v
} }
} }
return res, nil return res, nil
@ -948,21 +827,20 @@ func getVersions(dir string) (map[string]version, error) {
func extractVersions(service string, versionDirs []os.DirEntry) (map[string]version, error) { func extractVersions(service string, versionDirs []os.DirEntry) (map[string]version, error) {
res := make(map[string]version) res := make(map[string]version)
for _, vDir := range versionDirs { for _, vDir := range versionDirs {
if !vDir.IsDir() { if vDir.IsDir() {
continue r := regexp.MustCompile(`v([0-9]+)([a-z]+)([0-9]*)`)
} matches := r.FindAllStringSubmatch(vDir.Name(), -1)
r := regexp.MustCompile(`v(\d+)([a-z]+)(\d*)`) if matches == nil {
matches := r.FindAllStringSubmatch(vDir.Name(), -1) continue
if matches == nil { }
continue svc, ver, err := handleVersion(service, matches[0])
} if err != nil {
svc, ver, err := handleVersion(service, matches[0]) return nil, err
if err != nil { }
return nil, err
}
if svc != nil && ver != nil { if svc != nil && ver != nil {
res[*svc] = *ver res[*svc] = *ver
}
} }
} }
return res, nil return res, nil
@ -1049,25 +927,30 @@ func getTokens(fileName string) ([]string, error) {
return nil, err return nil, err
} }
ast.Inspect( ast.Inspect(node, func(n ast.Node) bool {
node, func(n ast.Node) bool { // Suche nach Typ-Deklarationen (structs)
// Suche nach Typ-Deklarationen (structs) ts, ok := n.(*ast.TypeSpec)
ts, ok := n.(*ast.TypeSpec) if ok {
if ok { if strings.Contains(ts.Name.Name, "Model") {
if strings.Contains(ts.Name.Name, "Model") { // fmt.Printf("found model: %s\n", ts.Name.Name)
ast.Inspect( ast.Inspect(ts, func(sn ast.Node) bool {
ts, func(sn ast.Node) bool { tts, tok := sn.(*ast.Field)
tts, tok := sn.(*ast.Field) if tok {
if tok { // fmt.Printf(" found: %+v\n", tts.Names[0])
result = append(result, tts.Names[0].String()) // spew.Dump(tts.Type)
}
return true result = append(result, tts.Names[0].String())
},
) // fld, fldOk := tts.Type.(*ast.Ident)
} //if fldOk {
// fmt.Printf("type: %+v\n", fld)
//}
}
return true
})
} }
return true }
}, return true
) })
return result, nil return result, nil
} }

View file

@ -3,7 +3,6 @@ package build
import ( import (
"fmt" "fmt"
"io" "io"
"log/slog"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -75,24 +74,14 @@ func Copy(srcFile, dstFile string) error {
return err return err
} }
defer func(out *os.File) { defer out.Close()
err := out.Close()
if err != nil {
slog.Error("failed to close file", slog.Any("err", err))
}
}(out)
in, err := os.Open(srcFile) in, err := os.Open(srcFile)
if err != nil { if err != nil {
return err return err
} }
defer func(in *os.File) { defer in.Close()
err := in.Close()
if err != nil {
slog.Error("error closing destination file", slog.Any("err", err))
}
}(in)
_, err = io.Copy(out, in) _, err = io.Copy(out, in)
if err != nil { if err != nil {

View file

@ -16,7 +16,7 @@ var buildCmd = &cobra.Command{
Use: "build", Use: "build",
Short: "Build the necessary boilerplate", Short: "Build the necessary boilerplate",
Long: `...`, Long: `...`,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, args []string) error {
b := build.Builder{ b := build.Builder{
SkipClone: skipClone, SkipClone: skipClone,
SkipCleanup: skipCleanup, SkipCleanup: skipCleanup,
@ -30,7 +30,7 @@ func NewBuildCmd() *cobra.Command {
return buildCmd return buildCmd
} }
func init() { //nolint:gochecknoinits // This is the standard way to set up Cobra commands func init() { // nolint: gochecknoinits
buildCmd.Flags().BoolVarP(&skipCleanup, "skip-clean", "c", false, "Skip cleanup steps") buildCmd.Flags().BoolVarP(&skipCleanup, "skip-clean", "c", false, "Skip cleanup steps")
buildCmd.Flags().BoolVarP(&skipClone, "skip-clone", "g", false, "Skip cloning from git") buildCmd.Flags().BoolVarP(&skipClone, "skip-clone", "g", false, "Skip cloning from git")
buildCmd.Flags().BoolVarP(&packagesOnly, "packages-only", "p", false, "Only generate packages") buildCmd.Flags().BoolVarP(&packagesOnly, "packages-only", "p", false, "Only generate packages")

View file

@ -1,114 +0,0 @@
package cmd
import (
"fmt"
"os"
"path"
"github.com/spf13/cobra"
)
var examplesCmd = &cobra.Command{
Use: "examples",
Short: "create examples",
Long: `...`,
RunE: func(_ *cobra.Command, _ []string) error {
// filePathStr := "stackit/internal/services/postgresflexalpha/database/datasources_gen/database_data_source_gen.go"
//
// src, err := os.ReadFile(filePathStr)
// if err != nil {
// return err
//}
//
// i := interp.New(
// interp.Options{
// GoPath: "/home/henselinm/.asdf/installs/golang/1.25.6/packages",
// BuildTags: nil,
// Stdin: nil,
// Stdout: nil,
// Stderr: nil,
// Args: nil,
// Env: nil,
// SourcecodeFilesystem: nil,
// Unrestricted: false,
// },
//)
// err = i.Use(i.Symbols("github.com/hashicorp/terraform-plugin-framework-validators"))
// if err != nil {
// return err
//}
// err = i.Use(stdlib.Symbols)
// if err != nil {
// return err
//}
// _, err = i.Eval(string(src))
// if err != nil {
// return err
//}
//
// v, err := i.Eval("DatabaseDataSourceSchema")
// if err != nil {
// return err
//}
//
// bar := v.Interface().(func(string) string)
//
// r := bar("Kung")
// println(r)
//
// evalPath, err := i.EvalPath(filePathStr)
// if err != nil {
// return err
//}
//
// fmt.Printf("%+v\n", evalPath)
// _, err = i.Eval(`import "fmt"`)
// if err != nil {
// return err
//}
// _, err = i.Eval(`func Hallo() { fmt.Println("Hi!") }`)
// if err != nil {
// return err
//}
// v = i.Symbols("Hallo")
// fmt.Println(v)
return workServices()
},
}
func workServices() error {
startPath := path.Join("stackit", "internal", "services")
services, err := os.ReadDir(startPath)
if err != nil {
return err
}
for _, entry := range services {
if !entry.IsDir() {
continue
}
resources, err := os.ReadDir(path.Join(startPath, entry.Name()))
if err != nil {
return err
}
for _, res := range resources {
if !res.IsDir() {
continue
}
fmt.Println("Gefunden:", startPath, "subdir", entry.Name(), "resource", res.Name())
}
}
return nil
}
func NewExamplesCmd() *cobra.Command {
return examplesCmd
}
// func init() { // nolint: gochecknoinits
// examplesCmd.Flags().BoolVarP(&example, "example", "e", false, "example")
//}

View file

@ -24,7 +24,7 @@ var getFieldsCmd = &cobra.Command{
Use: "get-fields", Use: "get-fields",
Short: "get fields from file", Short: "get fields from file",
Long: `...`, Long: `...`,
PreRunE: func(_ *cobra.Command, _ []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
typeStr := "data_source" typeStr := "data_source"
if resType != "resource" && resType != "datasource" { if resType != "resource" && resType != "datasource" {
return fmt.Errorf("--type can only be resource or datasource") return fmt.Errorf("--type can only be resource or datasource")
@ -76,13 +76,13 @@ var getFieldsCmd = &cobra.Command{
//// Enum check //// Enum check
// switch format { // switch format {
// case "json", "yaml": //case "json", "yaml":
//default: //default:
// return fmt.Errorf("invalid --format: %s (want json|yaml)", format) // return fmt.Errorf("invalid --format: %s (want json|yaml)", format)
//} //}
return nil return nil
}, },
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return getFields(filePath) return getFields(filePath)
}, },
} }
@ -107,26 +107,31 @@ func getTokens(fileName string) ([]string, error) {
return nil, err return nil, err
} }
ast.Inspect( ast.Inspect(node, func(n ast.Node) bool {
node, func(n ast.Node) bool { // Suche nach Typ-Deklarationen (structs)
// Suche nach Typ-Deklarationen (structs) ts, ok := n.(*ast.TypeSpec)
ts, ok := n.(*ast.TypeSpec) if ok {
if ok { if strings.Contains(ts.Name.Name, "Model") {
if strings.Contains(ts.Name.Name, "Model") { // fmt.Printf("found model: %s\n", ts.Name.Name)
ast.Inspect( ast.Inspect(ts, func(sn ast.Node) bool {
ts, func(sn ast.Node) bool { tts, tok := sn.(*ast.Field)
tts, tok := sn.(*ast.Field) if tok {
if tok { // fmt.Printf(" found: %+v\n", tts.Names[0])
result = append(result, tts.Names[0].String()) // spew.Dump(tts.Type)
}
return true result = append(result, tts.Names[0].String())
},
) // fld, fldOk := tts.Type.(*ast.Ident)
} //if fldOk {
// fmt.Printf("type: %+v\n", fld)
//}
}
return true
})
} }
return true }
}, return true
) })
return result, nil return result, nil
} }
@ -134,15 +139,9 @@ func NewGetFieldsCmd() *cobra.Command {
return getFieldsCmd return getFieldsCmd
} }
func init() { //nolint:gochecknoinits //this is the only way to add the command to the rootCmd func init() { // nolint: gochecknoinits
getFieldsCmd.Flags().StringVarP(&inFile, "infile", "i", "", "input filename incl path") getFieldsCmd.Flags().StringVarP(&inFile, "infile", "i", "", "input filename incl path")
getFieldsCmd.Flags().StringVarP(&svcName, "service", "s", "", "service name") getFieldsCmd.Flags().StringVarP(&svcName, "service", "s", "", "service name")
getFieldsCmd.Flags().StringVarP(&resName, "resource", "r", "", "resource name") getFieldsCmd.Flags().StringVarP(&resName, "resource", "r", "", "resource name")
getFieldsCmd.Flags().StringVarP( getFieldsCmd.Flags().StringVarP(&resType, "type", "t", "resource", "resource type (data-source or resource [default])")
&resType,
"type",
"t",
"resource",
"resource type (data-source or resource [default])",
)
} }

View file

@ -35,27 +35,36 @@ type GpgPublicKey struct {
} }
func (p *Provider) CreateArchitectureFiles() error { func (p *Provider) CreateArchitectureFiles() error {
// var namespace, provider, distPath, repoName, version, gpgFingerprint, gpgPubKeyFile, domain string
log.Println("* Creating architecture files in target directories") log.Println("* Creating architecture files in target directories")
// filename = terraform-provider-[provider]_0.0.1_darwin_amd64.zip - provider_name + version + target + architecture + .zip
// prefix := fmt.Sprintf("v1/providers/%s/%s/%s/", namespace, provider, version)
prefix := path.Join("v1", "providers", p.Namespace, p.Provider, p.Version) prefix := path.Join("v1", "providers", p.Namespace, p.Provider, p.Version)
// pathPrefix := fmt.Sprintf("release/%s", prefix)
pathPrefix := path.Join("release", prefix) pathPrefix := path.Join("release", prefix)
// urlPrefix := fmt.Sprintf("https://%s/%s", domain, prefix)
urlPrefix, err := url.JoinPath("https://", p.Domain, prefix) urlPrefix, err := url.JoinPath("https://", p.Domain, prefix)
if err != nil { if err != nil {
return fmt.Errorf("error creating base url: %w", err) return fmt.Errorf("error creating base url: %w", err)
} }
// download url = https://example.com/v1/providers/namespace/provider/0.0.1/download/terraform-provider_0.0.1_darwin_amd64.zip
downloadUrlPrefix, err := url.JoinPath(urlPrefix, "download") downloadUrlPrefix, err := url.JoinPath(urlPrefix, "download")
if err != nil { if err != nil {
return fmt.Errorf("error crearting download url: %w", err) return fmt.Errorf("error crearting download url: %w", err)
} }
downloadPathPrefix := path.Join(pathPrefix, "download") downloadPathPrefix := path.Join(pathPrefix, "download")
// shasums url = https://example.com/v1/providers/namespace/provider/0.0.1/terraform-provider_0.0.1_SHA256SUMS
shasumsUrl, err := url.JoinPath(urlPrefix, fmt.Sprintf("%s_%s_SHA256SUMS", p.RepoName, p.Version)) shasumsUrl, err := url.JoinPath(urlPrefix, fmt.Sprintf("%s_%s_SHA256SUMS", p.RepoName, p.Version))
if err != nil { if err != nil {
return fmt.Errorf("error creating shasums url: %w", err) return fmt.Errorf("error creating shasums url: %w", err)
} }
// shasums_signature_url = https://example.com/v1/providers/namespace/provider/0.0.1/terraform-provider_0.0.1_SHA256SUMS.sig
shasumsSigUrl := shasumsUrl + ".sig" shasumsSigUrl := shasumsUrl + ".sig"
gpgAsciiPub, err := p.ReadGpgFile() gpgAsciiPub, err := p.ReadGpgFile()
@ -107,6 +116,33 @@ func (p *Provider) CreateArchitectureFiles() error {
}, },
}, },
} }
// var architectureTemplate = []byte(fmt.Sprintf(`
//{
// "protocols": [
// "4.0",
// "5.1",
// "6.0"
// ],
// "os": "%s",
// "arch": "%s",
// "filename": "%s",
// "download_url": "%s",
// "shasums_url": "%s",
// "shasums_signature_url": "%s",
// "shasum": "%s",
// "signing_keys": {
// "gpg_public_keys": [
// {
// "key_id": "%s",
// "ascii_armor": "%s",
// "trust_signature": "",
// "source": "",
// "source_url": ""
// }
// ]
// }
//}
// `, target, arch, fileName, downloadUrl, shasumsUrl, shasumsSigUrl, shasum, gpgFingerprint, gpgAsciiPub))
log.Printf(" - Arch file: %s", archFileName) log.Printf(" - Arch file: %s", archFileName)
@ -124,12 +160,8 @@ func WriteArchitectureFile(filePath string, arch Architecture) error {
if err != nil { if err != nil {
return fmt.Errorf("error encoding data: %w", err) return fmt.Errorf("error encoding data: %w", err)
} }
//nolint:gosec // this file is not sensitive, so we can use os.ModePerm
err = os.WriteFile( err = os.WriteFile(filePath, jsonString, os.ModePerm)
filePath,
jsonString,
os.ModePerm,
)
if err != nil { if err != nil {
return fmt.Errorf("error writing data: %w", err) return fmt.Errorf("error writing data: %w", err)
} }

View file

@ -161,12 +161,10 @@ func (p *Provider) createVersionsFile() error {
target := fileNameSplit[2] target := fileNameSplit[2]
arch := fileNameSplit[3] arch := fileNameSplit[3]
version.Platforms = append( version.Platforms = append(version.Platforms, Platform{
version.Platforms, Platform{ OS: target,
OS: target, Arch: arch,
Arch: arch, })
},
)
} }
data := Data{} data := Data{}
@ -208,19 +206,16 @@ func (p *Provider) CreateWellKnown() error {
log.Println("* Creating .well-known directory") log.Println("* Creating .well-known directory")
pathString := path.Join(p.RootPath, "release", ".well-known") pathString := path.Join(p.RootPath, "release", ".well-known")
//nolint:gosec // this file is not sensitive, so we can use ModePerm
err := os.MkdirAll(pathString, os.ModePerm) err := os.MkdirAll(pathString, os.ModePerm)
if err != nil && !errors.Is(err, fs.ErrExist) { if err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("error creating '%s' dir: %w", pathString, err) return fmt.Errorf("error creating '%s' dir: %w", pathString, err)
} }
log.Println(" - Writing to .well-known/terraform.json file") log.Println(" - Writing to .well-known/terraform.json file")
//nolint:gosec // this file is not sensitive, so we can use 0644
err = os.WriteFile( err = os.WriteFile(
fmt.Sprintf("%s/terraform.json", pathString), fmt.Sprintf("%s/terraform.json", pathString),
[]byte(`{"providers.v1": "/v1/providers/"}`), []byte(`{"providers.v1": "/v1/providers/"}`),
0o644, 0644,
) )
if err != nil { if err != nil {
return err return err
@ -229,10 +224,9 @@ func (p *Provider) CreateWellKnown() error {
return nil return nil
} }
func CreateDir(pathValue string) error { func CreateDir(path string) error {
log.Printf("* Creating %s directory", pathValue) log.Printf("* Creating %s directory", path)
//nolint:gosec // this file is not sensitive, so we can use ModePerm err := os.MkdirAll(path, os.ModePerm)
err := os.MkdirAll(pathValue, os.ModePerm)
if errors.Is(err, fs.ErrExist) { if errors.Is(err, fs.ErrExist) {
return nil return nil
} }
@ -275,23 +269,13 @@ func CopyFile(src, dst string) (int64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func(source *os.File) { defer source.Close()
err := source.Close()
if err != nil {
slog.Error("error closing source file", slog.Any("err", err))
}
}(source)
destination, err := os.Create(dst) destination, err := os.Create(dst)
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func(destination *os.File) { defer destination.Close()
err := destination.Close()
if err != nil {
slog.Error("error closing destination file", slog.Any("err", err))
}
}(destination)
nBytes, err := io.Copy(destination, source) nBytes, err := io.Copy(destination, source)
return nBytes, err return nBytes, err
} }

View file

@ -1,38 +0,0 @@
{
log {
level debug
}
filesystem tf s3 {
bucket "terraform-provider-privatepreview"
region eu01
endpoint https://object.storage.eu01.onstackit.cloud
use_path_style
}
}
tfregistry.sysops.stackit.rocks {
encode zstd gzip
handle_path /docs/* {
root /srv/www
templates
@md {
file {path}
path *.md
}
rewrite @md /markdown.html
file_server {
browse
}
}
file_server {
fs tf
browse
}
}

View file

@ -35,12 +35,7 @@ func (d *Data) WriteToFile(filePath string) error {
return fmt.Errorf("error encoding data: %w", err) return fmt.Errorf("error encoding data: %w", err)
} }
//nolint:gosec // this file is not sensitive, so we can use os.ModePerm err = os.WriteFile(filePath, jsonString, os.ModePerm)
err = os.WriteFile(
filePath,
jsonString,
os.ModePerm,
)
if err != nil { if err != nil {
return fmt.Errorf("error writing data: %w", err) return fmt.Errorf("error writing data: %w", err)
} }
@ -91,13 +86,7 @@ func (d *Data) LoadFromUrl(uri string) error {
if err != nil { if err != nil {
return err return err
} }
defer func(name string) { defer os.Remove(file.Name()) // Clean up
//nolint:gosec // The file path is generated by os.CreateTemp and is not user-controllable
err := os.Remove(name)
if err != nil {
slog.Error("failed to remove temporary file", slog.Any("err", err))
}
}(file.Name()) // Clean up
err = DownloadFile( err = DownloadFile(
u.String(), u.String(),
@ -134,30 +123,20 @@ func (v *Version) AddProtocol(p string) error {
// DownloadFile will download a url and store it in local filepath. // DownloadFile will download a url and store it in local filepath.
// It writes to the destination file as it downloads it, without // It writes to the destination file as it downloads it, without
// loading the entire file into memory. // loading the entire file into memory.
func DownloadFile(urlValue, filepath string) error { func DownloadFile(url string, filepath string) error {
// Create the file // Create the file
//nolint:gosec // path traversal is not a concern here, as the filepath is generated by us and not user input
out, err := os.Create(filepath) out, err := os.Create(filepath)
if err != nil { if err != nil {
return err return err
} }
defer func(out *os.File) { defer out.Close()
err := out.Close()
if err != nil {
slog.Error("failed to close file", slog.Any("err", err))
}
}(out)
// Get the data // Get the data
resp, err := http.Get(url)
//nolint:gosec,bodyclose // this is a controlled URL, not user input
resp, err := http.Get(urlValue)
if err != nil { if err != nil {
return err return err
} }
defer func(Body io.ReadCloser) { defer resp.Body.Close()
_ = Body.Close()
}(resp.Body)
// Write the body to file // Write the body to file
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)

View file

@ -29,32 +29,20 @@ var publishCmd = &cobra.Command{
Use: "publish", Use: "publish",
Short: "Publish terraform provider", Short: "Publish terraform provider",
Long: `...`, Long: `...`,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, args []string) error {
return publish() return publish()
}, },
} }
func init() { //nolint:gochecknoinits //this is the standard way to set up cobra commands func init() { // nolint: gochecknoinits
publishCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace for the Terraform registry.") publishCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace for the Terraform registry.")
publishCmd.Flags().StringVarP(&domain, "domain", "d", "", "Domain for the Terraform registry.") publishCmd.Flags().StringVarP(&domain, "domain", "d", "", "Domain for the Terraform registry.")
publishCmd.Flags().StringVarP(&providerName, "providerName", "p", "", "ProviderName for the Terraform registry.") publishCmd.Flags().StringVarP(&providerName, "providerName", "p", "", "ProviderName for the Terraform registry.")
publishCmd.Flags().StringVarP(&distPath, "distPath", "x", "dist", "Dist Path for the Terraform registry.") publishCmd.Flags().StringVarP(&distPath, "distPath", "x", "dist", "Dist Path for the Terraform registry.")
publishCmd.Flags().StringVarP(&repoName, "repoName", "r", "", "RepoName for the Terraform registry.") publishCmd.Flags().StringVarP(&repoName, "repoName", "r", "", "RepoName for the Terraform registry.")
publishCmd.Flags().StringVarP(&version, "version", "v", "", "Version for the Terraform registry.") publishCmd.Flags().StringVarP(&version, "version", "v", "", "Version for the Terraform registry.")
publishCmd.Flags().StringVarP( publishCmd.Flags().StringVarP(&gpgFingerprint, "gpgFingerprint", "f", "", "GPG Fingerprint for the Terraform registry.")
&gpgFingerprint, publishCmd.Flags().StringVarP(&gpgPubKeyFile, "gpgPubKeyFile", "k", "", "GPG PubKey file name for the Terraform registry.")
"gpgFingerprint",
"f",
"",
"GPG Fingerprint for the Terraform registry.",
)
publishCmd.Flags().StringVarP(
&gpgPubKeyFile,
"gpgPubKeyFile",
"k",
"",
"GPG PubKey file name for the Terraform registry.",
)
err := publishCmd.MarkFlagRequired("namespace") err := publishCmd.MarkFlagRequired("namespace")
if err != nil { if err != nil {
@ -117,7 +105,6 @@ func publish() error {
// Create release dir - only the contents of this need to be uploaded to S3 // Create release dir - only the contents of this need to be uploaded to S3
log.Printf("* Creating release directory") log.Printf("* Creating release directory")
//nolint:gosec // this directory is not sensitive, so we can use 0750
err = os.MkdirAll(path.Join(p.RootPath, "release"), os.ModePerm) err = os.MkdirAll(path.Join(p.RootPath, "release"), os.ModePerm)
if err != nil && !errors.Is(err, fs.ErrExist) { if err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("error creating '%s' dir: %w", path.Join(p.RootPath, "release"), err) return fmt.Errorf("error creating '%s' dir: %w", path.Join(p.RootPath, "release"), err)

View file

@ -5,7 +5,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"github.com/SladkyCitron/slogcolor" "github.com/MatusOllah/slogcolor"
cc "github.com/ivanpirog/coloredcobra" cc "github.com/ivanpirog/coloredcobra"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/cmd/cmd" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/cmd/cmd"
@ -30,7 +30,6 @@ func main() {
cmd.NewBuildCmd(), cmd.NewBuildCmd(),
cmd.NewPublishCmd(), cmd.NewPublishCmd(),
cmd.NewGetFieldsCmd(), cmd.NewGetFieldsCmd(),
cmd.NewExamplesCmd(),
) )
err := rootCmd.Execute() err := rootCmd.Execute()

View file

@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_database Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_database (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_postgresflexalpha_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `database_id` (Number) The ID of the database.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `id` (String) Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`database_id`\".",
- `name` (String) The name of the database.
- `owner` (String) The owner of the database.
- `tf_original_api_id` (Number) The id of the database.

View file

@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_flavor Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_flavor (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_postgresflexalpha_flavor" "flavor" {
project_id = var.project_id
region = var.region
cpu = 4
ram = 16
node_type = "Single"
storage_class = "premium-perf2-stackit"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `cpu` (Number) The cpu count of the instance.
- `node_type` (String) defines the nodeType it can be either single or replica
- `project_id` (String) The cpu count of the instance.
- `ram` (Number) The memory of the instance in Gibibyte.
- `region` (String) The flavor description.
- `storage_class` (String) The memory of the instance in Gibibyte.
### Read-Only
- `description` (String) The flavor description.
- `flavor_id` (String) The flavor id of the instance flavor.
- `id` (String) The terraform id of the instance flavor.
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
- `storage_classes` (Attributes List) (see [below for nested schema](#nestedatt--storage_classes))
<a id="nestedatt--storage_classes"></a>
### Nested Schema for `storage_classes`
Read-Only:
- `class` (String)
- `max_io_per_sec` (Number)
- `max_through_in_mb` (Number)

View file

@ -0,0 +1,68 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_flavors Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_flavors (Data Source)
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Optional
- `page` (Number) Number of the page of items list to be returned.
- `size` (Number) Number of items to be returned on each page.
- `sort` (String) Sorting of the flavors to be returned on each page.
### Read-Only
- `flavors` (Attributes List) List of flavors available for the project. (see [below for nested schema](#nestedatt--flavors))
- `pagination` (Attributes) (see [below for nested schema](#nestedatt--pagination))
<a id="nestedatt--flavors"></a>
### Nested Schema for `flavors`
Read-Only:
- `cpu` (Number) The cpu count of the instance.
- `description` (String) The flavor description.
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
- `memory` (Number) The memory of the instance in Gibibyte.
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
- `node_type` (String) defines the nodeType it can be either single or replica
- `storage_classes` (Attributes List) maximum storage which can be ordered for the flavor in Gigabyte. (see [below for nested schema](#nestedatt--flavors--storage_classes))
- `tf_original_api_id` (String) The id of the instance flavor.
<a id="nestedatt--flavors--storage_classes"></a>
### Nested Schema for `flavors.storage_classes`
Read-Only:
- `class` (String)
- `max_io_per_sec` (Number)
- `max_through_in_mb` (Number)
<a id="nestedatt--pagination"></a>
### Nested Schema for `pagination`
Read-Only:
- `page` (Number)
- `size` (Number)
- `sort` (String)
- `total_pages` (Number)
- `total_rows` (Number)

View file

@ -0,0 +1,87 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_instance Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_instance (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_postgresflexalpha_instance" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `acl` (List of String) List of IPV4 cidr.
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `connection_info` (Attributes) The DNS name and port in the instance overview (see [below for nested schema](#nestedatt--connection_info))
- `encryption` (Attributes) The configuration for instance's volume and backup storage encryption.
⚠︝ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected. (see [below for nested schema](#nestedatt--encryption))
- `flavor_id` (String) The id of the instance flavor.
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `name` (String) The name of the instance.
- `network` (Attributes) The access configuration of the instance (see [below for nested schema](#nestedatt--network))
- `replicas` (Number) How many replicas the instance should have.
- `retention_days` (Number) How long backups are retained. The value can only be between 32 and 365 days.
- `status` (String) The current status of the instance.
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `tf_original_api_id` (String) The ID of the instance.
- `version` (String) The Postgres version used for the instance. See [Versions Endpoint](/documentation/postgres-flex-service/version/v3alpha1#tag/Version) for supported version parameters.
<a id="nestedatt--connection_info"></a>
### Nested Schema for `connection_info`
Read-Only:
- `host` (String) The host of the instance.
- `port` (Number) The port of the instance.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Read-Only:
- `kek_key_id` (String) The encryption-key key identifier
- `kek_key_ring_id` (String) The encryption-key keyring identifier
- `kek_key_version` (String) The encryption-key version
- `service_account` (String)
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Read-Only:
- `access_scope` (String) The access scope of the instance. It defines if the instance is public or airgapped.
- `acl` (List of String) List of IPV4 cidr.
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Read-Only:
- `performance_class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.

View file

@ -0,0 +1,42 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_user Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_user (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_postgresflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
user_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
- `user_id` (Number) The ID of the user.
### Optional
- `id` (String) Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".",
### Read-Only
- `name` (String) The name of the user.
- `roles` (List of String) A list of user roles.
- `status` (String) The current status of the user.
- `tf_original_api_id` (Number) The ID of the user.

View file

@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_database Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_database (Data Source)
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `database_name` (String) The name of the database.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility_level` (Number) CompatibilityLevel of the Database.
- `id` (String) Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`database_id`\".",
- `name` (String) The name of the database.
- `owner` (String) The owner of the database.
- `tf_original_api_id` (Number) The id of the database.

View file

@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_flavor Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_flavor (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexalpha_flavor" "flavor" {
project_id = var.project_id
region = var.region
cpu = 4
ram = 16
node_type = "Single"
storage_class = "premium-perf2-stackit"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `cpu` (Number) The cpu count of the instance.
- `node_type` (String) defines the nodeType it can be either single or replica
- `project_id` (String) The cpu count of the instance.
- `ram` (Number) The memory of the instance in Gibibyte.
- `region` (String) The flavor description.
- `storage_class` (String) The memory of the instance in Gibibyte.
### Read-Only
- `description` (String) The flavor description.
- `flavor_id` (String) The flavor id of the instance flavor.
- `id` (String) The terraform id of the instance flavor.
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
- `storage_classes` (Attributes List) (see [below for nested schema](#nestedatt--storage_classes))
<a id="nestedatt--storage_classes"></a>
### Nested Schema for `storage_classes`
Read-Only:
- `class` (String)
- `max_io_per_sec` (Number)
- `max_through_in_mb` (Number)

View file

@ -0,0 +1,78 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_instance Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_instance (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexalpha_instance" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `edition` (String) Edition of the MSSQL server instance
- `encryption` (Attributes) this defines which key to use for storage encryption (see [below for nested schema](#nestedatt--encryption))
- `flavor_id` (String) The id of the instance flavor.
- `id` (String) Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`\".
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `name` (String) The name of the instance.
- `network` (Attributes) The access configuration of the instance (see [below for nested schema](#nestedatt--network))
- `replicas` (Number) How many replicas the instance should have.
- `retention_days` (Number) The days for how long the backup files should be stored before cleaned up. 30 to 365
- `status` (String)
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `tf_original_api_id` (String) The ID of the instance.
- `version` (String) The sqlserver version used for the instance.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Read-Only:
- `kek_key_id` (String) The key identifier
- `kek_key_ring_id` (String) The keyring identifier
- `kek_key_version` (String) The key version
- `service_account` (String)
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Read-Only:
- `access_scope` (String) The network access scope of the instance
⚠️ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected.
- `acl` (List of String) List of IPV4 cidr.
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Read-Only:
- `class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.

View file

@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_user Data Source - stackitprivatepreview"
subcategory: ""
description: |-
SQLServer Flex user data source schema. Must have a region specified in the provider configuration.
---
# stackitprivatepreview_sqlserverflexalpha_user (Data Source)
SQLServer Flex user data source schema. Must have a `region` specified in the provider configuration.
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
user_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `instance_id` (String) ID of the SQLServer Flex instance.
- `project_id` (String) STACKIT project ID to which the instance is associated.
- `user_id` (Number) User ID.
### Optional
- `region` (String) The resource region. If not defined, the provider region is used.
### Read-Only
- `default_database` (String)
- `host` (String)
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`".
- `port` (Number)
- `roles` (Set of String) Database access levels for the user.
- `status` (String)
- `username` (String) Username of the SQLServer Flex instance.

View file

@ -0,0 +1,40 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexbeta_database Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexbeta_database (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexbeta_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
database_name = "dbname"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `database_name` (String) The name of the database.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility_level` (Number) CompatibilityLevel of the Database.
- `id` (String) The terraform internal identifier.
- `name` (String) The name of the database.
- `owner` (String) The owner of the database.
- `tf_original_api_id` (Number) The id of the database.

View file

@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexbeta_flavor Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexbeta_flavor (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexbeta_flavor" "flavor" {
project_id = var.project_id
region = var.region
cpu = 4
ram = 16
node_type = "Single"
storage_class = "premium-perf2-stackit"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `cpu` (Number) The cpu count of the instance.
- `node_type` (String) defines the nodeType it can be either single or HA
- `project_id` (String) The project ID of the flavor.
- `ram` (Number) The memory of the instance in Gibibyte.
- `region` (String) The region of the flavor.
- `storage_class` (String) The memory of the instance in Gibibyte.
### Read-Only
- `description` (String) The flavor description.
- `flavor_id` (String) The id of the instance flavor.
- `id` (String) The id of the instance flavor.
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
- `storage_classes` (Attributes List) maximum storage which can be ordered for the flavor in Gigabyte. (see [below for nested schema](#nestedatt--storage_classes))
<a id="nestedatt--storage_classes"></a>
### Nested Schema for `storage_classes`
Read-Only:
- `class` (String)
- `max_io_per_sec` (Number)
- `max_through_in_mb` (Number)

View file

@ -0,0 +1,77 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexbeta_instance Data Source - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexbeta_instance (Data Source)
## Example Usage
```terraform
data "stackitprivatepreview_sqlserverflexbeta_instance" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `edition` (String) Edition of the MSSQL server instance
- `encryption` (Attributes) this defines which key to use for storage encryption (see [below for nested schema](#nestedatt--encryption))
- `flavor_id` (String) The id of the instance flavor.
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `name` (String) The name of the instance.
- `network` (Attributes) The access configuration of the instance (see [below for nested schema](#nestedatt--network))
- `replicas` (Number) How many replicas the instance should have.
- `retention_days` (Number) The days for how long the backup files should be stored before cleaned up. 30 to 365
- `status` (String)
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `tf_original_api_id` (String) The ID of the instance.
- `version` (String) The sqlserver version used for the instance.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Read-Only:
- `kek_key_id` (String) The key identifier
- `kek_key_ring_id` (String) The keyring identifier
- `kek_key_version` (String) The key version
- `service_account` (String)
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Read-Only:
- `access_scope` (String) The network access scope of the instance
⚠️ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected.
- `acl` (List of String) List of IPV4 cidr.
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Read-Only:
- `class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.

83
docs/index.md Normal file
View file

@ -0,0 +1,83 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview Provider"
description: |-
---
# stackitprivatepreview Provider
## Example Usage
```terraform
provider "stackitprivatepreview" {
default_region = "eu01"
}
provider "stackitprivatepreview" {
default_region = "eu01"
service_account_key_path = "service_account.json"
}
# Authentication
# Key flow
provider "stackitprivatepreview" {
default_region = "eu01"
service_account_key = var.service_account_key
private_key = var.private_key
}
# Key flow (using path)
provider "stackitprivatepreview" {
default_region = "eu01"
service_account_key_path = var.service_account_key_path
private_key_path = var.private_key_path
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Optional
- `authorization_custom_endpoint` (String) Custom endpoint for the Membership service
- `cdn_custom_endpoint` (String) Custom endpoint for the CDN service
- `credentials_path` (String) Path of JSON from where the credentials are read. Takes precedence over the env var `STACKIT_CREDENTIALS_PATH`. Default value is `~/.stackit/credentials.json`.
- `default_region` (String) Region will be used as the default location for regional services. Not all services require a region, some are global
- `dns_custom_endpoint` (String) Custom endpoint for the DNS service
- `enable_beta_resources` (Boolean) Enable beta resources. Default is false.
- `experiments` (List of String) Enables experiments. These are unstable features without official support. More information can be found in the README. Available Experiments: iam, routing-tables, network
- `git_custom_endpoint` (String) Custom endpoint for the Git service
- `iaas_custom_endpoint` (String) Custom endpoint for the IaaS service
- `kms_custom_endpoint` (String) Custom endpoint for the KMS service
- `loadbalancer_custom_endpoint` (String) Custom endpoint for the Load Balancer service
- `logme_custom_endpoint` (String) Custom endpoint for the LogMe service
- `mariadb_custom_endpoint` (String) Custom endpoint for the MariaDB service
- `modelserving_custom_endpoint` (String) Custom endpoint for the AI Model Serving service
- `mongodbflex_custom_endpoint` (String) Custom endpoint for the MongoDB Flex service
- `objectstorage_custom_endpoint` (String) Custom endpoint for the Object Storage service
- `observability_custom_endpoint` (String) Custom endpoint for the Observability service
- `opensearch_custom_endpoint` (String) Custom endpoint for the OpenSearch service
- `postgresflex_custom_endpoint` (String) Custom endpoint for the PostgresFlex service
- `private_key` (String) Private RSA key used for authentication, relevant for the key flow. It takes precedence over the private key that is included in the service account key.
- `private_key_path` (String) Path for the private RSA key used for authentication, relevant for the key flow. It takes precedence over the private key that is included in the service account key.
- `rabbitmq_custom_endpoint` (String) Custom endpoint for the RabbitMQ service
- `redis_custom_endpoint` (String) Custom endpoint for the Redis service
- `region` (String, Deprecated) Region will be used as the default location for regional services. Not all services require a region, some are global
- `resourcemanager_custom_endpoint` (String) Custom endpoint for the Resource Manager service
- `scf_custom_endpoint` (String) Custom endpoint for the Cloud Foundry (SCF) service
- `secretsmanager_custom_endpoint` (String) Custom endpoint for the Secrets Manager service
- `server_backup_custom_endpoint` (String) Custom endpoint for the Server Backup service
- `server_update_custom_endpoint` (String) Custom endpoint for the Server Update service
- `service_account_custom_endpoint` (String) Custom endpoint for the Service Account service
- `service_account_email` (String, Deprecated) Service account email. It can also be set using the environment variable STACKIT_SERVICE_ACCOUNT_EMAIL. It is required if you want to use the resource manager project resource.
- `service_account_key` (String) Service account key used for authentication. If set, the key flow will be used to authenticate all operations.
- `service_account_key_path` (String) Path for the service account key used for authentication. If set, the key flow will be used to authenticate all operations.
- `service_account_token` (String, Deprecated) Token used for authentication. If set, the token flow will be used to authenticate all operations.
- `service_enablement_custom_endpoint` (String) Custom endpoint for the Service Enablement API
- `ske_custom_endpoint` (String) Custom endpoint for the Kubernetes Engine (SKE) service
- `sqlserverflex_custom_endpoint` (String) Custom endpoint for the SQL Server Flex service
- `token_custom_endpoint` (String) Custom endpoint for the token API, which is used to request access tokens when using the key flow

View file

@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_database Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_database (Resource)
## Example Usage
```terraform
resource "stackitprivatepreview_postgresflexalpha_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "mydb"
owner = "myusername"
}
# Only use the import statement, if you want to import an existing postgresflex database
import {
to = stackitprivatepreview_postgresflexalpha_database.import-example
id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.postgres_database_id}"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) The name of the database.
### Optional
- `database_id` (Number) The ID of the database.
- `instance_id` (String) The ID of the instance.
- `owner` (String) The owner of the database.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `id` (Number) The id of the database.

View file

@ -0,0 +1,131 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_instance Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_instance (Resource)
## Example Usage
```terraform
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
}
# Only use the import statement, if you want to import an existing postgresflex instance
import {
to = stackitprivatepreview_postgresflexalpha_instance.import-example
id = "${var.project_id},${var.region},${var.postgres_instance_id}"
}
import {
to = stackitprivatepreview_postgresflexalpha_instance.import-example
identity = {
project_id = var.project_id
region = var.region
instance_id = var.postgres_instance_id
}
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `flavor_id` (String) The id of the instance flavor.
- `name` (String) The name of the instance.
- `network` (Attributes) The access configuration of the instance (see [below for nested schema](#nestedatt--network))
- `replicas` (Number) How many replicas the instance should have.
- `retention_days` (Number) How long backups are retained. The value can only be between 32 and 365 days.
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `version` (String) The Postgres version used for the instance. See [Versions Endpoint](/documentation/postgres-flex-service/version/v3alpha1#tag/Version) for supported version parameters.
### Optional
- `encryption` (Attributes) The configuration for instance's volume and backup storage encryption.
⚠︝ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected. (see [below for nested schema](#nestedatt--encryption))
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `acl` (List of String) List of IPV4 cidr.
- `connection_info` (Attributes) The DNS name and port in the instance overview (see [below for nested schema](#nestedatt--connection_info))
- `id` (String) The ID of the instance.
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `status` (String) The current status of the instance.
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Required:
- `acl` (List of String) List of IPV4 cidr.
Optional:
- `access_scope` (String) The access scope of the instance. It defines if the instance is public or airgapped.
Read-Only:
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Required:
- `performance_class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Required:
- `kek_key_id` (String) The encryption-key key identifier
- `kek_key_ring_id` (String) The encryption-key keyring identifier
- `kek_key_version` (String) The encryption-key version
- `service_account` (String)
<a id="nestedatt--connection_info"></a>
### Nested Schema for `connection_info`
Read-Only:
- `host` (String) The host of the instance.
- `port` (Number) The port of the instance.

View file

@ -0,0 +1,50 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_postgresflexalpha_user Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_postgresflexalpha_user (Resource)
## Example Usage
```terraform
resource "stackitprivatepreview_postgresflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "username"
roles = ["role"]
}
# Only use the import statement, if you want to import an existing postgresflex user
import {
to = stackitprivatepreview_postgresflexalpha_user.import-example
id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.user_id}"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) The name of the user.
### Optional
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
- `roles` (List of String) A list containing the user roles for the instance.
- `user_id` (Number) The ID of the user.
### Read-Only
- `connection_string` (String) The connection string for the user to the instance.
- `id` (Number) The ID of the user.
- `password` (String) The password for the user.
- `status` (String) The current status of the user.

View file

@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_database Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_database (Resource)
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) The name of the database.
- `owner` (String) The owner of the database.
### Optional
- `collation` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility` (Number) CompatibilityLevel of the Database.
- `database_name` (String) The name of the database.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility_level` (Number) CompatibilityLevel of the Database.
- `id` (Number) The id of the database.

View file

@ -0,0 +1,103 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_instance Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_instance (Resource)
## Example Usage
```terraform
resource "stackitprivatepreview_sqlserverflexalpha_instance" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance"
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
backup_schedule = "00 00 * * *"
flavor = {
cpu = 4
ram = 16
}
storage = {
class = "class"
size = 5
}
version = 2022
}
# Only use the import statement, if you want to import an existing sqlserverflex instance
import {
to = stackitprivatepreview_sqlserverflexalpha_instance.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id}"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `flavor_id` (String) The id of the instance flavor.
- `name` (String) The name of the instance.
- `network` (Attributes) the network configuration of the instance. (see [below for nested schema](#nestedatt--network))
- `retention_days` (Number) The days for how long the backup files should be stored before cleaned up. 30 to 365
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `version` (String) The sqlserver version used for the instance.
### Optional
- `encryption` (Attributes) this defines which key to use for storage encryption (see [below for nested schema](#nestedatt--encryption))
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `edition` (String) Edition of the MSSQL server instance
- `id` (String) The ID of the instance.
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `replicas` (Number) How many replicas the instance should have.
- `status` (String)
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Required:
- `acl` (List of String) List of IPV4 cidr.
Optional:
- `access_scope` (String) The network access scope of the instance
⚠️ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected.
Read-Only:
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Required:
- `class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Required:
- `kek_key_id` (String) The key identifier
- `kek_key_ring_id` (String) The keyring identifier
- `kek_key_version` (String) The key version
- `service_account` (String)

View file

@ -0,0 +1,53 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexalpha_user Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexalpha_user (Resource)
## Example Usage
```terraform
resource "stackitprivatepreview_sqlserverflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
username = "username"
roles = ["role"]
}
# Only use the import statement, if you want to import an existing sqlserverflex user
import {
to = stackitprivatepreview_sqlserverflexalpha_user.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id},${var.sql_user_id}"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `roles` (List of String) A list containing the user roles for the instance. A list with the valid user roles can be retrieved using the List Roles endpoint.
- `username` (String) The name of the user.
### Optional
- `default_database` (String) The default database for a user of the instance.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
- `user_id` (Number) The ID of the user.
### Read-Only
- `host` (String) The host of the instance in which the user belongs to.
- `id` (Number) The ID of the user.
- `password` (String) The password for the user.
- `port` (Number) The port of the instance in which the user belongs to.
- `status` (String) The current status of the user.
- `uri` (String) The connection string for the user to the instance.

View file

@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexbeta_database Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexbeta_database (Resource)
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String) The name of the database.
- `owner` (String) The owner of the database.
### Optional
- `collation` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility` (Number) CompatibilityLevel of the Database.
- `database_name` (String) The name of the database.
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
- `compatibility_level` (Number) CompatibilityLevel of the Database.
- `id` (Number) The id of the database.

View file

@ -0,0 +1,158 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackitprivatepreview_sqlserverflexbeta_instance Resource - stackitprivatepreview"
subcategory: ""
description: |-
---
# stackitprivatepreview_sqlserverflexbeta_instance (Resource)
## Example Usage
```terraform
# without encryption and SNA
resource "stackitprivatepreview_sqlserverflexbeta_instance" "instance" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance"
backup_schedule = "0 3 * * *"
retention_days = 31
flavor_id = "flavor_id"
storage = {
class = "premium-perf2-stackit"
size = 50
}
version = 2022
network = {
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
access_scope = "SNA"
}
}
# without encryption and PUBLIC
resource "stackitprivatepreview_sqlserverflexbeta_instance" "instance" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance"
backup_schedule = "0 3 * * *"
retention_days = 31
flavor_id = "flavor_id"
storage = {
class = "premium-perf2-stackit"
size = 50
}
version = 2022
network = {
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
access_scope = "PUBLIC"
}
}
# with encryption and SNA
resource "stackitprivatepreview_sqlserverflexbeta_instance" "instance" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance"
backup_schedule = "0 3 * * *"
retention_days = 31
flavor_id = "flavor_id"
storage = {
class = "premium-perf2-stackit"
size = 50
}
version = 2022
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 = "SNA"
}
}
# Only use the import statement, if you want to import an existing sqlserverflex instance
import {
to = stackitprivatepreview_sqlserverflexalpha_instance.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id}"
}
# import with identity
import {
to = stackitprivatepreview_sqlserverflexalpha_instance.import-example
identity = {
project_id = var.project_id
region = var.region
instance_id = var.sql_instance_id
}
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `backup_schedule` (String) The schedule for on what time and how often the database backup will be created. The schedule is written as a cron schedule.
- `flavor_id` (String) The id of the instance flavor.
- `name` (String) The name of the instance.
- `network` (Attributes) the network configuration of the instance. (see [below for nested schema](#nestedatt--network))
- `retention_days` (Number) The days for how long the backup files should be stored before cleaned up. 30 to 365
- `storage` (Attributes) The object containing information about the storage size and class. (see [below for nested schema](#nestedatt--storage))
- `version` (String) The sqlserver version used for the instance.
### Optional
- `encryption` (Attributes) this defines which key to use for storage encryption (see [below for nested schema](#nestedatt--encryption))
- `instance_id` (String) The ID of the instance.
- `project_id` (String) The STACKIT project ID.
- `region` (String) The region which should be addressed
### Read-Only
- `edition` (String) Edition of the MSSQL server instance
- `id` (String) The ID of the instance.
- `is_deletable` (Boolean) Whether the instance can be deleted or not.
- `replicas` (Number) How many replicas the instance should have.
- `status` (String)
<a id="nestedatt--network"></a>
### Nested Schema for `network`
Required:
- `acl` (List of String) List of IPV4 cidr.
Optional:
- `access_scope` (String) The network access scope of the instance
⚠️ **Note:** This feature is in private preview. Supplying this object is only permitted for enabled accounts. If your account does not have access, the request will be rejected.
Read-Only:
- `instance_address` (String)
- `router_address` (String)
<a id="nestedatt--storage"></a>
### Nested Schema for `storage`
Required:
- `class` (String) The storage class for the storage.
- `size` (Number) The storage size in Gigabytes.
<a id="nestedatt--encryption"></a>
### Nested Schema for `encryption`
Required:
- `kek_key_id` (String) The key identifier
- `kek_key_ring_id` (String) The keyring identifier
- `kek_key_version` (String) The key version
- `service_account` (String)

View file

@ -9,14 +9,4 @@ resource "stackitprivatepreview_postgresflexalpha_database" "example" {
import { import {
to = stackitprivatepreview_postgresflexalpha_database.import-example to = stackitprivatepreview_postgresflexalpha_database.import-example
id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.postgres_database_id}" id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.postgres_database_id}"
} }
import {
to = stackitprivatepreview_postgresflexalpha_database.import-example
identity = {
project_id = "project_id"
region = "region"
instance_id = "instance_id"
database_id = "database_id"
}
}

View file

@ -1,4 +1,4 @@
resource "stackitprivatepreview_postgresflexalpha_instance" "example-instance" { resource "stackitprivatepreview_postgresflexalpha_instance" "msh-instance-only" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-instance" name = "example-instance"
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"] acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
@ -17,7 +17,7 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "example-instance" {
service_account = "service@account.email" service_account = "service@account.email"
} }
network = { network = {
acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"] acl = ["XXX.XXX.XXX.X/XX", "XX.XXX.XX.X/XX"]
access_scope = "PUBLIC" access_scope = "PUBLIC"
} }
version = 17 version = 17

View file

@ -1,7 +1,7 @@
resource "stackitprivatepreview_postgresflexalpha_user" "example" { resource "stackitprivatepreview_postgresflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "username" name = "username"
roles = ["role"] roles = ["role"]
} }
@ -9,14 +9,4 @@ resource "stackitprivatepreview_postgresflexalpha_user" "example" {
import { import {
to = stackitprivatepreview_postgresflexalpha_user.import-example to = stackitprivatepreview_postgresflexalpha_user.import-example
id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.user_id}" id = "${var.project_id},${var.region},${var.postgres_instance_id},${var.user_id}"
} }
import {
to = stackitprivatepreview_postgresflexalpha_user.import-example
identity = {
project_id = "project.id"
region = "region"
instance_id = "instance.id"
user_id = "user.id"
}
}

View file

@ -1,24 +0,0 @@
resource "stackitprivatepreview_sqlserverflexalpha_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
collation = ""
compatibility = "160"
name = ""
owner = ""
}
# Only use the import statement, if you want to import a existing sqlserverflex database
import {
to = stackitprivatepreview_sqlserverflexalpha_database.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id},${var.sql_user_id}"
}
import {
to = stackitprivatepreview_sqlserverflexalpha_database.import-example
identity = {
project_id = "project.id"
region = "region"
instance_id = "instance.id"
database_id = "database.id"
}
}

View file

@ -1,12 +0,0 @@
resource "stackitprivatepreview_sqlserverflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
username = "username"
roles = ["role"]
}
# Only use the import statement, if you want to import an existing sqlserverflex user
import {
to = stackitprivatepreview_sqlserverflexalpha_user.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id},${var.sql_user_id}"
}

View file

@ -1,12 +0,0 @@
resource "stackitprivatepreview_sqlserverflexalpha_user" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
username = "username"
roles = ["role"]
}
# Only use the import statement, if you want to import an existing sqlserverflex user
import {
to = stackitprivatepreview_sqlserverflexalpha_user.import-example
id = "${var.project_id},${var.region},${var.sql_instance_id},${var.sql_user_id}"
}

261
go.mod
View file

@ -2,17 +2,10 @@ module tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stac
go 1.25.6 go 1.25.6
require ( require (
github.com/SladkyCitron/slogcolor v1.8.0 github.com/MatusOllah/slogcolor v1.7.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/golangci/golangci-lint/v2 v2.10.1
github.com/google/go-cmp v0.7.0 github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-codegen-framework v0.4.1
github.com/hashicorp/terraform-plugin-codegen-openapi v0.3.0
github.com/hashicorp/terraform-plugin-docs v0.24.0
github.com/hashicorp/terraform-plugin-framework v1.17.0 github.com/hashicorp/terraform-plugin-framework v1.17.0
github.com/hashicorp/terraform-plugin-framework-validators v0.19.0 github.com/hashicorp/terraform-plugin-framework-validators v0.19.0
github.com/hashicorp/terraform-plugin-go v0.29.0 github.com/hashicorp/terraform-plugin-go v0.29.0
@ -20,283 +13,75 @@ require (
github.com/hashicorp/terraform-plugin-testing v1.14.0 github.com/hashicorp/terraform-plugin-testing v1.14.0
github.com/iancoleman/strcase v0.3.0 github.com/iancoleman/strcase v0.3.0
github.com/ivanpirog/coloredcobra v1.0.1 github.com/ivanpirog/coloredcobra v1.0.1
github.com/jarcoal/httpmock v1.4.1
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/ldez/go-git-cmd-wrapper/v2 v2.9.1 github.com/ldez/go-git-cmd-wrapper/v2 v2.9.1
github.com/spf13/cobra v1.10.2 github.com/spf13/cobra v1.10.2
github.com/stackitcloud/stackit-sdk-go/core v0.21.1 github.com/stackitcloud/stackit-sdk-go/core v0.21.0
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.23-alpha github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.23-alpha
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.4.1
github.com/teambition/rrule-go v1.8.2 github.com/teambition/rrule-go v1.8.2
golang.org/x/tools v0.42.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require github.com/hashicorp/go-retryablehttp v0.7.8 // indirect require (
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
golang.org/x/telemetry v0.0.0-20260116145544-c6413dc483f5 // indirect
)
require ( require (
4d63.com/gocheckcompilerdirectives v1.3.0 // indirect
4d63.com/gochecknoglobals v0.2.2 // indirect
codeberg.org/chavacava/garif v0.2.0 // indirect
codeberg.org/polyfloyd/go-errorlint v1.9.0 // indirect
dario.cat/mergo v1.0.1 // indirect dario.cat/mergo v1.0.1 // indirect
dev.gaijin.team/go/exhaustruct/v4 v4.0.0 // indirect
dev.gaijin.team/go/golib v0.6.0 // indirect
github.com/4meepo/tagalign v1.4.3 // indirect
github.com/Abirdcfly/dupword v0.1.7 // indirect
github.com/AdminBenni/iota-mixing v1.0.0 // indirect
github.com/AlwxSin/noinlineerr v1.0.5 // indirect
github.com/Antonboom/errname v1.1.1 // indirect
github.com/Antonboom/nilnil v1.1.1 // indirect
github.com/Antonboom/testifylint v1.6.4 // indirect
github.com/BurntSushi/toml v1.6.0 // indirect
github.com/Djarvur/go-err113 v0.1.1 // indirect
github.com/Kunde21/markdownfmt/v3 v3.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/MirrexOne/unqueryvet v1.5.3 // indirect
github.com/OpenPeeDeeP/depguard/v2 v2.2.1 // indirect
github.com/ProtonMail/go-crypto v1.3.0 // indirect github.com/ProtonMail/go-crypto v1.3.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect github.com/agext/levenshtein v1.2.3 // indirect
github.com/alecthomas/chroma/v2 v2.23.1 // indirect
github.com/alecthomas/go-check-sumtype v0.3.1 // indirect
github.com/alexkohler/nakedret/v2 v2.0.6 // indirect
github.com/alexkohler/prealloc v1.0.2 // indirect
github.com/alfatraining/structtag v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
github.com/alingse/nilnesserr v0.2.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect github.com/cloudflare/circl v1.6.2 // indirect
github.com/ashanbrown/forbidigo/v2 v2.3.0 // indirect
github.com/ashanbrown/makezero/v2 v2.1.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bkielbasa/cyclop v1.2.3 // indirect
github.com/blizzy78/varnamelen v0.8.0 // indirect
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
github.com/bombsimon/wsl/v4 v4.7.0 // indirect
github.com/bombsimon/wsl/v5 v5.6.0 // indirect
github.com/breml/bidichk v0.3.3 // indirect
github.com/breml/errchkjson v0.4.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/butuzov/ireturn v0.4.0 // indirect
github.com/butuzov/mirror v1.3.0 // indirect
github.com/catenacyber/perfsprint v0.10.1 // indirect
github.com/ccojocar/zxcvbn-go v1.0.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charithe/durationcheck v0.0.11 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/x/ansi v0.10.1 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/ckaznocha/intrange v0.3.1 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
github.com/curioswitch/go-reassign v0.3.0 // indirect
github.com/daixiang0/gci v0.13.7 // indirect
github.com/dave/dst v0.27.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denis-tingaikin/go-header v0.5.0 // indirect
github.com/dlclark/regexp2 v1.11.5 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/ettle/strcase v0.2.0 // indirect
github.com/fatih/color v1.18.0 // indirect github.com/fatih/color v1.18.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
github.com/firefart/nonamedreturns v1.0.6 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/ghostiam/protogetter v0.3.20 // indirect
github.com/go-critic/go-critic v0.14.3 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
github.com/go-toolsmith/astcopy v1.1.0 // indirect
github.com/go-toolsmith/astequal v1.2.0 // indirect
github.com/go-toolsmith/astfmt v1.1.0 // indirect
github.com/go-toolsmith/astp v1.1.0 // indirect
github.com/go-toolsmith/strparse v1.1.0 // indirect
github.com/go-toolsmith/typep v1.1.0 // indirect
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
github.com/go-xmlfmt/xmlfmt v1.1.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/godoc-lint/godoc-lint v0.11.2 // indirect
github.com/gofrs/flock v0.13.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/golangci/asciicheck v0.5.0 // indirect
github.com/golangci/dupl v0.0.0-20250308024227-f665c8d69b32 // indirect
github.com/golangci/go-printf-func-name v0.1.1 // indirect
github.com/golangci/gofmt v0.0.0-20250106114630-d62b90e6713d // indirect
github.com/golangci/golines v0.15.0 // indirect
github.com/golangci/misspell v0.8.0 // indirect
github.com/golangci/plugin-module-register v0.1.2 // indirect
github.com/golangci/revgrep v0.8.0 // indirect
github.com/golangci/swaggoswag v0.0.0-20250504205917-77f2aca3143e // indirect
github.com/golangci/unconvert v0.0.0-20250410112200-a129a6e6413e // indirect
github.com/gordonklaus/ineffassign v0.2.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.5.0 // indirect
github.com/gostaticanalysis/forcetypeassert v0.2.0 // indirect
github.com/gostaticanalysis/nilerr v0.1.2 // indirect
github.com/hashicorp/cli v1.1.7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.5.0 // indirect github.com/hashicorp/go-cty v1.5.0 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.7.0 // indirect github.com/hashicorp/go-plugin v1.7.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.8.0 // indirect github.com/hashicorp/go-version v1.8.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/hashicorp/hc-install v0.9.2 // indirect
github.com/hashicorp/hc-install v0.9.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.24.0 // indirect github.com/hashicorp/hcl/v2 v2.24.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.25.0 // indirect github.com/hashicorp/terraform-exec v0.24.0 // indirect
github.com/hashicorp/terraform-json v0.27.2 // indirect github.com/hashicorp/terraform-json v0.27.2 // indirect
github.com/hashicorp/terraform-plugin-codegen-spec v0.2.0 // indirect github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.2 // indirect
github.com/hashicorp/terraform-registry-address v0.4.0 // indirect github.com/hashicorp/terraform-registry-address v0.4.0 // indirect
github.com/hashicorp/terraform-svchost v0.2.0 // indirect github.com/hashicorp/terraform-svchost v0.2.0 // indirect
github.com/hashicorp/yamux v0.1.2 // indirect github.com/hashicorp/yamux v0.1.2 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jgautheron/goconst v1.8.2 // indirect github.com/kr/text v0.2.0 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jjti/go-spancheck v0.6.5 // indirect
github.com/julz/importas v0.2.0 // indirect
github.com/karamaru-alpha/copyloopvar v1.2.2 // indirect
github.com/kisielk/errcheck v1.9.0 // indirect
github.com/kkHAIKE/contextcheck v1.1.6 // indirect
github.com/kulti/thelper v0.7.1 // indirect
github.com/kunwardeep/paralleltest v1.0.15 // indirect
github.com/lasiar/canonicalheader v1.1.2 // indirect
github.com/ldez/exptostd v0.4.5 // indirect
github.com/ldez/gomoddirectives v0.8.0 // indirect
github.com/ldez/grignotin v0.10.1 // indirect
github.com/ldez/structtags v0.6.1 // indirect
github.com/ldez/tagliatelle v0.7.2 // indirect
github.com/ldez/usetesting v0.5.0 // indirect
github.com/leonklingele/grouper v1.1.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/macabu/inamedparam v0.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/manuelarte/embeddedstructfieldcheck v0.4.0 // indirect
github.com/manuelarte/funcorder v0.5.0 // indirect
github.com/maratori/testableexamples v1.0.1 // indirect
github.com/maratori/testpackage v1.1.2 // indirect
github.com/matoous/godox v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mgechev/revive v1.14.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moricho/tparallel v0.3.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/nakabonne/nestif v0.3.1 // indirect
github.com/nishanths/exhaustive v0.12.0 // indirect
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/nunnatsa/ginkgolinter v0.23.0 // indirect
github.com/oklog/run v1.2.0 // indirect github.com/oklog/run v1.2.0 // indirect
github.com/pb33f/libopenapi v0.15.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/quasilyte/go-ruleguard v0.4.5 // indirect
github.com/quasilyte/go-ruleguard/dsl v0.3.23 // indirect
github.com/quasilyte/gogrep v0.5.0 // indirect
github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727 // indirect
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/raeperd/recvcheck v0.2.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/ryancurrah/gomodguard v1.4.1 // indirect
github.com/ryanrolds/sqlclosecheck v0.5.1 // indirect
github.com/sanposhiho/wastedassign/v2 v2.1.0 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect
github.com/sashamelentyev/interfacebloat v1.1.0 // indirect
github.com/sashamelentyev/usestdlibvars v1.29.0 // indirect
github.com/securego/gosec/v2 v2.23.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.4 // indirect
github.com/sivchari/containedctx v1.0.3 // indirect
github.com/sonatard/noctx v0.4.0 // indirect
github.com/sourcegraph/go-diff v0.7.0 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect github.com/spf13/pflag v1.0.10 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.3.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.11.1 // indirect github.com/stretchr/testify v1.11.1 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/tetafro/godot v1.5.4 // indirect
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67 // indirect
github.com/timonwong/loggercheck v0.11.0 // indirect
github.com/tomarrell/wrapcheck/v2 v2.12.0 // indirect
github.com/tommy-muehle/go-mnd/v2 v2.5.1 // indirect
github.com/ultraware/funlen v0.2.0 // indirect
github.com/ultraware/whitespace v0.2.0 // indirect
github.com/uudashr/gocognit v1.2.0 // indirect
github.com/uudashr/iface v1.4.1 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xen0n/gosmopolitan v1.3.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.3.0 // indirect
github.com/ykadowak/zerologlint v0.1.5 // indirect
github.com/yuin/goldmark v1.7.7 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
github.com/zclconf/go-cty v1.17.0 // indirect github.com/zclconf/go-cty v1.17.0 // indirect
gitlab.com/bosi/decorder v0.4.2 // indirect golang.org/x/crypto v0.47.0 // indirect
go-simpler.org/musttag v0.14.0 // indirect golang.org/x/mod v0.32.0 // indirect
go-simpler.org/sloglint v0.11.1 // indirect golang.org/x/net v0.49.0 // indirect
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
go.augendre.info/arangolint v0.4.0 // indirect
go.augendre.info/fatcontext v0.9.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
golang.org/x/exp/typeparams v0.0.0-20260209203927-2842357ff358 // indirect
golang.org/x/mod v0.33.0 // indirect
golang.org/x/net v0.50.0 // indirect
golang.org/x/sync v0.19.0 // indirect golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.41.0 // indirect golang.org/x/sys v0.40.0 // indirect
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 // indirect golang.org/x/text v0.33.0 // indirect
golang.org/x/text v0.34.0 // indirect golang.org/x/tools v0.41.0 // indirect
google.golang.org/appengine v1.6.8 // indirect google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260120221211-b8f7ae30c516 // indirect
google.golang.org/grpc v1.79.1 // indirect google.golang.org/grpc v1.78.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
honnef.co/go/tools v0.7.0 // indirect
mvdan.cc/gofumpt v0.9.2 // indirect
mvdan.cc/unparam v0.0.0-20251027182757-5beb8c8f8f15 // indirect
) )
tool golang.org/x/tools/cmd/goimports

1101
go.sum

File diff suppressed because it is too large Load diff

View file

@ -2,13 +2,6 @@
version: "2" version: "2"
run: run:
concurrency: 4 concurrency: 4
output:
formats:
text:
print-linter-name: true
print-issued-lines: true
colors: true
path: stdout
linters: linters:
enable: enable:
- bodyclose - bodyclose
@ -75,10 +68,6 @@ linters:
- name: empty-lines - name: empty-lines
- name: early-return - name: early-return
exclusions: exclusions:
paths:
- stackit-sdk-generator/
- generated/
- pkg_gen/
generated: lax generated: lax
warn-unused: true warn-unused: true
# Excluding configuration per-path, per-linter, per-text and per-source. # Excluding configuration per-path, per-linter, per-text and per-source.
@ -87,6 +76,14 @@ linters:
- path: _test\.go - path: _test\.go
linters: linters:
- gochecknoinits - gochecknoinits
paths:
- third_party/
- builtin/
- examples/
- tools/copy.go
- tools/main.go
- pkg_gen/
- cmd/
formatters: formatters:
enable: enable:
- gofmt - gofmt
@ -94,4 +91,12 @@ formatters:
settings: settings:
goimports: goimports:
local-prefixes: local-prefixes:
- tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview - tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview
exclusions:
generated: lax
paths:
- third_party/
- builtin/
- examples/
- pkg_gen/
- cmd/

View file

@ -1,39 +0,0 @@
package testutils
import (
"fmt"
"net/http"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/jarcoal/httpmock"
)
func TestName() string {
pc, _, _, _ := runtime.Caller(1)
nameFull := runtime.FuncForPC(pc).Name()
nameEnd := filepath.Ext(nameFull)
name := strings.TrimPrefix(nameEnd, ".")
return name
}
func ActivateEnvironmentHttpMocks() {
httpmock.RegisterNoResponder(
func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("no responder found for %s %s, please check your http mocks", req.Method, req.URL)
},
)
httpmock.RegisterRegexpResponder(
"GET",
regexp.MustCompile(`^https://api\.bap\.microsoft\.com/providers/Microsoft\.BusinessAppPlatform/locations/(europe|unitedstates)/environmentLanguages\?api-version=2023-06-01$`),
func(_ *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(
http.StatusOK,
httpmock.File("../../services/languages/tests/datasource/Validate_Read/get_languages.json").String(),
), nil
},
)
}

View file

@ -65,15 +65,15 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "msh-sna-pe-example2
resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser" { resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser" {
project_id = var.project_id project_id = var.project_id
instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example.instance_id instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example.instance_id
name = var.db_admin_username username = var.db_admin_username
roles = ["createdb", "login", "login"] roles = ["createdb", "login"]
# roles = ["createdb", "login", "createrole"] # roles = ["createdb", "login", "createrole"]
} }
resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser2" { resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser2" {
project_id = var.project_id project_id = var.project_id
instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example2.instance_id instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example2.instance_id
name = var.db_admin_username username = var.db_admin_username
roles = ["createdb", "login"] roles = ["createdb", "login"]
# roles = ["createdb", "login", "createrole"] # roles = ["createdb", "login", "createrole"]
} }
@ -81,7 +81,7 @@ resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser2" {
resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbuser" { resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbuser" {
project_id = var.project_id project_id = var.project_id
instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example.instance_id instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example.instance_id
name = var.db_name username = var.db_username
roles = ["login"] roles = ["login"]
# roles = ["createdb", "login", "createrole"] # roles = ["createdb", "login", "createrole"]
} }

View file

@ -1,5 +1,5 @@
data "stackitprivatepreview_sqlserverflexbeta_flavor" "sqlserver_flavor" { data "stackitprivatepreview_sqlserverflexalpha_flavor" "sqlserver_flavor" {
project_id = var.project_id project_id = var.project_id
region = "eu01" region = "eu01"
cpu = 4 cpu = 4
@ -9,5 +9,5 @@ data "stackitprivatepreview_sqlserverflexbeta_flavor" "sqlserver_flavor" {
} }
output "sqlserver_flavor" { output "sqlserver_flavor" {
value = data.stackitprivatepreview_sqlserverflexbeta_flavor.sqlserver_flavor.flavor_id value = data.stackitprivatepreview_sqlserverflexalpha_flavor.sqlserver_flavor.flavor_id
} }

View file

@ -18,15 +18,15 @@
# value = stackit_kms_key.key.key_id # value = stackit_kms_key.key.key_id
# } # }
resource "stackitprivatepreview_sqlserverflexbeta_instance" "msh-beta-sna-001" { resource "stackitprivatepreview_sqlserverflexalpha_instance" "msh-sna-001" {
project_id = var.project_id project_id = var.project_id
name = "msh-beta-sna-001" name = "msh-sna-001"
backup_schedule = "0 3 * * *" backup_schedule = "0 3 * * *"
retention_days = 31 retention_days = 31
flavor_id = data.stackitprivatepreview_sqlserverflexbeta_flavor.sqlserver_flavor.flavor_id flavor_id = data.stackitprivatepreview_sqlserverflexalpha_flavor.sqlserver_flavor.flavor_id
storage = { storage = {
class = "premium-perf2-stackit" class = "premium-perf2-stackit"
size = 10 size = 50
} }
version = 2022 version = 2022
encryption = { encryption = {
@ -34,11 +34,9 @@ resource "stackitprivatepreview_sqlserverflexbeta_instance" "msh-beta-sna-001" {
#keyring_id = stackit_kms_keyring.keyring.keyring_id #keyring_id = stackit_kms_keyring.keyring.keyring_id
#key_version = 1 #key_version = 1
# key with scope public # key with scope public
# kek_key_id = "fe039bcf-8d7b-431a-801d-9e81371a6b7b" kek_key_id = "fe039bcf-8d7b-431a-801d-9e81371a6b7b"
kek_key_id = "c6878f92-ce55-4b79-8236-ba9d001d7967" # msh-k-001
# key_id = var.key_id # key_id = var.key_id
# kek_key_ring_id = var.keyring_id kek_key_ring_id = var.keyring_id
kek_key_ring_id = "0dea3f5f-9947-4dda-a9d3-18418832cefe" # msh-kr-sna01
kek_key_version = var.key_version kek_key_version = var.key_version
service_account = var.sa_email service_account = var.sa_email
} }
@ -48,16 +46,83 @@ resource "stackitprivatepreview_sqlserverflexbeta_instance" "msh-beta-sna-001" {
} }
} }
resource "stackitprivatepreview_sqlserverflexbeta_user" "betauser" { resource "stackitprivatepreview_sqlserverflexalpha_instance" "msh-sna-101" {
project_id = var.project_id project_id = var.project_id
instance_id = stackitprivatepreview_sqlserverflexbeta_instance.msh-beta-sna-001.instance_id name = "msh-sna-101"
username = "betauser" backup_schedule = "0 3 * * *"
roles = ["##STACKIT_DatabaseManager##", "##STACKIT_LoginManager##"] retention_days = 31
flavor_id = data.stackitprivatepreview_sqlserverflexalpha_flavor.sqlserver_flavor.flavor_id
storage = {
class = "premium-perf2-stackit"
size = 50
}
version = 2022
encryption = {
#key_id = stackit_kms_key.key.key_id
#keyring_id = stackit_kms_keyring.keyring.keyring_id
#key_version = 1
# key with scope public
kek_key_id = "fe039bcf-8d7b-431a-801d-9e81371a6b7b"
# key_id = var.key_id
kek_key_ring_id = var.keyring_id
kek_key_version = var.key_version
service_account = var.sa_email
}
network = {
acl = ["0.0.0.0/0", "193.148.160.0/19"]
access_scope = "SNA"
}
} }
resource "stackitprivatepreview_sqlserverflexbeta_database" "betadb" { resource "stackitprivatepreview_sqlserverflexalpha_instance" "msh-nosna-001" {
project_id = var.project_id project_id = var.project_id
instance_id = stackitprivatepreview_sqlserverflexbeta_instance.msh-beta-sna-001.instance_id name = "msh-nosna-001"
name = "mshtest002" backup_schedule = "0 3 * * *"
owner = stackitprivatepreview_sqlserverflexbeta_user.betauser.username retention_days = 31
flavor_id = data.stackitprivatepreview_sqlserverflexalpha_flavor.sqlserver_flavor.flavor_id
storage = {
class = "premium-perf2-stackit"
size = 50
}
version = 2022
# encryption = {
# #key_id = stackit_kms_key.key.key_id
# #keyring_id = stackit_kms_keyring.keyring.keyring_id
# #key_version = 1
# #key_id = var.key_id
# # key with scope public
# key_id = "fe039bcf-8d7b-431a-801d-9e81371a6b7b"
# keyring_id = var.keyring_id
# key_version = var.key_version
# service_account = var.sa_email
# }
network = {
acl = ["0.0.0.0/0", "193.148.160.0/19"]
access_scope = "PUBLIC"
}
} }
# data "stackitprivatepreview_sqlserverflexalpha_instance" "test" {
# project_id = var.project_id
# instance_id = var.instance_id
# region = "eu01"
# }
# output "test" {
# value = data.stackitprivatepreview_sqlserverflexalpha_instance.test
# }
# resource "stackitprivatepreview_sqlserverflexalpha_user" "ptlsdbadminuser" {
# project_id = var.project_id
# instance_id = stackitprivatepreview_sqlserverflexalpha_instance.sqlsrv.instance_id
# username = var.db_admin_username
# roles = ["##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"]
# }
# resource "stackitprivatepreview_sqlserverflexalpha_user" "ptlsdbuser" {
# project_id = var.project_id
# instance_id = stackitprivatepreview_sqlserverflexalpha_instance.sqlsrv.instance_id
# username = var.db_username
# roles = ["##STACKIT_LoginManager##"]
# }

19
scripts/lint-golangci-lint.sh Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env bash
# This script lints the SDK modules and the internal examples
# Pre-requisites: golangci-lint
set -eo pipefail
ROOT_DIR=$(git rev-parse --show-toplevel)
GOLANG_CI_YAML_PATH="${ROOT_DIR}/golang-ci.yaml"
GOLANG_CI_ARGS="--allow-parallel-runners --timeout=5m --config=${GOLANG_CI_YAML_PATH}"
if type -p golangci-lint >/dev/null; then
:
else
echo "golangci-lint not installed, unable to proceed."
exit 1
fi
cd ${ROOT_DIR}
golangci-lint run ${GOLANG_CI_ARGS}

View file

@ -17,7 +17,11 @@ elif [ "$action" = "tools" ]; then
go mod download go mod download
go install golang.org/x/tools/cmd/goimports@v0.42.0 # go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.2
# go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.21.0
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.24.0
else else
echo "Invalid action: '$action', please use $0 help for help" echo "Invalid action: '$action', please use $0 help for help"
fi fi

View file

@ -14,5 +14,5 @@ fi
mkdir -p ${ROOT_DIR}/docs mkdir -p ${ROOT_DIR}/docs
echo ">> Generating documentation" echo ">> Generating documentation"
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs generate \ tfplugindocs generate \
--provider-name "stackitprivatepreview" --provider-name "stackitprivatepreview"

View file

@ -32,7 +32,7 @@ const (
type EphemeralProviderData struct { type EphemeralProviderData struct {
ProviderData ProviderData
PrivateKey string //nolint:gosec //this is a placeholder and not used in this code PrivateKey string
PrivateKeyPath string PrivateKeyPath string
ServiceAccountKey string ServiceAccountKey string
ServiceAccountKeyPath string ServiceAccountKeyPath string
@ -105,13 +105,11 @@ func DiagsToError(diags diag.Diagnostics) error {
diagsError := diags.Errors() diagsError := diags.Errors()
diagsStrings := make([]string, 0) diagsStrings := make([]string, 0)
for _, diagnostic := range diagsError { for _, diagnostic := range diagsError {
diagsStrings = append( diagsStrings = append(diagsStrings, fmt.Sprintf(
diagsStrings, fmt.Sprintf( "(%s) %s",
"(%s) %s", diagnostic.Summary(),
diagnostic.Summary(), diagnostic.Detail(),
diagnostic.Detail(), ))
),
)
} }
return fmt.Errorf("%s", strings.Join(diagsStrings, ";")) return fmt.Errorf("%s", strings.Join(diagsStrings, ";"))
} }
@ -138,22 +136,14 @@ func LogAndAddWarning(ctx context.Context, diags *diag.Diagnostics, summary, det
func LogAndAddWarningBeta(ctx context.Context, diags *diag.Diagnostics, name string, resourceType ResourceType) { func LogAndAddWarningBeta(ctx context.Context, diags *diag.Diagnostics, name string, resourceType ResourceType) {
warnTitle := fmt.Sprintf("The %s %q is in beta", resourceType, name) warnTitle := fmt.Sprintf("The %s %q is in beta", resourceType, name)
warnContent := fmt.Sprintf( warnContent := fmt.Sprintf("The %s %q is in beta and may be subject to breaking changes in the future. Use with caution.", resourceType, name)
"The %s %q is in beta and may be subject to breaking changes in the future. Use with caution.",
resourceType,
name,
)
tflog.Warn(ctx, fmt.Sprintf("%s | %s", warnTitle, warnContent)) tflog.Warn(ctx, fmt.Sprintf("%s | %s", warnTitle, warnContent))
diags.AddWarning(warnTitle, warnContent) diags.AddWarning(warnTitle, warnContent)
} }
func LogAndAddErrorBeta(ctx context.Context, diags *diag.Diagnostics, name string, resourceType ResourceType) { func LogAndAddErrorBeta(ctx context.Context, diags *diag.Diagnostics, name string, resourceType ResourceType) {
errTitle := fmt.Sprintf("The %s %q is in beta and beta is not enabled", resourceType, name) errTitle := fmt.Sprintf("The %s %q is in beta and beta is not enabled", resourceType, name)
errContent := fmt.Sprintf( errContent := fmt.Sprintf(`The %s %q is in beta and the beta functionality is currently not enabled. To enable it, set the environment variable STACKIT_TF_ENABLE_BETA_RESOURCES to "true" or set the "enable_beta_resources" provider field to true.`, resourceType, name)
`The %s %q is in beta and the beta functionality is currently not enabled. To enable it, set the environment variable STACKIT_TF_ENABLE_BETA_RESOURCES to "true" or set the "enable_beta_resources" provider field to true.`,
resourceType,
name,
)
tflog.Error(ctx, fmt.Sprintf("%s | %s", errTitle, errContent)) tflog.Error(ctx, fmt.Sprintf("%s | %s", errTitle, errContent))
diags.AddError(errTitle, errContent) diags.AddError(errTitle, errContent)
} }
@ -171,10 +161,8 @@ func LogResponse(ctx context.Context) context.Context {
traceId := runtime.GetTraceId(ctx) traceId := runtime.GetTraceId(ctx)
ctx = tflog.SetField(ctx, "x-trace-id", traceId) ctx = tflog.SetField(ctx, "x-trace-id", traceId)
tflog.Info( tflog.Info(ctx, "response data", map[string]interface{}{
ctx, "response data", map[string]interface{}{ "x-trace-id": traceId,
"x-trace-id": traceId, })
},
)
return ctx return ctx
} }

View file

@ -1,237 +0,0 @@
package core
import (
"context"
"crypto/rand"
"errors"
"fmt"
"math/big"
"net/http"
"time"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
const (
// backoffMultiplier is the factor by which the delay is multiplied for exponential backoff.
backoffMultiplier = 2
// jitterFactor is the divisor used to calculate jitter (e.g., half of the base delay).
jitterFactor = 2
)
var (
// ErrRequestFailedAfterRetries is returned when a request fails after all retry attempts.
ErrRequestFailedAfterRetries = errors.New("request failed after all retry attempts")
)
// RetryRoundTripper implements an http.RoundTripper that adds automatic retry logic for failed requests.
type RetryRoundTripper struct {
next http.RoundTripper
maxRetries int
initialDelay time.Duration
maxDelay time.Duration
perTryTimeout time.Duration
}
// NewRetryRoundTripper creates a new instance of the RetryRoundTripper with the specified configuration.
func NewRetryRoundTripper(
next http.RoundTripper,
maxRetries int,
initialDelay, maxDelay, perTryTimeout time.Duration,
) *RetryRoundTripper {
return &RetryRoundTripper{
next: next,
maxRetries: maxRetries,
initialDelay: initialDelay,
maxDelay: maxDelay,
perTryTimeout: perTryTimeout,
}
}
// RoundTrip executes the request and retries on failure.
func (rrt *RetryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := rrt.executeRequest(req)
if !rrt.shouldRetry(resp, err) {
if err != nil {
return resp, fmt.Errorf("initial request failed, not retrying: %w", err)
}
return resp, nil
}
return rrt.retryLoop(req, resp, err)
}
// executeRequest performs a single HTTP request with a per-try timeout.
func (rrt *RetryRoundTripper) executeRequest(req *http.Request) (*http.Response, error) {
ctx, cancel := context.WithTimeout(req.Context(), rrt.perTryTimeout)
defer cancel()
resp, err := rrt.next.RoundTrip(req.WithContext(ctx))
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return resp, fmt.Errorf("per-try timeout of %v exceeded: %w", rrt.perTryTimeout, err)
}
return resp, fmt.Errorf("http roundtrip failed: %w", err)
}
return resp, nil
}
// retryLoop handles the retry logic for a failed request.
func (rrt *RetryRoundTripper) retryLoop(
req *http.Request,
initialResp *http.Response,
initialErr error,
) (*http.Response, error) {
var (
lastErr = initialErr
resp = initialResp
currentDelay = rrt.initialDelay
)
ctx := req.Context()
for attempt := 1; attempt <= rrt.maxRetries; attempt++ {
rrt.logRetryAttempt(ctx, attempt, currentDelay, lastErr)
waitDuration := rrt.calculateWaitDurationWithJitter(ctx, currentDelay)
if err := rrt.waitForDelay(ctx, waitDuration); err != nil {
return nil, err // Context was canceled during wait.
}
// Exponential backoff for the next potential retry.
currentDelay = rrt.updateCurrentDelay(currentDelay)
// Retry attempt.
resp, lastErr = rrt.executeRequest(req)
if !rrt.shouldRetry(resp, lastErr) {
if lastErr != nil {
return resp, fmt.Errorf("request failed on retry attempt %d: %w", attempt, lastErr)
}
return resp, nil
}
}
return nil, rrt.handleFinalError(ctx, resp, lastErr)
}
// logRetryAttempt logs the details of a retry attempt.
func (rrt *RetryRoundTripper) logRetryAttempt(
ctx context.Context,
attempt int,
delay time.Duration,
err error,
) {
tflog.Info(
ctx, "Request failed, retrying...", map[string]interface{}{
"attempt": attempt,
"max_attempts": rrt.maxRetries,
"delay": delay,
"error": err,
},
)
}
// updateCurrentDelay calculates the next delay for exponential backoff.
func (rrt *RetryRoundTripper) updateCurrentDelay(currentDelay time.Duration) time.Duration {
currentDelay *= backoffMultiplier
if currentDelay > rrt.maxDelay {
return rrt.maxDelay
}
return currentDelay
}
// handleFinalError constructs and returns the final error after all retries have been exhausted.
func (rrt *RetryRoundTripper) handleFinalError(
ctx context.Context,
resp *http.Response,
lastErr error,
) error {
if resp != nil {
if err := resp.Body.Close(); err != nil {
tflog.Warn(
ctx, "Failed to close response body", map[string]interface{}{
"error": err.Error(),
},
)
}
}
if lastErr != nil {
return fmt.Errorf("%w: %w", ErrRequestFailedAfterRetries, lastErr)
}
// This case occurs if shouldRetry was true due to a retryable status code,
// but all retries failed with similar status codes.
if resp != nil {
return fmt.Errorf(
"%w: last retry attempt failed with status code %d",
ErrRequestFailedAfterRetries,
resp.StatusCode,
)
}
return fmt.Errorf("%w: no response received", ErrRequestFailedAfterRetries)
}
// shouldRetry determines if a request should be retried based on the response or an error.
func (rrt *RetryRoundTripper) shouldRetry(resp *http.Response, err error) bool {
if err != nil {
return true
}
if resp != nil {
if resp.StatusCode == http.StatusBadGateway ||
resp.StatusCode == http.StatusServiceUnavailable ||
resp.StatusCode == http.StatusGatewayTimeout {
return true
}
}
return false
}
// calculateWaitDurationWithJitter calculates the backoff duration for the next retry,
// adding a random jitter to prevent thundering herd issues.
func (rrt *RetryRoundTripper) calculateWaitDurationWithJitter(
ctx context.Context,
baseDelay time.Duration,
) time.Duration {
if baseDelay <= 0 {
return 0
}
maxJitter := int64(baseDelay / jitterFactor)
if maxJitter <= 0 {
return baseDelay
}
random, err := rand.Int(rand.Reader, big.NewInt(maxJitter))
if err != nil {
tflog.Warn(
ctx, "Failed to generate random jitter, proceeding without it.", map[string]interface{}{
"error": err.Error(),
},
)
return baseDelay
}
jitter := time.Duration(random.Int64())
return baseDelay + jitter
}
// waitForDelay pauses execution for a given duration or until the context is canceled.
func (rrt *RetryRoundTripper) waitForDelay(ctx context.Context, delay time.Duration) error {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled during backoff wait: %w", ctx.Err())
case <-time.After(delay):
return nil
}
}

View file

@ -1,252 +0,0 @@
package core
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
type mockRoundTripper struct {
roundTripFunc func(req *http.Request) (*http.Response, error)
callCount int32
}
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&m.callCount, 1)
return m.roundTripFunc(req)
}
func (m *mockRoundTripper) CallCount() int32 {
return atomic.LoadInt32(&m.callCount)
}
func TestRetryRoundTripper_RoundTrip(t *testing.T) {
t.Parallel()
testRetryConfig := func(next http.RoundTripper) *RetryRoundTripper {
return NewRetryRoundTripper(
next,
3,
1*time.Millisecond,
10*time.Millisecond,
50*time.Millisecond,
)
}
noRetryTests := []struct {
name string
mockStatusCode int
expectedStatusCode int
}{
{
name: "should succeed on the first try",
mockStatusCode: http.StatusOK,
expectedStatusCode: http.StatusOK,
},
{
name: "should not retry on a non-retryable status code like 400",
mockStatusCode: http.StatusBadRequest,
expectedStatusCode: http.StatusBadRequest,
},
}
for _, testCase := range noRetryTests {
t.Run(
testCase.name, func(t *testing.T) {
t.Parallel()
mock := &mockRoundTripper{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: testCase.mockStatusCode,
Body: io.NopCloser(nil),
Request: req,
}, nil
},
}
tripper := testRetryConfig(mock)
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
resp, err := tripper.RoundTrip(req)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("failed to close response body: %v", closeErr)
}
}()
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if resp.StatusCode != testCase.expectedStatusCode {
t.Fatalf("expected status code %d, got %d", testCase.expectedStatusCode, resp.StatusCode)
}
if mock.CallCount() != 1 {
t.Fatalf("expected 1 call, got %d", mock.CallCount())
}
},
)
}
t.Run(
"should retry on retryable status code (503) and eventually fail", func(t *testing.T) {
t.Parallel()
mock := &mockRoundTripper{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusServiceUnavailable,
Body: io.NopCloser(nil),
Request: req,
}, nil
},
}
tripper := testRetryConfig(mock)
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
resp, err := tripper.RoundTrip(req)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("failed to close response body: %v", closeErr)
}
}()
}
if err == nil {
t.Fatal("expected an error, but got nil")
}
expectedErrorMsg := "last retry attempt failed with status code 503"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Fatalf("expected error to contain %q, got %q", expectedErrorMsg, err.Error())
}
if mock.CallCount() != 4 { // 1 initial + 3 retries
t.Fatalf("expected 4 calls, got %d", mock.CallCount())
}
},
)
t.Run(
"should succeed after one retry", func(t *testing.T) {
t.Parallel()
mock := &mockRoundTripper{}
mock.roundTripFunc = func(req *http.Request) (*http.Response, error) {
if mock.CallCount() < 2 {
return &http.Response{
StatusCode: http.StatusServiceUnavailable,
Body: io.NopCloser(nil),
Request: req,
}, nil
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(nil),
Request: req,
}, nil
}
tripper := testRetryConfig(mock)
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
resp, err := tripper.RoundTrip(req)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("failed to close response body: %v", closeErr)
}
}()
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
if mock.CallCount() != 2 {
t.Fatalf("expected 2 calls, got %d", mock.CallCount())
}
},
)
t.Run(
"should retry on network error", func(t *testing.T) {
t.Parallel()
mockErr := errors.New("simulated network error")
mock := &mockRoundTripper{
roundTripFunc: func(_ *http.Request) (*http.Response, error) {
return nil, mockErr
},
}
tripper := testRetryConfig(mock)
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
resp, err := tripper.RoundTrip(req)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("failed to close response body: %v", closeErr)
}
}()
}
if !errors.Is(err, mockErr) {
t.Fatalf("expected error to be %v, got %v", mockErr, err)
}
if mock.CallCount() != 4 { // 1 initial + 3 retries
t.Fatalf("expected 4 calls, got %d", mock.CallCount())
}
},
)
t.Run(
"should abort retries if the main context is canceled", func(t *testing.T) {
t.Parallel()
mock := &mockRoundTripper{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
select {
case <-time.After(100 * time.Millisecond):
return nil, errors.New("this should not be returned")
case <-req.Context().Done():
return nil, req.Context().Err()
}
},
}
tripper := testRetryConfig(mock)
baseCtx := context.Background()
ctx, cancel := context.WithTimeout(baseCtx, 20*time.Millisecond)
defer cancel()
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody).WithContext(ctx)
resp, err := tripper.RoundTrip(req)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("failed to close response body: %v", closeErr)
}
}()
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected error to be context.DeadlineExceeded, got %v", err)
}
if mock.CallCount() != 1 {
t.Fatalf("expected 1 call, got %d", mock.CallCount())
}
},
)
}

View file

@ -179,7 +179,7 @@ func (r *databaseResource) Create(
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
region := model.Region.ValueString() region := model.InstanceId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
@ -281,12 +281,24 @@ func (r *databaseResource) Read(
return return
} }
// Read identity data
var identityData DatabaseResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId, region, instanceId, databaseId, errExt := r.extractIdentityData(model, identityData)
instanceId := model.InstanceId.ValueString() if errExt != nil {
region := model.Region.ValueString() core.LogAndAddError(
databaseId := model.DatabaseId.ValueInt64() ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -321,7 +333,6 @@ func (r *databaseResource) Read(
return return
} }
// TODO: use values from api to identify drift
// Save identity into Terraform state // Save identity into Terraform state
identity := DatabaseResourceIdentityModel{ identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId), ProjectID: types.StringValue(projectId),
@ -356,12 +367,24 @@ func (r *databaseResource) Update(
return return
} }
// Read identity data
var identityData DatabaseResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId, region, instanceId, databaseId64, errExt := r.extractIdentityData(model, identityData)
instanceId := model.InstanceId.ValueString() if errExt != nil {
region := model.Region.ValueString() core.LogAndAddError(
databaseId64 := model.DatabaseId.ValueInt64() ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
if databaseId64 > math.MaxInt32 { if databaseId64 > math.MaxInt32 {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (databaseId)") core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (databaseId)")

View file

@ -33,27 +33,15 @@ func InstanceDataSourceSchema(ctx context.Context) schema.Schema {
}, },
"connection_info": schema.SingleNestedAttribute{ "connection_info": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"write": schema.SingleNestedAttribute{ "host": schema.StringAttribute{
Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{
Computed: true,
Description: "The host of the instance.",
MarkdownDescription: "The host of the instance.",
},
"port": schema.Int64Attribute{
Computed: true,
Description: "The port of the instance.",
MarkdownDescription: "The port of the instance.",
},
},
CustomType: WriteType{
ObjectType: types.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
},
Computed: true, Computed: true,
Description: "The DNS name and port in the instance overview", Description: "The host of the instance.",
MarkdownDescription: "The DNS name and port in the instance overview", MarkdownDescription: "The host of the instance.",
},
"port": schema.Int64Attribute{
Computed: true,
Description: "The port of the instance.",
MarkdownDescription: "The port of the instance.",
}, },
}, },
CustomType: ConnectionInfoType{ CustomType: ConnectionInfoType{
@ -62,8 +50,8 @@ func InstanceDataSourceSchema(ctx context.Context) schema.Schema {
}, },
}, },
Computed: true, Computed: true,
Description: "The connection information of the instance", Description: "The DNS name and port in the instance overview",
MarkdownDescription: "The connection information of the instance", MarkdownDescription: "The DNS name and port in the instance overview",
}, },
"encryption": schema.SingleNestedAttribute{ "encryption": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
@ -255,22 +243,40 @@ func (t ConnectionInfoType) ValueFromObject(ctx context.Context, in basetypes.Ob
attributes := in.Attributes() attributes := in.Attributes()
writeAttribute, ok := attributes["write"] hostAttribute, ok := attributes["host"]
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Missing", "Attribute Missing",
`write is missing from object`) `host is missing from object`)
return nil, diags return nil, diags
} }
writeVal, ok := writeAttribute.(basetypes.ObjectValue) hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Wrong Type", "Attribute Wrong Type",
fmt.Sprintf(`write expected to be basetypes.ObjectValue, was: %T`, writeAttribute)) fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return nil, diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
} }
if diags.HasError() { if diags.HasError() {
@ -278,7 +284,8 @@ func (t ConnectionInfoType) ValueFromObject(ctx context.Context, in basetypes.Ob
} }
return ConnectionInfoValue{ return ConnectionInfoValue{
Write: writeVal, Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown, state: attr.ValueStateKnown,
}, diags }, diags
} }
@ -346,22 +353,40 @@ func NewConnectionInfoValue(attributeTypes map[string]attr.Type, attributes map[
return NewConnectionInfoValueUnknown(), diags return NewConnectionInfoValueUnknown(), diags
} }
writeAttribute, ok := attributes["write"] hostAttribute, ok := attributes["host"]
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Missing", "Attribute Missing",
`write is missing from object`) `host is missing from object`)
return NewConnectionInfoValueUnknown(), diags return NewConnectionInfoValueUnknown(), diags
} }
writeVal, ok := writeAttribute.(basetypes.ObjectValue) hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Wrong Type", "Attribute Wrong Type",
fmt.Sprintf(`write expected to be basetypes.ObjectValue, was: %T`, writeAttribute)) fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return NewConnectionInfoValueUnknown(), diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
} }
if diags.HasError() { if diags.HasError() {
@ -369,7 +394,8 @@ func NewConnectionInfoValue(attributeTypes map[string]attr.Type, attributes map[
} }
return ConnectionInfoValue{ return ConnectionInfoValue{
Write: writeVal, Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown, state: attr.ValueStateKnown,
}, diags }, diags
} }
@ -442,401 +468,12 @@ func (t ConnectionInfoType) ValueType(ctx context.Context) attr.Value {
var _ basetypes.ObjectValuable = ConnectionInfoValue{} var _ basetypes.ObjectValuable = ConnectionInfoValue{}
type ConnectionInfoValue struct { type ConnectionInfoValue struct {
Write basetypes.ObjectValue `tfsdk:"write"`
state attr.ValueState
}
func (v ConnectionInfoValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 1)
var val tftypes.Value
var err error
attrTypes["write"] = basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
}.TerraformType(ctx)
objectType := tftypes.Object{AttributeTypes: attrTypes}
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 1)
val, err = v.Write.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["write"] = val
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
return tftypes.NewValue(objectType, vals), nil
case attr.ValueStateNull:
return tftypes.NewValue(objectType, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(objectType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Object state in ToTerraformValue: %s", v.state))
}
}
func (v ConnectionInfoValue) IsNull() bool {
return v.state == attr.ValueStateNull
}
func (v ConnectionInfoValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown
}
func (v ConnectionInfoValue) String() string {
return "ConnectionInfoValue"
}
func (v ConnectionInfoValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
var write basetypes.ObjectValue
if v.Write.IsNull() {
write = types.ObjectNull(
WriteValue{}.AttributeTypes(ctx),
)
}
if v.Write.IsUnknown() {
write = types.ObjectUnknown(
WriteValue{}.AttributeTypes(ctx),
)
}
if !v.Write.IsNull() && !v.Write.IsUnknown() {
write = types.ObjectValueMust(
WriteValue{}.AttributeTypes(ctx),
v.Write.Attributes(),
)
}
attributeTypes := map[string]attr.Type{
"write": basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
}
if v.IsNull() {
return types.ObjectNull(attributeTypes), diags
}
if v.IsUnknown() {
return types.ObjectUnknown(attributeTypes), diags
}
objVal, diags := types.ObjectValue(
attributeTypes,
map[string]attr.Value{
"write": write,
})
return objVal, diags
}
func (v ConnectionInfoValue) Equal(o attr.Value) bool {
other, ok := o.(ConnectionInfoValue)
if !ok {
return false
}
if v.state != other.state {
return false
}
if v.state != attr.ValueStateKnown {
return true
}
if !v.Write.Equal(other.Write) {
return false
}
return true
}
func (v ConnectionInfoValue) Type(ctx context.Context) attr.Type {
return ConnectionInfoType{
basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx),
},
}
}
func (v ConnectionInfoValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{
"write": basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
}
}
var _ basetypes.ObjectTypable = WriteType{}
type WriteType struct {
basetypes.ObjectType
}
func (t WriteType) Equal(o attr.Type) bool {
other, ok := o.(WriteType)
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t WriteType) String() string {
return "WriteType"
}
func (t WriteType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
attributes := in.Attributes()
hostAttribute, ok := attributes["host"]
if !ok {
diags.AddError(
"Attribute Missing",
`host is missing from object`)
return nil, diags
}
hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return nil, diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
}
if diags.HasError() {
return nil, diags
}
return WriteValue{
Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown,
}, diags
}
func NewWriteValueNull() WriteValue {
return WriteValue{
state: attr.ValueStateNull,
}
}
func NewWriteValueUnknown() WriteValue {
return WriteValue{
state: attr.ValueStateUnknown,
}
}
func NewWriteValue(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) (WriteValue, diag.Diagnostics) {
var diags diag.Diagnostics
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/521
ctx := context.Background()
for name, attributeType := range attributeTypes {
attribute, ok := attributes[name]
if !ok {
diags.AddError(
"Missing WriteValue Attribute Value",
"While creating a WriteValue value, a missing attribute value was detected. "+
"A WriteValue must contain values for all attributes, even if null or unknown. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("WriteValue Attribute Name (%s) Expected Type: %s", name, attributeType.String()),
)
continue
}
if !attributeType.Equal(attribute.Type(ctx)) {
diags.AddError(
"Invalid WriteValue Attribute Type",
"While creating a WriteValue value, an invalid attribute value was detected. "+
"A WriteValue must use a matching attribute type for the value. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("WriteValue Attribute Name (%s) Expected Type: %s\n", name, attributeType.String())+
fmt.Sprintf("WriteValue Attribute Name (%s) Given Type: %s", name, attribute.Type(ctx)),
)
}
}
for name := range attributes {
_, ok := attributeTypes[name]
if !ok {
diags.AddError(
"Extra WriteValue Attribute Value",
"While creating a WriteValue value, an extra attribute value was detected. "+
"A WriteValue must not contain values beyond the expected attribute types. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Extra WriteValue Attribute Name: %s", name),
)
}
}
if diags.HasError() {
return NewWriteValueUnknown(), diags
}
hostAttribute, ok := attributes["host"]
if !ok {
diags.AddError(
"Attribute Missing",
`host is missing from object`)
return NewWriteValueUnknown(), diags
}
hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return NewWriteValueUnknown(), diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
}
if diags.HasError() {
return NewWriteValueUnknown(), diags
}
return WriteValue{
Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown,
}, diags
}
func NewWriteValueMust(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) WriteValue {
object, diags := NewWriteValue(attributeTypes, attributes)
if diags.HasError() {
// This could potentially be added to the diag package.
diagsStrings := make([]string, 0, len(diags))
for _, diagnostic := range diags {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"%s | %s | %s",
diagnostic.Severity(),
diagnostic.Summary(),
diagnostic.Detail()))
}
panic("NewWriteValueMust received error(s): " + strings.Join(diagsStrings, "\n"))
}
return object
}
func (t WriteType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
if in.Type() == nil {
return NewWriteValueNull(), nil
}
if !in.Type().Equal(t.TerraformType(ctx)) {
return nil, fmt.Errorf("expected %s, got %s", t.TerraformType(ctx), in.Type())
}
if !in.IsKnown() {
return NewWriteValueUnknown(), nil
}
if in.IsNull() {
return NewWriteValueNull(), nil
}
attributes := map[string]attr.Value{}
val := map[string]tftypes.Value{}
err := in.As(&val)
if err != nil {
return nil, err
}
for k, v := range val {
a, err := t.AttrTypes[k].ValueFromTerraform(ctx, v)
if err != nil {
return nil, err
}
attributes[k] = a
}
return NewWriteValueMust(WriteValue{}.AttributeTypes(ctx), attributes), nil
}
func (t WriteType) ValueType(ctx context.Context) attr.Value {
return WriteValue{}
}
var _ basetypes.ObjectValuable = WriteValue{}
type WriteValue struct {
Host basetypes.StringValue `tfsdk:"host"` Host basetypes.StringValue `tfsdk:"host"`
Port basetypes.Int64Value `tfsdk:"port"` Port basetypes.Int64Value `tfsdk:"port"`
state attr.ValueState state attr.ValueState
} }
func (v WriteValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) { func (v ConnectionInfoValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 2) attrTypes := make(map[string]tftypes.Type, 2)
var val tftypes.Value var val tftypes.Value
@ -881,19 +518,19 @@ func (v WriteValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error)
} }
} }
func (v WriteValue) IsNull() bool { func (v ConnectionInfoValue) IsNull() bool {
return v.state == attr.ValueStateNull return v.state == attr.ValueStateNull
} }
func (v WriteValue) IsUnknown() bool { func (v ConnectionInfoValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown return v.state == attr.ValueStateUnknown
} }
func (v WriteValue) String() string { func (v ConnectionInfoValue) String() string {
return "WriteValue" return "ConnectionInfoValue"
} }
func (v WriteValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) { func (v ConnectionInfoValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics var diags diag.Diagnostics
attributeTypes := map[string]attr.Type{ attributeTypes := map[string]attr.Type{
@ -919,8 +556,8 @@ func (v WriteValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, d
return objVal, diags return objVal, diags
} }
func (v WriteValue) Equal(o attr.Value) bool { func (v ConnectionInfoValue) Equal(o attr.Value) bool {
other, ok := o.(WriteValue) other, ok := o.(ConnectionInfoValue)
if !ok { if !ok {
return false return false
@ -945,15 +582,15 @@ func (v WriteValue) Equal(o attr.Value) bool {
return true return true
} }
func (v WriteValue) Type(ctx context.Context) attr.Type { func (v ConnectionInfoValue) Type(ctx context.Context) attr.Type {
return WriteType{ return ConnectionInfoType{
basetypes.ObjectType{ basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx), AttrTypes: v.AttributeTypes(ctx),
}, },
} }
} }
func (v WriteValue) AttributeTypes(ctx context.Context) map[string]attr.Type { func (v ConnectionInfoValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{ return map[string]attr.Type{
"host": basetypes.StringType{}, "host": basetypes.StringType{},
"port": basetypes.Int64Type{}, "port": basetypes.Int64Type{},

View file

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
postgresflexalphadatasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/datasources_gen" postgresflexalphadatasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/datasources_gen"
@ -19,6 +20,28 @@ func mapGetInstanceResponseToModel(
m *postgresflexalpharesource.InstanceModel, m *postgresflexalpharesource.InstanceModel,
resp *postgresflex.GetInstanceResponse, resp *postgresflex.GetInstanceResponse,
) error { ) error {
tflog.Debug(
ctx, ">>>> MSH DEBUG <<<<", map[string]interface{}{
"id": m.Id.ValueString(),
"instance_id": m.InstanceId.ValueString(),
"backup_schedule": m.BackupSchedule.ValueString(),
"flavor_id": m.FlavorId.ValueString(),
"encryption.kek_key_id": m.Encryption.KekKeyId.ValueString(),
"encryption.kek_key_ring_id": m.Encryption.KekKeyRingId.ValueString(),
"encryption.kek_key_version": m.Encryption.KekKeyVersion.ValueString(),
"encryption.service_account": m.Encryption.ServiceAccount.ValueString(),
"is_deletable": m.IsDeletable.ValueBool(),
"name": m.Name.ValueString(),
"status": m.Status.ValueString(),
"retention_days": m.RetentionDays.ValueInt64(),
"replicas": m.Replicas.ValueInt64(),
"network.instance_address": m.Network.InstanceAddress.ValueString(),
"network.router_address": m.Network.RouterAddress.ValueString(),
"version": m.Version.ValueString(),
"network.acl": m.Network.Acl.String(),
},
)
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule()) m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
m.Encryption = postgresflexalpharesource.NewEncryptionValueNull() m.Encryption = postgresflexalpharesource.NewEncryptionValueNull()
if resp.HasEncryption() { if resp.HasEncryption() {
@ -33,9 +56,9 @@ func mapGetInstanceResponseToModel(
) )
} }
isConnectionInfoIncomplete := resp.ConnectionInfo == nil || resp.ConnectionInfo.Write == nil || isConnectionInfoIncomplete := resp.ConnectionInfo == nil ||
resp.ConnectionInfo.Write.Host == nil || *resp.ConnectionInfo.Write.Host == "" || resp.ConnectionInfo.Host == nil || *resp.ConnectionInfo.Host == "" ||
resp.ConnectionInfo.Write.Port == nil || *resp.ConnectionInfo.Write.Port == 0 resp.ConnectionInfo.Port == nil || *resp.ConnectionInfo.Port == 0
if isConnectionInfoIncomplete { if isConnectionInfoIncomplete {
m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueNull() m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueNull()
@ -43,17 +66,22 @@ func mapGetInstanceResponseToModel(
m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueMust( m.ConnectionInfo = postgresflexalpharesource.NewConnectionInfoValueMust(
postgresflexalpharesource.ConnectionInfoValue{}.AttributeTypes(ctx), postgresflexalpharesource.ConnectionInfoValue{}.AttributeTypes(ctx),
map[string]attr.Value{ map[string]attr.Value{
"write": postgresflexalpharesource.NewWriteValueMust( "host": types.StringPointerValue(resp.ConnectionInfo.Host),
postgresflexalpharesource.WriteValue{}.AttributeTypes(ctx), "port": types.Int64PointerValue(resp.ConnectionInfo.Port),
map[string]attr.Value{
"host": types.StringPointerValue(resp.ConnectionInfo.Write.Host),
"port": types.Int64PointerValue(resp.ConnectionInfo.Write.Port),
},
),
}, },
) )
} }
m.ConnectionInfo.Host = types.StringValue("")
if host, ok := resp.ConnectionInfo.GetHostOk(); ok {
m.ConnectionInfo.Host = types.StringValue(host)
}
m.ConnectionInfo.Port = types.Int64Value(0)
if port, ok := resp.ConnectionInfo.GetPortOk(); ok {
m.ConnectionInfo.Port = types.Int64Value(port)
}
m.FlavorId = types.StringValue(resp.GetFlavorId()) m.FlavorId = types.StringValue(resp.GetFlavorId())
if m.Id.IsNull() || m.Id.IsUnknown() { if m.Id.IsNull() || m.Id.IsUnknown() {
m.Id = utils.BuildInternalTerraformId( m.Id = utils.BuildInternalTerraformId(
@ -159,9 +187,9 @@ func mapGetDataInstanceResponseToModel(
} }
func handleConnectionInfo(ctx context.Context, m *dataSourceModel, resp *postgresflex.GetInstanceResponse) { func handleConnectionInfo(ctx context.Context, m *dataSourceModel, resp *postgresflex.GetInstanceResponse) {
isConnectionInfoIncomplete := resp.ConnectionInfo == nil || resp.ConnectionInfo.Write == nil || isConnectionInfoIncomplete := resp.ConnectionInfo == nil ||
resp.ConnectionInfo.Write.Host == nil || *resp.ConnectionInfo.Write.Host == "" || resp.ConnectionInfo.Host == nil || *resp.ConnectionInfo.Host == "" ||
resp.ConnectionInfo.Write.Port == nil || *resp.ConnectionInfo.Write.Port == 0 resp.ConnectionInfo.Port == nil || *resp.ConnectionInfo.Port == 0
if isConnectionInfoIncomplete { if isConnectionInfoIncomplete {
m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueNull() m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueNull()
@ -169,13 +197,8 @@ func handleConnectionInfo(ctx context.Context, m *dataSourceModel, resp *postgre
m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueMust( m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueMust(
postgresflexalphadatasource.ConnectionInfoValue{}.AttributeTypes(ctx), postgresflexalphadatasource.ConnectionInfoValue{}.AttributeTypes(ctx),
map[string]attr.Value{ map[string]attr.Value{
"write": postgresflexalphadatasource.NewWriteValueMust( "host": types.StringPointerValue(resp.ConnectionInfo.Host),
postgresflexalphadatasource.WriteValue{}.AttributeTypes(ctx), "port": types.Int64PointerValue(resp.ConnectionInfo.Port),
map[string]attr.Value{
"host": types.StringPointerValue(resp.ConnectionInfo.Write.Host),
"port": types.Int64PointerValue(resp.ConnectionInfo.Write.Port),
},
),
}, },
) )
} }

View file

@ -243,8 +243,7 @@ func (r *instanceResource) Create(
return return
} }
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId). waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx)
WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -328,6 +327,10 @@ func (r *instanceResource) Read(
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
// projectId := model.ProjectId.ValueString()
// region := r.providerData.GetRegionWithOverride(model.Region)
// instanceId := model.InstanceId.ValueString()
var projectId string var projectId string
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() { if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
projectId = model.ProjectId.ValueString() projectId = model.ProjectId.ValueString()
@ -431,6 +434,18 @@ func (r *instanceResource) Update(
return return
} }
// if model.InstanceId.IsNull() || model.InstanceId.IsUnknown() {
// core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", "instanceId is null or unknown")
// return
//}
//
// if model.ProjectId.IsNull() || model.ProjectId.IsUnknown() {
// core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", "projectId is null or unknown")
// return
//}
// projectId := model.ProjectId.ValueString()
// instanceId := model.InstanceId.ValueString()
projectId := identityData.ProjectID.ValueString() projectId := identityData.ProjectID.ValueString()
instanceId := identityData.InstanceID.ValueString() instanceId := identityData.InstanceID.ValueString()
region := model.Region.ValueString() region := model.Region.ValueString()
@ -578,7 +593,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format: [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -35,27 +35,15 @@ func InstanceResourceSchema(ctx context.Context) schema.Schema {
}, },
"connection_info": schema.SingleNestedAttribute{ "connection_info": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"write": schema.SingleNestedAttribute{ "host": schema.StringAttribute{
Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{
Computed: true,
Description: "The host of the instance.",
MarkdownDescription: "The host of the instance.",
},
"port": schema.Int64Attribute{
Computed: true,
Description: "The port of the instance.",
MarkdownDescription: "The port of the instance.",
},
},
CustomType: WriteType{
ObjectType: types.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
},
Computed: true, Computed: true,
Description: "The DNS name and port in the instance overview", Description: "The host of the instance.",
MarkdownDescription: "The DNS name and port in the instance overview", MarkdownDescription: "The host of the instance.",
},
"port": schema.Int64Attribute{
Computed: true,
Description: "The port of the instance.",
MarkdownDescription: "The port of the instance.",
}, },
}, },
CustomType: ConnectionInfoType{ CustomType: ConnectionInfoType{
@ -64,8 +52,8 @@ func InstanceResourceSchema(ctx context.Context) schema.Schema {
}, },
}, },
Computed: true, Computed: true,
Description: "The connection information of the instance", Description: "The DNS name and port in the instance overview",
MarkdownDescription: "The connection information of the instance", MarkdownDescription: "The DNS name and port in the instance overview",
}, },
"encryption": schema.SingleNestedAttribute{ "encryption": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
@ -275,22 +263,40 @@ func (t ConnectionInfoType) ValueFromObject(ctx context.Context, in basetypes.Ob
attributes := in.Attributes() attributes := in.Attributes()
writeAttribute, ok := attributes["write"] hostAttribute, ok := attributes["host"]
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Missing", "Attribute Missing",
`write is missing from object`) `host is missing from object`)
return nil, diags return nil, diags
} }
writeVal, ok := writeAttribute.(basetypes.ObjectValue) hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Wrong Type", "Attribute Wrong Type",
fmt.Sprintf(`write expected to be basetypes.ObjectValue, was: %T`, writeAttribute)) fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return nil, diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
} }
if diags.HasError() { if diags.HasError() {
@ -298,7 +304,8 @@ func (t ConnectionInfoType) ValueFromObject(ctx context.Context, in basetypes.Ob
} }
return ConnectionInfoValue{ return ConnectionInfoValue{
Write: writeVal, Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown, state: attr.ValueStateKnown,
}, diags }, diags
} }
@ -366,22 +373,40 @@ func NewConnectionInfoValue(attributeTypes map[string]attr.Type, attributes map[
return NewConnectionInfoValueUnknown(), diags return NewConnectionInfoValueUnknown(), diags
} }
writeAttribute, ok := attributes["write"] hostAttribute, ok := attributes["host"]
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Missing", "Attribute Missing",
`write is missing from object`) `host is missing from object`)
return NewConnectionInfoValueUnknown(), diags return NewConnectionInfoValueUnknown(), diags
} }
writeVal, ok := writeAttribute.(basetypes.ObjectValue) hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok { if !ok {
diags.AddError( diags.AddError(
"Attribute Wrong Type", "Attribute Wrong Type",
fmt.Sprintf(`write expected to be basetypes.ObjectValue, was: %T`, writeAttribute)) fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return NewConnectionInfoValueUnknown(), diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
} }
if diags.HasError() { if diags.HasError() {
@ -389,7 +414,8 @@ func NewConnectionInfoValue(attributeTypes map[string]attr.Type, attributes map[
} }
return ConnectionInfoValue{ return ConnectionInfoValue{
Write: writeVal, Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown, state: attr.ValueStateKnown,
}, diags }, diags
} }
@ -462,401 +488,12 @@ func (t ConnectionInfoType) ValueType(ctx context.Context) attr.Value {
var _ basetypes.ObjectValuable = ConnectionInfoValue{} var _ basetypes.ObjectValuable = ConnectionInfoValue{}
type ConnectionInfoValue struct { type ConnectionInfoValue struct {
Write basetypes.ObjectValue `tfsdk:"write"`
state attr.ValueState
}
func (v ConnectionInfoValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 1)
var val tftypes.Value
var err error
attrTypes["write"] = basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
}.TerraformType(ctx)
objectType := tftypes.Object{AttributeTypes: attrTypes}
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 1)
val, err = v.Write.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["write"] = val
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
return tftypes.NewValue(objectType, vals), nil
case attr.ValueStateNull:
return tftypes.NewValue(objectType, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(objectType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Object state in ToTerraformValue: %s", v.state))
}
}
func (v ConnectionInfoValue) IsNull() bool {
return v.state == attr.ValueStateNull
}
func (v ConnectionInfoValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown
}
func (v ConnectionInfoValue) String() string {
return "ConnectionInfoValue"
}
func (v ConnectionInfoValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
var write basetypes.ObjectValue
if v.Write.IsNull() {
write = types.ObjectNull(
WriteValue{}.AttributeTypes(ctx),
)
}
if v.Write.IsUnknown() {
write = types.ObjectUnknown(
WriteValue{}.AttributeTypes(ctx),
)
}
if !v.Write.IsNull() && !v.Write.IsUnknown() {
write = types.ObjectValueMust(
WriteValue{}.AttributeTypes(ctx),
v.Write.Attributes(),
)
}
attributeTypes := map[string]attr.Type{
"write": basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
}
if v.IsNull() {
return types.ObjectNull(attributeTypes), diags
}
if v.IsUnknown() {
return types.ObjectUnknown(attributeTypes), diags
}
objVal, diags := types.ObjectValue(
attributeTypes,
map[string]attr.Value{
"write": write,
})
return objVal, diags
}
func (v ConnectionInfoValue) Equal(o attr.Value) bool {
other, ok := o.(ConnectionInfoValue)
if !ok {
return false
}
if v.state != other.state {
return false
}
if v.state != attr.ValueStateKnown {
return true
}
if !v.Write.Equal(other.Write) {
return false
}
return true
}
func (v ConnectionInfoValue) Type(ctx context.Context) attr.Type {
return ConnectionInfoType{
basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx),
},
}
}
func (v ConnectionInfoValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{
"write": basetypes.ObjectType{
AttrTypes: WriteValue{}.AttributeTypes(ctx),
},
}
}
var _ basetypes.ObjectTypable = WriteType{}
type WriteType struct {
basetypes.ObjectType
}
func (t WriteType) Equal(o attr.Type) bool {
other, ok := o.(WriteType)
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t WriteType) String() string {
return "WriteType"
}
func (t WriteType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
attributes := in.Attributes()
hostAttribute, ok := attributes["host"]
if !ok {
diags.AddError(
"Attribute Missing",
`host is missing from object`)
return nil, diags
}
hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return nil, diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
}
if diags.HasError() {
return nil, diags
}
return WriteValue{
Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown,
}, diags
}
func NewWriteValueNull() WriteValue {
return WriteValue{
state: attr.ValueStateNull,
}
}
func NewWriteValueUnknown() WriteValue {
return WriteValue{
state: attr.ValueStateUnknown,
}
}
func NewWriteValue(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) (WriteValue, diag.Diagnostics) {
var diags diag.Diagnostics
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/521
ctx := context.Background()
for name, attributeType := range attributeTypes {
attribute, ok := attributes[name]
if !ok {
diags.AddError(
"Missing WriteValue Attribute Value",
"While creating a WriteValue value, a missing attribute value was detected. "+
"A WriteValue must contain values for all attributes, even if null or unknown. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("WriteValue Attribute Name (%s) Expected Type: %s", name, attributeType.String()),
)
continue
}
if !attributeType.Equal(attribute.Type(ctx)) {
diags.AddError(
"Invalid WriteValue Attribute Type",
"While creating a WriteValue value, an invalid attribute value was detected. "+
"A WriteValue must use a matching attribute type for the value. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("WriteValue Attribute Name (%s) Expected Type: %s\n", name, attributeType.String())+
fmt.Sprintf("WriteValue Attribute Name (%s) Given Type: %s", name, attribute.Type(ctx)),
)
}
}
for name := range attributes {
_, ok := attributeTypes[name]
if !ok {
diags.AddError(
"Extra WriteValue Attribute Value",
"While creating a WriteValue value, an extra attribute value was detected. "+
"A WriteValue must not contain values beyond the expected attribute types. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Extra WriteValue Attribute Name: %s", name),
)
}
}
if diags.HasError() {
return NewWriteValueUnknown(), diags
}
hostAttribute, ok := attributes["host"]
if !ok {
diags.AddError(
"Attribute Missing",
`host is missing from object`)
return NewWriteValueUnknown(), diags
}
hostVal, ok := hostAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`host expected to be basetypes.StringValue, was: %T`, hostAttribute))
}
portAttribute, ok := attributes["port"]
if !ok {
diags.AddError(
"Attribute Missing",
`port is missing from object`)
return NewWriteValueUnknown(), diags
}
portVal, ok := portAttribute.(basetypes.Int64Value)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`port expected to be basetypes.Int64Value, was: %T`, portAttribute))
}
if diags.HasError() {
return NewWriteValueUnknown(), diags
}
return WriteValue{
Host: hostVal,
Port: portVal,
state: attr.ValueStateKnown,
}, diags
}
func NewWriteValueMust(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) WriteValue {
object, diags := NewWriteValue(attributeTypes, attributes)
if diags.HasError() {
// This could potentially be added to the diag package.
diagsStrings := make([]string, 0, len(diags))
for _, diagnostic := range diags {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"%s | %s | %s",
diagnostic.Severity(),
diagnostic.Summary(),
diagnostic.Detail()))
}
panic("NewWriteValueMust received error(s): " + strings.Join(diagsStrings, "\n"))
}
return object
}
func (t WriteType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
if in.Type() == nil {
return NewWriteValueNull(), nil
}
if !in.Type().Equal(t.TerraformType(ctx)) {
return nil, fmt.Errorf("expected %s, got %s", t.TerraformType(ctx), in.Type())
}
if !in.IsKnown() {
return NewWriteValueUnknown(), nil
}
if in.IsNull() {
return NewWriteValueNull(), nil
}
attributes := map[string]attr.Value{}
val := map[string]tftypes.Value{}
err := in.As(&val)
if err != nil {
return nil, err
}
for k, v := range val {
a, err := t.AttrTypes[k].ValueFromTerraform(ctx, v)
if err != nil {
return nil, err
}
attributes[k] = a
}
return NewWriteValueMust(WriteValue{}.AttributeTypes(ctx), attributes), nil
}
func (t WriteType) ValueType(ctx context.Context) attr.Value {
return WriteValue{}
}
var _ basetypes.ObjectValuable = WriteValue{}
type WriteValue struct {
Host basetypes.StringValue `tfsdk:"host"` Host basetypes.StringValue `tfsdk:"host"`
Port basetypes.Int64Value `tfsdk:"port"` Port basetypes.Int64Value `tfsdk:"port"`
state attr.ValueState state attr.ValueState
} }
func (v WriteValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) { func (v ConnectionInfoValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 2) attrTypes := make(map[string]tftypes.Type, 2)
var val tftypes.Value var val tftypes.Value
@ -901,19 +538,19 @@ func (v WriteValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error)
} }
} }
func (v WriteValue) IsNull() bool { func (v ConnectionInfoValue) IsNull() bool {
return v.state == attr.ValueStateNull return v.state == attr.ValueStateNull
} }
func (v WriteValue) IsUnknown() bool { func (v ConnectionInfoValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown return v.state == attr.ValueStateUnknown
} }
func (v WriteValue) String() string { func (v ConnectionInfoValue) String() string {
return "WriteValue" return "ConnectionInfoValue"
} }
func (v WriteValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) { func (v ConnectionInfoValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics var diags diag.Diagnostics
attributeTypes := map[string]attr.Type{ attributeTypes := map[string]attr.Type{
@ -939,8 +576,8 @@ func (v WriteValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, d
return objVal, diags return objVal, diags
} }
func (v WriteValue) Equal(o attr.Value) bool { func (v ConnectionInfoValue) Equal(o attr.Value) bool {
other, ok := o.(WriteValue) other, ok := o.(ConnectionInfoValue)
if !ok { if !ok {
return false return false
@ -965,15 +602,15 @@ func (v WriteValue) Equal(o attr.Value) bool {
return true return true
} }
func (v WriteValue) Type(ctx context.Context) attr.Type { func (v ConnectionInfoValue) Type(ctx context.Context) attr.Type {
return WriteType{ return ConnectionInfoType{
basetypes.ObjectType{ basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx), AttrTypes: v.AttributeTypes(ctx),
}, },
} }
} }
func (v WriteValue) AttributeTypes(ctx context.Context) map[string]attr.Type { func (v ConnectionInfoValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{ return map[string]attr.Type{
"host": basetypes.StringType{}, "host": basetypes.StringType{},
"port": basetypes.Int64Type{}, "port": basetypes.Int64Type{},

View file

@ -30,46 +30,39 @@ var testInstances []string
func init() { func init() {
sweeperName := fmt.Sprintf("%s_%s", pfx, "sweeper") sweeperName := fmt.Sprintf("%s_%s", pfx, "sweeper")
resource.AddTestSweepers( resource.AddTestSweepers(sweeperName, &resource.Sweeper{
sweeperName, &resource.Sweeper{ Name: sweeperName,
Name: sweeperName, F: func(region string) error {
F: func(_ string) error { // region is passed by the testing framework ctx := context.Background()
ctx := context.Background() apiClientConfigOptions := []config.ConfigurationOption{}
apiClientConfigOptions := []config.ConfigurationOption{} apiClient, err := postgresflexalpha2.NewAPIClient(apiClientConfigOptions...)
apiClient, err := postgresflexalpha2.NewAPIClient(apiClientConfigOptions...) if err != nil {
if err != nil { log.Fatalln(err)
log.Fatalln(err) }
}
instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, testutils.Region). instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, testutils.Region).
Size(100). Size(100).
Execute() Execute()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
for _, inst := range instances.GetInstances() { for _, inst := range instances.GetInstances() {
if strings.HasPrefix(inst.GetName(), "tf-acc-") { if strings.HasPrefix(inst.GetName(), "tf-acc-") {
for _, item := range testInstances { for _, item := range testInstances {
if inst.GetName() == item { if inst.GetName() == item {
delErr := apiClient.DeleteInstanceRequestExecute( delErr := apiClient.DeleteInstanceRequestExecute(ctx, testutils.ProjectId, testutils.Region, inst.GetId())
ctx, if delErr != nil {
testutils.ProjectId, // TODO: maybe just warn?
testutils.Region, log.Fatalln(delErr)
inst.GetId(),
)
if delErr != nil {
// TODO: maybe just warn?
log.Fatalln(delErr)
}
} }
} }
} }
} }
return nil }
}, return nil
}, },
) })
} }
func TestInstanceResourceSchema(t *testing.T) { func TestInstanceResourceSchema(t *testing.T) {
@ -202,67 +195,57 @@ func TestAccInstance(t *testing.T) {
updSizeData := exData updSizeData := exData
updSizeData.Size = 25 updSizeData.Size = 25
resource.ParallelTest( resource.ParallelTest(t, resource.TestCase{
t, resource.TestCase{ PreCheck: func() {
PreCheck: func() { testAccPreCheck(t)
testAccPreCheck(t) t.Logf(" ... working on instance %s", exData.TfName)
t.Logf(" ... working on instance %s", exData.TfName) testInstances = append(testInstances, exData.TfName)
testInstances = append(testInstances, exData.TfName)
},
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
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"),
),
},
// Update name and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
updNameData,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
testutils.ResStr(pfx, "instance", exData.TfName),
"name",
updNameData.Name,
),
),
},
// Update size and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
updSizeData,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
testutils.ResStr(pfx, "instance", exData.TfName),
"storage.size",
strconv.Itoa(int(updSizeData.Size)),
),
),
},
//// Import test
//{
// ResourceName: "example_resource.test",
// ImportState: true,
// ImportStateVerify: true,
// },
},
}, },
) ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
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"),
),
},
// Update name and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
updNameData,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "instance", exData.TfName), "name", updNameData.Name),
),
},
// Update size and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
updSizeData,
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
testutils.ResStr(pfx, "instance", exData.TfName),
"storage.size",
strconv.Itoa(int(updSizeData.Size)),
),
),
},
//// Import test
//{
// ResourceName: "example_resource.test",
// ImportState: true,
// ImportStateVerify: true,
// },
},
})
} }
func TestAccInstanceWithUsers(t *testing.T) { func TestAccInstanceWithUsers(t *testing.T) {
@ -277,35 +260,29 @@ func TestAccInstanceWithUsers(t *testing.T) {
}, },
} }
resource.ParallelTest( resource.ParallelTest(t, resource.TestCase{
t, resource.TestCase{ PreCheck: func() {
PreCheck: func() { testAccPreCheck(t)
testAccPreCheck(t) t.Logf(" ... working on instance %s", data.TfName)
t.Logf(" ... working on instance %s", data.TfName) testInstances = append(testInstances, data.TfName)
testInstances = append(testInstances, data.TfName) },
}, ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{
Steps: []resource.TestStep{ // Create and verify
// Create and verify {
{ Config: testutils.StringFromTemplateMust(
Config: testutils.StringFromTemplateMust( "testdata/instance_template.gompl",
"testdata/instance_template.gompl", data,
data, ),
), Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(testutils.ResStr(pfx, "instance", data.TfName), "name", data.Name),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", data.TfName), "id"),
testutils.ResStr(pfx, "instance", data.TfName), resource.TestCheckResourceAttr(testutils.ResStr(pfx, "user", userName), "name", userName),
"name", resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "user", userName), "id"),
data.Name, ),
),
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", data.TfName), "id"),
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "user", userName), "name", userName),
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "user", userName), "id"),
),
},
}, },
}, },
) })
} }
func TestAccInstanceWithDatabases(t *testing.T) { func TestAccInstanceWithDatabases(t *testing.T) {
@ -329,38 +306,32 @@ func TestAccInstanceWithDatabases(t *testing.T) {
}, },
} }
resource.ParallelTest( resource.ParallelTest(t, resource.TestCase{
t, resource.TestCase{ PreCheck: func() {
PreCheck: func() { testAccPreCheck(t)
testAccPreCheck(t) t.Logf(" ... working on instance %s", data.TfName)
t.Logf(" ... working on instance %s", data.TfName) testInstances = append(testInstances, data.TfName)
testInstances = append(testInstances, data.TfName) },
}, ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{
Steps: []resource.TestStep{ // Create and verify
// Create and verify {
{ Config: testutils.StringFromTemplateMust(
Config: testutils.StringFromTemplateMust( "testdata/instance_template.gompl",
"testdata/instance_template.gompl", data,
data, ),
), Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(testutils.ResStr(pfx, "instance", data.TfName), "name", data.Name),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", data.TfName), "id"),
testutils.ResStr(pfx, "instance", data.TfName), resource.TestCheckResourceAttr(testutils.ResStr(pfx, "user", userName), "name", userName),
"name", resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "user", userName), "id"),
data.Name, resource.TestCheckResourceAttr(testutils.ResStr(pfx, "database", dbName), "name", dbName),
), resource.TestCheckResourceAttr(testutils.ResStr(pfx, "database", dbName), "owner", userName),
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", data.TfName), "id"), resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "database", dbName), "id"),
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "user", userName), "name", userName), ),
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "user", userName), "id"),
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "database", dbName), "name", dbName),
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "database", dbName), "owner", userName),
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "database", dbName), "id"),
),
},
}, },
}, },
) })
} }
// func setupMockServer() *httptest.Server { // func setupMockServer() *httptest.Server {
@ -490,7 +461,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
// "project_id": testutils.ProjectId, // "project_id": testutils.ProjectId,
//} //}
// //
// func configResources(backupSchedule string, _ *string) string { //func configResources(backupSchedule string, _ *string) string {
// return fmt.Sprintf( // return fmt.Sprintf(
// ` // `
// %s // %s
@ -564,7 +535,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
// ) // )
//} //}
// //
// func TestAccPostgresFlexFlexResource(t *testing.T) { //func TestAccPostgresFlexFlexResource(t *testing.T) {
// resource.ParallelTest( // resource.ParallelTest(
// t, resource.TestCase{ // t, resource.TestCase{
// ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, // ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
@ -983,7 +954,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
// ) // )
//} //}
// //
// func testAccCheckPostgresFlexDestroy(s *terraform.State) error { //func testAccCheckPostgresFlexDestroy(s *terraform.State) error {
// ctx := context.Background() // ctx := context.Background()
// var client *postgresflex.APIClient // var client *postgresflex.APIClient
// var err error // var err error

View file

@ -48,7 +48,7 @@ resource "stackitprivatepreview_postgresflexalpha_database" "{{ $db.Name }}" {
project_id = "{{ $db.ProjectId }}" project_id = "{{ $db.ProjectId }}"
instance_id = stackitprivatepreview_postgresflexalpha_instance.{{ $tfName }}.instance_id instance_id = stackitprivatepreview_postgresflexalpha_instance.{{ $tfName }}.instance_id
name = "{{ $db.Name }}" name = "{{ $db.Name }}"
owner = stackitprivatepreview_postgresflexalpha_user.{{ $db.Owner }}.name owner = "{{ $db.Owner }}"
} }
{{ end }} {{ end }}
{{ end }} {{ end }}

View file

@ -164,16 +164,16 @@ func TestMapFieldsCreate(t *testing.T) {
}, },
testRegion, testRegion,
resourceModel{ resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringNull(), Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(), Password: types.StringNull(),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringNull(), Status: types.StringNull(),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },
@ -186,16 +186,16 @@ func TestMapFieldsCreate(t *testing.T) {
}, },
testRegion, testRegion,
resourceModel{ resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("username"), Name: types.StringValue("username"),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(), Password: types.StringNull(),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringValue("status"), Status: types.StringValue("status"),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },
@ -208,16 +208,16 @@ func TestMapFieldsCreate(t *testing.T) {
}, },
testRegion, testRegion,
resourceModel{ resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringNull(), Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(), Password: types.StringNull(),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringNull(), Status: types.StringNull(),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },
@ -285,15 +285,15 @@ func TestMapFields(t *testing.T) {
}, },
testRegion, testRegion,
resourceModel{ resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(int64(1)), UserId: types.Int64Value(int64(1)),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringNull(), Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringNull(), Status: types.StringNull(),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },
@ -324,9 +324,9 @@ func TestMapFields(t *testing.T) {
}, },
), ),
), ),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringNull(), Status: types.StringNull(),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },
@ -338,15 +338,15 @@ func TestMapFields(t *testing.T) {
}, },
testRegion, testRegion,
resourceModel{ resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringNull(), Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
Status: types.StringNull(), Status: types.StringNull(),
//ConnectionString: types.StringNull(), ConnectionString: types.StringNull(),
}, },
true, true,
}, },

View file

@ -2,12 +2,10 @@ fields:
- name: 'id' - name: 'id'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'user_id' - name: 'user_id'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'instance_id' - name: 'instance_id'
validators: validators:
@ -15,7 +13,6 @@ fields:
- validate.UUID - validate.UUID
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'project_id' - name: 'project_id'
validators: validators:
@ -35,30 +32,24 @@ fields:
- name: 'password' - name: 'password'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'host' - name: 'host'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'port' - name: 'port'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'region' - name: 'region'
modifiers: modifiers:
- 'RequiresReplace' - 'RequiresReplace'
- 'RequiresReplace'
- name: 'status' - name: 'status'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'connection_string' - name: 'connection_string'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'

View file

@ -5,7 +5,6 @@ import (
_ "embed" _ "embed"
"fmt" "fmt"
"math" "math"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -30,12 +29,11 @@ import (
var ( var (
// Ensure the implementation satisfies the expected interfaces. // Ensure the implementation satisfies the expected interfaces.
_ resource.Resource = &userResource{} _ resource.Resource = &userResource{}
_ resource.ResourceWithConfigure = &userResource{} _ resource.ResourceWithConfigure = &userResource{}
_ resource.ResourceWithImportState = &userResource{} _ resource.ResourceWithImportState = &userResource{}
_ resource.ResourceWithModifyPlan = &userResource{} _ resource.ResourceWithModifyPlan = &userResource{}
_ resource.ResourceWithIdentity = &userResource{} _ resource.ResourceWithIdentity = &userResource{}
_ resource.ResourceWithValidateConfig = &userResource{}
// Error message constants // Error message constants
extractErrorSummary = "extracting failed" extractErrorSummary = "extracting failed"
@ -140,39 +138,6 @@ func (r *userResource) Schema(ctx context.Context, _ resource.SchemaRequest, res
resp.Schema = s resp.Schema = s
} }
func (r *userResource) ValidateConfig(
ctx context.Context,
req resource.ValidateConfigRequest,
resp *resource.ValidateConfigResponse,
) {
var data resourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var roles []string
diags := data.Roles.ElementsAs(ctx, &roles, false)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
var resRoles []string
for _, role := range roles {
if slices.Contains(resRoles, role) {
resp.Diagnostics.AddAttributeError(
path.Root("roles"),
"Attribute Configuration Error",
"defined roles MUST NOT contain duplicates",
)
return
}
resRoles = append(resRoles, role)
}
}
// Create creates the resource and sets the initial Terraform state. // Create creates the resource and sets the initial Terraform state.
func (r *userResource) Create( func (r *userResource) Create(
ctx context.Context, ctx context.Context,
@ -252,6 +217,7 @@ func (r *userResource) Create(
model.UserId = types.Int64Value(id) model.UserId = types.Int64Value(id)
model.Password = types.StringValue(userResp.GetPassword()) model.Password = types.StringValue(userResp.GetPassword())
model.Status = types.StringValue(userResp.GetStatus()) model.Status = types.StringValue(userResp.GetStatus())
model.ConnectionString = types.StringValue(userResp.GetConnectionString())
waitResp, err := postgresflexalphaWait.GetUserByIdWaitHandler( waitResp, err := postgresflexalphaWait.GetUserByIdWaitHandler(
ctx, ctx,
@ -746,6 +712,5 @@ func (r *userResource) expandRoles(ctx context.Context, rolesSet types.List, dia
} }
var roles []string var roles []string
diags.Append(rolesSet.ElementsAs(ctx, &roles, false)...) diags.Append(rolesSet.ElementsAs(ctx, &roles, false)...)
slices.Sort(roles)
return roles return roles
} }

View file

@ -14,6 +14,11 @@ import (
func UserResourceSchema(ctx context.Context) schema.Schema { func UserResourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{ return schema.Schema{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"connection_string": schema.StringAttribute{
Computed: true,
Description: "The connection string for the user to the instance.",
MarkdownDescription: "The connection string for the user to the instance.",
},
"id": schema.Int64Attribute{ "id": schema.Int64Attribute{
Computed: true, Computed: true,
Description: "The ID of the user.", Description: "The ID of the user.",
@ -75,13 +80,14 @@ func UserResourceSchema(ctx context.Context) schema.Schema {
} }
type UserModel struct { type UserModel struct {
Id types.Int64 `tfsdk:"id"` ConnectionString types.String `tfsdk:"connection_string"`
InstanceId types.String `tfsdk:"instance_id"` Id types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"` InstanceId types.String `tfsdk:"instance_id"`
Password types.String `tfsdk:"password"` Name types.String `tfsdk:"name"`
ProjectId types.String `tfsdk:"project_id"` Password types.String `tfsdk:"password"`
Region types.String `tfsdk:"region"` ProjectId types.String `tfsdk:"project_id"`
Roles types.List `tfsdk:"roles"` Region types.String `tfsdk:"region"`
Status types.String `tfsdk:"status"` Roles types.List `tfsdk:"roles"`
UserId types.Int64 `tfsdk:"user_id"` Status types.String `tfsdk:"status"`
UserId types.Int64 `tfsdk:"user_id"`
} }

View file

@ -10,34 +10,34 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
sqlserverflexalphaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/database/datasources_gen" sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/database/datasources_gen"
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
) )
// dataSourceModel maps the data source schema data.
type dataSourceModel struct {
sqlserverflexalphaGen.DatabaseModel
TerraformID types.String `tfsdk:"id"`
}
var _ datasource.DataSource = (*databaseDataSource)(nil) var _ datasource.DataSource = (*databaseDataSource)(nil)
const errorPrefix = "[sqlserverflexalpha - Database]" // NewDatabaseDataSource creates a new database data source.
func NewDatabaseDataSource() datasource.DataSource { func NewDatabaseDataSource() datasource.DataSource {
return &databaseDataSource{} return &databaseDataSource{}
} }
type dataSourceModel struct {
sqlserverflexalphaGen.DatabaseModel
TerraformId types.String `tfsdk:"id"`
}
type databaseDataSource struct { type databaseDataSource struct {
client *sqlserverflexalphaPkg.APIClient client *sqlserverflexalpha.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
// Metadata returns the data source type name.
func (d *databaseDataSource) Metadata( func (d *databaseDataSource) Metadata(
_ context.Context, _ context.Context,
req datasource.MetadataRequest, req datasource.MetadataRequest,
@ -46,13 +46,16 @@ func (d *databaseDataSource) Metadata(
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database"
} }
// Schema defines the data source schema.
func (d *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (d *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = sqlserverflexalphaGen.DatabaseDataSourceSchema(ctx) s := sqlserverflexalphaGen.DatabaseDataSourceSchema(ctx)
resp.Schema.Attributes["id"] = schema.StringAttribute{ s.Attributes["id"] = schema.StringAttribute{
Computed: true, Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
Description: "The terraform internal identifier.", "`database_id`\\\".\",",
MarkdownDescription: "The terraform internal identifier.", Computed: true,
} }
resp.Schema = s
} }
// Configure adds the provider configured client to the data source. // Configure adds the provider configured client to the data source.
@ -67,41 +70,19 @@ func (d *databaseDataSource) Configure(
return return
} }
apiClientConfigOptions := []config.ConfigurationOption{ apiClient := sqlserverflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
config.WithCustomAuth(d.providerData.RoundTripper), if resp.Diagnostics.HasError() {
utils.UserAgentConfigOption(d.providerData.Version),
}
if d.providerData.SQLServerFlexCustomEndpoint != "" {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithEndpoint(d.providerData.SQLServerFlexCustomEndpoint),
)
} else {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithRegion(d.providerData.GetRegion()),
)
}
apiClient, err := sqlserverflexalphaPkg.NewAPIClient(apiClientConfigOptions...)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring API client",
fmt.Sprintf(
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
err,
),
)
return return
} }
d.client = apiClient d.client = apiClient
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix)) tflog.Info(ctx, "SQL SERVER Flex alpha database client configured")
} }
// Read retrieves the resource's state from the API.
func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data dataSourceModel var model dataSourceModel
// Read Terraform configuration data into the model diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
@ -109,17 +90,22 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
// Extract identifiers from the plan // Extract identifiers from the plan
projectId := data.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
region := d.providerData.GetRegionWithOverride(data.Region) instanceId := model.InstanceId.ValueString()
instanceId := data.InstanceId.ValueString() region := d.providerData.GetRegionWithOverride(model.Region)
databaseName := model.DatabaseName.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "database_name", databaseName)
databaseName := data.DatabaseName.ValueString() // Fetch database from the API
databaseResp, err := d.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute() databaseResp, err := d.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
if resp.Diagnostics.HasError() {
return
}
if err != nil { if err != nil {
handleReadError(ctx, &resp.Diagnostics, err, projectId, instanceId) handleReadError(ctx, &resp.Diagnostics, err, projectId, instanceId)
resp.State.RemoveResource(ctx) resp.State.RemoveResource(ctx)
@ -127,8 +113,9 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
} }
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
// Map response body to schema and populate Computed attribute values // Map response body to schema and populate Computed attribute values
err = mapFields(databaseResp, &data, region) err = mapFields(databaseResp, &model, region)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -139,10 +126,14 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
return return
} }
// Save data into Terraform state // Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SQL Server Flex beta database read") tflog.Info(ctx, "SQL Server Flex alpha database read")
} }
// handleReadError centralizes API error handling for the Read operation. // handleReadError centralizes API error handling for the Read operation.

View file

@ -41,7 +41,7 @@ func mapFields(source *sqlserverflexalpha.GetDatabaseResponse, model *dataSource
model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel()) model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel())
model.CollationName = types.StringValue(source.GetCollationName()) model.CollationName = types.StringValue(source.GetCollationName())
model.TerraformId = utils.BuildInternalTerraformId( model.TerraformID = utils.BuildInternalTerraformId(
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
region, region,
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
@ -80,12 +80,6 @@ func mapResourceFields(source *sqlserverflexalpha.GetDatabaseResponse, model *re
model.ProjectId = types.StringValue(model.ProjectId.ValueString()) model.ProjectId = types.StringValue(model.ProjectId.ValueString())
model.InstanceId = types.StringValue(model.InstanceId.ValueString()) model.InstanceId = types.StringValue(model.InstanceId.ValueString())
model.Compatibility = types.Int64Value(source.GetCompatibilityLevel())
model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel())
model.Collation = types.StringValue(source.GetCollationName()) // it does not come back from api
model.CollationName = types.StringValue(source.GetCollationName())
return nil return nil
} }

View file

@ -35,7 +35,7 @@ func TestMapFields(t *testing.T) {
Name: utils.Ptr("my-db"), Name: utils.Ptr("my-db"),
CollationName: utils.Ptr("collation"), CollationName: utils.Ptr("collation"),
CompatibilityLevel: utils.Ptr(int64(150)), CompatibilityLevel: utils.Ptr(int64(150)),
Owner: utils.Ptr("my-owner"), Owner: utils.Ptr("\"my-owner\""),
}, },
model: &dataSourceModel{ model: &dataSourceModel{
DatabaseModel: datasource.DatabaseModel{ DatabaseModel: datasource.DatabaseModel{
@ -58,7 +58,7 @@ func TestMapFields(t *testing.T) {
CompatibilityLevel: types.Int64Value(150), CompatibilityLevel: types.Int64Value(150),
CollationName: types.StringValue("collation"), CollationName: types.StringValue("collation"),
}, },
TerraformId: types.StringValue("my-project,eu01,my-instance,my-db"), TerraformID: types.StringValue("my-project,eu01,my-instance,my-db"),
}, },
}, },
}, },
@ -127,7 +127,7 @@ func TestMapResourceFields(t *testing.T) {
source: &sqlserverflexalpha.GetDatabaseResponse{ source: &sqlserverflexalpha.GetDatabaseResponse{
Id: utils.Ptr(int64(1)), Id: utils.Ptr(int64(1)),
Name: utils.Ptr("my-db"), Name: utils.Ptr("my-db"),
Owner: utils.Ptr("my-owner"), Owner: utils.Ptr("\"my-owner\""),
}, },
model: &resourceModel{ model: &resourceModel{
ProjectId: types.StringValue("my-project"), ProjectId: types.StringValue("my-project"),
@ -137,17 +137,13 @@ func TestMapResourceFields(t *testing.T) {
}, },
expected: expected{ expected: expected{
model: &resourceModel{ model: &resourceModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
Name: types.StringValue("my-db"), Name: types.StringValue("my-db"),
Compatibility: types.Int64Value(0), DatabaseName: types.StringValue("my-db"),
CompatibilityLevel: types.Int64Value(0), InstanceId: types.StringValue("my-instance"),
Collation: types.StringValue(""), ProjectId: types.StringValue("my-project"),
CollationName: types.StringValue(""), Region: types.StringValue("eu01"),
DatabaseName: types.StringValue("my-db"), Owner: types.StringValue("my-owner"),
InstanceId: types.StringValue("my-instance"),
ProjectId: types.StringValue("my-project"),
Region: types.StringValue("eu01"),
Owner: types.StringValue("my-owner"),
}, },
}, },
}, },

View file

@ -31,7 +31,7 @@ fields:
- name: 'owner' - name: 'owner'
modifiers: modifiers:
- 'UseStateForUnknown' - 'RequiresReplace'
- name: 'database_name' - name: 'database_name'
modifiers: modifiers:
@ -43,7 +43,6 @@ fields:
- name: 'compatibility' - name: 'compatibility'
modifiers: modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace' - 'RequiresReplace'
- name: 'compatibility_level' - name: 'compatibility_level'

View file

@ -6,8 +6,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
@ -19,12 +19,11 @@ import (
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/conversion"
wait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
"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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
sqlserverflexalphaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/database/resources_gen" sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/database/resources_gen"
) )
var ( var (
@ -36,6 +35,10 @@ var (
// Define errors // Define errors
errDatabaseNotFound = errors.New("database not found") errDatabaseNotFound = errors.New("database not found")
// Error message constants
extractErrorSummary = "extracting failed"
extractErrorMessage = "Extracting identity data: %v"
) )
func NewDatabaseResource() resource.Resource { func NewDatabaseResource() resource.Resource {
@ -43,13 +46,9 @@ func NewDatabaseResource() resource.Resource {
} }
// resourceModel describes the resource data model. // resourceModel describes the resource data model.
type resourceModel = sqlserverflexalphaResGen.DatabaseModel type resourceModel = sqlserverflexalphaGen.DatabaseModel
type databaseResource struct {
client *sqlserverflexalpha.APIClient
providerData core.ProviderData
}
// DatabaseResourceIdentityModel describes the resource's identity attributes.
type DatabaseResourceIdentityModel struct { type DatabaseResourceIdentityModel struct {
ProjectID types.String `tfsdk:"project_id"` ProjectID types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"` Region types.String `tfsdk:"region"`
@ -57,11 +56,12 @@ type DatabaseResourceIdentityModel struct {
DatabaseName types.String `tfsdk:"database_name"` DatabaseName types.String `tfsdk:"database_name"`
} }
func (r *databaseResource) Metadata( type databaseResource struct {
_ context.Context, client *sqlserverflexalpha.APIClient
req resource.MetadataRequest, providerData core.ProviderData
resp *resource.MetadataResponse, }
) {
func (r *databaseResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database"
} }
@ -69,7 +69,7 @@ func (r *databaseResource) Metadata(
var modifiersFileByte []byte var modifiersFileByte []byte
func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
s := sqlserverflexalphaResGen.DatabaseResourceSchema(ctx) s := sqlserverflexalphaGen.DatabaseResourceSchema(ctx)
fields, err := utils.ReadModifiersConfig(modifiersFileByte) fields, err := utils.ReadModifiersConfig(modifiersFileByte)
if err != nil { if err != nil {
@ -124,10 +124,10 @@ func (r *databaseResource) Configure(
config.WithCustomAuth(r.providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
utils.UserAgentConfigOption(r.providerData.Version), utils.UserAgentConfigOption(r.providerData.Version),
} }
if r.providerData.SQLServerFlexCustomEndpoint != "" { if r.providerData.PostgresFlexCustomEndpoint != "" {
apiClientConfigOptions = append( apiClientConfigOptions = append(
apiClientConfigOptions, apiClientConfigOptions,
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint), config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint),
) )
} else { } else {
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion())) apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
@ -148,54 +148,50 @@ func (r *databaseResource) Configure(
} }
func (r *databaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { func (r *databaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data resourceModel var model resourceModel
createErr := "DB create error"
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
region := data.Region.ValueString() region := model.Region.ValueString()
instanceId := data.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
databaseName := data.Name.ValueString() // Generate API request body from model
ctx = tflog.SetField(ctx, "database_name", databaseName) payload, err := toCreatePayload(&model)
payLoad := sqlserverflexalpha.CreateDatabaseRequestPayload{}
if !data.Collation.IsNull() && !data.Collation.IsUnknown() {
payLoad.Collation = data.Collation.ValueStringPointer()
}
if !data.Compatibility.IsNull() && !data.Compatibility.IsUnknown() {
payLoad.Compatibility = data.Compatibility.ValueInt64Pointer()
}
payLoad.Name = data.Name.ValueStringPointer()
payLoad.Owner = data.Owner.ValueStringPointer()
createResp, err := r.client.CreateDatabaseRequest(ctx, projectId, region, instanceId).
CreateDatabaseRequestPayload(payLoad).
Execute()
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
&resp.Diagnostics, &resp.Diagnostics,
createErr, "Error creating database",
fmt.Sprintf("Calling API: %v", err), fmt.Sprintf("Creating API payload: %v", err),
) )
return return
} }
// Create new database
databaseResp, err := r.client.CreateDatabaseRequest(
ctx,
projectId,
region,
instanceId,
).CreateDatabaseRequestPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Calling API: %v", err))
return
}
if createResp == nil || createResp.Id == nil { ctx = core.LogResponse(ctx)
if databaseResp == nil || databaseResp.Id == nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
&resp.Diagnostics, &resp.Diagnostics,
@ -205,9 +201,11 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
databaseId := *createResp.Id databaseId := *databaseResp.Id
databaseName := model.DatabaseName.String()
ctx = tflog.SetField(ctx, "database_id", databaseId) ctx = tflog.SetField(ctx, "database_id", databaseId)
ctx = tflog.SetField(ctx, "database_name", databaseName)
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
@ -223,69 +221,6 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
// TODO: is this necessary to wait for the database-> API say 200 ?
waitResp, err := wait.CreateDatabaseWaitHandler(
ctx,
r.client,
projectId,
instanceId,
region,
databaseName,
).SetSleepBeforeWait(
30 * time.Second,
).SetTimeout(
15 * time.Minute,
).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
createErr,
fmt.Sprintf("Database creation waiting: %v", err),
)
return
}
if waitResp.Id == nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
createErr,
"Database creation waiting: returned id is nil",
)
return
}
if *waitResp.Id != databaseId {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
createErr,
"Database creation waiting: returned id is different",
)
return
}
if *waitResp.Owner != data.Owner.ValueString() {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
createErr,
"Database creation waiting: returned owner is different",
)
return
}
if *waitResp.Name != data.Name.ValueString() {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
createErr,
"Database creation waiting: returned name is different",
)
return
}
database, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute() database, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
@ -298,7 +233,7 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
} }
// Map response body to schema // Map response body to schema
err = mapResourceFields(database, &data, region) err = mapResourceFields(database, &model, region)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -310,13 +245,11 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
} }
// Set state to fully populated data // Set state to fully populated data
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
// Save data into Terraform state
tflog.Info(ctx, "sqlserverflexalpha.Database created") tflog.Info(ctx, "sqlserverflexalpha.Database created")
} }
@ -328,12 +261,24 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
return return
} }
// Read identity data
var identityData DatabaseResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
region := model.Region.ValueString() if errExt != nil {
instanceId := model.InstanceId.ValueString() core.LogAndAddError(
databaseName := model.DatabaseName.ValueString() ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -365,7 +310,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
return return
} }
// Save identity into Terraform state // Set data returned by API in identity
identity := DatabaseResourceIdentityModel{ identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId), ProjectID: types.StringValue(projectId),
Region: types.StringValue(region), Region: types.StringValue(region),
@ -409,10 +354,15 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
region := model.Region.ValueString() if errExt != nil {
instanceId := model.InstanceId.ValueString() core.LogAndAddError(
databaseName := model.DatabaseName.ValueString() ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -422,15 +372,7 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
// Delete existing record set // Delete existing record set
err := r.client.DeleteDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseName) err := r.client.DeleteDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseName)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting database", fmt.Sprintf("Calling API: %v", err))
ctx,
&resp.Diagnostics,
"Error deleting database",
fmt.Sprintf(
"Calling API: %v\nname: %s, region: %s, instanceId: %s", err, databaseName, region, instanceId,
),
)
return
} }
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
@ -446,12 +388,11 @@ func (r *databaseResource) ModifyPlan(
req resource.ModifyPlanRequest, req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse, resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var configModel resourceModel
// skip initial empty configuration to avoid follow-up errors // skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() { if req.Config.Raw.IsNull() {
return return
} }
var configModel resourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
@ -475,7 +416,7 @@ func (r *databaseResource) ModifyPlan(
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,zone_id,record_set_id // The expected import identifier format is: [project_id],[region],[instance_id],[database_id]
func (r *databaseResource) ImportState( func (r *databaseResource) ImportState(
ctx context.Context, ctx context.Context,
req resource.ImportStateRequest, req resource.ImportStateRequest,
@ -491,31 +432,36 @@ func (r *databaseResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing database", "Error importing database",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format [project_id],[region],[instance_id],[database_name] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id],[database_name], got %q",
req.ID, req.ID,
), ),
) )
return return
} }
databaseId, err := strconv.ParseInt(idParts[3], 10, 64)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error importing database",
fmt.Sprintf("Invalid database_id format: %q. It must be a valid integer.", idParts[3]),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_name"), idParts[3])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_name"), databaseId)...)
var identityData DatabaseResourceIdentityModel core.LogAndAddWarning(
identityData.ProjectID = types.StringValue(idParts[0]) ctx,
identityData.Region = types.StringValue(idParts[1]) &resp.Diagnostics,
identityData.InstanceID = types.StringValue(idParts[2]) "Sqlserverflexalpha database imported with empty password",
identityData.DatabaseName = types.StringValue(idParts[3]) "The database password is not imported as it is only available upon creation of a new database. The password field will be empty.",
)
resp.Diagnostics.Append(resp.Identity.Set(ctx, &identityData)...) tflog.Info(ctx, "Sqlserverflexalpha database state imported")
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "sqlserverflexalpha database state imported")
return
} }
// If no ID is provided, attempt to read identity attributes from the import configuration // If no ID is provided, attempt to read identity attributes from the import configuration
@ -535,5 +481,48 @@ func (r *databaseResource) ImportState(
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_name"), databaseName)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_name"), databaseName)...)
tflog.Info(ctx, "sqlserverflexalpha database state imported") tflog.Info(ctx, "Sqlserverflexalpha database state imported")
}
// extractIdentityData extracts essential identifiers from the resource model, falling back to the identity model.
func (r *databaseResource) extractIdentityData(
model resourceModel,
identity DatabaseResourceIdentityModel,
) (projectId, region, instanceId, databaseName string, err error) {
if !model.Name.IsNull() && !model.Name.IsUnknown() {
databaseName = model.Name.ValueString()
} else {
if identity.DatabaseName.IsNull() || identity.DatabaseName.IsUnknown() {
return "", "", "", "", fmt.Errorf("database_name not found in config")
}
databaseName = identity.DatabaseName.ValueString()
}
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
projectId = model.ProjectId.ValueString()
} else {
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
return "", "", "", "", fmt.Errorf("project_id not found in config")
}
projectId = identity.ProjectID.ValueString()
}
if !model.Region.IsNull() && !model.Region.IsUnknown() {
region = r.providerData.GetRegionWithOverride(model.Region)
} else {
if identity.Region.IsNull() || identity.Region.IsUnknown() {
return "", "", "", "", fmt.Errorf("region not found in config")
}
region = r.providerData.GetRegionWithOverride(identity.Region)
}
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
instanceId = model.InstanceId.ValueString()
} else {
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
return "", "", "", "", fmt.Errorf("instance_id not found in config")
}
instanceId = identity.InstanceID.ValueString()
}
return projectId, region, instanceId, databaseName, nil
} }

View file

@ -1,4 +1,4 @@
package sqlserverflexalphaFlavor package sqlserverFlexAlphaFlavor
import ( import (
"context" "context"
@ -10,14 +10,14 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
sqlserverflexalphaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/flavor/datasources_gen" sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/flavor/datasources_gen"
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
) )
// Ensure the implementation satisfies the expected interfaces. // Ensure the implementation satisfies the expected interfaces.
@ -26,6 +26,11 @@ var (
_ datasource.DataSourceWithConfigure = &flavorDataSource{} _ datasource.DataSourceWithConfigure = &flavorDataSource{}
) )
// NewFlavorDataSource is a helper function to simplify the provider implementation.
func NewFlavorDataSource() datasource.DataSource {
return &flavorDataSource{}
}
type FlavorModel struct { type FlavorModel struct {
ProjectId types.String `tfsdk:"project_id"` ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"` Region types.String `tfsdk:"region"`
@ -41,58 +46,39 @@ type FlavorModel struct {
StorageClasses types.List `tfsdk:"storage_classes"` StorageClasses types.List `tfsdk:"storage_classes"`
} }
// NewFlavorDataSource is a helper function to simplify the provider implementation.
func NewFlavorDataSource() datasource.DataSource {
return &flavorDataSource{}
}
// flavorDataSource is the data source implementation. // flavorDataSource is the data source implementation.
type flavorDataSource struct { type flavorDataSource struct {
client *sqlserverflexalphaPkg.APIClient client *sqlserverflexalpha.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
// Metadata returns the data source type name. // Metadata returns the data source type name.
func (r *flavorDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { func (r *flavorDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_flavor" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_flavor"
} }
// Configure adds the provider configured client to the data source. // Configure adds the provider configured client to the data source.
func (r *flavorDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { func (r *flavorDataSource) Configure(
ctx context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
var ok bool var ok bool
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok { if !ok {
return return
} }
apiClientConfigOptions := []config.ConfigurationOption{ apiClient := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
config.WithCustomAuth(r.providerData.RoundTripper), if resp.Diagnostics.HasError() {
utils.UserAgentConfigOption(r.providerData.Version),
}
if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
)
} else {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithRegion(r.providerData.GetRegion()),
)
}
apiClient, err := sqlserverflexalphaPkg.NewAPIClient(apiClientConfigOptions...)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring API client",
fmt.Sprintf(
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
err,
),
)
return return
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "SQL Server Flex instance client configured") tflog.Info(ctx, "Postgres Flex instance client configured")
} }
func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
@ -100,13 +86,13 @@ func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaReques
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{ "project_id": schema.StringAttribute{
Required: true, Required: true,
Description: "The project ID of the flavor.", Description: "The cpu count of the instance.",
MarkdownDescription: "The project ID of the flavor.", MarkdownDescription: "The cpu count of the instance.",
}, },
"region": schema.StringAttribute{ "region": schema.StringAttribute{
Required: true, Required: true,
Description: "The region of the flavor.", Description: "The flavor description.",
MarkdownDescription: "The region of the flavor.", MarkdownDescription: "The flavor description.",
}, },
"cpu": schema.Int64Attribute{ "cpu": schema.Int64Attribute{
Required: true, Required: true,
@ -123,16 +109,6 @@ func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaReques
Description: "The memory of the instance in Gibibyte.", Description: "The memory of the instance in Gibibyte.",
MarkdownDescription: "The memory of the instance in Gibibyte.", MarkdownDescription: "The memory of the instance in Gibibyte.",
}, },
"node_type": schema.StringAttribute{
Required: true,
Description: "defines the nodeType it can be either single or HA",
MarkdownDescription: "defines the nodeType it can be either single or HA",
},
"flavor_id": schema.StringAttribute{
Computed: true,
Description: "The id of the instance flavor.",
MarkdownDescription: "The id of the instance flavor.",
},
"description": schema.StringAttribute{ "description": schema.StringAttribute{
Computed: true, Computed: true,
Description: "The flavor description.", Description: "The flavor description.",
@ -140,8 +116,13 @@ func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaReques
}, },
"id": schema.StringAttribute{ "id": schema.StringAttribute{
Computed: true, Computed: true,
Description: "The id of the instance flavor.", Description: "The terraform id of the instance flavor.",
MarkdownDescription: "The id of the instance flavor.", MarkdownDescription: "The terraform id of the instance flavor.",
},
"flavor_id": schema.StringAttribute{
Computed: true,
Description: "The flavor id of the instance flavor.",
MarkdownDescription: "The flavor id of the instance flavor.",
}, },
"max_gb": schema.Int64Attribute{ "max_gb": schema.Int64Attribute{
Computed: true, Computed: true,
@ -153,7 +134,13 @@ func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaReques
Description: "minimum storage which is required to order in Gigabyte.", Description: "minimum storage which is required to order in Gigabyte.",
MarkdownDescription: "minimum storage which is required to order in Gigabyte.", MarkdownDescription: "minimum storage which is required to order in Gigabyte.",
}, },
"node_type": schema.StringAttribute{
Required: true,
Description: "defines the nodeType it can be either single or replica",
MarkdownDescription: "defines the nodeType it can be either single or replica",
},
"storage_classes": schema.ListNestedAttribute{ "storage_classes": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{ NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{ Attributes: map[string]schema.Attribute{
"class": schema.StringAttribute{ "class": schema.StringAttribute{
@ -172,89 +159,8 @@ func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaReques
}, },
}, },
}, },
Computed: true,
Description: "maximum storage which can be ordered for the flavor in Gigabyte.",
MarkdownDescription: "maximum storage which can be ordered for the flavor in Gigabyte.",
}, },
}, },
//Attributes: map[string]schema.Attribute{
// "project_id": schema.StringAttribute{
// Required: true,
// Description: "The cpu count of the instance.",
// MarkdownDescription: "The cpu count of the instance.",
// },
// "region": schema.StringAttribute{
// Required: true,
// Description: "The flavor description.",
// MarkdownDescription: "The flavor description.",
// },
// "cpu": schema.Int64Attribute{
// Required: true,
// Description: "The cpu count of the instance.",
// MarkdownDescription: "The cpu count of the instance.",
// },
// "ram": schema.Int64Attribute{
// Required: true,
// Description: "The memory of the instance in Gibibyte.",
// MarkdownDescription: "The memory of the instance in Gibibyte.",
// },
// "storage_class": schema.StringAttribute{
// Required: true,
// Description: "The memory of the instance in Gibibyte.",
// MarkdownDescription: "The memory of the instance in Gibibyte.",
// },
// "description": schema.StringAttribute{
// Computed: true,
// Description: "The flavor description.",
// MarkdownDescription: "The flavor description.",
// },
// "id": schema.StringAttribute{
// Computed: true,
// Description: "The terraform id of the instance flavor.",
// MarkdownDescription: "The terraform id of the instance flavor.",
// },
// "flavor_id": schema.StringAttribute{
// Computed: true,
// Description: "The flavor id of the instance flavor.",
// MarkdownDescription: "The flavor id of the instance flavor.",
// },
// "max_gb": schema.Int64Attribute{
// Computed: true,
// Description: "maximum storage which can be ordered for the flavor in Gigabyte.",
// MarkdownDescription: "maximum storage which can be ordered for the flavor in Gigabyte.",
// },
// "min_gb": schema.Int64Attribute{
// Computed: true,
// Description: "minimum storage which is required to order in Gigabyte.",
// MarkdownDescription: "minimum storage which is required to order in Gigabyte.",
// },
// "node_type": schema.StringAttribute{
// Required: true,
// Description: "defines the nodeType it can be either single or replica",
// MarkdownDescription: "defines the nodeType it can be either single or replica",
// },
// "storage_classes": schema.ListNestedAttribute{
// Computed: true,
// NestedObject: schema.NestedAttributeObject{
// Attributes: map[string]schema.Attribute{
// "class": schema.StringAttribute{
// Computed: true,
// },
// "max_io_per_sec": schema.Int64Attribute{
// Computed: true,
// },
// "max_through_in_mb": schema.Int64Attribute{
// Computed: true,
// },
// },
// CustomType: sqlserverflexalphaGen.StorageClassesType{
// ObjectType: types.ObjectType{
// AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx),
// },
// },
// },
// },
// },
} }
} }
@ -279,7 +185,7 @@ func (r *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return return
} }
var foundFlavors []sqlserverflexalphaPkg.ListFlavors var foundFlavors []sqlserverflexalpha.ListFlavors
for _, flavor := range flavors { for _, flavor := range flavors {
if model.Cpu.ValueInt64() != *flavor.Cpu { if model.Cpu.ValueInt64() != *flavor.Cpu {
continue continue
@ -314,11 +220,13 @@ func (r *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest,
model.MinGb = types.Int64Value(*f.MinGB) model.MinGb = types.Int64Value(*f.MinGB)
if f.StorageClasses == nil { if f.StorageClasses == nil {
model.StorageClasses = types.ListNull(sqlserverflexalphaGen.StorageClassesType{ model.StorageClasses = types.ListNull(
ObjectType: basetypes.ObjectType{ sqlserverflexalphaGen.StorageClassesType{
AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx), ObjectType: basetypes.ObjectType{
AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx),
},
}, },
}) )
} else { } else {
var scList []attr.Value var scList []attr.Value
for _, sc := range *f.StorageClasses { for _, sc := range *f.StorageClasses {
@ -351,5 +259,5 @@ func (r *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest,
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "SQL Server Flex flavors read") tflog.Info(ctx, "Postgres Flex flavors read")
} }

View file

@ -1,4 +1,4 @@
package sqlserverflexalphaFlavor package sqlserverFlexAlphaFlavor
import ( import (
"context" "context"

View file

@ -1,4 +1,4 @@
package sqlserverflexalphaFlavor package sqlserverFlexAlphaFlavor
import ( import (
"context" "context"

View file

@ -2,39 +2,30 @@ package sqlserverflexalpha
import ( import (
"context" "context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
sqlserverflexalphaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/flavors/datasources_gen" sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/flavors/datasources_gen"
) )
// dataSourceModel maps the data source schema data.
type dataSourceModel = sqlserverflexalphaGen.FlavorsModel
var _ datasource.DataSource = (*flavorsDataSource)(nil) var _ datasource.DataSource = (*flavorsDataSource)(nil)
const errorPrefix = "[sqlserverflexalpha - Flavors]" // TODO: Use NewFlavorsDataSource when datasource is implemented
func NewFlavorsDataSource() datasource.DataSource { func NewFlavorsDataSource() datasource.DataSource {
return &flavorsDataSource{} return &flavorsDataSource{}
} }
type dataSourceModel struct {
sqlserverflexalphaGen.FlavorsModel
TerraformId types.String `tfsdk:"id"`
}
type flavorsDataSource struct { type flavorsDataSource struct {
client *sqlserverflexalphaPkg.APIClient client *sqlserverflexalpha.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
@ -48,11 +39,6 @@ func (d *flavorsDataSource) Metadata(
func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = sqlserverflexalphaGen.FlavorsDataSourceSchema(ctx) resp.Schema = sqlserverflexalphaGen.FlavorsDataSourceSchema(ctx)
resp.Schema.Attributes["id"] = schema.StringAttribute{
Computed: true,
Description: "The terraform internal identifier.",
MarkdownDescription: "The terraform internal identifier.",
}
} }
// Configure adds the provider configured client to the data source. // Configure adds the provider configured client to the data source.
@ -67,34 +53,12 @@ func (d *flavorsDataSource) Configure(
return return
} }
apiClientConfigOptions := []config.ConfigurationOption{ apiClient := sqlserverflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
config.WithCustomAuth(d.providerData.RoundTripper), if resp.Diagnostics.HasError() {
utils.UserAgentConfigOption(d.providerData.Version),
}
if d.providerData.SQLServerFlexCustomEndpoint != "" {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithEndpoint(d.providerData.SQLServerFlexCustomEndpoint),
)
} else {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithRegion(d.providerData.GetRegion()),
)
}
apiClient, err := sqlserverflexalphaPkg.NewAPIClient(apiClientConfigOptions...)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring API client",
fmt.Sprintf(
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
err,
),
)
return return
} }
d.client = apiClient d.client = apiClient
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix)) tflog.Info(ctx, "SQL SERVER Flex flavors client configured")
} }
func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
@ -107,50 +71,11 @@ func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest
return return
} }
ctx = core.InitProviderContext(ctx) // Todo: Read API call logic
projectId := data.ProjectId.ValueString() // Example data value setting
region := d.providerData.GetRegionWithOverride(data.Region) // data.Id = types.StringValue("example-id")
// TODO: implement right identifier for flavors
flavorsId := data.Flavors
ctx = tflog.SetField(ctx, "project_id", projectId) // Save data into Terraform state
ctx = tflog.SetField(ctx, "region", region)
// TODO: implement needed fields
ctx = tflog.SetField(ctx, "flavors_id", flavorsId)
// TODO: refactor to correct implementation
_, err := d.client.GetFlavorsRequest(ctx, projectId, region).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: refactor to correct implementation of internal tf id
data.TerraformId = utils.BuildInternalTerraformId(projectId, region)
// TODO: fill remaining fields
// data.Flavors = types.Sometype(apiResponse.GetFlavors())
// data.Page = types.Sometype(apiResponse.GetPage())
// data.Pagination = types.Sometype(apiResponse.GetPagination())
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
// data.Region = types.Sometype(apiResponse.GetRegion())
// data.Size = types.Sometype(apiResponse.GetSize())
// data.Sort = types.Sometype(apiResponse.GetSort())// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Info(ctx, fmt.Sprintf("%s read successful", errorPrefix))
} }

View file

@ -1,3 +1,5 @@
// Copyright (c) STACKIT
package sqlserverflexalpha package sqlserverflexalpha
import ( import (
@ -5,40 +7,47 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"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/conversion"
sqlserverflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/datasources_gen"
sqlserverflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/core" "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" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
sqlserverflexalphaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/datasources_gen"
) )
var _ datasource.DataSource = (*instanceDataSource)(nil) // dataSourceModel maps the data source schema data.
type dataSourceModel struct {
sqlserverflexalpha2.InstanceModel
TerraformID types.String `tfsdk:"id"`
}
const errorPrefix = "[sqlserverflexalpha - Instance]" // Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &instanceDataSource{}
)
// NewInstanceDataSource is a helper function to simplify the provider implementation.
func NewInstanceDataSource() datasource.DataSource { func NewInstanceDataSource() datasource.DataSource {
return &instanceDataSource{} return &instanceDataSource{}
} }
// dataSourceModel maps the data source schema data. // instanceDataSource is the data source implementation.
type dataSourceModel struct {
sqlserverflexalphaGen.InstanceModel
TerraformID types.String `tfsdk:"id"`
}
type instanceDataSource struct { type instanceDataSource struct {
client *sqlserverflexalphaPkg.APIClient client *sqlserverflex.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
func (d *instanceDataSource) Metadata( // Metadata returns the data source type name.
func (r *instanceDataSource) Metadata(
_ context.Context, _ context.Context,
req datasource.MetadataRequest, req datasource.MetadataRequest,
resp *datasource.MetadataResponse, resp *datasource.MetadataResponse,
@ -46,80 +55,66 @@ func (d *instanceDataSource) Metadata(
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_instance" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_instance"
} }
func (d *instanceDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = sqlserverflexalphaGen.InstanceDataSourceSchema(ctx)
}
// Configure adds the provider configured client to the data source. // Configure adds the provider configured client to the data source.
func (d *instanceDataSource) Configure( func (r *instanceDataSource) Configure(
ctx context.Context, ctx context.Context,
req datasource.ConfigureRequest, req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse, resp *datasource.ConfigureResponse,
) { ) {
var ok bool var ok bool
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok { if !ok {
return return
} }
apiClientConfigOptions := []config.ConfigurationOption{ apiClient := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
config.WithCustomAuth(d.providerData.RoundTripper), if resp.Diagnostics.HasError() {
utils.UserAgentConfigOption(d.providerData.Version),
}
if d.providerData.SQLServerFlexCustomEndpoint != "" {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithEndpoint(d.providerData.SQLServerFlexCustomEndpoint),
)
} else {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithRegion(d.providerData.GetRegion()),
)
}
apiClient, err := sqlserverflexalphaPkg.NewAPIClient(apiClientConfigOptions...)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring API client",
fmt.Sprintf(
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
err,
),
)
return return
} }
d.client = apiClient r.client = apiClient
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix)) tflog.Info(ctx, "SQLServer Flex instance client configured")
} }
func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // Schema defines the schema for the data source.
var data dataSourceModel func (r *instanceDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
s := sqlserverflexalpha.InstanceDataSourceSchema(ctx)
s.Attributes["id"] = schema.StringAttribute{
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`\\\".",
Computed: true,
}
// Read Terraform configuration data into the model resp.Schema = s
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) }
// Read refreshes the Terraform state with the latest data.
func (r *instanceDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform
var model dataSourceModel
diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
region := d.providerData.GetRegionWithOverride(data.Region) instanceId := model.InstanceId.ValueString()
instanceId := data.InstanceId.ValueString() region := r.providerData.GetRegionWithOverride(model.Region)
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
instanceResp, err := d.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute() instanceResp, err := r.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
if err != nil { if err != nil {
utils.LogError( utils.LogError(
ctx, ctx,
&resp.Diagnostics, &resp.Diagnostics,
err, err,
"Reading instance", "Reading instance",
fmt.Sprintf("instance with ID %q does not exist in project %q.", instanceId, projectId), fmt.Sprintf("Instance with ID %q does not exist in project %q.", instanceId, projectId),
map[int]string{ map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
}, },
@ -130,17 +125,49 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
err = mapDataResponseToModel(ctx, instanceResp, &data, resp.Diagnostics) // var storage = &storageModel{}
// if !model.Storage.IsNull() && !model.Storage.IsUnknown() {
// diags = model.Storage.As(ctx, storage, basetypes.ObjectAsOptions{})
// resp.Diagnostics.Append(diags...)
// if resp.Diagnostics.HasError() {
// return
// }
//}
//
// var encryption = &encryptionModel{}
//if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() {
// diags = model.Encryption.As(ctx, encryption, basetypes.ObjectAsOptions{})
// resp.Diagnostics.Append(diags...)
// if resp.Diagnostics.HasError() {
// return
// }
//}
//
//var network = &networkModel{}
//if !model.Network.IsNull() && !model.Network.IsUnknown() {
// diags = model.Network.As(ctx, network, basetypes.ObjectAsOptions{})
// resp.Diagnostics.Append(diags...)
// if resp.Diagnostics.HasError() {
// return
// }
//}
err = mapFields(ctx, instanceResp, &model, resp.Diagnostics)
// err = mapFields(ctx, instanceResp, &model, storage, encryption, network, region)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
&resp.Diagnostics, &resp.Diagnostics,
fmt.Sprintf("%s Read", errorPrefix), "Error reading instance",
fmt.Sprintf("Processing API payload: %v", err), fmt.Sprintf("Processing API payload: %v", err),
) )
return return
} }
// Set refreshed state
// Save data into Terraform state diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SQLServer Flex instance read")
} }

View file

@ -2,7 +2,6 @@ package sqlserverflexalpha
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"math" "math"
@ -11,91 +10,29 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha" sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/conversion"
sqlserverflexalphaDataGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/datasources_gen" sqlserverflexResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
sqlserverflexalphaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
) )
func mapResponseToModel( // instanceModel is a type constraint for models that can be mapped from a GetInstanceResponse.
ctx context.Context, type instanceModel interface {
resp *sqlserverflexalpha.GetInstanceResponse, *dataSourceModel | *resourceModel
m *sqlserverflexalphaResGen.InstanceModel,
tfDiags diag.Diagnostics,
) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
m.Edition = types.StringValue(string(resp.GetEdition()))
m.Encryption = handleEncryption(m, resp)
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfDiags.Append(diags...)
if diags.HasError() {
return fmt.Errorf(
"error converting network acl response value",
)
}
net, diags := sqlserverflexalphaResGen.NewNetworkValue(
sqlserverflexalphaResGen.NetworkValue{}.AttributeTypes(ctx),
map[string]attr.Value{
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
"acl": netAcl,
"instance_address": types.StringValue(resp.Network.GetInstanceAddress()),
"router_address": types.StringValue(resp.Network.GetRouterAddress()),
},
)
tfDiags.Append(diags...)
if diags.HasError() {
return errors.New("error converting network response value")
}
m.Network = net
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Status = types.StringValue(string(resp.GetStatus()))
stor, diags := sqlserverflexalphaResGen.NewStorageValue(
sqlserverflexalphaResGen.StorageValue{}.AttributeTypes(ctx),
map[string]attr.Value{
"class": types.StringValue(resp.Storage.GetClass()),
"size": types.Int64Value(resp.Storage.GetSize()),
},
)
tfDiags.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting storage response value")
}
m.Storage = stor
m.Version = types.StringValue(string(resp.GetVersion()))
return nil
} }
func mapDataResponseToModel( func mapFields[T instanceModel](
ctx context.Context, ctx context.Context,
resp *sqlserverflexalpha.GetInstanceResponse, resp *sqlserverflex.GetInstanceResponse,
m *dataSourceModel, m T,
tfDiags diag.Diagnostics, tfDiags diag.Diagnostics,
) error { ) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
m.Edition = types.StringValue(string(resp.GetEdition()))
m.Encryption = handleDSEncryption(m, resp)
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl()) netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfDiags.Append(diags...) tfDiags.Append(diags...)
if diags.HasError() { if diags.HasError() {
return fmt.Errorf( return fmt.Errorf("error converting network acl response value")
"error converting network acl response value",
)
} }
net, diags := sqlserverflexalphaDataGen.NewNetworkValue( net, diags := sqlserverflexResGen.NewNetworkValue(
sqlserverflexalphaDataGen.NetworkValue{}.AttributeTypes(ctx), sqlserverflexResGen.NetworkValue{}.AttributeTypes(ctx),
map[string]attr.Value{ map[string]attr.Value{
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())), "access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
"acl": netAcl, "acl": netAcl,
@ -105,15 +42,11 @@ func mapDataResponseToModel(
) )
tfDiags.Append(diags...) tfDiags.Append(diags...)
if diags.HasError() { if diags.HasError() {
return errors.New("error converting network response value") return fmt.Errorf("error converting network response value")
} }
m.Network = net
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Status = types.StringValue(string(resp.GetStatus()))
stor, diags := sqlserverflexalphaDataGen.NewStorageValue( stor, diags := sqlserverflexResGen.NewStorageValue(
sqlserverflexalphaDataGen.StorageValue{}.AttributeTypes(ctx), sqlserverflexResGen.StorageValue{}.AttributeTypes(ctx),
map[string]attr.Value{ map[string]attr.Value{
"class": types.StringValue(resp.Storage.GetClass()), "class": types.StringValue(resp.Storage.GetClass()),
"size": types.Int64Value(resp.Storage.GetSize()), "size": types.Int64Value(resp.Storage.GetSize()),
@ -123,61 +56,62 @@ func mapDataResponseToModel(
if diags.HasError() { if diags.HasError() {
return fmt.Errorf("error converting storage response value") return fmt.Errorf("error converting storage response value")
} }
m.Storage = stor
m.Version = types.StringValue(string(resp.GetVersion())) // The interface conversion is safe due to the type constraint.
model := any(m)
if rm, ok := model.(*resourceModel); ok {
rm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
rm.Edition = types.StringValue(string(resp.GetEdition()))
rm.Encryption = handleEncryption(rm.Encryption, resp)
rm.FlavorId = types.StringValue(resp.GetFlavorId())
rm.Id = types.StringValue(resp.GetId())
rm.InstanceId = types.StringValue(resp.GetId())
rm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
rm.Name = types.StringValue(resp.GetName())
rm.Network = net
rm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
rm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
rm.Status = types.StringValue(string(resp.GetStatus()))
rm.Storage = stor
rm.Version = types.StringValue(string(resp.GetVersion()))
} else if dm, ok := model.(*dataSourceModel); ok {
dm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
dm.Edition = types.StringValue(string(resp.GetEdition()))
dm.Encryption = handleEncryption(dm.Encryption, resp)
dm.FlavorId = types.StringValue(resp.GetFlavorId())
dm.Id = types.StringValue(resp.GetId())
dm.InstanceId = types.StringValue(resp.GetId())
dm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
dm.Name = types.StringValue(resp.GetName())
dm.Network = net
dm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
dm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
dm.Status = types.StringValue(string(resp.GetStatus()))
dm.Storage = stor
dm.Version = types.StringValue(string(resp.GetVersion()))
}
return nil return nil
} }
func handleEncryption( func handleEncryption(
m *sqlserverflexalphaResGen.InstanceModel, encryptionValue sqlserverflexResGen.EncryptionValue,
resp *sqlserverflexalpha.GetInstanceResponse, resp *sqlserverflex.GetInstanceResponse,
) sqlserverflexalphaResGen.EncryptionValue { ) sqlserverflexResGen.EncryptionValue {
if !resp.HasEncryption() || if !resp.HasEncryption() ||
resp.Encryption == nil || resp.Encryption == nil ||
resp.Encryption.KekKeyId == nil || resp.Encryption.KekKeyId == nil ||
resp.Encryption.KekKeyRingId == nil || resp.Encryption.KekKeyRingId == nil ||
resp.Encryption.KekKeyVersion == nil || resp.Encryption.KekKeyVersion == nil ||
resp.Encryption.ServiceAccount == nil { resp.Encryption.ServiceAccount == nil {
if m.Encryption.IsNull() || m.Encryption.IsUnknown() { if encryptionValue.IsNull() || encryptionValue.IsUnknown() {
return sqlserverflexalphaResGen.NewEncryptionValueNull() return sqlserverflexResGen.NewEncryptionValueNull()
} }
return m.Encryption return encryptionValue
} }
enc := sqlserverflexalphaResGen.NewEncryptionValueNull() enc := sqlserverflexResGen.NewEncryptionValueNull()
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
enc.KekKeyId = types.StringValue(kVal)
}
if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
enc.KekKeyRingId = types.StringValue(kkVal)
}
if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
enc.KekKeyVersion = types.StringValue(kkvVal)
}
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
enc.ServiceAccount = types.StringValue(sa)
}
return enc
}
func handleDSEncryption(
m *dataSourceModel,
resp *sqlserverflexalpha.GetInstanceResponse,
) sqlserverflexalphaDataGen.EncryptionValue {
if !resp.HasEncryption() ||
resp.Encryption == nil ||
resp.Encryption.KekKeyId == nil ||
resp.Encryption.KekKeyRingId == nil ||
resp.Encryption.KekKeyVersion == nil ||
resp.Encryption.ServiceAccount == nil {
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
return sqlserverflexalphaDataGen.NewEncryptionValueNull()
}
return m.Encryption
}
enc := sqlserverflexalphaDataGen.NewEncryptionValueNull()
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok { if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
enc.KekKeyId = types.StringValue(kVal) enc.KekKeyId = types.StringValue(kVal)
} }
@ -195,25 +129,25 @@ func handleDSEncryption(
func toCreatePayload( func toCreatePayload(
ctx context.Context, ctx context.Context,
model *sqlserverflexalphaResGen.InstanceModel, model *sqlserverflexResGen.InstanceModel,
) (*sqlserverflexalpha.CreateInstanceRequestPayload, error) { ) (*sqlserverflex.CreateInstanceRequestPayload, error) {
if model == nil { if model == nil {
return nil, fmt.Errorf("nil model") return nil, fmt.Errorf("nil model")
} }
storagePayload := &sqlserverflexalpha.CreateInstanceRequestPayloadGetStorageArgType{} storagePayload := &sqlserverflex.CreateInstanceRequestPayloadGetStorageArgType{}
if !model.Storage.IsNull() && !model.Storage.IsUnknown() { if !model.Storage.IsNull() && !model.Storage.IsUnknown() {
storagePayload.Class = model.Storage.Class.ValueStringPointer() storagePayload.Class = model.Storage.Class.ValueStringPointer()
storagePayload.Size = model.Storage.Size.ValueInt64Pointer() storagePayload.Size = model.Storage.Size.ValueInt64Pointer()
} }
var encryptionPayload *sqlserverflexalpha.CreateInstanceRequestPayloadGetEncryptionArgType = nil var encryptionPayload *sqlserverflex.CreateInstanceRequestPayloadGetEncryptionArgType = nil
if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() && if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() &&
!model.Encryption.KekKeyId.IsNull() && model.Encryption.KekKeyId.IsUnknown() && model.Encryption.KekKeyId.ValueString() != "" && !model.Encryption.KekKeyId.IsNull() && model.Encryption.KekKeyId.IsUnknown() && model.Encryption.KekKeyId.ValueString() != "" &&
!model.Encryption.KekKeyRingId.IsNull() && !model.Encryption.KekKeyRingId.IsUnknown() && model.Encryption.KekKeyRingId.ValueString() != "" && !model.Encryption.KekKeyRingId.IsNull() && !model.Encryption.KekKeyRingId.IsUnknown() && model.Encryption.KekKeyRingId.ValueString() != "" &&
!model.Encryption.KekKeyVersion.IsNull() && !model.Encryption.KekKeyVersion.IsUnknown() && model.Encryption.KekKeyVersion.ValueString() != "" && !model.Encryption.KekKeyVersion.IsNull() && !model.Encryption.KekKeyVersion.IsUnknown() && model.Encryption.KekKeyVersion.ValueString() != "" &&
!model.Encryption.ServiceAccount.IsNull() && !model.Encryption.ServiceAccount.IsUnknown() && model.Encryption.ServiceAccount.ValueString() != "" { !model.Encryption.ServiceAccount.IsNull() && !model.Encryption.ServiceAccount.IsUnknown() && model.Encryption.ServiceAccount.ValueString() != "" {
encryptionPayload = &sqlserverflexalpha.CreateInstanceRequestPayloadGetEncryptionArgType{ encryptionPayload = &sqlserverflex.CreateInstanceRequestPayloadGetEncryptionArgType{
KekKeyId: model.Encryption.KekKeyId.ValueStringPointer(), KekKeyId: model.Encryption.KekKeyId.ValueStringPointer(),
KekKeyRingId: model.Encryption.KekKeyVersion.ValueStringPointer(), KekKeyRingId: model.Encryption.KekKeyVersion.ValueStringPointer(),
KekKeyVersion: model.Encryption.KekKeyRingId.ValueStringPointer(), KekKeyVersion: model.Encryption.KekKeyRingId.ValueStringPointer(),
@ -221,9 +155,9 @@ func toCreatePayload(
} }
} }
networkPayload := &sqlserverflexalpha.CreateInstanceRequestPayloadGetNetworkArgType{} networkPayload := &sqlserverflex.CreateInstanceRequestPayloadGetNetworkArgType{}
if !model.Network.IsNull() && !model.Network.IsUnknown() { if !model.Network.IsNull() && !model.Network.IsUnknown() {
networkPayload.AccessScope = sqlserverflexalpha.CreateInstanceRequestPayloadNetworkGetAccessScopeAttributeType( networkPayload.AccessScope = sqlserverflex.CreateInstanceRequestPayloadNetworkGetAccessScopeAttributeType(
model.Network.AccessScope.ValueStringPointer(), model.Network.AccessScope.ValueStringPointer(),
) )
@ -235,7 +169,7 @@ func toCreatePayload(
networkPayload.Acl = &resList networkPayload.Acl = &resList
} }
return &sqlserverflexalpha.CreateInstanceRequestPayload{ return &sqlserverflex.CreateInstanceRequestPayload{
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule), BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
Encryption: encryptionPayload, Encryption: encryptionPayload,
FlavorId: conversion.StringValueToPointer(model.FlavorId), FlavorId: conversion.StringValueToPointer(model.FlavorId),
@ -243,24 +177,20 @@ func toCreatePayload(
Network: networkPayload, Network: networkPayload,
RetentionDays: conversion.Int64ValueToPointer(model.RetentionDays), RetentionDays: conversion.Int64ValueToPointer(model.RetentionDays),
Storage: storagePayload, Storage: storagePayload,
Version: sqlserverflexalpha.CreateInstanceRequestPayloadGetVersionAttributeType( Version: sqlserverflex.CreateInstanceRequestPayloadGetVersionAttributeType(conversion.StringValueToPointer(model.Version)),
conversion.StringValueToPointer(model.Version),
),
}, nil }, nil
} }
// TODO: check func with his args
func toUpdatePayload( func toUpdatePayload(
ctx context.Context, ctx context.Context,
m *sqlserverflexalphaResGen.InstanceModel, m *sqlserverflexResGen.InstanceModel,
resp *resource.UpdateResponse, resp *resource.UpdateResponse,
) (*sqlserverflexalpha.UpdateInstanceRequestPayload, error) { ) (*sqlserverflex.UpdateInstanceRequestPayload, error) {
if m == nil {
return nil, fmt.Errorf("nil model")
}
if m.Replicas.ValueInt64() > math.MaxUint32 { if m.Replicas.ValueInt64() > math.MaxUint32 {
return nil, fmt.Errorf("replicas value is too big for uint32") return nil, fmt.Errorf("replicas value is too big for uint32")
} }
replVal := sqlserverflexalpha.Replicas(uint32(m.Replicas.ValueInt64())) // nolint:gosec // check is performed above replVal := sqlserverflex.Replicas(uint32(m.Replicas.ValueInt64())) // nolint:gosec // check is performed above
var netAcl []string var netAcl []string
diags := m.Network.Acl.ElementsAs(ctx, &netAcl, false) diags := m.Network.Acl.ElementsAs(ctx, &netAcl, false)
@ -268,16 +198,16 @@ func toUpdatePayload(
if diags.HasError() { if diags.HasError() {
return nil, fmt.Errorf("error converting model network acl value") return nil, fmt.Errorf("error converting model network acl value")
} }
return &sqlserverflexalpha.UpdateInstanceRequestPayload{ return &sqlserverflex.UpdateInstanceRequestPayload{
BackupSchedule: m.BackupSchedule.ValueStringPointer(), BackupSchedule: m.BackupSchedule.ValueStringPointer(),
FlavorId: m.FlavorId.ValueStringPointer(), FlavorId: m.FlavorId.ValueStringPointer(),
Name: m.Name.ValueStringPointer(), Name: m.Name.ValueStringPointer(),
Network: sqlserverflexalpha.NewUpdateInstanceRequestPayloadNetwork(netAcl), Network: &sqlserverflex.UpdateInstanceRequestPayloadNetwork{
Replicas: &replVal, Acl: &netAcl,
RetentionDays: m.RetentionDays.ValueInt64Pointer(), },
Storage: &sqlserverflexalpha.StorageUpdate{Size: m.Storage.Size.ValueInt64Pointer()}, Replicas: &replVal,
Version: sqlserverflexalpha.UpdateInstanceRequestPayloadGetVersionAttributeType( RetentionDays: m.RetentionDays.ValueInt64Pointer(),
m.Version.ValueStringPointer(), Storage: &sqlserverflex.StorageUpdate{Size: m.Storage.Size.ValueInt64Pointer()},
), Version: sqlserverflex.UpdateInstanceRequestPayloadGetVersionAttributeType(m.Version.ValueStringPointer()),
}, nil }, nil
} }

View file

@ -1,3 +1,5 @@
// Copyright (c) STACKIT
package sqlserverflexalpha package sqlserverflexalpha
import ( import (
@ -8,25 +10,26 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
sqlserverflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
wait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
wait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
sqlserverflexalphaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen" "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
) )
// Ensure the implementation satisfies the expected interfaces.
var ( var (
_ resource.Resource = &instanceResource{} _ resource.Resource = &instanceResource{}
_ resource.ResourceWithConfigure = &instanceResource{} _ resource.ResourceWithConfigure = &instanceResource{}
@ -35,17 +38,19 @@ var (
_ resource.ResourceWithIdentity = &instanceResource{} _ resource.ResourceWithIdentity = &instanceResource{}
) )
// NewInstanceResource is a helper function to simplify the provider implementation.
func NewInstanceResource() resource.Resource { func NewInstanceResource() resource.Resource {
return &instanceResource{} return &instanceResource{}
} }
type instanceResource struct { //nolint:unused // TODO: remove if not needed later
client *sqlserverflexalpha.APIClient var validNodeTypes []string = []string{
providerData core.ProviderData "Single",
"Replica",
} }
// resourceModel describes the resource data model. // resourceModel describes the resource data model.
type resourceModel = sqlserverflexalphaResGen.InstanceModel type resourceModel = sqlserverflexalpha2.InstanceModel
type InstanceResourceIdentityModel struct { type InstanceResourceIdentityModel struct {
ProjectID types.String `tfsdk:"project_id"` ProjectID types.String `tfsdk:"project_id"`
@ -53,19 +58,93 @@ type InstanceResourceIdentityModel struct {
InstanceID types.String `tfsdk:"instance_id"` InstanceID types.String `tfsdk:"instance_id"`
} }
func (r *instanceResource) Metadata( // instanceResource is the resource implementation.
_ context.Context, type instanceResource struct {
req resource.MetadataRequest, client *sqlserverflexalpha.APIClient
resp *resource.MetadataResponse, providerData core.ProviderData
) { }
// Metadata returns the resource type name.
func (r *instanceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_instance" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_instance"
} }
// Configure adds the provider configured client to the resource.
func (r *instanceResource) Configure(
ctx context.Context,
req resource.ConfigureRequest,
resp *resource.ConfigureResponse,
) {
var ok bool
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
apiClient := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
r.client = apiClient
tflog.Info(ctx, "SQLServer Flex instance client configured")
}
// ModifyPlan implements resource.ResourceWithModifyPlan.
// Use the modifier to set the effective region in the current plan.
func (r *instanceResource) ModifyPlan(
ctx context.Context,
req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform
// skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() {
return
}
var configModel sqlserverflexalpha2.InstanceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
if resp.Diagnostics.HasError() {
return
}
if req.Plan.Raw.IsNull() {
return
}
var planModel sqlserverflexalpha2.InstanceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
if resp.Diagnostics.HasError() {
return
}
var identityModel InstanceResourceIdentityModel
identityModel.ProjectID = planModel.ProjectId
identityModel.Region = planModel.Region
if !planModel.InstanceId.IsNull() && !planModel.InstanceId.IsUnknown() {
identityModel.InstanceID = planModel.InstanceId
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identityModel)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() {
return
}
}
//go:embed planModifiers.yaml //go:embed planModifiers.yaml
var modifiersFileByte []byte var modifiersFileByte []byte
// Schema defines the schema for the resource.
func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
s := sqlserverflexalphaResGen.InstanceResourceSchema(ctx) schema := sqlserverflexalpha2.InstanceResourceSchema(ctx)
fields, err := utils.ReadModifiersConfig(modifiersFileByte) fields, err := utils.ReadModifiersConfig(modifiersFileByte)
if err != nil { if err != nil {
@ -73,12 +152,12 @@ func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest,
return return
} }
err = utils.AddPlanModifiersToResourceSchema(fields, &s) err = utils.AddPlanModifiersToResourceSchema(fields, &schema)
if err != nil { if err != nil {
resp.Diagnostics.AddError("error adding plan modifiers", err.Error()) resp.Diagnostics.AddError("error adding plan modifiers", err.Error())
return return
} }
resp.Schema = s resp.Schema = schema
} }
func (r *instanceResource) IdentitySchema( func (r *instanceResource) IdentitySchema(
@ -101,354 +180,15 @@ func (r *instanceResource) IdentitySchema(
} }
} }
// Configure adds the provider configured client to the resource. // Create creates the resource and sets the initial Terraform state.
func (r *instanceResource) Configure( func (r *instanceResource) Create(
ctx context.Context, ctx context.Context,
req resource.ConfigureRequest, req resource.CreateRequest,
resp *resource.ConfigureResponse, resp *resource.CreateResponse,
) {
var ok bool
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
apiClientConfigOptions := []config.ConfigurationOption{
config.WithCustomAuth(r.providerData.RoundTripper),
utils.UserAgentConfigOption(r.providerData.Version),
}
if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClientConfigOptions = append(
apiClientConfigOptions,
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
)
} else {
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
}
apiClient, err := sqlserverflexalpha.NewAPIClient(apiClientConfigOptions...)
if err != nil {
resp.Diagnostics.AddError(
"Error configuring API client",
fmt.Sprintf(
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
err,
),
)
return
}
r.client = apiClient
tflog.Info(ctx, "sqlserverflexalpha.Instance client configured")
}
// ModifyPlan implements resource.ResourceWithModifyPlan.
// Use the modifier to set the effective region in the current plan.
func (r *instanceResource) ModifyPlan(
ctx context.Context,
req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
// skip initial empty configuration to avoid follow-up errors var model resourceModel
if req.Config.Raw.IsNull() { diags := req.Plan.Get(ctx, &model)
return resp.Diagnostics.Append(diags...)
}
var configModel resourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
if resp.Diagnostics.HasError() {
return
}
if req.Plan.Raw.IsNull() {
return
}
var planModel resourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *instanceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data resourceModel
crateErr := "[SQL Server Flex BETA - Create] error"
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString()
region := data.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
// Generate API request body from model
payload, err := toCreatePayload(ctx, &data)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
crateErr,
fmt.Sprintf("Creating API payload: %v", err),
)
return
}
// Create new Instance
createResp, err := r.client.CreateInstanceRequest(
ctx,
projectId,
region,
).CreateInstanceRequestPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, crateErr, fmt.Sprintf("Calling API: %v", err))
return
}
ctx = core.LogResponse(ctx)
InstanceId := *createResp.Id
// Example data value setting
data.InstanceId = types.StringValue("id-from-response")
identity := InstanceResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(InstanceId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
waitResp, err := wait.CreateInstanceWaitHandler(
ctx,
r.client,
projectId,
InstanceId,
region,
).SetSleepBeforeWait(
10 * time.Second,
).SetTimeout(
90 * time.Minute,
).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
crateErr,
fmt.Sprintf("Instance creation waiting: %v", err),
)
return
}
if waitResp.Id == nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
crateErr,
"Instance creation waiting: returned id is nil",
)
return
}
// Map response body to schema
err = mapResponseToModel(ctx, waitResp, &data, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
crateErr,
fmt.Sprintf("processing API payload: %v", err),
)
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Info(ctx, "sqlserverflexalpha.Instance created")
}
func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data resourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Read identity data
var identityData InstanceResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString()
region := data.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
instanceId := data.InstanceId.ValueString()
ctx = tflog.SetField(ctx, "instance_id", instanceId)
instanceResp, err := r.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
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 && oapiErr.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", err.Error())
return
}
ctx = core.LogResponse(ctx)
// Map response body to schema
err = mapResponseToModel(ctx, instanceResp, &data, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error reading instance",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Save identity into Terraform state
identity := InstanceResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "sqlserverflexalpha.Instance read")
}
func (r *instanceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data resourceModel
updateInstanceError := "Error updating instance"
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString()
region := data.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
instanceId := data.InstanceId.ValueString()
ctx = tflog.SetField(ctx, "instance_id", instanceId)
// Generate API request body from model
payload, err := toUpdatePayload(ctx, &data, resp)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
updateInstanceError,
fmt.Sprintf("Creating API payload: %v", err),
)
return
}
// Update existing instance
err = r.client.UpdateInstanceRequest(
ctx,
projectId,
region,
instanceId,
).UpdateInstanceRequestPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, updateInstanceError, err.Error())
return
}
ctx = core.LogResponse(ctx)
waitResp, err := wait.
UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).
SetSleepBeforeWait(15 * time.Second).
SetTimeout(45 * time.Minute).
WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
updateInstanceError,
fmt.Sprintf("Instance update waiting: %v", err),
)
return
}
// Map response body to schema
err = mapResponseToModel(ctx, waitResp, &data, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
updateInstanceError,
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
identity := InstanceResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "sqlserverflexalpha.Instance updated")
}
func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data resourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
@ -467,8 +207,284 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "region", region)
instanceId := identityData.InstanceID.ValueString() // Generate API request body from model
payload, err := toCreatePayload(ctx, &model)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating instance",
fmt.Sprintf("Creating API payload: %v", err),
)
return
}
// Create new instance
createResp, err := r.client.CreateInstanceRequest(
ctx,
projectId,
region,
).CreateInstanceRequestPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
return
}
ctx = core.LogResponse(ctx)
instanceId := *createResp.Id
// Set data returned by API in identity
identity := InstanceResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
utils.SetAndLogStateFields(
ctx, &resp.Diagnostics, &resp.State, map[string]any{
"id": utils.BuildInternalTerraformId(projectId, region, instanceId),
"instance_id": instanceId,
},
)
if resp.Diagnostics.HasError() {
return
}
// The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure"
// which can be avoided by sleeping before wait
waitResp, err := wait.CreateInstanceWaitHandler(
ctx,
r.client,
projectId,
instanceId,
region,
).SetSleepBeforeWait(
30 * time.Second,
).SetTimeout(
90 * time.Minute,
).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating instance",
fmt.Sprintf("Instance creation waiting: %v", err),
)
return
}
if waitResp.FlavorId == nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating instance",
"Instance creation waiting: returned flavor id is nil",
)
return
}
// Map response body to schema
// err = mapFields(ctx, waitResp, &model, storage, encryption, network, region)
err = mapFields(ctx, waitResp, &model, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating instance",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SQLServer Flex instance created")
}
// Read refreshes the Terraform state with the latest data.
func (r *instanceResource) Read(
ctx context.Context,
req resource.ReadRequest,
resp *resource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform
var model resourceModel
diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Read identity data
var identityData InstanceResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString()
region := r.providerData.GetRegionWithOverride(model.Region)
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
instanceResp, err := r.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
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 && oapiErr.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", err.Error())
return
}
ctx = core.LogResponse(ctx)
// Map response body to schema
err = mapFields(ctx, instanceResp, &model, resp.Diagnostics)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error reading instance",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Set refreshed state
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Set data returned by API in identity
identity := InstanceResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SQLServer Flex instance read")
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *instanceResource) Update(
ctx context.Context,
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) { // nolint:gocritic // function signature required by Terraform
// Retrieve values from plan
var model resourceModel
diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
// Generate API request body from model
payload, err := toUpdatePayload(ctx, &model, resp)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error updating instance",
fmt.Sprintf("Creating API payload: %v", err),
)
return
}
// Update existing instance
err = r.client.UpdateInstanceRequest(
ctx,
projectId,
region,
instanceId,
).UpdateInstanceRequestPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error())
return
}
ctx = core.LogResponse(ctx)
waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error updating instance",
fmt.Sprintf("Instance update waiting: %v", err),
)
return
}
// Map response body to schema
err = mapFields(ctx, waitResp, &model, resp.Diagnostics)
// err = mapFields(ctx, waitResp, &model, storage, encryption, network, region)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error updating instance",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SQLServer Flex instance updated")
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *instanceResource) Delete(
ctx context.Context,
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) { // nolint:gocritic // function signature required by Terraform
// Retrieve values from state
var model resourceModel
diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
// Delete existing instance // Delete existing instance
err := r.client.DeleteInstanceRequest(ctx, projectId, region, instanceId).Execute() err := r.client.DeleteInstanceRequest(ctx, projectId, region, instanceId).Execute()
@ -479,7 +495,7 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
delResp, err := wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx) _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -489,31 +505,16 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
) )
return return
} }
tflog.Info(ctx, "SQLServer Flex instance deleted")
if delResp != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error deleting instance",
"wait handler returned non nil result",
)
return
}
resp.State.RemoveResource(ctx)
tflog.Info(ctx, "sqlserverflexalpha.Instance deleted")
} }
// ImportState imports a resource into the Terraform state on success. // ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,zone_id,record_set_id // The expected format of the resource import identifier is: project_id,instance_id
func (r *instanceResource) ImportState( func (r *instanceResource) ImportState(
ctx context.Context, ctx context.Context,
req resource.ImportStateRequest, req resource.ImportStateRequest,
resp *resource.ImportStateResponse, resp *resource.ImportStateResponse,
) { ) {
ctx = core.InitProviderContext(ctx)
if req.ID != "" { if req.ID != "" {
idParts := strings.Split(req.ID, core.Separator) idParts := strings.Split(req.ID, core.Separator)
@ -522,7 +523,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format: [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )
@ -550,5 +551,5 @@ func (r *instanceResource) ImportState(
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...)
tflog.Info(ctx, "sqlserverflexalpha instance state imported") tflog.Info(ctx, "SQLServer Flex instance state imported")
} }

View file

@ -0,0 +1,823 @@
package sqlserverflexalpha
// type sqlserverflexClientMocked struct {
// returnError bool
// listFlavorsResp *sqlserverflex.GetFlavorsResponse
// }
//
// func (c *sqlserverflexClientMocked) GetFlavorsExecute(_ context.Context, _, _ string) (*sqlserverflex.GetFlavorsResponse, error) {
// if c.returnError {
// return nil, fmt.Errorf("get flavors failed")
// }
//
// return c.listFlavorsResp, nil
// }
// func TestMapFields(t *testing.T) {
// t.Skip("Skipping - needs refactoring")
// const testRegion = "region"
// tests := []struct {
// description string
// state Model
// input *sqlserverflex.GetInstanceResponse
// storage *storageModel
// encryption *encryptionModel
// network *networkModel
// region string
// expected Model
// isValid bool
// }{
// {
// "default_values",
// Model{
// InstanceId: types.StringValue("iid"),
// ProjectId: types.StringValue("pid"),
// Replicas: types.Int64Value(1),
// RetentionDays: types.Int64Value(1),
// Version: types.StringValue("v1"),
// Edition: types.StringValue("edition 1"),
// Status: types.StringValue("status"),
// IsDeletable: types.BoolValue(true),
// },
// &sqlserverflex.GetInstanceResponse{
// FlavorId: utils.Ptr("flavor_id"),
// Replicas: sqlserverflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(1))),
// RetentionDays: utils.Ptr(int64(1)),
// Version: sqlserverflex.GetInstanceResponseGetVersionAttributeType(utils.Ptr("v1")),
// Edition: sqlserverflex.GetInstanceResponseGetEditionAttributeType(utils.Ptr("edition 1")),
// Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
// IsDeletable: utils.Ptr(true),
// },
// &storageModel{},
// &encryptionModel{},
// &networkModel{
// ACL: types.ListNull(basetypes.StringType{}),
// },
// testRegion,
// Model{
// Id: types.StringValue("pid,region,iid"),
// InstanceId: types.StringValue("iid"),
// ProjectId: types.StringValue("pid"),
// Name: types.StringNull(),
// BackupSchedule: types.StringNull(),
// Replicas: types.Int64Value(1),
// Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
// "class": types.StringNull(),
// "size": types.Int64Null(),
// }),
// Encryption: types.ObjectValueMust(encryptionTypes, map[string]attr.Value{
// "keyring_id": types.StringNull(),
// "key_id": types.StringNull(),
// "key_version": types.StringNull(),
// "service_account": types.StringNull(),
// }),
// Network: types.ObjectValueMust(networkTypes, map[string]attr.Value{
// "acl": types.ListNull(types.StringType),
// "access_scope": types.StringNull(),
// "instance_address": types.StringNull(),
// "router_address": types.StringNull(),
// }),
// IsDeletable: types.BoolValue(true),
// Edition: types.StringValue("edition 1"),
// Status: types.StringValue("status"),
// RetentionDays: types.Int64Value(1),
// Version: types.StringValue("v1"),
// Region: types.StringValue(testRegion),
// },
// true,
// },
// {
// "simple_values",
// Model{
// InstanceId: types.StringValue("iid"),
// ProjectId: types.StringValue("pid"),
// },
// &sqlserverflex.GetInstanceResponse{
// BackupSchedule: utils.Ptr("schedule"),
// FlavorId: utils.Ptr("flavor_id"),
// Id: utils.Ptr("iid"),
// Name: utils.Ptr("name"),
// Replicas: sqlserverflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
// Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
// Storage: &sqlserverflex.Storage{
// Class: utils.Ptr("class"),
// Size: utils.Ptr(int64(78)),
// },
// Edition: sqlserverflex.GetInstanceResponseGetEditionAttributeType(utils.Ptr("edition")),
// RetentionDays: utils.Ptr(int64(1)),
// Version: sqlserverflex.GetInstanceResponseGetVersionAttributeType(utils.Ptr("version")),
// IsDeletable: utils.Ptr(true),
// Encryption: nil,
// Network: &sqlserverflex.InstanceNetwork{
// AccessScope: nil,
// Acl: &[]string{
// "ip1",
// "ip2",
// "",
// },
// InstanceAddress: nil,
// RouterAddress: nil,
// },
// },
// &storageModel{},
// &encryptionModel{},
// &networkModel{
// ACL: types.ListValueMust(basetypes.StringType{}, []attr.Value{
// types.StringValue("ip1"),
// types.StringValue("ip2"),
// types.StringValue(""),
// }),
// },
// testRegion,
// Model{
// Id: types.StringValue("pid,region,iid"),
// InstanceId: types.StringValue("iid"),
// ProjectId: types.StringValue("pid"),
// Name: types.StringValue("name"),
// BackupSchedule: types.StringValue("schedule"),
// Replicas: types.Int64Value(56),
// Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
// "class": types.StringValue("class"),
// "size": types.Int64Value(78),
// }),
// Network: types.ObjectValueMust(networkTypes, map[string]attr.Value{
// "acl": types.ListValueMust(types.StringType, []attr.Value{
// types.StringValue("ip1"),
// types.StringValue("ip2"),
// types.StringValue(""),
// }),
// "access_scope": types.StringNull(),
// "instance_address": types.StringNull(),
// "router_address": types.StringNull(),
// }),
// Edition: types.StringValue("edition"),
// RetentionDays: types.Int64Value(1),
// Version: types.StringValue("version"),
// Region: types.StringValue(testRegion),
// IsDeletable: types.BoolValue(true),
// Encryption: types.ObjectValueMust(encryptionTypes, map[string]attr.Value{
// "keyring_id": types.StringNull(),
// "key_id": types.StringNull(),
// "key_version": types.StringNull(),
// "service_account": types.StringNull(),
// }),
// Status: types.StringValue("status"),
// },
// true,
// },
// // {
// // "simple_values_no_flavor_and_storage",
// // Model{
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // },
// // &sqlserverflex.GetInstanceResponse{
// // Acl: &[]string{
// // "ip1",
// // "ip2",
// // "",
// // },
// // BackupSchedule: utils.Ptr("schedule"),
// // FlavorId: nil,
// // Id: utils.Ptr("iid"),
// // Name: utils.Ptr("name"),
// // Replicas: sqlserverflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
// // Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
// // Storage: nil,
// // Edition: sqlserverflex.GetInstanceResponseGetEditionAttributeType(utils.Ptr("edition")),
// // RetentionDays: utils.Ptr(int64(1)),
// // Version: sqlserverflex.GetInstanceResponseGetVersionAttributeType(utils.Ptr("version")),
// // },
// // &flavorModel{
// // CPU: types.Int64Value(12),
// // RAM: types.Int64Value(34),
// // },
// // &storageModel{
// // Class: types.StringValue("class"),
// // Size: types.Int64Value(78),
// // },
// // &optionsModel{
// // Edition: types.StringValue("edition"),
// // RetentionDays: types.Int64Value(1),
// // },
// // testRegion,
// // Model{
// // Id: types.StringValue("pid,region,iid"),
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // Name: types.StringValue("name"),
// // ACL: types.ListValueMust(types.StringType, []attr.Value{
// // types.StringValue("ip1"),
// // types.StringValue("ip2"),
// // types.StringValue(""),
// // }),
// // BackupSchedule: types.StringValue("schedule"),
// // Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
// // "id": types.StringNull(),
// // "description": types.StringNull(),
// // "cpu": types.Int64Value(12),
// // "ram": types.Int64Value(34),
// // }),
// // Replicas: types.Int64Value(56),
// // Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
// // "class": types.StringValue("class"),
// // "size": types.Int64Value(78),
// // }),
// // Options: types.ObjectValueMust(optionsTypes, map[string]attr.Value{
// // "edition": types.StringValue("edition"),
// // "retention_days": types.Int64Value(1),
// // }),
// // Version: types.StringValue("version"),
// // Region: types.StringValue(testRegion),
// // },
// // true,
// // },
// // {
// // "acls_unordered",
// // Model{
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // ACL: types.ListValueMust(types.StringType, []attr.Value{
// // types.StringValue("ip2"),
// // types.StringValue(""),
// // types.StringValue("ip1"),
// // }),
// // },
// // &sqlserverflex.GetInstanceResponse{
// // Acl: &[]string{
// // "",
// // "ip1",
// // "ip2",
// // },
// // BackupSchedule: utils.Ptr("schedule"),
// // FlavorId: nil,
// // Id: utils.Ptr("iid"),
// // Name: utils.Ptr("name"),
// // Replicas: sqlserverflex.GetInstanceResponseGetReplicasAttributeType(utils.Ptr(int32(56))),
// // Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr("status")),
// // Storage: nil,
// // //Options: &map[string]string{
// // // "edition": "edition",
// // // "retentionDays": "1",
// // //},
// // Version: sqlserverflex.GetInstanceResponseGetVersionAttributeType(utils.Ptr("version")),
// // },
// // &flavorModel{
// // CPU: types.Int64Value(12),
// // RAM: types.Int64Value(34),
// // },
// // &storageModel{
// // Class: types.StringValue("class"),
// // Size: types.Int64Value(78),
// // },
// // &optionsModel{},
// // testRegion,
// // Model{
// // Id: types.StringValue("pid,region,iid"),
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // Name: types.StringValue("name"),
// // ACL: types.ListValueMust(types.StringType, []attr.Value{
// // types.StringValue("ip2"),
// // types.StringValue(""),
// // types.StringValue("ip1"),
// // }),
// // BackupSchedule: types.StringValue("schedule"),
// // Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
// // "id": types.StringNull(),
// // "description": types.StringNull(),
// // "cpu": types.Int64Value(12),
// // "ram": types.Int64Value(34),
// // }),
// // Replicas: types.Int64Value(56),
// // Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
// // "class": types.StringValue("class"),
// // "size": types.Int64Value(78),
// // }),
// // Options: types.ObjectValueMust(optionsTypes, map[string]attr.Value{
// // "edition": types.StringValue("edition"),
// // "retention_days": types.Int64Value(1),
// // }),
// // Version: types.StringValue("version"),
// // Region: types.StringValue(testRegion),
// // },
// // true,
// // },
// // {
// // "nil_response",
// // Model{
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // },
// // nil,
// // &flavorModel{},
// // &storageModel{},
// // &optionsModel{},
// // testRegion,
// // Model{},
// // false,
// // },
// // {
// // "no_resource_id",
// // Model{
// // InstanceId: types.StringValue("iid"),
// // ProjectId: types.StringValue("pid"),
// // },
// // &sqlserverflex.GetInstanceResponse{},
// // &flavorModel{},
// // &storageModel{},
// // &optionsModel{},
// // testRegion,
// // Model{},
// // false,
// // },
// }
// for _, tt := range tests {
// t.Run(tt.description, func(t *testing.T) {
// err := mapFields(context.Background(), tt.input, &tt.state, tt.storage, tt.encryption, tt.network, tt.region)
// if !tt.isValid && err == nil {
// t.Fatalf("Should have failed")
// }
// if tt.isValid && err != nil {
// t.Fatalf("Should not have failed: %v", err)
// }
// if tt.isValid {
// diff := cmp.Diff(tt.state, tt.expected)
// if diff != "" {
// t.Fatalf("Data does not match: %s", diff)
// }
// }
// })
// }
//}
// func TestToCreatePayload(t *testing.T) {
// tests := []struct {
// description string
// input *Model
// inputAcl []string
// inputFlavor *flavorModel
// inputStorage *storageModel
// inputOptions *optionsModel
// expected *sqlserverflex.CreateInstanceRequestPayload
// isValid bool
// }{
// {
// "default_values",
// &Model{},
// []string{},
// &flavorModel{},
// &storageModel{},
// &optionsModel{},
// &sqlserverflex.CreateInstanceRequestPayload{
// Acl: &sqlserverflex.CreateInstanceRequestPayloadGetAclArgType{},
// Storage: &sqlserverflex.CreateInstanceRequestPayloadGetStorageArgType{},
// },
// true,
// },
// {
// "simple_values",
// &Model{
// BackupSchedule: types.StringValue("schedule"),
// Name: types.StringValue("name"),
// Replicas: types.Int64Value(12),
// Version: types.StringValue("version"),
// },
// []string{
// "ip_1",
// "ip_2",
// },
// &flavorModel{
// Id: types.StringValue("flavor_id"),
// },
// &storageModel{
// Class: types.StringValue("class"),
// Size: types.Int64Value(34),
// },
// &optionsModel{
// Edition: types.StringValue("edition"),
// RetentionDays: types.Int64Value(1),
// },
// &sqlserverflex.CreateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{
// "ip_1",
// "ip_2",
// },
// },
// BackupSchedule: utils.Ptr("schedule"),
// FlavorId: utils.Ptr("flavor_id"),
// Name: utils.Ptr("name"),
// Storage: &sqlserverflex.CreateInstancePayloadStorage{
// Class: utils.Ptr("class"),
// Size: utils.Ptr(int64(34)),
// },
// Options: &sqlserverflex.CreateInstancePayloadOptions{
// Edition: utils.Ptr("edition"),
// RetentionDays: utils.Ptr("1"),
// },
// Version: utils.Ptr("version"),
// },
// true,
// },
// {
// "null_fields_and_int_conversions",
// &Model{
// BackupSchedule: types.StringNull(),
// Name: types.StringNull(),
// Replicas: types.Int64Value(2123456789),
// Version: types.StringNull(),
// },
// []string{
// "",
// },
// &flavorModel{
// Id: types.StringNull(),
// },
// &storageModel{
// Class: types.StringNull(),
// Size: types.Int64Null(),
// },
// &optionsModel{
// Edition: types.StringNull(),
// RetentionDays: types.Int64Null(),
// },
// &sqlserverflex.CreateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{
// "",
// },
// },
// BackupSchedule: nil,
// FlavorId: nil,
// Name: nil,
// Storage: &sqlserverflex.CreateInstancePayloadStorage{
// Class: nil,
// Size: nil,
// },
// Options: &sqlserverflex.CreateInstancePayloadOptions{},
// Version: nil,
// },
// true,
// },
// {
// "nil_model",
// nil,
// []string{},
// &flavorModel{},
// &storageModel{},
// &optionsModel{},
// nil,
// false,
// },
// {
// "nil_acl",
// &Model{},
// nil,
// &flavorModel{},
// &storageModel{},
// &optionsModel{},
// &sqlserverflex.CreateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{},
// Storage: &sqlserverflex.CreateInstancePayloadStorage{},
// Options: &sqlserverflex.CreateInstancePayloadOptions{},
// },
// true,
// },
// {
// "nil_flavor",
// &Model{},
// []string{},
// nil,
// &storageModel{},
// &optionsModel{},
// nil,
// false,
// },
// {
// "nil_storage",
// &Model{},
// []string{},
// &flavorModel{},
// nil,
// &optionsModel{},
// &sqlserverflex.CreateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{},
// },
// Storage: &sqlserverflex.CreateInstancePayloadStorage{},
// Options: &sqlserverflex.CreateInstancePayloadOptions{},
// },
// true,
// },
// {
// "nil_options",
// &Model{},
// []string{},
// &flavorModel{},
// &storageModel{},
// nil,
// &sqlserverflex.CreateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{},
// },
// Storage: &sqlserverflex.CreateInstancePayloadStorage{},
// Options: &sqlserverflex.CreateInstancePayloadOptions{},
// },
// true,
// },
// }
// for _, tt := range tests {
// t.Run(tt.description, func(t *testing.T) {
// output, err := toCreatePayload(tt.input, tt.inputAcl, tt.inputFlavor, tt.inputStorage, tt.inputOptions)
// if !tt.isValid && err == nil {
// t.Fatalf("Should have failed")
// }
// if tt.isValid && err != nil {
// t.Fatalf("Should not have failed: %v", err)
// }
// if tt.isValid {
// diff := cmp.Diff(output, tt.expected)
// if diff != "" {
// t.Fatalf("Data does not match: %s", diff)
// }
// }
// })
// }
// }
//
// func TestToUpdatePayload(t *testing.T) {
// tests := []struct {
// description string
// input *Model
// inputAcl []string
// inputFlavor *flavorModel
// expected *sqlserverflex.PartialUpdateInstancePayload
// isValid bool
// }{
// {
// "default_values",
// &Model{},
// []string{},
// &flavorModel{},
// &sqlserverflex.PartialUpdateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{},
// },
// },
// true,
// },
// {
// "simple_values",
// &Model{
// BackupSchedule: types.StringValue("schedule"),
// Name: types.StringValue("name"),
// Replicas: types.Int64Value(12),
// Version: types.StringValue("version"),
// },
// []string{
// "ip_1",
// "ip_2",
// },
// &flavorModel{
// Id: types.StringValue("flavor_id"),
// },
// &sqlserverflex.PartialUpdateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{
// "ip_1",
// "ip_2",
// },
// },
// BackupSchedule: utils.Ptr("schedule"),
// FlavorId: utils.Ptr("flavor_id"),
// Name: utils.Ptr("name"),
// Version: utils.Ptr("version"),
// },
// true,
// },
// {
// "null_fields_and_int_conversions",
// &Model{
// BackupSchedule: types.StringNull(),
// Name: types.StringNull(),
// Replicas: types.Int64Value(2123456789),
// Version: types.StringNull(),
// },
// []string{
// "",
// },
// &flavorModel{
// Id: types.StringNull(),
// },
// &sqlserverflex.PartialUpdateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{
// Items: &[]string{
// "",
// },
// },
// BackupSchedule: nil,
// FlavorId: nil,
// Name: nil,
// Version: nil,
// },
// true,
// },
// {
// "nil_model",
// nil,
// []string{},
// &flavorModel{},
// nil,
// false,
// },
// {
// "nil_acl",
// &Model{},
// nil,
// &flavorModel{},
// &sqlserverflex.PartialUpdateInstancePayload{
// Acl: &sqlserverflex.CreateInstancePayloadAcl{},
// },
// true,
// },
// {
// "nil_flavor",
// &Model{},
// []string{},
// nil,
// nil,
// false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.description, func(t *testing.T) {
// output, err := toUpdatePayload(tt.input, tt.inputAcl, tt.inputFlavor)
// if !tt.isValid && err == nil {
// t.Fatalf("Should have failed")
// }
// if tt.isValid && err != nil {
// t.Fatalf("Should not have failed: %v", err)
// }
// if tt.isValid {
// diff := cmp.Diff(output, tt.expected)
// if diff != "" {
// t.Fatalf("Data does not match: %s", diff)
// }
// }
// })
// }
// }
//
// func TestLoadFlavorId(t *testing.T) {
// tests := []struct {
// description string
// inputFlavor *flavorModel
// mockedResp *sqlserverflex.ListFlavorsResponse
// expected *flavorModel
// getFlavorsFails bool
// isValid bool
// }{
// {
// "ok_flavor",
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// &sqlserverflex.ListFlavorsResponse{
// Flavors: &[]sqlserverflex.InstanceFlavorEntry{
// {
// Id: utils.Ptr("fid-1"),
// Cpu: utils.Ptr(int64(2)),
// Description: utils.Ptr("description"),
// Ram: utils.Ptr(int64(8)),
// },
// },
// },
// &flavorModel{
// Id: types.StringValue("fid-1"),
// Description: types.StringValue("description"),
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// false,
// true,
// },
// {
// "ok_flavor_2",
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// &sqlserverflex.ListFlavorsResponse{
// Flavors: &[]sqlserverflex.InstanceFlavorEntry{
// {
// Id: utils.Ptr("fid-1"),
// Cpu: utils.Ptr(int64(2)),
// Description: utils.Ptr("description"),
// Ram: utils.Ptr(int64(8)),
// },
// {
// Id: utils.Ptr("fid-2"),
// Cpu: utils.Ptr(int64(1)),
// Description: utils.Ptr("description"),
// Ram: utils.Ptr(int64(4)),
// },
// },
// },
// &flavorModel{
// Id: types.StringValue("fid-1"),
// Description: types.StringValue("description"),
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// false,
// true,
// },
// {
// "no_matching_flavor",
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// &sqlserverflex.ListFlavorsResponse{
// Flavors: &[]sqlserverflex.InstanceFlavorEntry{
// {
// Id: utils.Ptr("fid-1"),
// Cpu: utils.Ptr(int64(1)),
// Description: utils.Ptr("description"),
// Ram: utils.Ptr(int64(8)),
// },
// {
// Id: utils.Ptr("fid-2"),
// Cpu: utils.Ptr(int64(1)),
// Description: utils.Ptr("description"),
// Ram: utils.Ptr(int64(4)),
// },
// },
// },
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// false,
// false,
// },
// {
// "nil_response",
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// &sqlserverflex.ListFlavorsResponse{},
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// false,
// false,
// },
// {
// "error_response",
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// &sqlserverflex.ListFlavorsResponse{},
// &flavorModel{
// CPU: types.Int64Value(2),
// RAM: types.Int64Value(8),
// },
// true,
// false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.description, func(t *testing.T) {
// client := &sqlserverflexClientMocked{
// returnError: tt.getFlavorsFails,
// listFlavorsResp: tt.mockedResp,
// }
// model := &Model{
// ProjectId: types.StringValue("pid"),
// }
// flavorModel := &flavorModel{
// CPU: tt.inputFlavor.CPU,
// RAM: tt.inputFlavor.RAM,
// }
// err := loadFlavorId(context.Background(), client, model, flavorModel)
// if !tt.isValid && err == nil {
// t.Fatalf("Should have failed")
// }
// if tt.isValid && err != nil {
// t.Fatalf("Should not have failed: %v", err)
// }
// if tt.isValid {
// diff := cmp.Diff(flavorModel, tt.expected)
// if diff != "" {
// t.Fatalf("Data does not match: %s", diff)
// }
// }
// })
// }
// }

View file

@ -4,68 +4,22 @@ import (
"context" "context"
_ "embed" _ "embed"
"fmt" "fmt"
"log"
"os" "os"
"strconv" "strconv"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
sqlserverflexalphaPkgGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance" sqlserverflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
// The fwresource import alias is so there is no collision // The fwresource import alias is so there is no collision
// with the more typical acceptance testing import: // with the more typical acceptance testing import:
// "github.com/hashicorp/terraform-plugin-testing/helper/resource" // "github.com/hashicorp/terraform-plugin-testing/helper/resource"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource" fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
) )
const providerPrefix = "stackitprivatepreview_sqlserverflexalpha"
var testInstances []string
func init() {
sweeperName := fmt.Sprintf("%s_%s", providerPrefix, "sweeper")
resource.AddTestSweepers(sweeperName, &resource.Sweeper{
Name: sweeperName,
F: func(region string) error {
ctx := context.Background()
apiClientConfigOptions := []config.ConfigurationOption{}
apiClient, err := sqlserverflexalphaPkgGen.NewAPIClient(apiClientConfigOptions...)
if err != nil {
log.Fatalln(err)
}
instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, 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, region, inst.GetId())
if delErr != nil {
// TODO: maybe just warn?
log.Fatalln(delErr)
}
}
}
}
}
return nil
},
})
}
func TestInstanceResourceSchema(t *testing.T) { func TestInstanceResourceSchema(t *testing.T) {
t.Parallel() t.Parallel()
@ -131,15 +85,9 @@ type User struct {
} }
type Database struct { type Database struct {
Name string Name string
ProjectId string ProjectId string
Owner string Owner string
Collation string
Compatibility string
}
func resName(res, name string) string {
return fmt.Sprintf("%s_%s.%s", providerPrefix, res, name)
} }
func getExample() resData { func getExample() resData {
@ -164,6 +112,10 @@ func getExample() resData {
func TestAccInstance(t *testing.T) { func TestAccInstance(t *testing.T) {
exData := getExample() exData := getExample()
resName := fmt.Sprintf(
"stackitprivatepreview_sqlserverflexalpha_instance.%s",
exData.TfName,
)
updNameData := exData updNameData := exData
updNameData.Name = "name-updated" updNameData.Name = "name-updated"
@ -171,12 +123,8 @@ func TestAccInstance(t *testing.T) {
updSizeData := exData updSizeData := exData
updSizeData.Size = 25 updSizeData.Size = 25
resource.ParallelTest(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { PreCheck: func() { testAccPreCheck(t) },
testAccPreCheck(t)
t.Logf(" ... working on instance %s", exData.TfName)
testInstances = append(testInstances, exData.TfName)
},
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// Create and verify // Create and verify
@ -186,9 +134,8 @@ func TestAccInstance(t *testing.T) {
exData, exData,
), ),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name), resource.TestCheckResourceAttr(resName, "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"), resource.TestCheckResourceAttrSet(resName, "id"),
// TODO: check all fields
), ),
}, },
// Update name and verify // Update name and verify
@ -198,7 +145,7 @@ func TestAccInstance(t *testing.T) {
updNameData, updNameData,
), ),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", updNameData.Name), resource.TestCheckResourceAttr(resName, "name", updNameData.Name),
), ),
}, },
// Update size and verify // Update size and verify
@ -209,18 +156,15 @@ func TestAccInstance(t *testing.T) {
), ),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
testutils.ResStr(providerPrefix, "instance", exData.TfName), resName,
"storage.size", "storage.size",
strconv.Itoa(int(updSizeData.Size)), strconv.Itoa(int(updSizeData.Size)),
), ),
), ),
}, },
{
RefreshState: true,
},
//// Import test //// Import test
//{ //{
// ResourceName: resName("instance", exData.TfName), // ResourceName: "example_resource.test",
// ImportState: true, // ImportState: true,
// ImportStateVerify: true, // ImportStateVerify: true,
// }, // },
@ -228,23 +172,57 @@ func TestAccInstance(t *testing.T) {
}) })
} }
func TestAccInstanceNoEncryption(t *testing.T) { func TestAccInstanceWithUsers(t *testing.T) {
data := getExample() data := getExample()
userName := "testUser"
data.Users = []User{
{
Name: userName,
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
},
}
resName := fmt.Sprintf(
"stackitprivatepreview_sqlserverflexalpha_instance.%s",
data.TfName,
)
resUserName := fmt.Sprintf(
"stackitprivatepreview_sqlserverflexalpha_user.%s",
userName,
)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
data,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName, "name", data.Name),
resource.TestCheckResourceAttrSet(resName, "id"),
resource.TestCheckResourceAttr(resUserName, "name", userName),
resource.TestCheckResourceAttrSet(resUserName, "id"),
),
},
},
})
}
func TestAccInstanceWithDatabases(t *testing.T) {
data := getExample()
dbName := "testDb" dbName := "testDb"
userName := "testUser" userName := "testUser"
data.Users = []User{ data.Users = []User{
{ {
Name: userName, Name: userName,
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"), ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
Roles: []string{ Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
"##STACKIT_DatabaseManager##",
"##STACKIT_LoginManager##",
"##STACKIT_ProcessManager##",
"##STACKIT_SQLAgentManager##",
"##STACKIT_SQLAgentUser##",
"##STACKIT_ServerManager##",
},
}, },
} }
data.Databases = []Database{ data.Databases = []Database{
@ -255,12 +233,23 @@ func TestAccInstanceNoEncryption(t *testing.T) {
}, },
} }
resource.ParallelTest(t, resource.TestCase{ resName := fmt.Sprintf(
PreCheck: func() { "stackitprivatepreview_sqlserverflexalpha_instance.%s",
testAccPreCheck(t) data.TfName,
t.Logf(" ... working on instance %s", data.TfName) )
testInstances = append(testInstances, data.TfName)
}, resUserName := fmt.Sprintf(
"stackitprivatepreview_sqlserverflexalpha_user.%s",
userName,
)
resDbName := fmt.Sprintf(
"stackitprivatepreview_sqlserverflexalpha_database.%s",
dbName,
)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// Create and verify // Create and verify
@ -270,162 +259,13 @@ func TestAccInstanceNoEncryption(t *testing.T) {
data, data,
), ),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
// check instance values are set resource.TestCheckResourceAttr(resName, "name", data.Name),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "id"), resource.TestCheckResourceAttrSet(resName, "id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "backup_schedule"), resource.TestCheckResourceAttr(resUserName, "name", userName),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "edition"), resource.TestCheckResourceAttrSet(resUserName, "id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "flavor_id"), resource.TestCheckResourceAttr(resDbName, "name", dbName),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "instance_id"), resource.TestCheckResourceAttr(resDbName, "owner", userName),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "is_deletable"), resource.TestCheckResourceAttrSet(resDbName, "id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "name"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "replicas"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "retention_days"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "status"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "version"),
resource.TestCheckNoResourceAttr(resName("instance", data.TfName), "encryption"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.instance_address"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.router_address"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.class"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.size"),
// check instance values are correct
resource.TestCheckResourceAttr(resName("instance", data.TfName), "name", data.Name),
// check user values are set
resource.TestCheckResourceAttrSet(resName("user", userName), "id"),
resource.TestCheckResourceAttrSet(resName("user", userName), "username"),
// resource.TestCheckResourceAttrSet(resName("user", userName), "roles"),
// func(s *terraform.State) error {
// return nil
// },
// check user values are correct
resource.TestCheckResourceAttr(resName("user", userName), "username", userName),
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", strconv.Itoa(len(data.Users[0].Roles))),
// check database values are set
resource.TestCheckResourceAttrSet(resName("database", dbName), "id"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "name"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "owner"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "compatibility"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "collation"),
// check database values are correct
resource.TestCheckResourceAttr(resName("database", dbName), "name", dbName),
resource.TestCheckResourceAttr(resName("database", dbName), "owner", userName),
),
},
},
})
}
func TestAccInstanceEncryption(t *testing.T) {
data := getExample()
dbName := "testDb"
userName := "testUser"
data.Users = []User{
{
Name: userName,
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
Roles: []string{"##STACKIT_DatabaseManager##", "##STACKIT_LoginManager##"},
},
}
data.Databases = []Database{
{
Name: dbName,
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
Owner: userName,
},
}
data.UseEncryption = true
data.KekKeyId = "fe039bcf-8d7b-431a-801d-9e81371a6b7b"
data.KekKeyRingId = "6a2d95ab-3c4c-4963-a2bb-08d17a320e27"
data.KekKeyVersion = 1
data.KekServiceAccount = "henselinm-u2v3ex1@sa.stackit.cloud"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
t.Logf(" ... working on instance %s", data.TfName)
testInstances = append(testInstances, data.TfName)
},
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
data,
),
Check: resource.ComposeAggregateTestCheckFunc(
// check instance values are set
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "backup_schedule"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "edition"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "flavor_id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "instance_id"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "is_deletable"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "name"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "replicas"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "retention_days"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "status"),
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "version"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.instance_address"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.router_address"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.class"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.size"),
// check instance values are correct
resource.TestCheckResourceAttr(resName("instance", data.TfName), "name", data.Name),
// check user values are set
resource.TestCheckResourceAttrSet(resName("user", userName), "id"),
resource.TestCheckResourceAttrSet(resName("user", userName), "username"),
// func(s *terraform.State) error {
// return nil
// },
// check user values are correct
resource.TestCheckResourceAttr(resName("user", userName), "username", userName),
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", "2"),
// check database values are set
resource.TestCheckResourceAttrSet(resName("database", dbName), "id"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "name"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "owner"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "compatibility"),
resource.TestCheckResourceAttrSet(resName("database", dbName), "collation"),
// check database values are correct
resource.TestCheckResourceAttr(resName("database", dbName), "name", dbName),
resource.TestCheckResourceAttr(resName("database", dbName), "owner", userName),
), ),
}, },
}, },

View file

@ -0,0 +1,483 @@
// Copyright (c) STACKIT
package sqlserverflexalpha_test
import (
"context"
_ "embed"
"fmt"
"maps"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
coreconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/wait"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
)
var (
//go:embed testdata/resource-max.tf
resourceMaxConfig string
//go:embed testdata/resource-min.tf
resourceMinConfig string
)
var testConfigVarsMin = config.Variables{
"project_id": config.StringVariable(testutils.ProjectId),
"name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(7, acctest.CharSetAlphaNum))),
"flavor_cpu": config.IntegerVariable(4),
"flavor_ram": config.IntegerVariable(16),
"flavor_description": config.StringVariable("SQLServer-Flex-4.16-Standard-EU01"),
"replicas": config.IntegerVariable(1),
"flavor_id": config.StringVariable("4.16-Single"),
"username": config.StringVariable(fmt.Sprintf("tf-acc-user-%s", acctest.RandStringFromCharSet(7, acctest.CharSetAlpha))),
"role": config.StringVariable("##STACKIT_LoginManager##"),
}
var testConfigVarsMax = config.Variables{
"project_id": config.StringVariable(testutils.ProjectId),
"name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(7, acctest.CharSetAlphaNum))),
"acl1": config.StringVariable("192.168.0.0/16"),
"flavor_cpu": config.IntegerVariable(4),
"flavor_ram": config.IntegerVariable(16),
"flavor_description": config.StringVariable("SQLServer-Flex-4.16-Standard-EU01"),
"storage_class": config.StringVariable("premium-perf2-stackit"),
"storage_size": config.IntegerVariable(40),
"server_version": config.StringVariable("2022"),
"replicas": config.IntegerVariable(1),
"options_retention_days": config.IntegerVariable(64),
"flavor_id": config.StringVariable("4.16-Single"),
"backup_schedule": config.StringVariable("00 6 * * *"),
"username": config.StringVariable(fmt.Sprintf("tf-acc-user-%s", acctest.RandStringFromCharSet(7, acctest.CharSetAlpha))),
"role": config.StringVariable("##STACKIT_LoginManager##"),
"region": config.StringVariable(testutils.Region),
}
func configVarsMinUpdated() config.Variables {
temp := maps.Clone(testConfigVarsMax)
temp["name"] = config.StringVariable(testutils.ConvertConfigVariable(temp["name"]) + "changed")
return temp
}
func configVarsMaxUpdated() config.Variables {
temp := maps.Clone(testConfigVarsMax)
temp["backup_schedule"] = config.StringVariable("00 12 * * *")
return temp
}
func TestAccSQLServerFlexMinResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccChecksqlserverflexDestroy,
Steps: []resource.TestStep{
// Creation
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMinConfig,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMin["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_description"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "replicas", testutils.ConvertConfigVariable(testConfigVarsMin["replicas"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_ram"])),
// User
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "password"),
),
},
// Update
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMinConfig,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMin["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_description"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_ram"])),
// User
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "password"),
),
},
// data source
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMinConfig,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMin["name"])),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_instance.instance", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_instance.instance", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_user.user", "instance_id",
),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.id", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_id"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_description"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_cpu"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMin["flavor_ram"])),
// User data
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "project_id", testutils.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "username", testutils.ConvertConfigVariable(testConfigVarsMin["username"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "roles.#", "1"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "roles.0", testutils.ConvertConfigVariable(testConfigVarsMax["role"])),
),
},
// Import
{
ConfigVariables: testConfigVarsMin,
ResourceName: "stackit_sqlserverflex_instance.instance",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_sqlserverflex_instance.instance"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_sqlserverflex_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", testutils.ProjectId, testutils.Region, instanceId), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backup_schedule"},
ImportStateCheck: func(s []*terraform.InstanceState) error {
if len(s) != 1 {
return fmt.Errorf("expected 1 state, got %d", len(s))
}
return nil
},
},
{
ResourceName: "stackit_sqlserverflex_user.user",
ConfigVariables: testConfigVarsMin,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_sqlserverflex_user.user"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_sqlserverflex_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", testutils.ProjectId, testutils.Region, instanceId, userId), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
// Update
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMinConfig,
ConfigVariables: configVarsMinUpdated(),
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(configVarsMinUpdated()["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(configVarsMinUpdated()["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.description"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(configVarsMinUpdated()["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(configVarsMinUpdated()["flavor_ram"])),
),
},
// Deletion is done by the framework implicitly
},
})
}
func TestAccSQLServerFlexMaxResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccChecksqlserverflexDestroy,
Steps: []resource.TestStep{
// Creation
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMaxConfig,
ConfigVariables: testConfigVarsMax,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMax["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.0", testutils.ConvertConfigVariable(testConfigVarsMax["acl1"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_description"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "replicas", testutils.ConvertConfigVariable(testConfigVarsMax["replicas"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_ram"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.class", testutils.ConvertConfigVariable(testConfigVarsMax["storage_class"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.size", testutils.ConvertConfigVariable(testConfigVarsMax["storage_size"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", testutils.ConvertConfigVariable(testConfigVarsMax["server_version"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", testutils.ConvertConfigVariable(testConfigVarsMax["options_retention_days"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", testutils.ConvertConfigVariable(testConfigVarsMax["backup_schedule"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "region", testutils.Region),
// User
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "password"),
),
},
// Update
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMaxConfig,
ConfigVariables: testConfigVarsMax,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMax["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.0", testutils.ConvertConfigVariable(testConfigVarsMax["acl1"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_description"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "replicas", testutils.ConvertConfigVariable(testConfigVarsMax["replicas"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_ram"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.class", testutils.ConvertConfigVariable(testConfigVarsMax["storage_class"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.size", testutils.ConvertConfigVariable(testConfigVarsMax["storage_size"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", testutils.ConvertConfigVariable(testConfigVarsMax["server_version"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", testutils.ConvertConfigVariable(testConfigVarsMax["options_retention_days"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", testutils.ConvertConfigVariable(testConfigVarsMax["backup_schedule"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "region", testutils.Region),
// User
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "password"),
),
},
// data source
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMaxConfig,
ConfigVariables: testConfigVarsMax,
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(testConfigVarsMax["name"])),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_instance.instance", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_instance.instance", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrPair(
"data.stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_user.user", "instance_id",
),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "acl.0", testutils.ConvertConfigVariable(testConfigVarsMax["acl1"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.id", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_id"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.description", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_description"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_cpu"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(testConfigVarsMax["flavor_ram"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "replicas", testutils.ConvertConfigVariable(testConfigVarsMax["replicas"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", testutils.ConvertConfigVariable(testConfigVarsMax["options_retention_days"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_instance.instance", "backup_schedule", testutils.ConvertConfigVariable(testConfigVarsMax["backup_schedule"])),
// User data
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "project_id", testutils.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "username", testutils.ConvertConfigVariable(testConfigVarsMax["username"])),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "roles.#", "1"),
resource.TestCheckResourceAttr("data.stackit_sqlserverflex_user.user", "roles.0", testutils.ConvertConfigVariable(testConfigVarsMax["role"])),
resource.TestCheckResourceAttrSet("data.stackit_sqlserverflex_user.user", "host"),
resource.TestCheckResourceAttrSet("data.stackit_sqlserverflex_user.user", "port"),
),
},
// Import
{
ConfigVariables: testConfigVarsMax,
ResourceName: "stackit_sqlserverflex_instance.instance",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_sqlserverflex_instance.instance"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_sqlserverflex_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", testutils.ProjectId, testutils.Region, instanceId), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backup_schedule"},
ImportStateCheck: func(s []*terraform.InstanceState) error {
if len(s) != 1 {
return fmt.Errorf("expected 1 state, got %d", len(s))
}
if s[0].Attributes["backup_schedule"] != testutils.ConvertConfigVariable(testConfigVarsMax["backup_schedule"]) {
return fmt.Errorf("expected backup_schedule %s, got %s", testConfigVarsMax["backup_schedule"], s[0].Attributes["backup_schedule"])
}
return nil
},
},
{
ResourceName: "stackit_sqlserverflex_user.user",
ConfigVariables: testConfigVarsMax,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_sqlserverflex_user.user"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_sqlserverflex_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", testutils.ProjectId, testutils.Region, instanceId, userId), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
// Update
{
Config: testutils.SQLServerFlexProviderConfig("") + "\n" + resourceMaxConfig,
ConfigVariables: configVarsMaxUpdated(),
Check: resource.ComposeAggregateTestCheckFunc(
// Instance data
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", testutils.ConvertConfigVariable(configVarsMaxUpdated()["project_id"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", testutils.ConvertConfigVariable(configVarsMaxUpdated()["name"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.0", testutils.ConvertConfigVariable(configVarsMaxUpdated()["acl1"])),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.description"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", testutils.ConvertConfigVariable(configVarsMaxUpdated()["flavor_cpu"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", testutils.ConvertConfigVariable(configVarsMaxUpdated()["flavor_ram"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "replicas", testutils.ConvertConfigVariable(configVarsMaxUpdated()["replicas"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.class", testutils.ConvertConfigVariable(configVarsMaxUpdated()["storage_class"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.size", testutils.ConvertConfigVariable(configVarsMaxUpdated()["storage_size"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", testutils.ConvertConfigVariable(configVarsMaxUpdated()["server_version"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", testutils.ConvertConfigVariable(configVarsMaxUpdated()["options_retention_days"])),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", testutils.ConvertConfigVariable(configVarsMaxUpdated()["backup_schedule"])),
),
},
// Deletion is done by the framework implicitly
},
})
}
func testAccChecksqlserverflexDestroy(s *terraform.State) error {
ctx := context.Background()
var client *sqlserverflex.APIClient
var err error
if testutils.SQLServerFlexCustomEndpoint == "" {
client, err = sqlserverflex.NewAPIClient()
} else {
client, err = sqlserverflex.NewAPIClient(
coreconfig.WithEndpoint(testutils.SQLServerFlexCustomEndpoint),
)
}
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
instancesToDestroy := []string{}
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_sqlserverflex_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.ListInstances(ctx, testutils.ProjectId, testutils.Region).Execute()
if err != nil {
return fmt.Errorf("getting instancesResp: %w", err)
}
items := *instancesResp.Items
for i := range items {
if items[i].Id == nil {
continue
}
if utils.Contains(instancesToDestroy, *items[i].Id) {
err := client.DeleteInstanceExecute(ctx, testutils.ProjectId, *items[i].Id, testutils.Region)
if err != nil {
return fmt.Errorf("destroying instance %s during CheckDestroy: %w", *items[i].Id, err)
}
_, err = wait.DeleteInstanceWaitHandler(ctx, client, testutils.ProjectId, *items[i].Id, testutils.Region).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("destroying instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err)
}
}
}
return nil
}

View file

@ -15,8 +15,8 @@ resource "stackitprivatepreview_sqlserverflexalpha_instance" "{{ .TfName }}" {
} }
{{ if .UseEncryption }} {{ if .UseEncryption }}
encryption = { encryption = {
kek_key_id = "{{ .KekKeyId }}" kek_key_id = {{ .KekKeyId }}
kek_key_ring_id = "{{ .KekKeyRingId }}" kek_key_ring_id = {{ .KekKeyRingId }}
kek_key_version = {{ .KekKeyVersion }} kek_key_version = {{ .KekKeyVersion }}
service_account = "{{ .KekServiceAccount }}" service_account = "{{ .KekServiceAccount }}"
} }
@ -44,17 +44,10 @@ resource "stackitprivatepreview_sqlserverflexalpha_user" "{{ $user.Name }}" {
{{ $tfName := .TfName }} {{ $tfName := .TfName }}
{{ range $db := .Databases }} {{ range $db := .Databases }}
resource "stackitprivatepreview_sqlserverflexalpha_database" "{{ $db.Name }}" { resource "stackitprivatepreview_sqlserverflexalpha_database" "{{ $db.Name }}" {
depends_on = [stackitprivatepreview_sqlserverflexalpha_user.{{ $db.Owner }}] project_id = "{{ $db.ProjectId }}"
project_id = "{{ $db.ProjectId }}" instance_id = stackitprivatepreview_sqlserverflexalpha_instance.{{ $tfName }}.instance_id
instance_id = stackitprivatepreview_sqlserverflexalpha_instance.{{ $tfName }}.instance_id name = "{{ $db.Name }}"
name = "{{ $db.Name }}" owner = "{{ $db.Owner }}"
owner = "{{ $db.Owner }}"
{{ if $db.Collation }}
collation = "{{ $db.Collation }}"
{{ end }}
{{ if $db.Compatibility }}
compatibility = "{{ $db.Compatibility }}"
{{ end }}
} }
{{ end }} {{ end }}
{{ end }} {{ end }}

View file

@ -5,45 +5,54 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/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/core"
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils" sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/validate"
sqlserverflexalphaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/user/datasources_gen"
) )
var _ datasource.DataSource = (*userDataSource)(nil) // Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &userDataSource{}
)
// NewUserDataSource is a helper function to simplify the provider implementation.
func NewUserDataSource() datasource.DataSource { func NewUserDataSource() datasource.DataSource {
return &userDataSource{} return &userDataSource{}
} }
type dataSourceModel struct { type dataSourceModel struct {
DefaultDatabase types.String `tfsdk:"default_database"` //TODO: check generated data source for the correct types and pointers
Host types.String `tfsdk:"host"` Id types.String `tfsdk:"id"` // needed by TF
Id types.String `tfsdk:"id"`
InstanceId types.String `tfsdk:"instance_id"`
Port types.Int64 `tfsdk:"port"`
ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"`
Roles types.List `tfsdk:"roles"`
Status types.String `tfsdk:"status"`
UserId types.Int64 `tfsdk:"user_id"` UserId types.Int64 `tfsdk:"user_id"`
InstanceId types.String `tfsdk:"instance_id"`
ProjectId types.String `tfsdk:"project_id"`
Username types.String `tfsdk:"username"` Username types.String `tfsdk:"username"`
Roles types.Set `tfsdk:"roles"`
Host types.String `tfsdk:"host"`
Port types.Int64 `tfsdk:"port"`
Region types.String `tfsdk:"region"`
Status types.String `tfsdk:"status"`
DefaultDatabase types.String `tfsdk:"default_database"`
} }
// userDataSource is the data source implementation.
type userDataSource struct { type userDataSource struct {
client *sqlserverflexalphaPkg.APIClient client *sqlserverflexalpha.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
func (d *userDataSource) Metadata( // Metadata returns the data source type name.
func (r *userDataSource) Metadata(
_ context.Context, _ context.Context,
req datasource.MetadataRequest, req datasource.MetadataRequest,
resp *datasource.MetadataResponse, resp *datasource.MetadataResponse,
@ -51,31 +60,108 @@ func (d *userDataSource) Metadata(
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_user" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_user"
} }
func (d *userDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = sqlserverflexalphaGen.UserDataSourceSchema(ctx)
}
// Configure adds the provider configured client to the data source. // Configure adds the provider configured client to the data source.
func (d *userDataSource) Configure( func (r *userDataSource) Configure(
ctx context.Context, ctx context.Context,
req datasource.ConfigureRequest, req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse, resp *datasource.ConfigureResponse,
) { ) {
var ok bool var ok bool
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok { if !ok {
return return
} }
apiClient := sqlserverflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) apiClient := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
d.client = apiClient r.client = apiClient
tflog.Info(ctx, "SQL SERVER Flex alpha database client configured") tflog.Info(ctx, "SQLServer Flex beta user client configured")
} }
func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // Schema defines the schema for the data source.
func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{
"main": "SQLServer Flex user data source schema. Must have a `region` specified in the provider configuration.",
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".",
"user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.",
"username": "Username of the SQLServer Flex instance.",
"roles": "Database access levels for the user.",
"password": "Password of the user account.",
"region": "The resource region. If not defined, the provider region is used.",
"status": "Status of the user.",
"default_database": "Default database of the user.",
}
resp.Schema = schema.Schema{
Description: descriptions["main"],
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: descriptions["id"],
Computed: true,
},
"user_id": schema.Int64Attribute{
Description: descriptions["user_id"],
Required: true,
Validators: []validator.Int64{
int64validator.AtLeast(1),
},
},
"instance_id": schema.StringAttribute{
Description: descriptions["instance_id"],
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"project_id": schema.StringAttribute{
Description: descriptions["project_id"],
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"username": schema.StringAttribute{
Description: descriptions["username"],
Computed: true,
},
"roles": schema.SetAttribute{
Description: descriptions["roles"],
ElementType: types.StringType,
Computed: true,
},
"host": schema.StringAttribute{
Computed: true,
},
"port": schema.Int64Attribute{
Computed: true,
},
"region": schema.StringAttribute{
// the region cannot be found automatically, so it has to be passed
Optional: true,
Description: descriptions["region"],
},
"status": schema.StringAttribute{
Computed: true,
},
"default_database": schema.StringAttribute{
Computed: true,
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (r *userDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform
var model dataSourceModel var model dataSourceModel
diags := req.Config.Get(ctx, &model) diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
@ -88,13 +174,13 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
userId := model.UserId.ValueInt64() userId := model.UserId.ValueInt64()
region := d.providerData.GetRegionWithOverride(model.Region) region := r.providerData.GetRegionWithOverride(model.Region)
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "user_id", userId) ctx = tflog.SetField(ctx, "user_id", userId)
ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "region", region)
recordSetResp, err := d.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute() recordSetResp, err := r.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
if err != nil { if err != nil {
utils.LogError( utils.LogError(
ctx, ctx,
@ -135,5 +221,5 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
tflog.Info(ctx, "SQLServer Flex beta instance read") tflog.Info(ctx, "SQLServer Flex alpha instance read")
} }

View file

@ -2,7 +2,6 @@ package sqlserverflexalpha
import ( import (
"fmt" "fmt"
"slices"
"strconv" "strconv"
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
@ -43,20 +42,17 @@ func mapDataSourceFields(userResp *sqlserverflexalpha.GetUserResponse, model *da
// Map roles // Map roles
if user.Roles == nil { if user.Roles == nil {
model.Roles = types.List(types.SetNull(types.StringType)) model.Roles = types.SetNull(types.StringType)
} else { } else {
resRoles := *user.Roles
slices.Sort(resRoles)
var roles []attr.Value var roles []attr.Value
for _, role := range resRoles { for _, role := range *user.Roles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesSet, diags := types.SetValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
if diags.HasError() { if diags.HasError() {
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags)) return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
} }
model.Roles = types.List(rolesSet) model.Roles = rolesSet
} }
// Set remaining attributes // Set remaining attributes
@ -96,11 +92,8 @@ func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *resourceMode
// Map roles // Map roles
if user.Roles != nil { if user.Roles != nil {
resRoles := *user.Roles
slices.Sort(resRoles)
var roles []attr.Value var roles []attr.Value
for _, role := range resRoles { for _, role := range *user.Roles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesSet, diags := types.SetValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
@ -146,12 +139,9 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *res
model.Password = types.StringValue(*user.Password) model.Password = types.StringValue(*user.Password)
if user.Roles != nil { if user.Roles != nil {
resRoles := *user.Roles
slices.Sort(resRoles)
var roles []attr.Value var roles []attr.Value
for _, role := range resRoles { for _, role := range *user.Roles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(role))
} }
rolesSet, diags := types.SetValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
if diags.HasError() { if diags.HasError() {
@ -164,9 +154,6 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *res
model.Roles = types.List(types.SetNull(types.StringType)) model.Roles = types.List(types.SetNull(types.StringType))
} }
model.Password = types.StringPointerValue(user.Password)
model.Uri = types.StringPointerValue(user.Uri)
model.Host = types.StringPointerValue(user.Host) model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port) model.Port = types.Int64PointerValue(user.Port)
model.Region = types.StringValue(region) model.Region = types.StringValue(region)

View file

@ -30,7 +30,7 @@ func TestMapDataSourceFields(t *testing.T) {
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Username: types.StringNull(), Username: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)), Roles: types.SetNull(types.StringType),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
@ -43,12 +43,9 @@ func TestMapDataSourceFields(t *testing.T) {
"simple_values", "simple_values",
&sqlserverflexalpha.GetUserResponse{ &sqlserverflexalpha.GetUserResponse{
Roles: &[]string{ Roles: &[]string{
"##STACKIT_SQLAgentUser##", "role_1",
"##STACKIT_DatabaseManager##", "role_2",
"##STACKIT_LoginManager##", "",
"##STACKIT_SQLAgentManager##",
"##STACKIT_ProcessManager##",
"##STACKIT_ServerManager##",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
Host: utils.Ptr("host"), Host: utils.Ptr("host"),
@ -63,17 +60,12 @@ func TestMapDataSourceFields(t *testing.T) {
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Username: types.StringValue("username"), Username: types.StringValue("username"),
Roles: types.List( Roles: types.SetValueMust(
types.SetValueMust( types.StringType, []attr.Value{
types.StringType, []attr.Value{ types.StringValue("role_1"),
types.StringValue("##STACKIT_DatabaseManager##"), types.StringValue("role_2"),
types.StringValue("##STACKIT_LoginManager##"), types.StringValue(""),
types.StringValue("##STACKIT_ProcessManager##"), },
types.StringValue("##STACKIT_SQLAgentManager##"),
types.StringValue("##STACKIT_SQLAgentUser##"),
types.StringValue("##STACKIT_ServerManager##"),
},
),
), ),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Port: types.Int64Value(1234), Port: types.Int64Value(1234),
@ -99,7 +91,7 @@ func TestMapDataSourceFields(t *testing.T) {
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Username: types.StringNull(), Username: types.StringNull(),
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})), Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion), Region: types.StringValue(testRegion),
@ -144,7 +136,7 @@ func TestMapDataSourceFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -189,8 +181,8 @@ func TestMapFieldsCreate(t *testing.T) {
&sqlserverflexalpha.CreateUserResponse{ &sqlserverflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(2)), Id: utils.Ptr(int64(2)),
Roles: &[]string{ Roles: &[]string{
"role_2",
"role_1", "role_1",
"role_2",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -210,9 +202,9 @@ func TestMapFieldsCreate(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -298,7 +290,7 @@ func TestMapFieldsCreate(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -338,8 +330,8 @@ func TestMapFields(t *testing.T) {
"simple_values", "simple_values",
&sqlserverflexalpha.GetUserResponse{ &sqlserverflexalpha.GetUserResponse{
Roles: &[]string{ Roles: &[]string{
"role_2",
"role_1", "role_1",
"role_2",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -356,9 +348,9 @@ func TestMapFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -429,7 +421,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -522,7 +514,7 @@ func TestToCreatePayload(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(tt.expected, output) diff := cmp.Diff(output, tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }

View file

@ -3,12 +3,15 @@ fields:
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'user_id'
modifiers:
- 'UseStateForUnknown'
- name: 'instance_id' - name: 'instance_id'
validators: validators:
- validate.NoSeparator - validate.NoSeparator
- validate.UUID - validate.UUID
modifiers: modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace' - 'RequiresReplace'
- name: 'project_id' - name: 'project_id'
@ -16,27 +19,33 @@ fields:
- validate.NoSeparator - validate.NoSeparator
- validate.UUID - validate.UUID
modifiers: modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace' - 'RequiresReplace'
- name: 'username'
modifiers:
- 'RequiresReplace'
- name: 'roles'
modifiers:
- 'RequiresReplace'
- name: 'password'
modifiers:
- 'UseStateForUnknown'
- name: 'host'
modifiers:
- 'UseStateForUnknown'
- name: 'port'
modifiers:
- 'UseStateForUnknown'
- name: 'region' - name: 'region'
modifiers: modifiers:
- 'RequiresReplace' - 'RequiresReplace'
- name: 'user_id' - name: 'status'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'username'
modifiers:
- 'UseStateForUnknown'
- name: 'roles'
modifiers:
- 'UseStateForUnknown'
- name: 'password'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
@ -44,6 +53,6 @@ fields:
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'status' - name: 'default_database'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'

View file

@ -6,44 +6,43 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"slices"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
sqlserverflexalphagen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/user/resources_gen"
sqlserverflexalphaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
sqlserverflexalphaWait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
"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/conversion"
sqlserverflexalphaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
sqlserverflexalphaWait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
"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/core"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
sqlserverflexalphaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/user/resources_gen"
) )
// Ensure the implementation satisfies the expected interfaces.
var ( var (
_ resource.Resource = &userResource{} _ resource.Resource = &userResource{}
_ resource.ResourceWithConfigure = &userResource{} _ resource.ResourceWithConfigure = &userResource{}
_ resource.ResourceWithImportState = &userResource{} _ resource.ResourceWithImportState = &userResource{}
_ resource.ResourceWithModifyPlan = &userResource{} _ resource.ResourceWithModifyPlan = &userResource{}
_ resource.ResourceWithIdentity = &userResource{} _ resource.ResourceWithIdentity = &userResource{}
_ resource.ResourceWithValidateConfig = &userResource{}
) )
// NewUserResource is a helper function to simplify the provider implementation.
func NewUserResource() resource.Resource { func NewUserResource() resource.Resource {
return &userResource{} return &userResource{}
} }
// resourceModel describes the resource data model. // resourceModel describes the resource data model.
type resourceModel = sqlserverflexalphaResGen.UserModel type resourceModel = sqlserverflexalphagen.UserModel
// UserResourceIdentityModel describes the resource's identity attributes. // UserResourceIdentityModel describes the resource's identity attributes.
type UserResourceIdentityModel struct { type UserResourceIdentityModel struct {
@ -53,11 +52,13 @@ type UserResourceIdentityModel struct {
UserID types.Int64 `tfsdk:"user_id"` UserID types.Int64 `tfsdk:"user_id"`
} }
// userResource is the resource implementation.
type userResource struct { type userResource struct {
client *sqlserverflexalpha.APIClient client *sqlserverflexalpha.APIClient
providerData core.ProviderData providerData core.ProviderData
} }
// Metadata returns the resource type name.
func (r *userResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { func (r *userResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_user" resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_user"
} }
@ -75,7 +76,7 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
return return
} }
r.client = apiClient r.client = apiClient
tflog.Info(ctx, "SQLServer Beta Flex user client configured") tflog.Info(ctx, "SQLServer Alpha Flex user client configured")
} }
// ModifyPlan implements resource.ResourceWithModifyPlan. // ModifyPlan implements resource.ResourceWithModifyPlan.
@ -117,7 +118,7 @@ var modifiersFileByte []byte
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *userResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *userResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
s := sqlserverflexalphaResGen.UserResourceSchema(ctx) s := sqlserverflexalphagen.UserResourceSchema(ctx)
fields, err := utils.ReadModifiersConfig(modifiersFileByte) fields, err := utils.ReadModifiersConfig(modifiersFileByte)
if err != nil { if err != nil {
@ -157,39 +158,6 @@ func (r *userResource) IdentitySchema(
} }
} }
func (r *userResource) ValidateConfig(
ctx context.Context,
req resource.ValidateConfigRequest,
resp *resource.ValidateConfigResponse,
) {
var data resourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var roles []string
diags := data.Roles.ElementsAs(ctx, &roles, false)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
var resRoles []string
for _, role := range roles {
if slices.Contains(resRoles, role) {
resp.Diagnostics.AddAttributeError(
path.Root("roles"),
"Attribute Configuration Error",
"defined roles MUST NOT contain duplicates",
)
return
}
resRoles = append(resRoles, role)
}
}
// Create creates the resource and sets the initial Terraform state. // Create creates the resource and sets the initial Terraform state.
func (r *userResource) Create( func (r *userResource) Create(
ctx context.Context, ctx context.Context,
@ -220,8 +188,6 @@ func (r *userResource) Create(
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
slices.Sort(roles)
} }
// Generate API request body from model // Generate API request body from model
@ -253,7 +219,6 @@ func (r *userResource) Create(
) )
return return
} }
userId := *userResp.Id userId := *userResp.Id
ctx = tflog.SetField(ctx, "user_id", userId) ctx = tflog.SetField(ctx, "user_id", userId)
@ -262,59 +227,15 @@ func (r *userResource) Create(
ProjectID: types.StringValue(projectId), ProjectID: types.StringValue(projectId),
Region: types.StringValue(region), Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId), InstanceID: types.StringValue(instanceId),
UserID: types.Int64Value(userId), UserID: types.Int64Value(userResp.GetId()),
} }
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...) resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
err = mapFieldsCreate(userResp, &model, region)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating user",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
waitResp, err := sqlserverflexalphaWait.CreateUserWaitHandler(
ctx,
r.client,
projectId,
instanceId,
region,
userId,
).SetSleepBeforeWait(
90 * time.Second,
).SetTimeout(
90 * time.Minute,
).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"create user",
fmt.Sprintf("Instance creation waiting: %v", err),
)
return
}
if waitResp.Id == nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"create user",
"Instance creation waiting: returned id is nil",
)
return
}
// Map response body to schema // Map response body to schema
err = mapFields(waitResp, &model, region) err = mapFieldsCreate(userResp, &model, region)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -375,18 +296,6 @@ func (r *userResource) Read(
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
// Map response body to schema
err = mapFields(recordSetResp, &model, region)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error reading user",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Set data returned by API in identity // Set data returned by API in identity
identity := UserResourceIdentityModel{ identity := UserResourceIdentityModel{
ProjectID: types.StringValue(projectId), ProjectID: types.StringValue(projectId),
@ -399,6 +308,18 @@ func (r *userResource) Read(
return return
} }
// Map response body to schema
err = mapFields(recordSetResp, &model, region)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error reading user",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Set refreshed state // Set refreshed state
diags = resp.State.Set(ctx, model) diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
@ -445,37 +366,17 @@ func (r *userResource) Delete(
// Delete existing record set // Delete existing record set
// err := r.client.DeleteUserRequest(ctx, projectId, region, instanceId, userId).Execute() // err := r.client.DeleteUserRequest(ctx, projectId, region, instanceId, userId).Execute()
err := r.client.DeleteUserRequestExecute(ctx, projectId, region, instanceId, userId)
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if !ok {
// TODO err handling
return
}
switch oapiErr.StatusCode {
case http.StatusNotFound:
resp.State.RemoveResource(ctx)
return
// case http.StatusInternalServerError:
// tflog.Warn(ctx, "[delete user] Wait handler got error 500")
// return false, nil, nil
default:
// TODO err handling
return
}
}
// Delete existing record set // Delete existing record set
_, err = sqlserverflexalphaWait.DeleteUserWaitHandler(ctx, r.client, projectId, region, instanceId, userId). _, err := sqlserverflexalphaWait.DeleteUserWaitHandler(ctx, r.client, projectId, region, instanceId, userId).
WaitWithContext(ctx) WaitWithContext(ctx)
// err := r.client.DeleteUserRequest(ctx, arg.projectId, arg.region, arg.instanceId, userId).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "User Delete Error", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "User Delete Error", fmt.Sprintf("Calling API: %v", err))
return return
} }
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
resp.State.RemoveResource(ctx) resp.State.RemoveResource(ctx)
tflog.Info(ctx, "SQLServer Flex user deleted") tflog.Info(ctx, "SQLServer Flex user deleted")
@ -521,7 +422,7 @@ func (r *userResource) ImportState(
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), userId)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), userId)...)
tflog.Info(ctx, "SQLServer Flex user state imported") tflog.Info(ctx, "Postgres Flex user state imported")
return return
} }

View file

@ -1,569 +0,0 @@
// Code generated by terraform-plugin-framework-generator DO NOT EDIT.
package sqlserverflexalpha
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"strings"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)
func VersionDataSourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Required: true,
Description: "The STACKIT project ID.",
MarkdownDescription: "The STACKIT project ID.",
},
"region": schema.StringAttribute{
Required: true,
Description: "The region which should be addressed",
MarkdownDescription: "The region which should be addressed",
Validators: []validator.String{
stringvalidator.OneOf(
"eu01",
),
},
},
"versions": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"beta": schema.BoolAttribute{
Computed: true,
Description: "Flag if the version is a beta version. If set the version may contain bugs and is not fully tested.",
MarkdownDescription: "Flag if the version is a beta version. If set the version may contain bugs and is not fully tested.",
},
"deprecated": schema.StringAttribute{
Computed: true,
Description: "Timestamp in RFC3339 format which says when the version will no longer be supported by STACKIT.",
MarkdownDescription: "Timestamp in RFC3339 format which says when the version will no longer be supported by STACKIT.",
},
"recommend": schema.BoolAttribute{
Computed: true,
Description: "Flag if the version is recommend by the STACKIT Team.",
MarkdownDescription: "Flag if the version is recommend by the STACKIT Team.",
},
"version": schema.StringAttribute{
Computed: true,
Description: "The sqlserver version used for the instance.",
MarkdownDescription: "The sqlserver version used for the instance.",
},
},
CustomType: VersionsType{
ObjectType: types.ObjectType{
AttrTypes: VersionsValue{}.AttributeTypes(ctx),
},
},
},
Computed: true,
Description: "A list containing available sqlserver versions.",
MarkdownDescription: "A list containing available sqlserver versions.",
},
},
}
}
type VersionModel struct {
ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"`
Versions types.List `tfsdk:"versions"`
}
var _ basetypes.ObjectTypable = VersionsType{}
type VersionsType struct {
basetypes.ObjectType
}
func (t VersionsType) Equal(o attr.Type) bool {
other, ok := o.(VersionsType)
if !ok {
return false
}
return t.ObjectType.Equal(other.ObjectType)
}
func (t VersionsType) String() string {
return "VersionsType"
}
func (t VersionsType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
var diags diag.Diagnostics
attributes := in.Attributes()
betaAttribute, ok := attributes["beta"]
if !ok {
diags.AddError(
"Attribute Missing",
`beta is missing from object`)
return nil, diags
}
betaVal, ok := betaAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`beta expected to be basetypes.BoolValue, was: %T`, betaAttribute))
}
deprecatedAttribute, ok := attributes["deprecated"]
if !ok {
diags.AddError(
"Attribute Missing",
`deprecated is missing from object`)
return nil, diags
}
deprecatedVal, ok := deprecatedAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`deprecated expected to be basetypes.StringValue, was: %T`, deprecatedAttribute))
}
recommendAttribute, ok := attributes["recommend"]
if !ok {
diags.AddError(
"Attribute Missing",
`recommend is missing from object`)
return nil, diags
}
recommendVal, ok := recommendAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`recommend expected to be basetypes.BoolValue, was: %T`, recommendAttribute))
}
versionAttribute, ok := attributes["version"]
if !ok {
diags.AddError(
"Attribute Missing",
`version is missing from object`)
return nil, diags
}
versionVal, ok := versionAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`version expected to be basetypes.StringValue, was: %T`, versionAttribute))
}
if diags.HasError() {
return nil, diags
}
return VersionsValue{
Beta: betaVal,
Deprecated: deprecatedVal,
Recommend: recommendVal,
Version: versionVal,
state: attr.ValueStateKnown,
}, diags
}
func NewVersionsValueNull() VersionsValue {
return VersionsValue{
state: attr.ValueStateNull,
}
}
func NewVersionsValueUnknown() VersionsValue {
return VersionsValue{
state: attr.ValueStateUnknown,
}
}
func NewVersionsValue(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) (VersionsValue, diag.Diagnostics) {
var diags diag.Diagnostics
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/521
ctx := context.Background()
for name, attributeType := range attributeTypes {
attribute, ok := attributes[name]
if !ok {
diags.AddError(
"Missing VersionsValue Attribute Value",
"While creating a VersionsValue value, a missing attribute value was detected. "+
"A VersionsValue must contain values for all attributes, even if null or unknown. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("VersionsValue Attribute Name (%s) Expected Type: %s", name, attributeType.String()),
)
continue
}
if !attributeType.Equal(attribute.Type(ctx)) {
diags.AddError(
"Invalid VersionsValue Attribute Type",
"While creating a VersionsValue value, an invalid attribute value was detected. "+
"A VersionsValue must use a matching attribute type for the value. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("VersionsValue Attribute Name (%s) Expected Type: %s\n", name, attributeType.String())+
fmt.Sprintf("VersionsValue Attribute Name (%s) Given Type: %s", name, attribute.Type(ctx)),
)
}
}
for name := range attributes {
_, ok := attributeTypes[name]
if !ok {
diags.AddError(
"Extra VersionsValue Attribute Value",
"While creating a VersionsValue value, an extra attribute value was detected. "+
"A VersionsValue must not contain values beyond the expected attribute types. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("Extra VersionsValue Attribute Name: %s", name),
)
}
}
if diags.HasError() {
return NewVersionsValueUnknown(), diags
}
betaAttribute, ok := attributes["beta"]
if !ok {
diags.AddError(
"Attribute Missing",
`beta is missing from object`)
return NewVersionsValueUnknown(), diags
}
betaVal, ok := betaAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`beta expected to be basetypes.BoolValue, was: %T`, betaAttribute))
}
deprecatedAttribute, ok := attributes["deprecated"]
if !ok {
diags.AddError(
"Attribute Missing",
`deprecated is missing from object`)
return NewVersionsValueUnknown(), diags
}
deprecatedVal, ok := deprecatedAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`deprecated expected to be basetypes.StringValue, was: %T`, deprecatedAttribute))
}
recommendAttribute, ok := attributes["recommend"]
if !ok {
diags.AddError(
"Attribute Missing",
`recommend is missing from object`)
return NewVersionsValueUnknown(), diags
}
recommendVal, ok := recommendAttribute.(basetypes.BoolValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`recommend expected to be basetypes.BoolValue, was: %T`, recommendAttribute))
}
versionAttribute, ok := attributes["version"]
if !ok {
diags.AddError(
"Attribute Missing",
`version is missing from object`)
return NewVersionsValueUnknown(), diags
}
versionVal, ok := versionAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`version expected to be basetypes.StringValue, was: %T`, versionAttribute))
}
if diags.HasError() {
return NewVersionsValueUnknown(), diags
}
return VersionsValue{
Beta: betaVal,
Deprecated: deprecatedVal,
Recommend: recommendVal,
Version: versionVal,
state: attr.ValueStateKnown,
}, diags
}
func NewVersionsValueMust(attributeTypes map[string]attr.Type, attributes map[string]attr.Value) VersionsValue {
object, diags := NewVersionsValue(attributeTypes, attributes)
if diags.HasError() {
// This could potentially be added to the diag package.
diagsStrings := make([]string, 0, len(diags))
for _, diagnostic := range diags {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"%s | %s | %s",
diagnostic.Severity(),
diagnostic.Summary(),
diagnostic.Detail()))
}
panic("NewVersionsValueMust received error(s): " + strings.Join(diagsStrings, "\n"))
}
return object
}
func (t VersionsType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
if in.Type() == nil {
return NewVersionsValueNull(), nil
}
if !in.Type().Equal(t.TerraformType(ctx)) {
return nil, fmt.Errorf("expected %s, got %s", t.TerraformType(ctx), in.Type())
}
if !in.IsKnown() {
return NewVersionsValueUnknown(), nil
}
if in.IsNull() {
return NewVersionsValueNull(), nil
}
attributes := map[string]attr.Value{}
val := map[string]tftypes.Value{}
err := in.As(&val)
if err != nil {
return nil, err
}
for k, v := range val {
a, err := t.AttrTypes[k].ValueFromTerraform(ctx, v)
if err != nil {
return nil, err
}
attributes[k] = a
}
return NewVersionsValueMust(VersionsValue{}.AttributeTypes(ctx), attributes), nil
}
func (t VersionsType) ValueType(ctx context.Context) attr.Value {
return VersionsValue{}
}
var _ basetypes.ObjectValuable = VersionsValue{}
type VersionsValue struct {
Beta basetypes.BoolValue `tfsdk:"beta"`
Deprecated basetypes.StringValue `tfsdk:"deprecated"`
Recommend basetypes.BoolValue `tfsdk:"recommend"`
Version basetypes.StringValue `tfsdk:"version"`
state attr.ValueState
}
func (v VersionsValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 4)
var val tftypes.Value
var err error
attrTypes["beta"] = basetypes.BoolType{}.TerraformType(ctx)
attrTypes["deprecated"] = basetypes.StringType{}.TerraformType(ctx)
attrTypes["recommend"] = basetypes.BoolType{}.TerraformType(ctx)
attrTypes["version"] = basetypes.StringType{}.TerraformType(ctx)
objectType := tftypes.Object{AttributeTypes: attrTypes}
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 4)
val, err = v.Beta.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["beta"] = val
val, err = v.Deprecated.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["deprecated"] = val
val, err = v.Recommend.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["recommend"] = val
val, err = v.Version.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["version"] = val
if err := tftypes.ValidateValue(objectType, vals); err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
return tftypes.NewValue(objectType, vals), nil
case attr.ValueStateNull:
return tftypes.NewValue(objectType, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(objectType, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled Object state in ToTerraformValue: %s", v.state))
}
}
func (v VersionsValue) IsNull() bool {
return v.state == attr.ValueStateNull
}
func (v VersionsValue) IsUnknown() bool {
return v.state == attr.ValueStateUnknown
}
func (v VersionsValue) String() string {
return "VersionsValue"
}
func (v VersionsValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
attributeTypes := map[string]attr.Type{
"beta": basetypes.BoolType{},
"deprecated": basetypes.StringType{},
"recommend": basetypes.BoolType{},
"version": basetypes.StringType{},
}
if v.IsNull() {
return types.ObjectNull(attributeTypes), diags
}
if v.IsUnknown() {
return types.ObjectUnknown(attributeTypes), diags
}
objVal, diags := types.ObjectValue(
attributeTypes,
map[string]attr.Value{
"beta": v.Beta,
"deprecated": v.Deprecated,
"recommend": v.Recommend,
"version": v.Version,
})
return objVal, diags
}
func (v VersionsValue) Equal(o attr.Value) bool {
other, ok := o.(VersionsValue)
if !ok {
return false
}
if v.state != other.state {
return false
}
if v.state != attr.ValueStateKnown {
return true
}
if !v.Beta.Equal(other.Beta) {
return false
}
if !v.Deprecated.Equal(other.Deprecated) {
return false
}
if !v.Recommend.Equal(other.Recommend) {
return false
}
if !v.Version.Equal(other.Version) {
return false
}
return true
}
func (v VersionsValue) Type(ctx context.Context) attr.Type {
return VersionsType{
basetypes.ObjectType{
AttrTypes: v.AttributeTypes(ctx),
},
}
}
func (v VersionsValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{
"beta": basetypes.BoolType{},
"deprecated": basetypes.StringType{},
"recommend": basetypes.BoolType{},
"version": basetypes.StringType{},
}
}

View file

@ -143,6 +143,7 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Info(ctx, "SQL Server Flex beta database read") tflog.Info(ctx, "SQL Server Flex beta database read")
} }
// handleReadError centralizes API error handling for the Read operation. // handleReadError centralizes API error handling for the Read operation.

View file

@ -2,6 +2,7 @@ package sqlserverflexbeta
import ( import (
"fmt" "fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
@ -33,7 +34,7 @@ func mapFields(source *sqlserverflexbeta.GetDatabaseResponse, model *dataSourceM
model.Id = types.Int64Value(databaseId) model.Id = types.Int64Value(databaseId)
model.DatabaseName = types.StringValue(source.GetName()) model.DatabaseName = types.StringValue(source.GetName())
model.Name = types.StringValue(source.GetName()) model.Name = types.StringValue(source.GetName())
model.Owner = types.StringValue(source.GetOwner()) model.Owner = types.StringValue(strings.Trim(source.GetOwner(), "\""))
model.Region = types.StringValue(region) model.Region = types.StringValue(region)
model.ProjectId = types.StringValue(model.ProjectId.ValueString()) model.ProjectId = types.StringValue(model.ProjectId.ValueString())
model.InstanceId = types.StringValue(model.InstanceId.ValueString()) model.InstanceId = types.StringValue(model.InstanceId.ValueString())
@ -74,7 +75,7 @@ func mapResourceFields(source *sqlserverflexbeta.GetDatabaseResponse, model *res
model.Id = types.Int64Value(databaseId) model.Id = types.Int64Value(databaseId)
model.DatabaseName = types.StringValue(source.GetName()) model.DatabaseName = types.StringValue(source.GetName())
model.Name = types.StringValue(source.GetName()) model.Name = types.StringValue(source.GetName())
model.Owner = types.StringValue(source.GetOwner()) model.Owner = types.StringValue(strings.Trim(source.GetOwner(), "\""))
model.Region = types.StringValue(region) model.Region = types.StringValue(region)
model.ProjectId = types.StringValue(model.ProjectId.ValueString()) model.ProjectId = types.StringValue(model.ProjectId.ValueString())
model.InstanceId = types.StringValue(model.InstanceId.ValueString()) model.InstanceId = types.StringValue(model.InstanceId.ValueString())

View file

@ -2,7 +2,6 @@ fields:
- name: 'id' - name: 'id'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'instance_id' - name: 'instance_id'
validators: validators:
@ -33,16 +32,13 @@ fields:
- name: 'owner' - name: 'owner'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'database_name' - name: 'database_name'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'collation_name' - name: 'collation_name'
modifiers: modifiers:
- 'RequiresReplace'
- 'UseStateForUnknown' - 'UseStateForUnknown'
- name: 'compatibility' - name: 'compatibility'
@ -53,4 +49,3 @@ fields:
- name: 'compatibility_level' - name: 'compatibility_level'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'

View file

@ -36,6 +36,10 @@ var (
// Define errors // Define errors
errDatabaseNotFound = errors.New("database not found") errDatabaseNotFound = errors.New("database not found")
// Error message constants
extractErrorSummary = "extracting failed"
extractErrorMessage = "Extracting identity data: %v"
) )
func NewDatabaseResource() resource.Resource { func NewDatabaseResource() resource.Resource {
@ -243,6 +247,7 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
// TODO: is this necessary to wait for the database-> API say 200 ?
waitResp, err := wait.CreateDatabaseWaitHandler( waitResp, err := wait.CreateDatabaseWaitHandler(
ctx, ctx,
r.client, r.client,
@ -305,8 +310,19 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
database, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error creating database",
fmt.Sprintf("Getting database details after creation: %v", err),
)
return
}
// Map response body to schema // Map response body to schema
err = mapResourceFields(waitResp, &data, region) err = mapResourceFields(database, &data, region)
if err != nil { if err != nil {
core.LogAndAddError( core.LogAndAddError(
ctx, ctx,
@ -336,12 +352,24 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
return return
} }
// Read identity data
var identityData DatabaseResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString() projectId, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
region := model.Region.ValueString() if errExt != nil {
instanceId := model.InstanceId.ValueString() core.LogAndAddError(
databaseName := model.DatabaseName.ValueString() ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -396,7 +424,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
func (r *databaseResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { func (r *databaseResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
// TODO: Check update api endpoint - not available at the moment, so return an error for now // TODO: Check update api endpoint - not available at the moment, so return an error for now
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating database", "there is no way to update a database") core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating database", "Database can't be updated")
} }
func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
@ -408,10 +436,24 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
return return
} }
projectId := model.ProjectId.ValueString() // Read identity data
region := model.Region.ValueString() var identityData DatabaseResourceIdentityModel
instanceId := model.InstanceId.ValueString() resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
databaseName := model.DatabaseName.ValueString() if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
if errExt != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
extractErrorSummary,
fmt.Sprintf(extractErrorMessage, errExt),
)
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -426,14 +468,10 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
&resp.Diagnostics, &resp.Diagnostics,
"Error deleting database", "Error deleting database",
fmt.Sprintf( fmt.Sprintf(
"Calling API: %v\nname: %s, region: %s, instanceId: %s", err, databaseName, region, instanceId, "Calling API: %v\nname: %s, region: %s, instanceId: %s", err, databaseName, region, instanceId))
),
)
return return
} }
// TODO: wait handler??
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
resp.State.RemoveResource(ctx) resp.State.RemoveResource(ctx)
@ -447,6 +485,7 @@ func (r *databaseResource) ModifyPlan(
req resource.ModifyPlanRequest, req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse, resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
// skip initial empty configuration to avoid follow-up errors // skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() { if req.Config.Raw.IsNull() {
return return
@ -509,7 +548,7 @@ func (r *databaseResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing database", "Error importing database",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format [project_id],[region],[instance_id],[database_name] Got: %q", "Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q",
req.ID, req.ID,
), ),
) )
@ -556,4 +595,45 @@ func (r *databaseResource) ImportState(
tflog.Info(ctx, "Sqlserverflexbeta database state imported") tflog.Info(ctx, "Sqlserverflexbeta database state imported")
} }
// extractIdentityData extracts essential identifiers from the resource model, falling back to the identity mode // extractIdentityData extracts essential identifiers from the resource model, falling back to the identity model.
func (r *databaseResource) extractIdentityData(
model resourceModel,
identity DatabaseResourceIdentityModel,
) (projectId, region, instanceId, databaseName string, err error) {
if !model.Name.IsNull() && !model.Name.IsUnknown() {
databaseName = model.Name.ValueString()
} else {
if identity.DatabaseName.IsNull() || identity.DatabaseName.IsUnknown() {
return "", "", "", "", fmt.Errorf("database_name not found in config")
}
databaseName = identity.DatabaseName.ValueString()
}
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
projectId = model.ProjectId.ValueString()
} else {
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
return "", "", "", "", fmt.Errorf("project_id not found in config")
}
projectId = identity.ProjectID.ValueString()
}
if !model.Region.IsNull() && !model.Region.IsUnknown() {
region = r.providerData.GetRegionWithOverride(model.Region)
} else {
if identity.Region.IsNull() || identity.Region.IsUnknown() {
return "", "", "", "", fmt.Errorf("region not found in config")
}
region = r.providerData.GetRegionWithOverride(identity.Region)
}
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
instanceId = model.InstanceId.ValueString()
} else {
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
return "", "", "", "", fmt.Errorf("instance_id not found in config")
}
instanceId = identity.InstanceID.ValueString()
}
return projectId, region, instanceId, databaseName, nil
}

View file

@ -25,7 +25,7 @@ func mapResponseToModel(
) error { ) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule()) m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
m.Edition = types.StringValue(string(resp.GetEdition())) m.Edition = types.StringValue(string(resp.GetEdition()))
m.Encryption = handleEncryption(ctx, m, resp) m.Encryption = handleEncryption(m, resp)
m.FlavorId = types.StringValue(resp.GetFlavorId()) m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId()) m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId()) m.InstanceId = types.StringValue(resp.GetId())
@ -81,7 +81,7 @@ func mapDataResponseToModel(
) error { ) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule()) m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
m.Edition = types.StringValue(string(resp.GetEdition())) m.Edition = types.StringValue(string(resp.GetEdition()))
m.Encryption = handleDSEncryption(ctx, m, resp) m.Encryption = handleDSEncryption(m, resp)
m.FlavorId = types.StringValue(resp.GetFlavorId()) m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId()) m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId()) m.InstanceId = types.StringValue(resp.GetId())
@ -130,7 +130,6 @@ func mapDataResponseToModel(
} }
func handleEncryption( func handleEncryption(
ctx context.Context,
m *sqlserverflexbetaResGen.InstanceModel, m *sqlserverflexbetaResGen.InstanceModel,
resp *sqlserverflexbeta.GetInstanceResponse, resp *sqlserverflexbeta.GetInstanceResponse,
) sqlserverflexbetaResGen.EncryptionValue { ) sqlserverflexbetaResGen.EncryptionValue {
@ -146,20 +145,23 @@ func handleEncryption(
return m.Encryption return m.Encryption
} }
enc := sqlserverflexbetaResGen.NewEncryptionValueMust( enc := sqlserverflexbetaResGen.NewEncryptionValueNull()
sqlserverflexbetaResGen.EncryptionValue{}.AttributeTypes(ctx), if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
map[string]attr.Value{ enc.KekKeyId = types.StringValue(kVal)
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()), }
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()), if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()), enc.KekKeyRingId = types.StringValue(kkVal)
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()), }
}, if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
) enc.KekKeyVersion = types.StringValue(kkvVal)
}
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
enc.ServiceAccount = types.StringValue(sa)
}
return enc return enc
} }
func handleDSEncryption( func handleDSEncryption(
ctx context.Context,
m *dataSourceModel, m *dataSourceModel,
resp *sqlserverflexbeta.GetInstanceResponse, resp *sqlserverflexbeta.GetInstanceResponse,
) sqlserverflexbetaDataGen.EncryptionValue { ) sqlserverflexbetaDataGen.EncryptionValue {
@ -175,15 +177,19 @@ func handleDSEncryption(
return m.Encryption return m.Encryption
} }
enc := sqlserverflexbetaDataGen.NewEncryptionValueMust( enc := sqlserverflexbetaDataGen.NewEncryptionValueNull()
sqlserverflexbetaDataGen.EncryptionValue{}.AttributeTypes(ctx), if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
map[string]attr.Value{ enc.KekKeyId = types.StringValue(kVal)
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()), }
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()), if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()), enc.KekKeyRingId = types.StringValue(kkVal)
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()), }
}, if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
) enc.KekKeyVersion = types.StringValue(kkvVal)
}
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
enc.ServiceAccount = types.StringValue(sa)
}
return enc return enc
} }
@ -202,12 +208,17 @@ func toCreatePayload(
} }
var encryptionPayload *sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType = nil var encryptionPayload *sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType = nil
if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() { if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() &&
encryptionPayload = &sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType{} !model.Encryption.KekKeyId.IsNull() && model.Encryption.KekKeyId.IsUnknown() && model.Encryption.KekKeyId.ValueString() != "" &&
encryptionPayload.KekKeyId = model.Encryption.KekKeyId.ValueStringPointer() !model.Encryption.KekKeyRingId.IsNull() && !model.Encryption.KekKeyRingId.IsUnknown() && model.Encryption.KekKeyRingId.ValueString() != "" &&
encryptionPayload.KekKeyRingId = model.Encryption.KekKeyRingId.ValueStringPointer() !model.Encryption.KekKeyVersion.IsNull() && !model.Encryption.KekKeyVersion.IsUnknown() && model.Encryption.KekKeyVersion.ValueString() != "" &&
encryptionPayload.KekKeyVersion = model.Encryption.KekKeyVersion.ValueStringPointer() !model.Encryption.ServiceAccount.IsNull() && !model.Encryption.ServiceAccount.IsUnknown() && model.Encryption.ServiceAccount.ValueString() != "" {
encryptionPayload.ServiceAccount = model.Encryption.ServiceAccount.ValueStringPointer() encryptionPayload = &sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType{
KekKeyId: model.Encryption.KekKeyId.ValueStringPointer(),
KekKeyRingId: model.Encryption.KekKeyVersion.ValueStringPointer(),
KekKeyVersion: model.Encryption.KekKeyRingId.ValueStringPointer(),
ServiceAccount: model.Encryption.ServiceAccount.ValueStringPointer(),
}
} }
networkPayload := &sqlserverflexbeta.CreateInstanceRequestPayloadGetNetworkArgType{} networkPayload := &sqlserverflexbeta.CreateInstanceRequestPayloadGetNetworkArgType{}
@ -236,6 +247,7 @@ func toCreatePayload(
conversion.StringValueToPointer(model.Version), conversion.StringValueToPointer(model.Version),
), ),
}, nil }, nil
} }
func toUpdatePayload( func toUpdatePayload(

View file

@ -1,273 +0,0 @@
package sqlserverflexbeta
import (
"context"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
sqlserverflexbetaPkgGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
sqlserverflexbetaRs "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/resources_gen"
)
func Test_handleDSEncryption(t *testing.T) {
type args struct {
m *dataSourceModel
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
}
tests := []struct {
name string
args args
want sqlserverflexbetaRs.EncryptionValue
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if got := handleDSEncryption(t.Context(), tt.args.m, tt.args.resp); !reflect.DeepEqual(got, tt.want) {
t.Errorf("handleDSEncryption() = %v, want %v", got, tt.want)
}
},
)
}
}
func Test_handleEncryption(t *testing.T) {
type args struct {
m *sqlserverflexbetaRs.InstanceModel
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
}
tests := []struct {
name string
args args
want sqlserverflexbetaRs.EncryptionValue
}{
{
name: "nil response",
args: args{
m: &sqlserverflexbetaRs.InstanceModel{},
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{},
},
want: sqlserverflexbetaRs.EncryptionValue{},
},
{
name: "nil response",
args: args{
m: &sqlserverflexbetaRs.InstanceModel{},
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{},
},
},
want: sqlserverflexbetaRs.NewEncryptionValueNull(),
},
{
name: "response with values",
args: args{
m: &sqlserverflexbetaRs.InstanceModel{},
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{
KekKeyId: utils.Ptr("kek_key_id"),
KekKeyRingId: utils.Ptr("kek_key_ring_id"),
KekKeyVersion: utils.Ptr("kek_key_version"),
ServiceAccount: utils.Ptr("kek_svc_acc"),
},
},
},
want: sqlserverflexbetaRs.EncryptionValue{
KekKeyId: types.StringValue("kek_key_id"),
KekKeyRingId: types.StringValue("kek_key_ring_id"),
KekKeyVersion: types.StringValue("kek_key_version"),
ServiceAccount: types.StringValue("kek_svc_acc"),
},
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if got := handleEncryption(t.Context(), tt.args.m, tt.args.resp); !reflect.DeepEqual(got, tt.want) {
t.Errorf("handleEncryption() = %v, want %v", got, tt.want)
}
},
)
}
}
func Test_mapDataResponseToModel(t *testing.T) {
type args struct {
ctx context.Context
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
m *dataSourceModel
tfDiags diag.Diagnostics
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if err := mapDataResponseToModel(
tt.args.ctx,
tt.args.resp,
tt.args.m,
tt.args.tfDiags,
); (err != nil) != tt.wantErr {
t.Errorf("mapDataResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
}
},
)
}
}
func Test_mapResponseToModel(t *testing.T) {
type args struct {
ctx context.Context
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
m *sqlserverflexbetaRs.InstanceModel
tfDiags diag.Diagnostics
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if err := mapResponseToModel(
tt.args.ctx,
tt.args.resp,
tt.args.m,
tt.args.tfDiags,
); (err != nil) != tt.wantErr {
t.Errorf("mapResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
}
},
)
}
}
func Test_toCreatePayload(t *testing.T) {
type args struct {
ctx context.Context
model *sqlserverflexbetaRs.InstanceModel
}
tests := []struct {
name string
args args
want *sqlserverflexbetaPkgGen.CreateInstanceRequestPayload
wantErr bool
}{
{
name: "simple",
args: args{
ctx: context.Background(),
model: &sqlserverflexbetaRs.InstanceModel{
Encryption: sqlserverflexbetaRs.NewEncryptionValueMust(
sqlserverflexbetaRs.EncryptionValue{}.AttributeTypes(context.Background()),
map[string]attr.Value{
"kek_key_id": types.StringValue("kek_key_id"),
"kek_key_ring_id": types.StringValue("kek_key_ring_id"),
"kek_key_version": types.StringValue("kek_key_version"),
"service_account": types.StringValue("sacc"),
},
),
Storage: sqlserverflexbetaRs.StorageValue{},
},
},
want: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayload{
BackupSchedule: nil,
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{
KekKeyId: utils.Ptr("kek_key_id"),
KekKeyRingId: utils.Ptr("kek_key_ring_id"),
KekKeyVersion: utils.Ptr("kek_key_version"),
ServiceAccount: utils.Ptr("sacc"),
},
FlavorId: nil,
Name: nil,
Network: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadNetwork{},
RetentionDays: nil,
Storage: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadGetStorageArgType{},
Version: nil,
},
wantErr: false,
},
{
name: "nil object",
args: args{
ctx: context.Background(),
model: &sqlserverflexbetaRs.InstanceModel{
Encryption: sqlserverflexbetaRs.NewEncryptionValueNull(),
Storage: sqlserverflexbetaRs.StorageValue{},
},
},
want: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayload{
BackupSchedule: nil,
Encryption: nil,
FlavorId: nil,
Name: nil,
Network: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadNetwork{},
RetentionDays: nil,
Storage: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadGetStorageArgType{},
Version: nil,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
got, err := toCreatePayload(tt.args.ctx, tt.args.model)
if (err != nil) != tt.wantErr {
t.Errorf("toCreatePayload() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("model mismatch (-want +got):\n%s", diff)
}
},
)
}
}
func Test_toUpdatePayload(t *testing.T) {
type args struct {
ctx context.Context
m *sqlserverflexbetaRs.InstanceModel
resp *resource.UpdateResponse
}
tests := []struct {
name string
args args
want *sqlserverflexbetaPkgGen.UpdateInstanceRequestPayload
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
got, err := toUpdatePayload(tt.args.ctx, tt.args.m, tt.args.resp)
if (err != nil) != tt.wantErr {
t.Errorf("toUpdatePayload() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("toUpdatePayload() got = %v, want %v", got, tt.want)
}
},
)
}
}

View file

@ -54,7 +54,7 @@ type InstanceResourceIdentityModel struct {
} }
func (r *instanceResource) Metadata( func (r *instanceResource) Metadata(
_ context.Context, ctx context.Context,
req resource.MetadataRequest, req resource.MetadataRequest,
resp *resource.MetadataResponse, resp *resource.MetadataResponse,
) { ) {
@ -64,7 +64,7 @@ func (r *instanceResource) Metadata(
//go:embed planModifiers.yaml //go:embed planModifiers.yaml
var modifiersFileByte []byte var modifiersFileByte []byte
func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *instanceResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
s := sqlserverflexbetaResGen.InstanceResourceSchema(ctx) s := sqlserverflexbetaResGen.InstanceResourceSchema(ctx)
fields, err := utils.ReadModifiersConfig(modifiersFileByte) fields, err := utils.ReadModifiersConfig(modifiersFileByte)
@ -147,6 +147,7 @@ func (r *instanceResource) ModifyPlan(
req resource.ModifyPlanRequest, req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse, resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
// skip initial empty configuration to avoid follow-up errors // skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() { if req.Config.Raw.IsNull() {
return return
@ -206,7 +207,6 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
) )
return return
} }
// Create new Instance // Create new Instance
createResp, err := r.client.CreateInstanceRequest( createResp, err := r.client.CreateInstanceRequest(
ctx, ctx,
@ -293,6 +293,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return return
} }
// Read identity data
var identityData InstanceResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString() projectId := data.ProjectId.ValueString()
@ -516,7 +523,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format: [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -15,7 +15,7 @@ import (
"github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta" sqlserverflexbeta2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance" sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance"
// The fwresource import alias is so there is no collision // The fwresource import alias is so there is no collision
@ -36,7 +36,7 @@ func init() {
F: func(region string) error { F: func(region string) error {
ctx := context.Background() ctx := context.Background()
apiClientConfigOptions := []config.ConfigurationOption{} apiClientConfigOptions := []config.ConfigurationOption{}
apiClient, err := sqlserverflexbetaResGen.NewAPIClient(apiClientConfigOptions...) apiClient, err := sqlserverflexbeta2.NewAPIClient(apiClientConfigOptions...)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -228,67 +228,6 @@ func TestAccInstance(t *testing.T) {
}) })
} }
func TestAccInstanceReApply(t *testing.T) {
exData := getExample()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
t.Logf(" ... working on instance %s", exData.TfName)
testInstances = append(testInstances, exData.TfName)
},
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
{
RefreshState: true,
},
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
// Import test
{
ResourceName: resName("instance", exData.TfName),
ImportStateKind: resource.ImportBlockWithResourceIdentity,
ImportState: true,
// ImportStateVerify is not supported with plannable import blocks
// ImportStateVerify: true,
},
},
})
}
func TestAccInstanceNoEncryption(t *testing.T) { func TestAccInstanceNoEncryption(t *testing.T) {
data := getExample() data := getExample()
@ -302,9 +241,9 @@ func TestAccInstanceNoEncryption(t *testing.T) {
"##STACKIT_DatabaseManager##", "##STACKIT_DatabaseManager##",
"##STACKIT_LoginManager##", "##STACKIT_LoginManager##",
"##STACKIT_ProcessManager##", "##STACKIT_ProcessManager##",
"##STACKIT_ServerManager##",
"##STACKIT_SQLAgentManager##", "##STACKIT_SQLAgentManager##",
"##STACKIT_SQLAgentUser##", "##STACKIT_SQLAgentUser##",
"##STACKIT_ServerManager##",
}, },
}, },
} }
@ -349,8 +288,8 @@ func TestAccInstanceNoEncryption(t *testing.T) {
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"), //resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"), //resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),
@ -402,7 +341,7 @@ func TestAccInstanceEncryption(t *testing.T) {
{ {
Name: userName, Name: userName,
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"), ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
Roles: []string{"##STACKIT_DatabaseManager##", "##STACKIT_LoginManager##"}, Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
}, },
} }
data.Databases = []Database{ data.Databases = []Database{
@ -450,8 +389,8 @@ func TestAccInstanceEncryption(t *testing.T) {
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"), //resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"), //resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"), // resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),

View file

@ -20,6 +20,8 @@ import (
var _ datasource.DataSource = (*userDataSource)(nil) var _ datasource.DataSource = (*userDataSource)(nil)
const errorPrefix = "[Sqlserverflexbeta - User]"
func NewUserDataSource() datasource.DataSource { func NewUserDataSource() datasource.DataSource {
return &userDataSource{} return &userDataSource{}
} }

View file

@ -2,7 +2,6 @@ package sqlserverflexbeta
import ( import (
"fmt" "fmt"
"slices"
"strconv" "strconv"
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
@ -46,9 +45,7 @@ func mapDataSourceFields(userResp *sqlserverflexbeta.GetUserResponse, model *dat
model.Roles = types.List(types.SetNull(types.StringType)) model.Roles = types.List(types.SetNull(types.StringType))
} else { } else {
var roles []attr.Value var roles []attr.Value
resRoles := *user.Roles for _, role := range *user.Roles {
slices.Sort(resRoles)
for _, role := range resRoles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesSet, diags := types.SetValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
@ -94,20 +91,16 @@ func mapFields(userResp *sqlserverflexbeta.GetUserResponse, model *resourceModel
model.Username = types.StringPointerValue(user.Username) model.Username = types.StringPointerValue(user.Username)
// Map roles // Map roles
if userResp.Roles != nil { if user.Roles != nil {
resRoles := *userResp.Roles
slices.Sort(resRoles)
var roles []attr.Value var roles []attr.Value
for _, role := range resRoles { for _, role := range *user.Roles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesSet, diags := types.SetValue(types.StringType, roles)
rolesSet, diags := types.ListValue(types.StringType, roles)
if diags.HasError() { if diags.HasError() {
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags)) return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
} }
model.Roles = rolesSet model.Roles = types.List(rolesSet)
} }
// Ensure roles is not null // Ensure roles is not null
@ -146,18 +139,15 @@ func mapFieldsCreate(userResp *sqlserverflexbeta.CreateUserResponse, model *reso
model.Password = types.StringValue(*user.Password) model.Password = types.StringValue(*user.Password)
if user.Roles != nil { if user.Roles != nil {
resRoles := *user.Roles
slices.Sort(resRoles)
var roles []attr.Value var roles []attr.Value
for _, role := range resRoles { for _, role := range *user.Roles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesList, diags := types.ListValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
if diags.HasError() { if diags.HasError() {
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags)) return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
} }
model.Roles = rolesList model.Roles = types.List(rolesSet)
} }
if model.Roles.IsNull() || model.Roles.IsUnknown() { if model.Roles.IsNull() || model.Roles.IsUnknown() {
@ -185,14 +175,9 @@ func toCreatePayload(
return nil, fmt.Errorf("nil model") return nil, fmt.Errorf("nil model")
} }
pl := sqlserverflexbeta.CreateUserRequestPayload{ return &sqlserverflexbeta.CreateUserRequestPayload{
Username: conversion.StringValueToPointer(model.Username), Username: conversion.StringValueToPointer(model.Username),
Roles: &roles, DefaultDatabase: conversion.StringValueToPointer(model.DefaultDatabase),
} Roles: &roles,
slices.Sort(roles) }, nil
if !model.DefaultDatabase.IsNull() || !model.DefaultDatabase.IsUnknown() {
pl.DefaultDatabase = conversion.StringValueToPointer(model.DefaultDatabase)
}
return &pl, nil
} }

View file

@ -63,9 +63,9 @@ func TestMapDataSourceFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -138,7 +138,7 @@ func TestMapDataSourceFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -204,9 +204,9 @@ func TestMapFieldsCreate(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -292,7 +292,7 @@ func TestMapFieldsCreate(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -332,8 +332,8 @@ func TestMapFields(t *testing.T) {
"simple_values", "simple_values",
&sqlserverflexbeta.GetUserResponse{ &sqlserverflexbeta.GetUserResponse{
Roles: &[]string{ Roles: &[]string{
"role_2",
"role_1", "role_1",
"role_2",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -350,9 +350,9 @@ func TestMapFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -423,7 +423,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(&tt.expected, state) diff := cmp.Diff(state, &tt.expected)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }

View file

@ -2,7 +2,6 @@ fields:
- name: 'id' - name: 'id'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'instance_id' - name: 'instance_id'
validators: validators:
@ -23,7 +22,6 @@ fields:
- name: 'region' - name: 'region'
modifiers: modifiers:
- 'RequiresReplace' - 'RequiresReplace'
- 'RequiresReplace'
- name: 'user_id' - name: 'user_id'
modifiers: modifiers:
@ -33,12 +31,10 @@ fields:
- name: 'username' - name: 'username'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'roles' - name: 'roles'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'password' - name: 'password'
modifiers: modifiers:

Some files were not shown because too many files have changed in this diff Show more