Skip to content

Commit

Permalink
added pagination support for members and removed depricated function …
Browse files Browse the repository at this point in the history
…with context resource func
  • Loading branch information
Narinder committed Oct 25, 2021
1 parent c83185d commit ce546eb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 48 deletions.
7 changes: 2 additions & 5 deletions ibm/data_source_ibm_iam_access_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (

func dataSourceIBMIAMAccessGroup() *schema.Resource {
return &schema.Resource{
Read: dataIBMIAMAccessGroupRead,
Exists: resourceIBMIAMAccessGroupExists,
Importer: &schema.ResourceImporter{},

Read: dataIBMIAMAccessGroupRead,
Schema: map[string]*schema.Schema{
"access_group_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -197,7 +194,7 @@ func dataIBMIAMAccessGroupRead(d *schema.ResourceData, meta interface{}) error {
if err != nil {
log.Printf("Error retrieving access group rules: %s. API Response: %s", err, detailedResponse)
}
ibmID, serviceID := flattenMembersData(members, res, allrecs)
ibmID, serviceID := flattenMembersData(members.Members, res, allrecs)

grpInstance := map[string]interface{}{
"id": grp.ID,
Expand Down
61 changes: 22 additions & 39 deletions ibm/resource_ibm_iam_access_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
package ibm

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceIBMIAMAccessGroup() *schema.Resource {
return &schema.Resource{
Create: resourceIBMIAMAccessGroupCreate,
Read: resourceIBMIAMAccessGroupRead,
Update: resourceIBMIAMAccessGroupUpdate,
Delete: resourceIBMIAMAccessGroupDelete,
Exists: resourceIBMIAMAccessGroupExists,
Importer: &schema.ResourceImporter{},
CreateContext: resourceIBMIAMAccessGroupCreate,
ReadContext: resourceIBMIAMAccessGroupRead,
UpdateContext: resourceIBMIAMAccessGroupUpdate,
DeleteContext: resourceIBMIAMAccessGroupDelete,
Importer: &schema.ResourceImporter{},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -45,15 +46,15 @@ func resourceIBMIAMAccessGroup() *schema.Resource {
}
}

func resourceIBMIAMAccessGroupCreate(d *schema.ResourceData, meta interface{}) error {
func resourceIBMIAMAccessGroupCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAccessGroupsClient, err := meta.(ClientSession).IAMAccessGroupsV2()
if err != nil {
return err
return diag.FromErr(err)
}

userDetails, err := meta.(ClientSession).BluemixUserDetails()
if err != nil {
return err
return diag.FromErr(err)
}

name := d.Get("name").(string)
Expand All @@ -64,24 +65,24 @@ func resourceIBMIAMAccessGroupCreate(d *schema.ResourceData, meta interface{}) e
}
agrp, detailedResponse, err := iamAccessGroupsClient.CreateAccessGroup(creatAccessGroupOptions)
if err != nil || agrp == nil {
return fmt.Errorf("Error creating access group: %s. API Response: %s", err, detailedResponse)
return diag.FromErr(fmt.Errorf("Error creating access group: %s. API Response: %s", err, detailedResponse))
}

d.SetId(*agrp.ID)

return resourceIBMIAMAccessGroupRead(d, meta)
return resourceIBMIAMAccessGroupRead(context, d, meta)
}

func resourceIBMIAMAccessGroupRead(d *schema.ResourceData, meta interface{}) error {
func resourceIBMIAMAccessGroupRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAccessGroupsClient, err := meta.(ClientSession).IAMAccessGroupsV2()
if err != nil {
return err
return diag.FromErr(err)
}
agrpID := d.Id()
getAccessGroupOptions := iamAccessGroupsClient.NewGetAccessGroupOptions(agrpID)
agrp, detailedResponse, err := iamAccessGroupsClient.GetAccessGroup(getAccessGroupOptions)
if err != nil || agrp == nil {
return fmt.Errorf("Error retrieving access group: %s. API Response: %s", err, detailedResponse)
return diag.FromErr(fmt.Errorf("Error retrieving access group: %s. API Response: %s", err, detailedResponse))
}
version := detailedResponse.GetHeaders().Get("etag")
d.Set("name", agrp.Name)
Expand All @@ -91,10 +92,10 @@ func resourceIBMIAMAccessGroupRead(d *schema.ResourceData, meta interface{}) err
return nil
}

func resourceIBMIAMAccessGroupUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceIBMIAMAccessGroupUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAccessGroupsClient, err := meta.(ClientSession).IAMAccessGroupsV2()
if err != nil {
return err
return diag.FromErr(err)
}
agrpID := d.Id()

Expand All @@ -117,18 +118,18 @@ func resourceIBMIAMAccessGroupUpdate(d *schema.ResourceData, meta interface{}) e
if hasChange {
agrp, detailedResponse, err := iamAccessGroupsClient.UpdateAccessGroup(updateAccessGroupOptions)
if err != nil || agrp == nil {
return fmt.Errorf("Error updating access group: %s. API Response: %s", err, detailedResponse)
return diag.FromErr(fmt.Errorf("Error updating access group: %s. API Response: %s", err, detailedResponse))
}
}

return resourceIBMIAMAccessGroupRead(d, meta)
return resourceIBMIAMAccessGroupRead(context, d, meta)

}

func resourceIBMIAMAccessGroupDelete(d *schema.ResourceData, meta interface{}) error {
func resourceIBMIAMAccessGroupDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAccessGroupsClient, err := meta.(ClientSession).IAMAccessGroupsV2()
if err != nil {
return err
return diag.FromErr(err)
}

agID := d.Id()
Expand All @@ -137,28 +138,10 @@ func resourceIBMIAMAccessGroupDelete(d *schema.ResourceData, meta interface{}) e
deleteAccessGroupOptions.SetForce(force)
detailedResponse, err := iamAccessGroupsClient.DeleteAccessGroup(deleteAccessGroupOptions)
if err != nil {
return fmt.Errorf("Error deleting access group: %s, API Response: %s", err, detailedResponse)
return diag.FromErr(fmt.Errorf("Error deleting access group: %s, API Response: %s", err, detailedResponse))
}

d.SetId("")

return nil
}

func resourceIBMIAMAccessGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
iamAccessGroupsClient, err := meta.(ClientSession).IAMAccessGroupsV2()
if err != nil {
return false, err
}
agID := d.Id()
getAccessGroupOptions := iamAccessGroupsClient.NewGetAccessGroupOptions(agID)
agrp, detailedResponse, err := iamAccessGroupsClient.GetAccessGroup(getAccessGroupOptions)
if err != nil || agrp == nil {
if detailedResponse != nil && detailedResponse.StatusCode == 404 {
return false, nil
}
return false, fmt.Errorf("Error communicating with the API: %s, API Response: %s", err, detailedResponse)
}

return *agrp.ID == agID, nil
}
19 changes: 17 additions & 2 deletions ibm/resource_ibm_iam_access_group_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,25 @@ func resourceIBMIAMAccessGroupMembersRead(context context.Context, d *schema.Res

grpID := parts[0]
listAccessGroupMembersOptions := iamAccessGroupsClient.NewListAccessGroupMembersOptions(grpID)
offset := int64(0)
// lets fetch 100 in a single pagination
limit := int64(100)
listAccessGroupMembersOptions.SetLimit(limit)
members, detailedResponse, err := iamAccessGroupsClient.ListAccessGroupMembers(listAccessGroupMembersOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error retrieving access group members: %s. API Response: %s", err, detailedResponse))
}
allMembers := members.Members
totalMembers := intValue(members.TotalCount)
for len(allMembers) < totalMembers {
offset = offset + limit
listAccessGroupMembersOptions.SetOffset(offset)
members, detailedResponse, err = iamAccessGroupsClient.ListAccessGroupMembers(listAccessGroupMembersOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error retrieving access group members: %s. API Response: %s", err, detailedResponse))
}
allMembers = append(allMembers, members.Members...)
}

d.Set("access_group_id", grpID)

Expand Down Expand Up @@ -176,8 +191,8 @@ func resourceIBMIAMAccessGroupMembersRead(context context.Context, d *schema.Res
}
}

d.Set("members", flattenAccessGroupMembers(members.Members, res, allrecs))
ibmID, serviceID := flattenMembersData(members, res, allrecs)
d.Set("members", flattenAccessGroupMembers(allMembers, res, allrecs))
ibmID, serviceID := flattenMembersData(allMembers, res, allrecs)
if len(ibmID) > 0 {
d.Set("ibm_ids", ibmID)
}
Expand Down
4 changes: 2 additions & 2 deletions ibm/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,10 +1288,10 @@ func StringContains(s []string, str string) bool {
return false
}

func flattenMembersData(list *iamaccessgroupsv2.GroupMembersList, users []usermanagementv2.UserInfo, serviceids []iamidentityv1.ServiceID) ([]string, []string) {
func flattenMembersData(list []iamaccessgroupsv2.ListGroupMembersResponseMember, users []usermanagementv2.UserInfo, serviceids []iamidentityv1.ServiceID) ([]string, []string) {
var ibmid []string
var serviceid []string
for _, m := range list.Members {
for _, m := range list {
if *m.Type == "user" {
for _, user := range users {
if user.IamID == *m.IamID {
Expand Down

0 comments on commit ce546eb

Please sign in to comment.