feat: builder template refactoring
This commit is contained in:
parent
4549ba63e5
commit
0faf30c95a
2 changed files with 83 additions and 22 deletions
|
|
@ -254,10 +254,12 @@ func (b *Builder) Build() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
PackageName string
|
PackageName string
|
||||||
NameCamel string
|
PackageNameCamel string
|
||||||
NamePascal string
|
PackageNamePascal string
|
||||||
NameSnake string
|
NameCamel string
|
||||||
|
NamePascal string
|
||||||
|
NameSnake string
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileExists(path string) bool {
|
func fileExists(path string) bool {
|
||||||
|
|
@ -318,13 +320,15 @@ func createBoilerplate(rootFolder, folder string) error {
|
||||||
tplName := "data_source_scaffold.gotmpl"
|
tplName := "data_source_scaffold.gotmpl"
|
||||||
err = writeTemplateToFile(
|
err = writeTemplateToFile(
|
||||||
tplName,
|
tplName,
|
||||||
path.Join(rootFolder, "tools", "templates", tplName),
|
path.Join(rootFolder, "cmd", "build", "templates", tplName),
|
||||||
path.Join(folder, svc.Name(), res.Name(), "datasource.go"),
|
path.Join(folder, svc.Name(), res.Name(), "datasource.go"),
|
||||||
&templateData{
|
&templateData{
|
||||||
PackageName: svc.Name(),
|
PackageName: svc.Name(),
|
||||||
NameCamel: ToCamelCase(resourceName),
|
PackageNameCamel: ToCamelCase(svc.Name()),
|
||||||
NamePascal: ToPascalCase(resourceName),
|
PackageNamePascal: ToPascalCase(svc.Name()),
|
||||||
NameSnake: resourceName,
|
NameCamel: ToCamelCase(resourceName),
|
||||||
|
NamePascal: ToPascalCase(resourceName),
|
||||||
|
NameSnake: resourceName,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -341,13 +345,15 @@ func createBoilerplate(rootFolder, folder string) error {
|
||||||
tplName := "resource_scaffold.gotmpl"
|
tplName := "resource_scaffold.gotmpl"
|
||||||
err = writeTemplateToFile(
|
err = writeTemplateToFile(
|
||||||
tplName,
|
tplName,
|
||||||
path.Join(rootFolder, "tools", "templates", tplName),
|
path.Join(rootFolder, "cmd", "build", "templates", tplName),
|
||||||
path.Join(folder, svc.Name(), res.Name(), "resource.go"),
|
path.Join(folder, svc.Name(), res.Name(), "resource.go"),
|
||||||
&templateData{
|
&templateData{
|
||||||
PackageName: svc.Name(),
|
PackageName: svc.Name(),
|
||||||
NameCamel: ToCamelCase(resourceName),
|
PackageNameCamel: ToCamelCase(svc.Name()),
|
||||||
NamePascal: ToPascalCase(resourceName),
|
PackageNamePascal: ToPascalCase(svc.Name()),
|
||||||
NameSnake: resourceName,
|
NameCamel: ToCamelCase(resourceName),
|
||||||
|
NamePascal: ToPascalCase(resourceName),
|
||||||
|
NameSnake: resourceName,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,10 @@ type {{.NameCamel}}Resource struct{
|
||||||
providerData core.ProviderData
|
providerData core.ProviderData
|
||||||
}
|
}
|
||||||
|
|
||||||
type InstanceResourceIdentityModel struct {
|
type {{.NamePascal}}ResourceIdentityModel struct {
|
||||||
ProjectID types.String `tfsdk:"project_id"`
|
ProjectID types.String `tfsdk:"project_id"`
|
||||||
Region types.String `tfsdk:"region"`
|
Region types.String `tfsdk:"region"`
|
||||||
InstanceID types.String `tfsdk:"instance_id"`
|
{{.NamePascal}}ID types.String `tfsdk:"instance_id"`
|
||||||
// TODO: implement further needed parts
|
// TODO: implement further needed parts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,14 +78,20 @@ func (r *{{.NameCamel}}Resource) Configure(
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||||
utils.UserAgentConfigOption(r.providerData.Version),
|
utils.UserAgentConfigOption(r.providerData.Version),
|
||||||
}
|
}
|
||||||
if r.providerData.PostgresFlexCustomEndpoint != "" {
|
if r.providerData.{{.PackageNamePascal}}CustomEndpoint != "" {
|
||||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint))
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.{{.PackageName}}CustomEndpoint))
|
||||||
} else {
|
} else {
|
||||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
||||||
}
|
}
|
||||||
apiClient, err := {{.PackageName}}.NewAPIClient(apiClientConfigOptions...)
|
apiClient, err := {{.PackageName}}.NewAPIClient(apiClientConfigOptions...)
|
||||||
if err != nil {
|
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))
|
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
|
||||||
|
|
@ -102,16 +108,30 @@ func (r *{{.NameCamel}}Resource) Create(ctx context.Context, req resource.Create
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read identity data
|
||||||
|
var identityData {{.NamePascal}}ResourceIdentityModel
|
||||||
|
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
|
projectId := identityData.ProjectID.ValueString()
|
||||||
|
region := identityData.Region.ValueString()
|
||||||
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||||
|
ctx = tflog.SetField(ctx, "region", region)
|
||||||
|
|
||||||
// TODO: Create API call logic
|
// TODO: Create API call logic
|
||||||
|
|
||||||
// Example data value setting
|
// Example data value setting
|
||||||
data.{{.NameCamel | ucfirst}}Id = types.StringValue("id-from-response")
|
data.{{.NameCamel | ucfirst}}Id = types.StringValue("id-from-response")
|
||||||
|
|
||||||
// TODO: Set data returned by API in identity
|
// TODO: Set data returned by API in identity
|
||||||
identity := InstanceResourceIdentityModel{
|
identity := {{.NamePascal}}ResourceIdentityModel{
|
||||||
ProjectID: types.StringValue(projectId),
|
ProjectID: types.StringValue(projectId),
|
||||||
Region: types.StringValue(region),
|
Region: types.StringValue(region),
|
||||||
InstanceID: types.StringValue(instanceId),
|
// InstanceID: types.StringValue(instanceId),
|
||||||
}
|
}
|
||||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
|
|
@ -133,7 +153,7 @@ func (r *{{.NameCamel}}Resource) Read(ctx context.Context, req resource.ReadRequ
|
||||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||||
|
|
||||||
// Read identity data
|
// Read identity data
|
||||||
var identityData InstanceResourceIdentityModel
|
var identityData {{.NamePascal}}ResourceIdentityModel
|
||||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
|
|
@ -148,6 +168,17 @@ func (r *{{.NameCamel}}Resource) Read(ctx context.Context, req resource.ReadRequ
|
||||||
// Save updated data into Terraform state
|
// Save updated data into Terraform state
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||||
|
|
||||||
|
// TODO: Set data returned by API in identity
|
||||||
|
identity := {{.NamePascal}}ResourceIdentityModel{
|
||||||
|
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, "{{.PackageName}}.{{.NamePascal}} read")
|
tflog.Info(ctx, "{{.PackageName}}.{{.NamePascal}} read")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,6 +192,17 @@ func (r *{{.NameCamel}}Resource) Update(ctx context.Context, req resource.Update
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read identity data
|
||||||
|
var identityData {{.NamePascal}}ResourceIdentityModel
|
||||||
|
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Todo: Update API call logic
|
// Todo: Update API call logic
|
||||||
|
|
||||||
// Save updated data into Terraform state
|
// Save updated data into Terraform state
|
||||||
|
|
@ -212,6 +254,19 @@ func (r *{{.NameCamel}}Resource) ModifyPlan(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var identityModel {{.NamePascal}}ResourceIdentityModel
|
||||||
|
identityModel.ProjectID = planModel.ProjectId
|
||||||
|
identityModel.Region = planModel.Region
|
||||||
|
// TODO: complete
|
||||||
|
//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)...)
|
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue