Skip to content

Commit

Permalink
Merge pull request #364 from Juniper/bug/363-system-data-source-id-field
Browse files Browse the repository at this point in the history
Bugfix: Populate missing name/id attributes in `apstra_datacenter_system` data source
  • Loading branch information
chrismarget-j authored Sep 27, 2023
2 parents 5da53eb + e91c678 commit 169160a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apstra/blueprint/node_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
14 changes: 13 additions & 1 deletion apstra/data_source_datacenter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)...)
}

0 comments on commit 169160a

Please sign in to comment.