From e91c6787e95a63487be8833973c499f15163005d Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Tue, 26 Sep 2023 18:13:04 -0400 Subject: [PATCH] fill missing name/id attributes in system data source --- apstra/blueprint/node_system.go | 2 +- apstra/data_source_datacenter_system.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apstra/blueprint/node_system.go b/apstra/blueprint/node_system.go index 1e67983e..c2b84061 100644 --- a/apstra/blueprint/node_system.go +++ b/apstra/blueprint/node_system.go @@ -47,7 +47,7 @@ func (o NodeTypeSystem) DataSourceAttributes() map[string]dataSourceSchema.Attri } } -func (o *NodeTypeSystem) ReadFromApi(ctx context.Context, client *apstra.Client, diags *diag.Diagnostics) { +func (o *NodeTypeSystem) AttributesFromApi(ctx context.Context, client *apstra.Client, diags *diag.Diagnostics) { type node struct { Id string `json:"id"` Hostname string `json:"hostname"` diff --git a/apstra/data_source_datacenter_system.go b/apstra/data_source_datacenter_system.go index a8895579..5d7fdef0 100644 --- a/apstra/data_source_datacenter_system.go +++ b/apstra/data_source_datacenter_system.go @@ -6,6 +6,7 @@ import ( "github.com/Juniper/terraform-provider-apstra/apstra/blueprint" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" ) var _ datasource.DataSourceWithConfigure = &dataSourceDatacenterSystemNode{} @@ -37,11 +38,22 @@ func (o *dataSourceDatacenterSystemNode) Read(ctx context.Context, req datasourc return } - config.ReadFromApi(ctx, o.client, &resp.Diagnostics) + // read "attributes" object element from the API + config.AttributesFromApi(ctx, o.client, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } + // if the user supplied "id", then "name" will be null. Fill it in. + if config.Name.IsNull() { + config.Name = config.Attributes.Attributes()["name"].(basetypes.StringValue) + } + + // if the user supplied "name", then "id" will be null. Fill it in. + if config.Id.IsNull() { + config.Id = config.Attributes.Attributes()["id"].(basetypes.StringValue) + } + // set state resp.Diagnostics.Append(resp.State.Set(ctx, &config)...) }