Skip to content

Commit 153a98a

Browse files
committed
Update Job and Job Template resources
1 parent 077d62a commit 153a98a

File tree

5 files changed

+66
-222
lines changed

5 files changed

+66
-222
lines changed

internal/provider/inventory_data_source.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func (d *InventoryDataSource) Schema(_ context.Context, _ datasource.SchemaReque
9696

9797
// Read refreshes the Terraform state with the latest data.
9898
func (d *InventoryDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
99-
10099
var state InventoryDataSourceModel
101100
var diags diag.Diagnostics
102101

@@ -106,8 +105,8 @@ func (d *InventoryDataSource) Read(ctx context.Context, req datasource.ReadReque
106105
return
107106
}
108107

109-
URI := path.Join(d.client.getApiEndpoint(), "inventories")
110-
resourceURL, err := ReturnAAPNamedURL(state.Id, state.Name, state.OrganizationName, URI)
108+
uri := path.Join(d.client.getApiEndpoint(), "inventories")
109+
resourceURL, err := ReturnAAPNamedURL(state.Id, state.Name, state.OrganizationName, uri)
111110
if err != nil {
112111
resp.Diagnostics.AddError("Minimal Data Not Supplied", "Expected either [id] or [name + organization_name] pair")
113112
return

internal/provider/job_template_data_source.go

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package provider
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"path"
9-
"strings"
108

119
"github.com/ansible/terraform-provider-aap/internal/provider/customtypes"
10+
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
1211
"github.com/hashicorp/terraform-plugin-framework/datasource"
1312
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1413
"github.com/hashicorp/terraform-plugin-framework/diag"
14+
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
1515
"github.com/hashicorp/terraform-plugin-framework/types"
1616
)
1717

@@ -46,8 +46,10 @@ type JobTemplateDataSource struct {
4646

4747
// Ensure the implementation satisfies the expected interfaces.
4848
var (
49-
_ datasource.DataSource = &JobTemplateDataSource{}
50-
_ datasource.DataSourceWithConfigure = &JobTemplateDataSource{}
49+
_ datasource.DataSource = &JobTemplateDataSource{}
50+
_ datasource.DataSourceWithConfigure = &JobTemplateDataSource{}
51+
_ datasource.DataSourceWithConfigValidators = &JobTemplateDataSource{}
52+
_ datasource.DataSourceWithValidateConfig = &JobTemplateDataSource{}
5153
)
5254

5355
// NewJobTemplateDataSource is a helper function to simplify the provider implementation.
@@ -115,7 +117,8 @@ func (d *JobTemplateDataSource) Read(ctx context.Context, req datasource.ReadReq
115117
return
116118
}
117119

118-
resourceURL, err := state.ValidateLookupParameters(d)
120+
uri := path.Join(d.client.getApiEndpoint(), "inventories")
121+
resourceURL, err := ReturnAAPNamedURL(state.Id, state.Name, state.OrganizationName, uri)
119122
if err != nil {
120123
resp.Diagnostics.AddError("Minimal Data Not Supplied", "Require [id] or [name and organization_name]")
121124
return
@@ -159,6 +162,61 @@ func (d *JobTemplateDataSource) Configure(_ context.Context, req datasource.Conf
159162
d.client = client
160163
}
161164

165+
func (d *JobTemplateDataSource) ConfigValidators(_ context.Context) []datasource.ConfigValidator {
166+
// You have at least an id or a name + organization_name pair
167+
return []datasource.ConfigValidator{
168+
datasourcevalidator.Any(
169+
datasourcevalidator.AtLeastOneOf(
170+
tfpath.MatchRoot("id")),
171+
datasourcevalidator.RequiredTogether(
172+
tfpath.MatchRoot("name"),
173+
tfpath.MatchRoot("organization_name")),
174+
),
175+
}
176+
}
177+
178+
func (d *JobTemplateDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
179+
var data JobTemplateDataSourceModel
180+
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+
}
218+
}
219+
162220
func (dm *JobTemplateDataSourceModel) ParseHttpResponse(body []byte) diag.Diagnostics {
163221
var diags diag.Diagnostics
164222

@@ -182,17 +240,3 @@ func (dm *JobTemplateDataSourceModel) ParseHttpResponse(body []byte) diag.Diagno
182240

183241
return diags
184242
}
185-
186-
// ValidateLookupParameters Validate the provided lookup parameters and return the appropriate resource url
187-
func (dm *JobTemplateDataSourceModel) ValidateLookupParameters(datasource *JobTemplateDataSource) (string, error) {
188-
// Here is where we can get the "named" JobTemplate, which is "JobTemplate Name"++"Organization Name" to derive uniqueness
189-
// we will take precedence if the Id is set to use that over the named_url attempt.
190-
if dm.Id != types.Int64Null() {
191-
return path.Join(datasource.client.getApiEndpoint(), "job_templates", dm.Id.String()), nil
192-
} else if dm.Name != types.StringNull() && dm.OrganizationName != types.StringNull() {
193-
namedUrl := strings.Join([]string{dm.Name.String()[1 : len(dm.Name.String())-1], "++", dm.OrganizationName.String()[1 : len(dm.OrganizationName.String())-1]}, "")
194-
return path.Join(datasource.client.getApiEndpoint(), "job_templates", namedUrl), nil
195-
} else {
196-
return types.StringNull().String(), errors.New("invalid job lookup parameters")
197-
}
198-
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func (p *aapProvider) Configure(ctx context.Context, req provider.ConfigureReque
145145
func (p *aapProvider) DataSources(_ context.Context) []func() datasource.DataSource {
146146
return []func() datasource.DataSource{
147147
NewInventoryDataSource,
148+
NewJobTemplateDataSource,
148149
}
149150
}
150151

internal/provider/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func ReturnAAPNamedURL(id types.Int64, name types.String, orgName types.String,
2727
}
2828

2929
return "", errors.New("invalid lookup parameters")
30-
3130
}
3231

3332
func IsValueProvided(value attr.Value) bool {

internal/provider/workflow_job_template_data_source.go

Lines changed: 0 additions & 199 deletions
This file was deleted.

0 commit comments

Comments
 (0)