chore: updated files - work save
This commit is contained in:
parent
51663cd8d0
commit
e91e10e29a
37 changed files with 1121 additions and 118 deletions
53
tools/formats.go
Normal file
53
tools/formats.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// snakeLetters will match to the first letter and an underscore followed by a letter
|
||||
var snakeLetters = regexp.MustCompile("(^[a-z])|_[a-z0-9]")
|
||||
|
||||
func ToPascalCase(in string) string {
|
||||
inputSplit := strings.Split(in, ".")
|
||||
|
||||
var ucName string
|
||||
|
||||
for _, v := range inputSplit {
|
||||
if len(v) < 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
firstChar := v[0:1]
|
||||
ucFirstChar := strings.ToUpper(firstChar)
|
||||
|
||||
if len(v) < 2 {
|
||||
ucName += ucFirstChar
|
||||
continue
|
||||
}
|
||||
|
||||
ucName += ucFirstChar + v[1:]
|
||||
}
|
||||
|
||||
return snakeLetters.ReplaceAllStringFunc(ucName, func(s string) string {
|
||||
return strings.ToUpper(strings.Replace(s, "_", "", -1))
|
||||
})
|
||||
}
|
||||
|
||||
func ToCamelCase(in string) string {
|
||||
pascal := ToPascalCase(in)
|
||||
|
||||
// Grab first rune and lower case it
|
||||
firstLetter, size := utf8.DecodeRuneInString(pascal)
|
||||
if firstLetter == utf8.RuneError && size <= 1 {
|
||||
return pascal
|
||||
}
|
||||
|
||||
return string(unicode.ToLower(firstLetter)) + pascal[size:]
|
||||
}
|
||||
|
||||
func ValidateSnakeCase(in string) bool {
|
||||
return snakeLetters.MatchString(string(in))
|
||||
}
|
||||
207
tools/main.go
207
tools/main.go
|
|
@ -14,7 +14,7 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"text/template"
|
||||
|
||||
"github.com/ldez/go-git-cmd-wrapper/v2/clone"
|
||||
"github.com/ldez/go-git-cmd-wrapper/v2/git"
|
||||
|
|
@ -50,6 +50,12 @@ func Build() error {
|
|||
return err
|
||||
}
|
||||
|
||||
slog.Info("Cleaning up old packages directory")
|
||||
err = os.RemoveAll(path.Join(*root, "pkg"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("Creating generator dir", "dir", fmt.Sprintf("%s/%s", *root, GEN_REPO_NAME))
|
||||
genDir, err := createGeneratorDir(*root, GEN_REPO, GEN_REPO_NAME)
|
||||
if err != nil {
|
||||
|
|
@ -162,6 +168,10 @@ func Build() error {
|
|||
}
|
||||
|
||||
slog.Info("Rearranging package directories")
|
||||
err = os.MkdirAll(path.Join(*root, "pkg"), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srcDir := path.Join(genDir, "sdk-repo-updated", "services")
|
||||
items, err := os.ReadDir(srcDir)
|
||||
if err != nil {
|
||||
|
|
@ -171,27 +181,30 @@ func Build() error {
|
|||
if item.IsDir() {
|
||||
slog.Info(" -> package", "name", item.Name())
|
||||
tgtDir := path.Join(*root, "pkg", item.Name())
|
||||
bakName := fmt.Sprintf("%s.%s", item.Name(), time.Now().Format("20060102-150405"))
|
||||
if _, err = os.Stat(tgtDir); !os.IsNotExist(err) {
|
||||
err = os.Rename(
|
||||
tgtDir,
|
||||
path.Join(*root, "pkg", bakName),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// no backup needed as we generate new
|
||||
//bakName := fmt.Sprintf("%s.%s", item.Name(), time.Now().Format("20060102-150405"))
|
||||
//if _, err = os.Stat(tgtDir); !os.IsNotExist(err) {
|
||||
// err = os.Rename(
|
||||
// tgtDir,
|
||||
// path.Join(*root, "pkg", bakName),
|
||||
// )
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//}
|
||||
err = os.Rename(path.Join(srcDir, item.Name()), tgtDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = os.Stat(path.Join(*root, "pkg", bakName, "wait")); !os.IsNotExist(err) {
|
||||
slog.Info(" Copying wait subfolder")
|
||||
err = os.Rename(path.Join(*root, "pkg", bakName, "wait"), path.Join(tgtDir, "wait"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// wait is placed outside now
|
||||
//if _, err = os.Stat(path.Join(*root, "pkg", bakName, "wait")); !os.IsNotExist(err) {
|
||||
// slog.Info(" Copying wait subfolder")
|
||||
// err = os.Rename(path.Join(*root, "pkg", bakName, "wait"), path.Join(tgtDir, "wait"))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -216,19 +229,17 @@ func Build() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// TODO
|
||||
for _, testDir := range []string{
|
||||
path.Join(*root, "stackit", "internal", "services"),
|
||||
} {
|
||||
fmt.Println(testDir)
|
||||
err = createBoilerplate(*root, path.Join(*root, "stackit", "internal", "services"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("Finally removing temporary files and directories")
|
||||
err = os.RemoveAll(path.Join(*root, "generated"))
|
||||
if err != nil {
|
||||
slog.Error("RemoveAll", "dir", path.Join(*root, "generated"), "err", err)
|
||||
return err
|
||||
}
|
||||
//err = os.RemoveAll(path.Join(*root, "generated"))
|
||||
//if err != nil {
|
||||
// slog.Error("RemoveAll", "dir", path.Join(*root, "generated"), "err", err)
|
||||
// return err
|
||||
//}
|
||||
|
||||
err = os.RemoveAll(path.Join(*root, GEN_REPO_NAME))
|
||||
if err != nil {
|
||||
|
|
@ -240,6 +251,146 @@ func Build() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type templateData struct {
|
||||
PackageName string
|
||||
NameCamel string
|
||||
NamePascal string
|
||||
NameSnake string
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func createBoilerplate(rootFolder, folder string) error {
|
||||
services, err := os.ReadDir(folder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, svc := range services {
|
||||
if !svc.IsDir() {
|
||||
continue
|
||||
}
|
||||
resources, err := os.ReadDir(path.Join(folder, svc.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handleDS := false
|
||||
handleRes := false
|
||||
foundDS := false
|
||||
foundRes := false
|
||||
for _, res := range resources {
|
||||
if !res.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
resourceName := res.Name()
|
||||
|
||||
dsFile := path.Join(folder, svc.Name(), res.Name(), "datasources_gen", fmt.Sprintf("%s_data_source_gen.go", res.Name()))
|
||||
handleDS = fileExists(dsFile)
|
||||
|
||||
resFile := path.Join(folder, svc.Name(), res.Name(), "resources_gen", fmt.Sprintf("%s_resource_gen.go", res.Name()))
|
||||
handleRes = fileExists(resFile)
|
||||
|
||||
dsGoFile := path.Join(folder, svc.Name(), res.Name(), "datasource.go")
|
||||
foundDS = fileExists(dsGoFile)
|
||||
|
||||
resGoFile := path.Join(folder, svc.Name(), res.Name(), "resource.go")
|
||||
foundRes = fileExists(resGoFile)
|
||||
|
||||
if handleDS && !foundDS {
|
||||
slog.Info("Creating missing datasource.go", "service", svc.Name(), "resource", resourceName)
|
||||
if !ValidateSnakeCase(resourceName) {
|
||||
return errors.New("resource name is invalid")
|
||||
}
|
||||
|
||||
tplName := "data_source_scaffold.gotmpl"
|
||||
err = writeTemplateToFile(
|
||||
tplName,
|
||||
path.Join(rootFolder, "tools", "templates", tplName),
|
||||
path.Join(folder, svc.Name(), res.Name(), "datasource.go"),
|
||||
&templateData{
|
||||
PackageName: svc.Name(),
|
||||
NameCamel: ToCamelCase(resourceName),
|
||||
NamePascal: ToPascalCase(resourceName),
|
||||
NameSnake: resourceName,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if handleRes && !foundRes {
|
||||
slog.Info("Creating missing resource.go", "service", svc.Name(), "resource", resourceName)
|
||||
if !ValidateSnakeCase(resourceName) {
|
||||
return errors.New("resource name is invalid")
|
||||
}
|
||||
|
||||
tplName := "resource_scaffold.gotmpl"
|
||||
err = writeTemplateToFile(
|
||||
tplName,
|
||||
path.Join(rootFolder, "tools", "templates", tplName),
|
||||
path.Join(folder, svc.Name(), res.Name(), "resource.go"),
|
||||
&templateData{
|
||||
PackageName: svc.Name(),
|
||||
NameCamel: ToCamelCase(resourceName),
|
||||
NamePascal: ToPascalCase(resourceName),
|
||||
NameSnake: resourceName,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ucfirst(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.ToUpper(s[:1]) + s[1:]
|
||||
}
|
||||
|
||||
func writeTemplateToFile(tplName, tplFile, outFile string, data *templateData) error {
|
||||
fn := template.FuncMap{
|
||||
"ucfirst": ucfirst,
|
||||
}
|
||||
|
||||
tmpl, err := template.New(tplName).Funcs(fn).ParseFiles(tplFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var f *os.File
|
||||
f, err = os.Create(outFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tmpl.Execute(f, *data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateServiceFiles(rootDir, generatorDir string) error {
|
||||
// slog.Info("Generating specs folder")
|
||||
err := os.MkdirAll(path.Join(rootDir, "generated", "specs"), 0755)
|
||||
|
|
|
|||
49
tools/templates/data_source_scaffold.gotmpl
Normal file
49
tools/templates/data_source_scaffold.gotmpl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
{{.PackageName}}Gen "github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/services/{{.PackageName}}/{{.NameSnake}}/datasources_gen"
|
||||
)
|
||||
|
||||
var _ datasource.DataSource = (*{{.NameCamel}}DataSource)(nil)
|
||||
|
||||
func New{{.NamePascal}}DataSource() datasource.DataSource {
|
||||
return &{{.NameCamel}}DataSource{}
|
||||
}
|
||||
|
||||
type {{.NameCamel}}DataSource struct{
|
||||
client *{{.PackageName}}.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *{{.NameCamel}}DataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_{{.PackageName}}_{{.NameSnake}}"
|
||||
}
|
||||
|
||||
func (d *{{.NameCamel}}DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = {{.PackageName}}Gen.{{.NamePascal}}DataSourceSchema(ctx)
|
||||
}
|
||||
|
||||
func (d *{{.NameCamel}}DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NameCamel}}Model
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Todo: Read API call logic
|
||||
|
||||
// Example data value setting
|
||||
// data.Id = types.StringValue("example-id")
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
39
tools/templates/provider_scaffold.gotmpl
Normal file
39
tools/templates/provider_scaffold.gotmpl
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/provider"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
)
|
||||
|
||||
var _ provider.Provider = (*{{.NameCamel}}Provider)(nil)
|
||||
|
||||
func New() func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &{{.NameCamel}}Provider{}
|
||||
}
|
||||
}
|
||||
|
||||
type {{.NameCamel}}Provider struct{}
|
||||
|
||||
func (p *{{.NameCamel}}Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
|
||||
}
|
||||
|
||||
func (p *{{.NameCamel}}Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
|
||||
}
|
||||
|
||||
func (p *{{.NameCamel}}Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "{{.NameSnake}}"
|
||||
}
|
||||
|
||||
func (p *{{.NameCamel}}Provider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
||||
return []func() datasource.DataSource{}
|
||||
}
|
||||
|
||||
func (p *{{.NameCamel}}Provider) Resources(ctx context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{}
|
||||
}
|
||||
208
tools/templates/resource_scaffold.gotmpl
Normal file
208
tools/templates/resource_scaffold.gotmpl
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
"github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
"github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
|
||||
{{.PackageName}}Gen "github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/services/{{.PackageName}}/{{.NameSnake}}/resources_gen"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &{{.NameCamel}}Resource{}
|
||||
_ resource.ResourceWithConfigure = &{{.NameCamel}}Resource{}
|
||||
_ resource.ResourceWithImportState = &{{.NameCamel}}Resource{}
|
||||
_ resource.ResourceWithModifyPlan = &{{.NameCamel}}Resource{}
|
||||
)
|
||||
|
||||
func New{{.NamePascal}}Resource() resource.Resource {
|
||||
return &{{.NameCamel}}Resource{}
|
||||
}
|
||||
|
||||
type {{.NameCamel}}Resource struct{
|
||||
client *{{.PackageName}}.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_{{.PackageName}}_{{.NameSnake}}"
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = {{.PackageName}}Gen.{{.NamePascal}}ResourceSchema(ctx)
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *{{.NameCamel}}Resource) 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
|
||||
}
|
||||
|
||||
apiClientConfigOptions := []config.ConfigurationOption{
|
||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
utils.UserAgentConfigOption(r.providerData.Version),
|
||||
}
|
||||
if r.providerData.PostgresFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint))
|
||||
} else {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
||||
}
|
||||
apiClient, err := {{.PackageName}}.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, "{{.PackageName}}.{{.NamePascal}} client configured")
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Create API call logic
|
||||
|
||||
// Example data value setting
|
||||
data.{{.NameCamel | ucfirst}}Id = types.StringValue("id-from-response")
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "{{.PackageName}}.{{.NamePascal}} created")
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Todo: Read API call logic
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "{{.PackageName}}.{{.NamePascal}} read")
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Todo: Update API call logic
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "{{.PackageName}}.{{.NamePascal}} updated")
|
||||
}
|
||||
|
||||
func (r *{{.NameCamel}}Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Todo: Delete API call logic
|
||||
|
||||
tflog.Info(ctx, "{{.PackageName}}.{{.NamePascal}} deleted")
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *{{.NameCamel}}Resource) ModifyPlan(
|
||||
ctx context.Context,
|
||||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planModel {{.PackageName}}Gen.{{.NamePascal}}Model
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
func (r *{{.NameCamel}}Resource) ImportState(
|
||||
ctx context.Context,
|
||||
req resource.ImportStateRequest,
|
||||
resp *resource.ImportStateResponse,
|
||||
) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
// Todo: Import logic
|
||||
if len(idParts) < 2 || idParts[0] == "" || idParts[1] == "" {
|
||||
core.LogAndAddError(
|
||||
ctx, &resp.Diagnostics,
|
||||
"Error importing database",
|
||||
fmt.Sprintf(
|
||||
"Expected import identifier with format [project_id],[region],..., got %q",
|
||||
req.ID,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
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])...)
|
||||
// ... more ...
|
||||
|
||||
core.LogAndAddWarning(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"{{.PackageName | ucfirst}} database imported with empty password",
|
||||
"The database password is not imported as it is only available upon creation of a new database. The password field will be empty.",
|
||||
)
|
||||
tflog.Info(ctx, "{{.PackageName | ucfirst}} {{.NameCamel}} state imported")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue