From 5deb2e04079e99c3d0e0fd7fedb2cffa9fb21060 Mon Sep 17 00:00:00 2001 From: Erik Zilber Date: Mon, 13 Jan 2025 12:56:45 -0500 Subject: [PATCH] Added support for migrations field for Placement Group Datasource (#1688) * Added support for migrations field in Placement Group DataSrouce * Docs * Point to latest linodego release * Fix docs * Addressed PR comments --- docs/data-sources/placement_group.md | 10 ++++ docs/data-sources/placement_groups.md | 10 ++++ .../framework_datasource_schema.go | 23 ++++++++ linode/placementgroup/framework_models.go | 56 ++++++++++++++++--- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/docs/data-sources/placement_group.md b/docs/data-sources/placement_group.md index 5434cb32a..dee750ff6 100644 --- a/docs/data-sources/placement_group.md +++ b/docs/data-sources/placement_group.md @@ -39,6 +39,16 @@ In addition to all arguments above, the following attributes are exported: * `members` - A set of Linodes currently assigned to this Placement Group. +* `migrations` - Any Linodes that are being migrated to or from the placement group. + + * `inbound` - A list of the Linodes the system is migrating into the placement group. + + * `linode_id` - The unique identifier for the Linode being migrated into the placement group. + + * `outbound` A list of the Linodes the system is migrating out of the placement group. + + * `linode_id` - The unique identifier for the Linode being migrated out of the placement group. + ### Members Represents a single Linode assigned to a Placement Group. diff --git a/docs/data-sources/placement_groups.md b/docs/data-sources/placement_groups.md index 48b0a65e7..79ead7d06 100644 --- a/docs/data-sources/placement_groups.md +++ b/docs/data-sources/placement_groups.md @@ -66,6 +66,16 @@ Each Linode Placement Group will be stored in the `placement_groups` attribute a * `is_compliant` - Whether this Linode is currently compliant with the group's placement group type. +* `migrations` - Any Linodes that are being migrated to or from the placement group. + + * `inbound` - A list of the Linodes the system is migrating into the placement group. + + * `linode_id` - The unique identifier for the Linode being migrated into the placement group. + + * `outbound` A list of the Linodes the system is migrating out of the placement group. + + * `linode_id` - The unique identifier for the Linode being migrated out of the placement group. + ## Filterable Fields * `id` diff --git a/linode/placementgroup/framework_datasource_schema.go b/linode/placementgroup/framework_datasource_schema.go index c27f2baf7..7c245d211 100644 --- a/linode/placementgroup/framework_datasource_schema.go +++ b/linode/placementgroup/framework_datasource_schema.go @@ -1,9 +1,17 @@ package placementgroup import ( + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" ) +var pgMigrationInstanceObjectType = types.ObjectType{ + AttrTypes: map[string]attr.Type{ + "linode_id": types.Int64Type, + }, +} + var DataSourceSchema = schema.Schema{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ @@ -47,5 +55,20 @@ var DataSourceSchema = schema.Schema{ }, }, }, + "migrations": schema.SingleNestedBlock{ + Description: "An object representing migrations associated with the placement group. Contains inbound and outbound migration lists.", + Attributes: map[string]schema.Attribute{ + "inbound": schema.ListAttribute{ + Description: "The compute instances being migrated into the placement group.", + Computed: true, + ElementType: pgMigrationInstanceObjectType, + }, + "outbound": schema.ListAttribute{ + Description: "The compute instances being migrated out of the placement group.", + Computed: true, + ElementType: pgMigrationInstanceObjectType, + }, + }, + }, }, } diff --git a/linode/placementgroup/framework_models.go b/linode/placementgroup/framework_models.go index 8b142f4c1..372ca1792 100644 --- a/linode/placementgroup/framework_models.go +++ b/linode/placementgroup/framework_models.go @@ -12,13 +12,14 @@ import ( ) type PlacementGroupDataSourceModel struct { - ID types.Int64 `tfsdk:"id"` - Label types.String `tfsdk:"label"` - Region types.String `tfsdk:"region"` - PlacementGroupType types.String `tfsdk:"placement_group_type"` - IsCompliant types.Bool `tfsdk:"is_compliant"` - PlacementGroupPolicy types.String `tfsdk:"placement_group_policy"` - Members []PlacementGroupMemberModel `tfsdk:"members"` + ID types.Int64 `tfsdk:"id"` + Label types.String `tfsdk:"label"` + Region types.String `tfsdk:"region"` + PlacementGroupType types.String `tfsdk:"placement_group_type"` + IsCompliant types.Bool `tfsdk:"is_compliant"` + PlacementGroupPolicy types.String `tfsdk:"placement_group_policy"` + Members []PlacementGroupMemberModel `tfsdk:"members"` + Migrations *PlacementGroupMigrationModel `tfsdk:"migrations"` } type PlacementGroupResourceModel struct { @@ -32,6 +33,15 @@ type PlacementGroupResourceModel struct { Members types.Set `tfsdk:"members"` } +type PlacementGroupMigrationModel struct { + Inbound []PlacementGroupMigrationInstanceModel `tfsdk:"inbound"` + Outbound []PlacementGroupMigrationInstanceModel `tfsdk:"outbound"` +} + +type PlacementGroupMigrationInstanceModel struct { + LinodeID types.Int64 `tfsdk:"linode_id"` +} + type PlacementGroupMemberModel struct { LinodeID types.Int64 `tfsdk:"linode_id"` IsCompliant types.Bool `tfsdk:"is_compliant"` @@ -55,6 +65,14 @@ func (data *PlacementGroupDataSourceModel) ParsePlacementGroup( } data.Members = members + + migrations := pg.Migrations + + if migrations != nil { + pgMigrations := new(PlacementGroupMigrationModel) + pgMigrations.FlattenMigrations(*migrations) + data.Migrations = pgMigrations + } } func (m *PlacementGroupMemberModel) FlattenMember(member linodego.PlacementGroupMember) { @@ -62,6 +80,30 @@ func (m *PlacementGroupMemberModel) FlattenMember(member linodego.PlacementGroup m.IsCompliant = types.BoolValue(member.IsCompliant) } +func (m *PlacementGroupMigrationModel) FlattenMigrations(migrations linodego.PlacementGroupMigrations) { + inbound := make([]PlacementGroupMigrationInstanceModel, len(migrations.Inbound)) + outbound := make([]PlacementGroupMigrationInstanceModel, len(migrations.Outbound)) + + for i, instance := range migrations.Inbound { + var m PlacementGroupMigrationInstanceModel + m.FlattenMigrationInstance(instance) + inbound[i] = m + } + + for i, instance := range migrations.Outbound { + var m PlacementGroupMigrationInstanceModel + m.FlattenMigrationInstance(instance) + outbound[i] = m + } + + m.Inbound = inbound + m.Outbound = outbound +} + +func (m *PlacementGroupMigrationInstanceModel) FlattenMigrationInstance(migrationInstance linodego.PlacementGroupMigrationInstance) { + m.LinodeID = types.Int64Value(int64(migrationInstance.LinodeID)) +} + func (m *PlacementGroupResourceModel) FlattenPlacementGroup( ctx context.Context, pg *linodego.PlacementGroup,