Skip to content

Commit

Permalink
VPC ID Filter is added when Subnet Name is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
SunithaGudisagarIBM1 authored and hkantare committed Nov 17, 2023
1 parent 11bf7d0 commit 16d4748
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
12 changes: 9 additions & 3 deletions ibm/service/vpc/data_source_ibm_is_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ func DataSourceIBMISSubnet() *schema.Resource {
},

isSubnetVPC: {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{isSubnetName},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", "identifier"),
},

isSubnetVPCName: {
Expand Down Expand Up @@ -229,11 +232,14 @@ func subnetGetByNameOrID(d *schema.ResourceData, meta interface{}) error {
start := ""
allrecs := []vpcv1.Subnet{}
getSubnetsListOptions := &vpcv1.ListSubnetsOptions{}

for {
if start != "" {
getSubnetsListOptions.Start = &start
}
if vpcIdOk, ok := d.GetOk(isSubnetVPC); ok {
vpcIDOk := vpcIdOk.(string)
getSubnetsListOptions.VPCID = &vpcIDOk
}
subnetsCollection, response, err := sess.ListSubnets(getSubnetsListOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching subnets List %s\n%s", err, response)
Expand Down
69 changes: 62 additions & 7 deletions ibm/service/vpc/data_source_ibm_is_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@ const (
isSubnets = "subnets"
isSubnetResourceGroupID = "resource_group"
isSubnetRoutingTableName = "routing_table_name"
isSubnetResourceZone = "zone"
isSubnetResourceVpc = "vpc"
isSubnetResourceVpcCrn = "vpc_crn"
isSubnetResourceVpcName = "vpc_name"
)

func DataSourceIBMISSubnets() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSubnetsRead,

Schema: map[string]*schema.Schema{
isSubnetResourceVpc: {
Type: schema.TypeString,
Description: "ID of the VPC",
Optional: true,
},
isSubnetResourceVpcName: {
Type: schema.TypeString,
Description: "Name of the VPC",
Optional: true,
},
isSubnetResourceVpcCrn: {
Type: schema.TypeString,
Description: "CRN of the VPC",
Optional: true,
},
isSubnetResourceZone: {
Type: schema.TypeString,
Description: "Name of the Zone ",
Optional: true,
},
isSubnetResourceGroupID: {
Type: schema.TypeString,
Description: "Resource Group ID",
Expand Down Expand Up @@ -177,18 +201,49 @@ func subnetList(d *schema.ResourceData, meta interface{}) error {
resourceTableName = v.(string)
}

options := &vpcv1.ListSubnetsOptions{}
if resourceGroup != "" {
options.SetResourceGroupID(resourceGroup)
var zone string
if v, ok := d.GetOk(isSubnetResourceZone); ok {
zone = v.(string)
}
if routingTable != "" {
options.SetRoutingTableID(routingTable)

var vpc string
if v, ok := d.GetOk(isSubnetResourceVpc); ok {
vpc = v.(string)
}

var vpcName string
if v, ok := d.GetOk(isSubnetResourceVpcName); ok {
vpcName = v.(string)
}
if resourceTableName != "" {
options.SetRoutingTableName(resourceTableName)

var vpcCrn string
if v, ok := d.GetOk(isSubnetResourceVpcCrn); ok {
vpcCrn = v.(string)
}

for {
options := &vpcv1.ListSubnetsOptions{}
if resourceGroup != "" {
options.SetResourceGroupID(resourceGroup)
}
if routingTable != "" {
options.SetRoutingTableID(routingTable)
}
if resourceTableName != "" {
options.SetRoutingTableName(resourceTableName)
}
if zone != "" {
options.SetZoneName(zone)
}
if vpc != "" {
options.SetVPCID(vpc)
}
if vpcName != "" {
options.SetVPCName(vpcName)
}
if vpcCrn != "" {
options.SetVPCCRN(vpcCrn)
}
if start != "" {
options.Start = &start
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/is_subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Review the argument references that you can specify for your data source.

- `identifier` - (Optional, String) The ID of the subnet,`name` and `identifier` are mutually exclusive.
- `name` - (Optional, String) The name of the subnet,`name` and `identifier` are mutually exclusive.
- `vpc` - (Optional, String) Filters the collection to resources with a vpc property matching the specified identifier. Subnet `name` must be specified with `vpc` filter.

## Attribute reference
In addition to all argument reference list, you can access the following attribute references after your data source is created.
Expand Down
10 changes: 7 additions & 3 deletions website/docs/d/is_subnets.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ data "ibm_is_subnets" "example4" {

Review the argument references that you can specify for your data source.

* `resource_group` - (Optional, string) The id of the resource group.
* `routing_table` - (Optional, string) The id of the routing table.
* `routing_table_name` - (Optional, string) The name of the routing table.
- `resource_group` - (Optional, string) The id of the resource group.
- `routing_table` - (Optional, string) The id of the routing table.
- `routing_table_name` - (Optional, string) The name of the routing table.
- `vpc` - (Optional, string) The id of the vpc.
- `vpc_crn` - (Optional, string) The crn of the vpc.
- `vpc_name` - (Optional, string) The name of vpc.
- `zone` - (Optional, string) The name of the zone.

## Attribute reference
You can access the following attribute references after your data source is created.
Expand Down

0 comments on commit 16d4748

Please sign in to comment.