From 092e02a1449fdb171f88668379265717be52de82 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 27 Nov 2024 11:59:40 +0000 Subject: [PATCH] Add serial number to system datasource May be useful to have for server discovery. --- internal/datasources/system/data_source.go | 34 ++++++++++++++++++++++ test/system/system_test.go | 9 ++++++ 2 files changed, 43 insertions(+) diff --git a/internal/datasources/system/data_source.go b/internal/datasources/system/data_source.go index 7e4d238..d85cfcf 100644 --- a/internal/datasources/system/data_source.go +++ b/internal/datasources/system/data_source.go @@ -12,6 +12,7 @@ import ( "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/client" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/constants" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/systems/privatecloudbusiness" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -156,8 +157,41 @@ func (s *DataSource) Read( return } + // TODO: Switch to GetStorageSerial() when FF-31483 is fixed + serial := system.GetStorageSystem().GetAdditionalData()["serialNumber"] + if serial == nil { + resp.Diagnostics.AddError( + "error reading system", + "storage serial number is nil", + ) + + return + } + serialNumber, ok := serial.(*string) + if !ok { + resp.Diagnostics.AddError( + "error reading system", + "storage serial number cannot be cast to *string", + ) + + return + } + data.Name = types.StringValue(*name) data.Id = types.StringValue(*id) + value := map[string]attr.Value{ + "storage_serial": types.StringValue(*serialNumber), + } + storageSystem, diags := NewStorageSystemValue( + data.StorageSystem.AttributeTypes(ctx), value, + ) + + resp.Diagnostics.Append((diags)...) + if resp.Diagnostics.HasError() { + return + } + data.StorageSystem = storageSystem + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } diff --git a/test/system/system_test.go b/test/system/system_test.go index f5b6c23..e7cf5f9 100644 --- a/test/system/system_test.go +++ b/test/system/system_test.go @@ -76,6 +76,10 @@ func TestAccSystemDataSource(t *testing.T) { "array-5305-grp", ), checkUUIDAttr("data.hpegl_pc_system.test", "id"), + resource.TestCheckResourceAttrSet( + "data.hpegl_pc_system.test", + "storage_system.storage_serial", + ), } if simulation { @@ -86,6 +90,11 @@ func TestAccSystemDataSource(t *testing.T) { "id", "126fd201-9e6e-5e31-9ffb-a766265b1fd3", ), + resource.TestCheckResourceAttr( + "data.hpegl_pc_system.test", + "storage_system.storage_serial", + "AF-836032", + ), ) }