Skip to content

Commit

Permalink
filter instances datasource by instance group
Browse files Browse the repository at this point in the history
  • Loading branch information
deepaksibm committed Aug 6, 2021
1 parent c5bd8f1 commit 70de597
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 9 deletions.
93 changes: 88 additions & 5 deletions ibm/data_source_ibm_is_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,45 @@ import (
)

const (
isInstances = "instances"
isInstances = "instances"
isInstanceGroupName = "instance_group_name"
)

func dataSourceIBMISInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstancesRead,

Schema: map[string]*schema.Schema{
isInstanceGroup: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn", "vpc_name", isInstanceGroupName},
Description: "Instance group ID to filter the instances attached to it",
},
isInstanceGroupName: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn", "vpc_name", isInstanceGroup},
Description: "Instance group name to filter the instances attached to it",
},
"vpc_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn"},
ConflictsWith: []string{"vpc", "vpc_crn", "instance_group"},
Description: "Name of the vpc to filter the instances attached to it",
},

"vpc": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc_name", "vpc_crn"},
ConflictsWith: []string{"vpc_name", "vpc_crn", "instance_group"},
Description: "VPC ID to filter the instances attached to it",
},

"vpc_crn": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc_name", "vpc"},
ConflictsWith: []string{"vpc_name", "vpc", "instance_group"},
Description: "VPC CRN to filter the instances attached to it",
},

Expand Down Expand Up @@ -348,7 +361,7 @@ func instancesList(d *schema.ResourceData, meta interface{}) error {
return err
}

var vpcName, vpcID, vpcCrn, resourceGroup string
var vpcName, vpcID, vpcCrn, resourceGroup, insGrp string

if vpc, ok := d.GetOk("vpc_name"); ok {
vpcName = vpc.(string)
Expand All @@ -366,6 +379,38 @@ func instancesList(d *schema.ResourceData, meta interface{}) error {
resourceGroup = rg.(string)
}

if insGrpInf, ok := d.GetOk(isInstanceGroup); ok {
insGrp = insGrpInf.(string)
} else if insGrpNameInf, ok := d.GetOk(isInstanceGroupName); ok {
insGrpName := insGrpNameInf.(string)
start := ""
allrecs := []vpcv1.InstanceGroup{}
for {
listInstanceGroupOptions := vpcv1.ListInstanceGroupsOptions{}
if start != "" {
listInstanceGroupOptions.Start = &start
}
instanceGroupsCollection, response, err := sess.ListInstanceGroups(&listInstanceGroupOptions)
if err != nil {
return fmt.Errorf("Error Fetching InstanceGroups %s\n%s", err, response)
}
start = GetNext(instanceGroupsCollection.Next)
allrecs = append(allrecs, instanceGroupsCollection.InstanceGroups...)

if start == "" {
break
}

}

for _, instanceGroup := range allrecs {
if *instanceGroup.Name == insGrpName {
insGrp = *instanceGroup.ID
break
}
}
}

listInstancesOptions := &vpcv1.ListInstancesOptions{}

if vpcName != "" {
Expand Down Expand Up @@ -399,6 +444,44 @@ func instancesList(d *schema.ResourceData, meta interface{}) error {
break
}
}

if insGrp != "" {
membershipMap := map[string]bool{}
start := ""
for {
listInstanceGroupMembershipsOptions := vpcv1.ListInstanceGroupMembershipsOptions{
InstanceGroupID: &insGrp,
}
if start != "" {
listInstanceGroupMembershipsOptions.Start = &start
}
instanceGroupMembershipCollection, response, err := sess.ListInstanceGroupMemberships(&listInstanceGroupMembershipsOptions)
if err != nil {
return fmt.Errorf("Error Getting InstanceGroup Membership Collection %s\n%s", err, response)
}

start = GetNext(instanceGroupMembershipCollection.Next)
for _, membershipItem := range instanceGroupMembershipCollection.Memberships {
membershipMap[*membershipItem.Instance.ID] = true
}

if start == "" {
break
}

}

//Filtering instance allrecs to contain instance group members only
i := 0
for _, ins := range allrecs {
if membershipMap[*ins.ID] {
allrecs[i] = ins
i++
}
}
allrecs = allrecs[:i]
}

instancesInfo := make([]map[string]interface{}, 0)
for _, instance := range allrecs {
id := *instance.ID
Expand Down
40 changes: 40 additions & 0 deletions ibm/data_source_ibm_is_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
})
}

func TestAccIBMISInstancesDataSource_InsGroupfilter(t *testing.T) {

randInt := acctest.RandIntRange(10, 100)
instanceGroupName := fmt.Sprintf("testinstancegroup%d", randInt)
publicKey := strings.TrimSpace(`
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVtuCfWKVGKaRmaRG6JQZY8YdxnDgGzVOK93IrV9R5Hl0JP1oiLLWlZQS2reAKb8lBqyDVEREpaoRUDjqDqXG8J/kR42FKN51su914pjSBc86wJ02VtT1Wm1zRbSg67kT+g8/T1jCgB5XBODqbcICHVP8Z1lXkgbiHLwlUrbz6OZkGJHo/M/kD1Eme8lctceIYNz/Ilm7ewMXZA4fsidpto9AjyarrJLufrOBl4MRVcZTDSJ7rLP982aHpu9pi5eJAjOZc7Og7n4ns3NFppiCwgVMCVUQbN5GBlWhZ1OsT84ZiTf+Zy8ew+Yg5T7Il8HuC7loWnz+esQPf0s3xhC/kTsGgZreIDoh/rxJfD67wKXetNSh5RH/n5BqjaOuXPFeNXmMhKlhj9nJ8scayx/wsvOGuocEIkbyJSLj3sLUU403OafgatEdnJOwbqg6rUNNF5RIjpJpL7eEWlKIi1j9LyhmPJ+fEO7TmOES82VpCMHpLbe4gf/MhhJ/Xy8DKh9s= root@ffd8363b1226
`)
vpcName := fmt.Sprintf("testvpc%d", randInt)
subnetName := fmt.Sprintf("testsubnet%d", randInt)
templateName := fmt.Sprintf("testtemplate%d", randInt)
sshKeyName := fmt.Sprintf("testsshkey%d", randInt)
resName := "data.ibm_is_instances.ds_instances1"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISInstanceGroupConfig(vpcName, subnetName, sshKeyName, publicKey, templateName, instanceGroupName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ibm_is_instance_group.instance_group", "name", instanceGroupName),
),
},
{
Config: testAccCheckIBMISInstancesDataSourceConfigInstanceGroup(instanceGroupName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resName, "instances.0.name"),
),
},
},
})
}

func testAccCheckIBMISInstancesDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_is_instances" "ds_instances" {
Expand All @@ -105,3 +139,9 @@ func testAccCheckIBMISInstancesDataSourceConfig1(vpcname string) string {
vpc_name = "%s"
}`, vpcname)
}
func testAccCheckIBMISInstancesDataSourceConfigInstanceGroup(insGrpName string) string {
return fmt.Sprintf(`
data "ibm_is_instances" "ds_instances1" {
instance_group_name = "%s"
}`, insGrpName)
}
4 changes: 2 additions & 2 deletions ibm/resource_ibm_is_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func testAccCheckIBMISInstanceGroupConfig(vpcName, subnetName, sshKeyName, publi
resource "ibm_is_instance_template" "instancetemplate1" {
name = "%s"
image = "r006-14140f94-fcc4-11e9-96e7-a72723715315"
image = "%s"
profile = "bx2-8x32"
primary_network_interface {
Expand All @@ -225,6 +225,6 @@ func testAccCheckIBMISInstanceGroupConfig(vpcName, subnetName, sshKeyName, publi
instance_count = 2
subnets = [ibm_is_subnet.subnet2.id]
}
`, vpcName, subnetName, sshKeyName, publicKey, templateName, instanceGroupName)
`, vpcName, subnetName, sshKeyName, publicKey, templateName, isImage, instanceGroupName)

}
6 changes: 4 additions & 2 deletions website/docs/d/is_instances.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ data "ibm_is_instances" "ds_instances1" {
## Argument reference
The input parameters that you need to specify for the data source.

- `resource_group` - (optional, string) Resource Group ID to filter the instances attached to it.
- `resource_group` - (optional, String) Resource Group ID to filter the instances attached to it.
- `vpc` - (Optional, String) The VPC ID to filter the instances attached.
- `vpc_crn` - (optional, string) VPC CRN to filter the instances attached to it.
- `vpc_crn` - (optional, String) VPC CRN to filter the instances attached to it.
- `vpc_name` - (Optional, String) The name of the VPC to filter the instances attached.
- `instance_group` - (Optional, String) Instance group ID to filter the instances attached to it.
- `instance_group_name` - (Optional, String) Instance group name to filter the instances attached to it.

## Attribute reference
In addition to all argument reference list, you can access the following attribute references after your data source is created.
Expand Down

0 comments on commit 70de597

Please sign in to comment.