Skip to content

Commit 3d86b7a

Browse files
committed
Update Job and Job Template resources
1 parent 74cc54c commit 3d86b7a

File tree

7 files changed

+72
-529
lines changed

7 files changed

+72
-529
lines changed

internal/provider/inventory_data_source.go

Lines changed: 3 additions & 5 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
@@ -184,7 +183,7 @@ func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req datasource
184183
if !IsValueProvided(data.Id) && !IsValueProvided(data.Name) {
185184
resp.Diagnostics.AddAttributeWarning(
186185
tfpath.Root("id"),
187-
"Missing Atribute Configuration",
186+
"Missing Attribute Configuration",
188187
"Expected either [id] or [name + organization_name] pair",
189188
)
190189
}
@@ -204,7 +203,6 @@ func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req datasource
204203
"Expected name to be configured with organization_name.",
205204
)
206205
}
207-
208206
}
209207

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

internal/provider/inventory_resource_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func TestInventoryResourceGenerateRequestBody(t *testing.T) {
8686
Variables: customtypes.NewAAPCustomStringValue("{\"foo\": \"bar\", \"nested\": {\"foobar\": \"baz\"}}"),
8787
},
8888
expected: []byte(
89-
`{"organization":2,"summary_fields":{"organization":{"id":2,"name":"test organization"},"inventory":{"id":1,"name":"test inventory"}},"related":{"named_url":"inventories/1"},"name":"test inventory","description":"A test inventory for testing","variables":"{\"foo\": \"bar\", \"nested\": {\"foobar\": \"baz\"}}"}`,
89+
`{"organization":2,"summary_fields":{"organization":{"id":2,"name":"test organization"},"inventory":{"id":1,"name":"test inventory"}},` +
90+
`"related":{"named_url":"inventories/1"},"name":"test inventory","description":"A test inventory for testing",` +
91+
`"variables":"{\"foo\": \"bar\", \"nested\": {\"foobar\": \"baz\"}}"}`,
9092
),
9193
},
9294
}

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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ func (p *aapProvider) DataSources(_ context.Context) []func() datasource.DataSou
146146
return []func() datasource.DataSource{
147147
NewInventoryDataSource,
148148
NewJobTemplateDataSource,
149-
NewWorkflowJobTemplateDataSource,
150149
}
151150
}
152151

@@ -155,7 +154,6 @@ func (p *aapProvider) Resources(_ context.Context) []func() resource.Resource {
155154
return []func() resource.Resource{
156155
NewInventoryResource,
157156
NewJobResource,
158-
NewWorkflowJobResource,
159157
NewGroupResource,
160158
NewHostResource,
161159
}

internal/provider/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ import (
1616
"github.com/hashicorp/terraform-plugin-framework/types"
1717
)
1818

19-
func ReturnAAPNamedURL(id types.Int64, name types.String, orgName types.String, URI string) (string, error) {
19+
func ReturnAAPNamedURL(id types.Int64, name types.String, orgName types.String, uri string) (string, error) {
2020
if IsValueProvided(id) {
21-
return path.Join(URI, id.String()), nil
21+
return path.Join(uri, id.String()), nil
2222
}
2323

2424
if IsValueProvided(name) && IsValueProvided(orgName) {
2525
namedUrl := fmt.Sprintf("%s++%s", name.ValueString(), orgName.ValueString())
26-
return path.Join(URI, namedUrl), nil
26+
return path.Join(uri, namedUrl), nil
2727
}
2828

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

3332
func IsValueProvided(value attr.Value) bool {

0 commit comments

Comments
 (0)