Skip to content

Commit 5ac0965

Browse files
chore: Adjusts scaffold command to generate config file for schema scaffolding (#1839)
1 parent a7dec60 commit 5ac0965

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ make scaffold resource_name=streamInstance type=resource
325325

326326
This will generate resource/data source files and accompanying test files needed for starting the development, and will contain multiple comments with `TODO:` statements which give guidance for the development.
327327

328+
As a follow up step, use [Scaffolding Schema and Model Definitions](#scaffolding-schema-and-model-definitions) to autogenerate the schema via the Open API specification. This will require making adjustments to the generated `./internal/service/<resource_name>/tfplugingen/generator_config.yml` file.
329+
328330
#### Scaffolding Schema and Model Definitions
329331

330332
Complementary to the `scaffold` command, there is a command which generates the initial Terraform schema definition and associated Go types for a resource or data source. This processes leverages [Code Generation Tools](https://developer.hashicorp.com/terraform/plugin/code-generation) developed by HashiCorp, which in turn make use of the [Atlas Admin API](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/) OpenAPI Specification.

tools/scaffold/scaffold.go

+12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func filesToGenerate(params *ScaffoldParams) ([]FileGeneration, error) {
7575
TemplatePath: "tools/scaffold/template/model_test.tmpl",
7676
OutputPath: fmt.Sprintf("%s/model_%s_test.go", folderPath, params.NameSnakeCase),
7777
},
78+
{
79+
TemplatePath: "tools/scaffold/template/generator_config.tmpl",
80+
OutputPath: fmt.Sprintf("%s/tfplugingen/generator_config.yml", folderPath),
81+
},
7882
}, nil
7983
case DataSourceCmd:
8084
return []FileGeneration{
@@ -94,6 +98,10 @@ func filesToGenerate(params *ScaffoldParams) ([]FileGeneration, error) {
9498
TemplatePath: "tools/scaffold/template/model_test.tmpl",
9599
OutputPath: fmt.Sprintf("%s/model_%s_test.go", folderPath, params.NameSnakeCase),
96100
},
101+
{
102+
TemplatePath: "tools/scaffold/template/generator_config.tmpl",
103+
OutputPath: fmt.Sprintf("%s/tfplugingen/generator_config.yml", folderPath),
104+
},
97105
}, nil
98106
case PluralDataSourceCmd:
99107
return []FileGeneration{
@@ -113,6 +121,10 @@ func filesToGenerate(params *ScaffoldParams) ([]FileGeneration, error) {
113121
TemplatePath: "tools/scaffold/template/model_test.tmpl",
114122
OutputPath: fmt.Sprintf("%s/model_%s_test.go", folderPath, params.NameSnakeCase),
115123
},
124+
{
125+
TemplatePath: "tools/scaffold/template/generator_config.tmpl",
126+
OutputPath: fmt.Sprintf("%s/tfplugingen/generator_config.yml", folderPath),
127+
},
116128
}, nil
117129
default:
118130
return nil, errors.New("unknown generation type provided")

tools/scaffold/template/datasource.tmpl

+3-14
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,10 @@ type {{.NameCamelCase}}DS struct {
2626
config.DSCommon
2727
}
2828

29-
// TODO: if resource exists and TF{{.NamePascalCase}}Model is identical to data source attributes the existing model should be reutilized
30-
// type TF{{.NamePascalCase}}DSModel struct {
31-
// ID types.String `tfsdk:"id"`
32-
// TODO: add attribute definitions
33-
//}
3429

3530
func (d *{{.NameCamelCase}}DS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
36-
resp.Schema = schema.Schema{
37-
Attributes: map[string]schema.Attribute{
38-
"id": schema.StringAttribute{
39-
Computed: true,
40-
},
41-
// TODO: add attribute definitions
42-
},
43-
}
31+
// TODO: Schema and model must be defined in data_source_{{.NameSnakeCase}}_schema.go. Details on scaffolding this file found in CONTRIBUTING.md under "Scaffolding Schema and Model Definitions"
32+
resp.Schema = DataSourceSchema(ctx)
4433
}
4534

4635
func (d *{{.NameCamelCase}}DS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
@@ -65,4 +54,4 @@ func (d *{{.NameCamelCase}}DS) Read(ctx context.Context, req datasource.ReadRequ
6554
return
6655
}
6756
resp.Diagnostics.Append(resp.State.Set(ctx, new{{.NamePascalCase}}Model)...)
68-
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
provider:
2+
name: mongodbatlas
3+
4+
# TODO: Endpoints from Atlas Admin API must be specified for schema and model generation. Singular or plural data sources can be removed if not used.
5+
6+
resources:
7+
{{.NameSnakeCase}}:
8+
read:
9+
path: /api/atlas/v2/*
10+
method: GET
11+
create:
12+
path: /api/atlas/v2/*
13+
method: POST
14+
15+
data_sources:
16+
{{.NameSnakeCase}}:
17+
read:
18+
path: /api/atlas/v2/*
19+
method: GET
20+
{{.NameSnakeCase}}s:
21+
read:
22+
path: /api/atlas/v2/*
23+
method: GET

tools/scaffold/template/pluraldatasource.tmpl

+3-14
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,9 @@ type {{.NameCamelCase}}sDS struct {
2626
config.DSCommon
2727
}
2828

29-
type TF{{.NamePascalCase}}sDSModel struct {
30-
ID types.String `tfsdk:"id"`
31-
// TODO: add attribute definitions
32-
Results []TF{{.NamePascalCase}}DSModel `tfsdk:"results"`
33-
PageNum types.Int64 `tfsdk:"page_num"`
34-
ItemsPerPage types.Int64 `tfsdk:"items_per_page"`
35-
TotalCount types.Int64 `tfsdk:"total_count"`
36-
}
37-
3829
func (d *{{.NameCamelCase}}sDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
39-
resp.Schema = dsschema.PaginatedDSSchema(
40-
map[string]schema.Attribute{}, // TODO: define arguments of the data source
41-
map[string]schema.Attribute{}, // TODO: define attributes of the result elements, schema from singular data source can be reused
42-
)
30+
// TODO: Schema and model must be defined in data_source_{{.NameSnakeCase}}s_schema.go. Details on scaffolding this file found in CONTRIBUTING.md under "Scaffolding Schema and Model Definitions"
31+
resp.Schema = PluralDataSourceSchema(ctx)
4332
}
4433

4534
func (d *{{.NameCamelCase}}sDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
@@ -64,4 +53,4 @@ func (d *{{.NameCamelCase}}sDS) Read(ctx context.Context, req datasource.ReadReq
6453
return
6554
}
6655
resp.Diagnostics.Append(resp.State.Set(ctx, new{{.NamePascalCase}}sModel)...)
67-
}
56+
}

tools/scaffold/template/resource.tmpl

+2-13
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,9 @@ type {{.NameCamelCase}}RS struct {
2626
config.RSCommon
2727
}
2828

29-
type TF{{.NamePascalCase}}Model struct {
30-
ID types.String `tfsdk:"id"`
31-
// TODO: add attribute definitions
32-
}
33-
3429
func (r *{{.NameCamelCase}}RS) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
35-
resp.Schema = schema.Schema{
36-
Attributes: map[string]schema.Attribute{
37-
"id": schema.StringAttribute{
38-
Computed: true,
39-
},
40-
// TODO: add attribute definitions
41-
},
42-
}
30+
// TODO: Schema and model must be defined in resource_{{.NameSnakeCase}}_schema.go. Details on scaffolding this file found in CONTRIBUTING.md under "Scaffolding Schema and Model Definitions"
31+
resp.Schema = ResourceSchema(ctx)
4332
}
4433

4534
func (r *{{.NameCamelCase}}RS) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {

0 commit comments

Comments
 (0)