Skip to content

Commit d98e8b8

Browse files
committed
Create BaseAAPDataSourceModel for shared fields
1 parent 3d86b7a commit d98e8b8

File tree

3 files changed

+69
-90
lines changed

3 files changed

+69
-90
lines changed

internal/provider/base_data_source.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/datasource"
7+
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
)
10+
11+
type BaseAAPDataSource struct {
12+
Id types.Int64 `tfsdk:"id"`
13+
OrganizationName types.String `tfsdk:"organization_name"`
14+
Name types.String `tfsdk:"name"`
15+
}
16+
17+
func ValidateAAPDataSourceConfig(data BaseAAPDataSource, ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
18+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
19+
20+
if resp.Diagnostics.HasError() {
21+
return
22+
}
23+
24+
if IsValueProvided(data.Id) {
25+
return
26+
}
27+
28+
if IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
29+
return
30+
}
31+
32+
if !IsValueProvided(data.Id) && !IsValueProvided(data.Name) {
33+
resp.Diagnostics.AddAttributeWarning(
34+
tfpath.Root("id"),
35+
"Missing Attribute Configuration",
36+
"Expected either [id] or [name + organization_name] pair",
37+
)
38+
}
39+
40+
if IsValueProvided(data.Name) && !IsValueProvided(data.OrganizationName) {
41+
resp.Diagnostics.AddAttributeWarning(
42+
tfpath.Root("organization_name"),
43+
"Missing Attribute Configuration",
44+
"Expected organization_name to be configured with name.",
45+
)
46+
}
47+
48+
if !IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
49+
resp.Diagnostics.AddAttributeWarning(
50+
tfpath.Root("name"),
51+
"Missing Attribute Configuration",
52+
"Expected name to be configured with organization_name.",
53+
)
54+
}
55+
}

internal/provider/inventory_data_source.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import (
1717

1818
// inventoryDataSourceModel maps the data source schema data.
1919
type InventoryDataSourceModel struct {
20-
Id types.Int64 `tfsdk:"id"`
21-
Organization types.Int64 `tfsdk:"organization"`
22-
OrganizationName types.String `tfsdk:"organization_name"`
23-
Url types.String `tfsdk:"url"`
24-
NamedUrl types.String `tfsdk:"named_url"`
25-
Name types.String `tfsdk:"name"`
26-
Description types.String `tfsdk:"description"`
27-
Variables customtypes.AAPCustomStringValue `tfsdk:"variables"`
20+
BaseAAPDataSource
21+
Organization types.Int64 `tfsdk:"organization"`
22+
Url types.String `tfsdk:"url"`
23+
NamedUrl types.String `tfsdk:"named_url"`
24+
Description types.String `tfsdk:"description"`
25+
Variables customtypes.AAPCustomStringValue `tfsdk:"variables"`
2826
}
2927

3028
// InventoryDataSource is the data source implementation.
@@ -166,43 +164,7 @@ func (d *InventoryDataSource) ConfigValidators(_ context.Context) []datasource.C
166164
func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
167165
var data InventoryDataSourceModel
168166

169-
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
170-
171-
if resp.Diagnostics.HasError() {
172-
return
173-
}
174-
175-
if IsValueProvided(data.Id) {
176-
return
177-
}
178-
179-
if IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
180-
return
181-
}
182-
183-
if !IsValueProvided(data.Id) && !IsValueProvided(data.Name) {
184-
resp.Diagnostics.AddAttributeWarning(
185-
tfpath.Root("id"),
186-
"Missing Attribute Configuration",
187-
"Expected either [id] or [name + organization_name] pair",
188-
)
189-
}
190-
191-
if IsValueProvided(data.Name) && !IsValueProvided(data.OrganizationName) {
192-
resp.Diagnostics.AddAttributeWarning(
193-
tfpath.Root("organization_name"),
194-
"Missing Attribute Configuration",
195-
"Expected organization_name to be configured with name.",
196-
)
197-
}
198-
199-
if !IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
200-
resp.Diagnostics.AddAttributeWarning(
201-
tfpath.Root("name"),
202-
"Missing Attribute Configuration",
203-
"Expected name to be configured with organization_name.",
204-
)
205-
}
167+
ValidateAAPDataSourceConfig(data.BaseAAPDataSource, ctx, req, resp)
206168
}
207169

208170
func (dm *InventoryDataSourceModel) ParseHttpResponse(body []byte) diag.Diagnostics {

internal/provider/job_template_data_source.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ type JobTemplateAPIModel struct {
2929

3030
// JobTemplateDataSourceModel maps the data source schema data.
3131
type JobTemplateDataSourceModel struct {
32-
Id types.Int64 `tfsdk:"id"`
33-
Organization types.Int64 `tfsdk:"organization"`
34-
OrganizationName types.String `tfsdk:"organization_name"`
35-
Url types.String `tfsdk:"url"`
36-
NamedUrl types.String `tfsdk:"named_url"`
37-
Name types.String `tfsdk:"name"`
38-
Description types.String `tfsdk:"description"`
39-
Variables customtypes.AAPCustomStringValue `tfsdk:"variables"`
32+
BaseAAPDataSource
33+
Organization types.Int64 `tfsdk:"organization"`
34+
Url types.String `tfsdk:"url"`
35+
NamedUrl types.String `tfsdk:"named_url"`
36+
Description types.String `tfsdk:"description"`
37+
Variables customtypes.AAPCustomStringValue `tfsdk:"variables"`
4038
}
4139

4240
// JobTemplateDataSource is the data source implementation.
@@ -178,43 +176,7 @@ func (d *JobTemplateDataSource) ConfigValidators(_ context.Context) []datasource
178176
func (d *JobTemplateDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
179177
var data JobTemplateDataSourceModel
180178

181-
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
182-
183-
if resp.Diagnostics.HasError() {
184-
return
185-
}
186-
187-
if IsValueProvided(data.Id) {
188-
return
189-
}
190-
191-
if IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
192-
return
193-
}
194-
195-
if !IsValueProvided(data.Id) && !IsValueProvided(data.Name) {
196-
resp.Diagnostics.AddAttributeWarning(
197-
tfpath.Root("id"),
198-
"Missing Attribute Configuration",
199-
"Expected either [id] or [name + organization_name] pair",
200-
)
201-
}
202-
203-
if IsValueProvided(data.Name) && !IsValueProvided(data.OrganizationName) {
204-
resp.Diagnostics.AddAttributeWarning(
205-
tfpath.Root("organization_name"),
206-
"Missing Attribute Configuration",
207-
"Expected organization_name to be configured with name.",
208-
)
209-
}
210-
211-
if !IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
212-
resp.Diagnostics.AddAttributeWarning(
213-
tfpath.Root("name"),
214-
"Missing Attribute Configuration",
215-
"Expected name to be configured with organization_name.",
216-
)
217-
}
179+
ValidateAAPDataSourceConfig(data.BaseAAPDataSource, ctx, req, resp)
218180
}
219181

220182
func (dm *JobTemplateDataSourceModel) ParseHttpResponse(body []byte) diag.Diagnostics {

0 commit comments

Comments
 (0)