Skip to content

Commit

Permalink
Added support for migrations field for Placement Group Datasource (#1688
Browse files Browse the repository at this point in the history
)

* Added support for migrations field in Placement Group DataSrouce

* Docs

* Point to latest linodego release

* Fix docs

* Addressed PR comments
  • Loading branch information
ezilber-akamai authored Jan 13, 2025
1 parent b9c5f3b commit 5deb2e0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
10 changes: 10 additions & 0 deletions docs/data-sources/placement_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions docs/data-sources/placement_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
23 changes: 23 additions & 0 deletions linode/placementgroup/framework_datasource_schema.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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,
},
},
},
},
}
56 changes: 49 additions & 7 deletions linode/placementgroup/framework_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand All @@ -55,13 +65,45 @@ 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) {
m.LinodeID = types.Int64Value(int64(member.LinodeID))
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,
Expand Down

0 comments on commit 5deb2e0

Please sign in to comment.