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