@@ -3,15 +3,15 @@ package provider
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "errors"
7
6
"fmt"
8
7
"path"
9
- "strings"
10
8
11
9
"github.com/ansible/terraform-provider-aap/internal/provider/customtypes"
10
+ "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
12
11
"github.com/hashicorp/terraform-plugin-framework/datasource"
13
12
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
14
13
"github.com/hashicorp/terraform-plugin-framework/diag"
14
+ tfpath "github.com/hashicorp/terraform-plugin-framework/path"
15
15
"github.com/hashicorp/terraform-plugin-framework/types"
16
16
)
17
17
@@ -46,8 +46,10 @@ type JobTemplateDataSource struct {
46
46
47
47
// Ensure the implementation satisfies the expected interfaces.
48
48
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 {}
51
53
)
52
54
53
55
// NewJobTemplateDataSource is a helper function to simplify the provider implementation.
@@ -115,7 +117,8 @@ func (d *JobTemplateDataSource) Read(ctx context.Context, req datasource.ReadReq
115
117
return
116
118
}
117
119
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 )
119
122
if err != nil {
120
123
resp .Diagnostics .AddError ("Minimal Data Not Supplied" , "Require [id] or [name and organization_name]" )
121
124
return
@@ -159,6 +162,61 @@ func (d *JobTemplateDataSource) Configure(_ context.Context, req datasource.Conf
159
162
d .client = client
160
163
}
161
164
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
+
162
220
func (dm * JobTemplateDataSourceModel ) ParseHttpResponse (body []byte ) diag.Diagnostics {
163
221
var diags diag.Diagnostics
164
222
@@ -182,17 +240,3 @@ func (dm *JobTemplateDataSourceModel) ParseHttpResponse(body []byte) diag.Diagno
182
240
183
241
return diags
184
242
}
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
- }
0 commit comments