Skip to content

Commit 209a3e5

Browse files
committed
Update inventory docs
1 parent dc7d667 commit 209a3e5

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

docs/data-sources/inventory.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ output "inventory_details" {
4545
<!-- schema generated by tfplugindocs -->
4646
## Schema
4747

48-
### Required
48+
### Optional
4949

5050
- `id` (Number) Inventory id
51+
- `name` (String) Name of the inventory
52+
- `organization_name` (String) The name for the organization to which the inventory belongs
5153

5254
### Read-Only
5355

5456
- `description` (String) Description of the inventory
55-
- `name` (String) Name of the inventory
57+
- `named_url` (String) The Named Url of the inventory
5658
- `organization` (Number) Identifier for the organization to which the inventory belongs
5759
- `url` (String) Url of the inventory
5860
- `variables` (String) Variables of the inventory. Will be either JSON or YAML string depending on how the variables were entered into AAP.

docs/resources/inventory.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,6 @@ output "inventory_xyz" {
111111
### Read-Only
112112

113113
- `id` (Number) Inventory id
114+
- `named_url` (String) Named URL of the inventory
115+
- `organization_name` (String) Name for the organization.
114116
- `url` (String) URL of the inventory

internal/provider/inventory_data_source.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1313
"github.com/hashicorp/terraform-plugin-framework/diag"
1414
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
15-
"github.com/hashicorp/terraform-plugin-framework/provider"
1615
"github.com/hashicorp/terraform-plugin-framework/types"
1716
)
1817

@@ -38,6 +37,7 @@ var (
3837
_ datasource.DataSource = &InventoryDataSource{}
3938
_ datasource.DataSourceWithConfigure = &InventoryDataSource{}
4039
_ datasource.DataSourceWithConfigValidators = &InventoryDataSource{}
40+
_ datasource.DataSourceWithValidateConfig = &InventoryDataSource{}
4141
)
4242

4343
// NewInventoryDataSource is a helper function to simplify the provider implementation.
@@ -105,9 +105,8 @@ func (d *InventoryDataSource) Read(ctx context.Context, req datasource.ReadReque
105105
return
106106
}
107107

108-
URI := path.Join(d.client.getApiEndpoint(), "inventories")
109-
resourceURL, err := ReturnAAPNamedURL(state.Id, state.Name, state.OrganizationName, URI)
110-
// resourceURL, err := state.ResourceUrlFromParameters(d)
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
@@ -164,7 +163,7 @@ func (d *InventoryDataSource) ConfigValidators(_ context.Context) []datasource.C
164163
}
165164
}
166165

167-
func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) {
166+
func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
168167
var data InventoryDataSourceModel
169168

170169
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
@@ -181,30 +180,29 @@ func (d *InventoryDataSource) ValidateConfig(ctx context.Context, req provider.V
181180
return
182181
}
183182

184-
if IsValueNotProvided(data.Id) && IsValueNotProvided(data.Name) {
183+
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
}
191190

192-
if IsValueProvided(data.Name) && IsValueNotProvided(data.OrganizationName) {
191+
if IsValueProvided(data.Name) && !IsValueProvided(data.OrganizationName) {
193192
resp.Diagnostics.AddAttributeWarning(
194193
tfpath.Root("organization_name"),
195194
"Missing Attribute Configuration",
196195
"Expected organization_name to be configured with name.",
197196
)
198197
}
199198

200-
if IsValueNotProvided(data.Name) && IsValueProvided(data.OrganizationName) {
199+
if !IsValueProvided(data.Name) && IsValueProvided(data.OrganizationName) {
201200
resp.Diagnostics.AddAttributeWarning(
202201
tfpath.Root("name"),
203202
"Missing Attribute Configuration",
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.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (r *InventoryResourceModel) generateRequestBody() ([]byte, diag.Diagnostics
291291
organizationId = r.Organization.ValueInt64()
292292
}
293293

294-
if IsValueNotProvided(r.NamedUrl) {
294+
if !IsValueProvided(r.NamedUrl) {
295295
namedURL, err := ReturnAAPNamedURL(r.Id, r.Name, r.OrganizationName, "inventories")
296296
// Squashing error here. If we can't generate the named url just leave it blank
297297
if err == nil {

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/utils.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +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
22-
} else if IsValueProvided(name) && IsValueProvided(orgName) {
21+
return path.Join(uri, id.String()), nil
22+
}
23+
24+
if IsValueProvided(name) && IsValueProvided(orgName) {
2325
namedUrl := fmt.Sprintf("%s++%s", name.ValueString(), orgName.ValueString())
24-
return path.Join(URI, namedUrl), nil
25-
} else {
26-
return "", errors.New("invalid lookup parameters")
26+
return path.Join(uri, namedUrl), nil
2727
}
28-
}
2928

30-
func IsValueNotProvided(value attr.Value) bool {
31-
return value.IsNull() || value.IsUnknown()
29+
return "", errors.New("invalid lookup parameters")
3230
}
3331

3432
func IsValueProvided(value attr.Value) bool {

0 commit comments

Comments
 (0)