Skip to content

Commit d03ec83

Browse files
[Resource] [Datasource] Granular CRN for pi_placement_group and pi_spp_placement_group resources and datasources (#6325)
Co-authored-by: Alexander Kita <[email protected]>
1 parent b41b44c commit d03ec83

14 files changed

+325
-12
lines changed

ibm/service/power/data_source_ibm_pi_placement_group.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/IBM-Cloud/power-go-client/clients/instance"
1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -33,6 +34,11 @@ func DataSourceIBMPIPlacementGroup() *schema.Resource {
3334
},
3435

3536
// Attribute
37+
Attr_CRN: {
38+
Computed: true,
39+
Description: "The CRN of this resource.",
40+
Type: schema.TypeString,
41+
},
3642
Attr_Members: {
3743
Computed: true,
3844
Description: "List of server instances IDs that are members of the placement group.",
@@ -44,6 +50,13 @@ func DataSourceIBMPIPlacementGroup() *schema.Resource {
4450
Description: "The value of the group's affinity policy. Valid values are affinity and anti-affinity.",
4551
Type: schema.TypeString,
4652
},
53+
Attr_UserTags: {
54+
Computed: true,
55+
Description: "List of user tags attached to the resource.",
56+
Elem: &schema.Schema{Type: schema.TypeString},
57+
Set: schema.HashString,
58+
Type: schema.TypeSet,
59+
},
4760
},
4861
}
4962
}
@@ -65,6 +78,14 @@ func dataSourceIBMPIPlacementGroupRead(ctx context.Context, d *schema.ResourceDa
6578
}
6679

6780
d.SetId(*response.ID)
81+
if response.Crn != "" {
82+
d.Set(Attr_CRN, response.Crn)
83+
userTags, err := flex.GetGlobalTagsUsingCRN(meta, string(response.Crn), "", UserTagType)
84+
if err != nil {
85+
log.Printf("Error on get of placement group (%s) user_tags: %s", *response.ID, err)
86+
}
87+
d.Set(Attr_UserTags, userTags)
88+
}
6889
d.Set(Attr_Members, response.Members)
6990
d.Set(Attr_Policy, response.Policy)
7091

ibm/service/power/data_source_ibm_pi_placement_groups.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/IBM-Cloud/power-go-client/clients/instance"
1111
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1213
"github.com/hashicorp/go-uuid"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -33,6 +34,11 @@ func DataSourceIBMPIPlacementGroups() *schema.Resource {
3334
Computed: true,
3435
Elem: &schema.Resource{
3536
Schema: map[string]*schema.Schema{
37+
Attr_CRN: {
38+
Computed: true,
39+
Description: "The CRN of this resource.",
40+
Type: schema.TypeString,
41+
},
3642
Attr_ID: {
3743
Computed: true,
3844
Description: "The ID of the placement group.",
@@ -54,6 +60,13 @@ func DataSourceIBMPIPlacementGroups() *schema.Resource {
5460
Description: "The value of the group's affinity policy. Valid values are affinity and anti-affinity.",
5561
Type: schema.TypeString,
5662
},
63+
Attr_UserTags: {
64+
Computed: true,
65+
Description: "List of user tags attached to the resource.",
66+
Elem: &schema.Schema{Type: schema.TypeString},
67+
Set: schema.HashString,
68+
Type: schema.TypeSet,
69+
},
5770
},
5871
},
5972
},
@@ -84,6 +97,14 @@ func dataSourceIBMPIPlacementGroupsRead(ctx context.Context, d *schema.ResourceD
8497
Attr_Name: placementGroup.Name,
8598
Attr_Policy: placementGroup.Policy,
8699
}
100+
if placementGroup.Crn != "" {
101+
key[Attr_CRN] = placementGroup.Crn
102+
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(placementGroup.Crn), "", UserTagType)
103+
if err != nil {
104+
log.Printf("Error on get of placement group (%s) user_tags: %s", *placementGroup.ID, err)
105+
}
106+
key[Attr_UserTags] = tags
107+
}
87108
result = append(result, key)
88109
}
89110

ibm/service/power/data_source_ibm_pi_spp_placement_group.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package power
55

66
import (
77
"context"
8+
"log"
89

910
"github.com/IBM-Cloud/power-go-client/clients/instance"
1011
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -32,6 +34,11 @@ func DataSourceIBMPISPPPlacementGroup() *schema.Resource {
3234
},
3335

3436
// Attributes
37+
Attr_CRN: {
38+
Computed: true,
39+
Description: "The CRN of this resource.",
40+
Type: schema.TypeString,
41+
},
3542
Attr_Members: {
3643
Computed: true,
3744
Description: "List of shared processor pool IDs that are members of the placement group.",
@@ -48,6 +55,13 @@ func DataSourceIBMPISPPPlacementGroup() *schema.Resource {
4855
Description: "The value of the group's affinity policy. Valid values are affinity and anti-affinity.",
4956
Type: schema.TypeString,
5057
},
58+
Attr_UserTags: {
59+
Computed: true,
60+
Description: "List of user tags attached to the resource.",
61+
Elem: &schema.Schema{Type: schema.TypeString},
62+
Set: schema.HashString,
63+
Type: schema.TypeSet,
64+
},
5165
},
5266
}
5367
}
@@ -68,6 +82,14 @@ func dataSourceIBMPISPPPlacementGroupRead(ctx context.Context, d *schema.Resourc
6882
}
6983

7084
d.SetId(*response.ID)
85+
if response.Crn != "" {
86+
d.Set(Attr_CRN, response.Crn)
87+
userTags, err := flex.GetGlobalTagsUsingCRN(meta, string(response.Crn), "", UserTagType)
88+
if err != nil {
89+
log.Printf("Error on get of spp placement group (%s) user_tags: %s", *response.ID, err)
90+
}
91+
d.Set(Attr_UserTags, userTags)
92+
}
7193
d.Set(Attr_Members, response.MemberSharedProcessorPools)
7294
d.Set(Attr_Name, response.Name)
7395
d.Set(Attr_Policy, response.Policy)

ibm/service/power/data_source_ibm_pi_spp_placement_groups.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package power
55

66
import (
77
"context"
8+
"log"
89

910
"github.com/IBM-Cloud/power-go-client/clients/instance"
1011
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1113
"github.com/hashicorp/go-uuid"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -32,6 +34,11 @@ func DataSourceIBMPISPPPlacementGroups() *schema.Resource {
3234
Description: "List of all the shared processor pool placement groups.",
3335
Elem: &schema.Resource{
3436
Schema: map[string]*schema.Schema{
37+
Attr_CRN: {
38+
Computed: true,
39+
Description: "The CRN of this resource.",
40+
Type: schema.TypeString,
41+
},
3542
Attr_Members: {
3643
Computed: true,
3744
Elem: &schema.Schema{Type: schema.TypeString},
@@ -53,6 +60,13 @@ func DataSourceIBMPISPPPlacementGroups() *schema.Resource {
5360
Description: "The ID of the shared processor pool placement group.",
5461
Type: schema.TypeString,
5562
},
63+
Attr_UserTags: {
64+
Computed: true,
65+
Description: "List of user tags attached to the resource.",
66+
Elem: &schema.Schema{Type: schema.TypeString},
67+
Set: schema.HashString,
68+
Type: schema.TypeSet,
69+
},
5670
},
5771
},
5872
Type: schema.TypeList,
@@ -83,6 +97,14 @@ func dataSourceIBMPISPPPlacementGroupsRead(ctx context.Context, d *schema.Resour
8397
Attr_Policy: placementGroup.Policy,
8498
Attr_SPPPlacementGroupID: placementGroup.ID,
8599
}
100+
if placementGroup.Crn != "" {
101+
key[Attr_CRN] = placementGroup.Crn
102+
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(placementGroup.Crn), "", UserTagType)
103+
if err != nil {
104+
log.Printf("Error on get of spp placement group (%s) user_tags: %s", *placementGroup.ID, err)
105+
}
106+
key[Attr_UserTags] = tags
107+
}
86108
result = append(result, key)
87109
}
88110

ibm/service/power/resource_ibm_pi_placement_group.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1717
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1920
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2021
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2122
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -34,6 +35,11 @@ func ResourceIBMPIPlacementGroup() *schema.Resource {
3435
Update: schema.DefaultTimeout(60 * time.Minute),
3536
Delete: schema.DefaultTimeout(60 * time.Minute),
3637
},
38+
CustomizeDiff: customdiff.Sequence(
39+
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
40+
return flex.ResourcePowerUserTagsCustomizeDiff(diff)
41+
},
42+
),
3743

3844
Schema: map[string]*schema.Schema{
3945
// Arguments
@@ -46,18 +52,33 @@ func ResourceIBMPIPlacementGroup() *schema.Resource {
4652
},
4753
Arg_PlacementGroupName: {
4854
Description: "The name of the placement group.",
55+
ForceNew: true,
4956
Required: true,
5057
Type: schema.TypeString,
5158
ValidateFunc: validation.NoZeroValues,
5259
},
5360
Arg_PlacementGroupPolicy: {
5461
Description: "The value of the group's affinity policy. Valid values are 'affinity' and 'anti-affinity'.",
62+
ForceNew: true,
5563
Required: true,
5664
Type: schema.TypeString,
5765
ValidateFunc: validate.ValidateAllowedStringValues([]string{Affinity, AntiAffinity}),
5866
},
67+
Arg_UserTags: {
68+
Computed: true,
69+
Description: "List of user tags attached to the resource.",
70+
Elem: &schema.Schema{Type: schema.TypeString},
71+
Optional: true,
72+
Set: schema.HashString,
73+
Type: schema.TypeSet,
74+
},
5975

6076
// Attributes
77+
Attr_CRN: {
78+
Computed: true,
79+
Description: "The CRN of the resource.",
80+
Type: schema.TypeString,
81+
},
6182
Attr_Members: {
6283
Computed: true,
6384
Description: "The list of server instances IDs that are members of the placement group.",
@@ -88,12 +109,23 @@ func resourceIBMPIPlacementGroupCreate(ctx context.Context, d *schema.ResourceDa
88109
Policy: &policy,
89110
}
90111

112+
if tags, ok := d.GetOk(Arg_UserTags); ok {
113+
body.UserTags = flex.FlattenSet(tags.(*schema.Set))
114+
}
115+
91116
response, err := client.Create(body)
92117
if err != nil || response == nil {
93118
return diag.FromErr(fmt.Errorf("error creating the shared processor pool: %s", err))
94119
}
95120

96121
log.Printf("Printing the placement group %+v", &response)
122+
if response.Crn != "" {
123+
oldList, newList := d.GetChange(Arg_UserTags)
124+
err = flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(response.Crn), "", UserTagType)
125+
if err != nil {
126+
log.Printf("Error on update of pi spp placement group (%s) pi_user_tags during creation: %s", *response.ID, err)
127+
}
128+
}
97129

98130
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, *response.ID))
99131
return resourceIBMPIPlacementGroupRead(ctx, d, meta)
@@ -122,11 +154,33 @@ func resourceIBMPIPlacementGroupRead(ctx context.Context, d *schema.ResourceData
122154
d.Set(Arg_PlacementGroupPolicy, response.Policy)
123155
d.Set(Attr_Members, response.Members)
124156
d.Set(Attr_PlacementGroupID, response.ID)
157+
if response.Crn != "" {
158+
d.Set(Attr_CRN, response.Crn)
159+
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(response.Crn), "", UserTagType)
160+
if err != nil {
161+
log.Printf("Error on get of ibm pi placement group (%s) pi_user_tags: %s", *response.ID, err)
162+
}
163+
d.Set(Arg_UserTags, tags)
164+
}
125165

126166
return nil
127167
}
128168

129169
func resourceIBMPIPlacementGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
170+
_, pgID, err := splitID(d.Id())
171+
if err != nil {
172+
return diag.FromErr(err)
173+
}
174+
if d.HasChange(Arg_UserTags) {
175+
if crn, ok := d.GetOk(Attr_CRN); ok {
176+
oldList, newList := d.GetChange(Arg_UserTags)
177+
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, crn.(string), "", UserTagType)
178+
if err != nil {
179+
log.Printf("Error on update of pi placement group (%s) pi_user_tags: %s", pgID, err)
180+
}
181+
}
182+
}
183+
130184
return resourceIBMPIPlacementGroupRead(ctx, d, meta)
131185
}
132186

ibm/service/power/resource_ibm_pi_placement_group_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ func TestAccIBMPIPlacementGroupBasic(t *testing.T) {
8787
})
8888
}
8989

90+
func TestAccIBMPIPlacementGroupUserTags(t *testing.T) {
91+
pgRes := "ibm_pi_placement_group.power_placement_group"
92+
name := fmt.Sprintf("tf-pi-placement-group-%d", acctest.RandIntRange(10, 100))
93+
policy := "affinity"
94+
userTagsString := `["test_tag","env:dev"]`
95+
userTagsStringUpdated := `["test_tag","env:dev","ibm"]`
96+
resource.Test(t, resource.TestCase{
97+
PreCheck: func() { acc.TestAccPreCheck(t) },
98+
Providers: acc.TestAccProviders,
99+
CheckDestroy: testAccCheckIBMPIPlacementGroupDestroy,
100+
Steps: []resource.TestStep{
101+
{
102+
Config: testAccCheckIBMPIPlacementGroupUserTagsConfig(name, policy, userTagsString),
103+
Check: resource.ComposeTestCheckFunc(
104+
testAccCheckIBMPIPlacementGroupExists(pgRes),
105+
resource.TestCheckResourceAttr(pgRes, "pi_placement_group_name", name),
106+
resource.TestCheckResourceAttrSet(pgRes, "placement_group_id"),
107+
resource.TestCheckResourceAttr(pgRes, "crn", name),
108+
resource.TestCheckResourceAttr(pgRes, "pi_user_tags.#", "2"),
109+
resource.TestCheckTypeSetElemAttr(pgRes, "pi_user_tags.*", "env:dev"),
110+
resource.TestCheckTypeSetElemAttr(pgRes, "pi_user_tags.*", "test_tag"),
111+
),
112+
},
113+
{
114+
Config: testAccCheckIBMPIPlacementGroupUserTagsConfig(name, policy, userTagsStringUpdated),
115+
Check: resource.ComposeTestCheckFunc(
116+
testAccCheckIBMPIPlacementGroupExists(pgRes),
117+
resource.TestCheckResourceAttr(pgRes, "pi_placement_group_name", name),
118+
resource.TestCheckResourceAttrSet(pgRes, "placement_group_id"),
119+
resource.TestCheckResourceAttr(pgRes, "crn", name),
120+
resource.TestCheckResourceAttr(pgRes, "pi_user_tags.#", "3"),
121+
resource.TestCheckTypeSetElemAttr(pgRes, "pi_user_tags.*", "env:dev"),
122+
resource.TestCheckTypeSetElemAttr(pgRes, "pi_user_tags.*", "test_tag"),
123+
resource.TestCheckTypeSetElemAttr(pgRes, "pi_user_tags.*", "ibm"),
124+
),
125+
},
126+
},
127+
})
128+
}
129+
90130
func testAccCheckIBMPIPlacementGroupDestroy(s *terraform.State) error {
91131
sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession()
92132
if err != nil {
@@ -608,3 +648,13 @@ func testAccCheckIBMPIDeletePlacementGroup(name string, policy string) string {
608648
pi_placement_group_policy = "%[3]s"
609649
}`, acc.Pi_cloud_instance_id, name, policy, acc.Pi_image, acc.Pi_sap_profile_id, acc.Pi_sap_image, acc.Pi_network_name)
610650
}
651+
652+
func testAccCheckIBMPIPlacementGroupUserTagsConfig(name string, policy string, userTagsString string) string {
653+
return fmt.Sprintf(`
654+
resource "ibm_pi_placement_group" "power_placement_group" {
655+
pi_cloud_instance_id = "%[1]s"
656+
pi_placement_group_name = "%[2]s"
657+
pi_placement_group_policy = "%[3]s"
658+
pi_user_tags = %[4]s
659+
}`, acc.Pi_cloud_instance_id, name, policy, userTagsString)
660+
}

0 commit comments

Comments
 (0)