Skip to content

Commit

Permalink
Datasource support for vpc load balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
VaishnaviGopal authored and hkantare committed Sep 1, 2020
1 parent 515ba07 commit c5a61c9
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-plugin-sdk v1.6.0 h1:Um5hsAL7kKsfTHtan8lybY/d03F2bHu4fjRB1H6Ag4U=
github.com/hashicorp/terraform-plugin-sdk v1.6.0/go.mod h1:H5QLx/uhwfxBZ59Bc5SqT19M4i+fYt7LZjHTpbLZiAg=
github.com/hashicorp/terraform-plugin-sdk v1.15.0 h1:bmYnTT7MqNXlUHDc7pT8E6uKT2g/upjlRLypJFK1OQU=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/vault v0.10.4/go.mod h1:KfSyffbKxoVyspOdlaGVjIuwLobi07qD1bAbosPMpP0=
Expand Down
328 changes: 328 additions & 0 deletions ibm/data_source_ibm_is_lb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
package ibm

import (
"fmt"
"log"

"github.com/IBM/vpc-go-sdk/vpcclassicv1"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceIBMISLB() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISLBRead,

Schema: map[string]*schema.Schema{
isLBName: {
Type: schema.TypeString,
Required: true,
Description: "Load Balancer name",
},

isLBType: {
Type: schema.TypeString,
Computed: true,
Description: "Load Balancer type",
},

isLBStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Load Balancer status",
},

isLBOperatingStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Load Balancer operating status",
},

isLBPublicIPs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Load Balancer Public IPs",
},

isLBPrivateIPs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Load Balancer private IPs",
},

isLBSubnets: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Load Balancer subnets list",
},

isLBTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: resourceIBMVPCHash,
Description: "Tags associated to Load Balancer",
},

isLBResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Load Balancer Resource group",
},

isLBHostName: {
Type: schema.TypeString,
Computed: true,
Description: "Load Balancer Host Name",
},

isLBListeners: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Load Balancer Listeners list",
},

isLBPools: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Load Balancer Pools list",
},

ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},

ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},

ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
},
}
}

func dataSourceIBMISLBRead(d *schema.ResourceData, meta interface{}) error {
userDetails, err := meta.(ClientSession).BluemixUserDetails()
if err != nil {
return err
}
name := d.Get(isLBName).(string)
if userDetails.generation == 1 {
err := classiclbGetbyName(d, meta, name)
if err != nil {
return err
}
} else {
err := lbGetByName(d, meta, name)
if err != nil {
return err
}
}
return nil
}

func classiclbGetbyName(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := classicVpcClient(meta)
if err != nil {
return err
}
listLoadBalancersOptions := &vpcclassicv1.ListLoadBalancersOptions{}
lbs, response, err := sess.ListLoadBalancers(listLoadBalancersOptions)
if err != nil {
return fmt.Errorf("Error Fetching Load Balancers %s\n%s", err, response)
}
for _, lb := range lbs.LoadBalancers {
if *lb.Name == name {
d.SetId(*lb.ID)
d.Set("id", *lb.ID)
d.Set(isLBName, *lb.Name)
if *lb.IsPublic {
d.Set(isLBType, "public")
} else {
d.Set(isLBType, "private")
}
d.Set(isLBStatus, *lb.ProvisioningStatus)
d.Set(isLBOperatingStatus, *lb.OperatingStatus)
publicIpList := make([]string, 0)
if lb.PublicIps != nil {
for _, ip := range lb.PublicIps {
if ip.Address != nil {
pubip := *ip.Address
publicIpList = append(publicIpList, pubip)
}
}
}
d.Set(isLBPublicIPs, publicIpList)
privateIpList := make([]string, 0)
if lb.PrivateIps != nil {
for _, ip := range lb.PrivateIps {
if ip.Address != nil {
prip := *ip.Address
privateIpList = append(privateIpList, prip)
}
}
}
d.Set(isLBPrivateIPs, privateIpList)
if lb.Subnets != nil {
subnetList := make([]string, 0)
for _, subnet := range lb.Subnets {
if subnet.ID != nil {
sub := *subnet.ID
subnetList = append(subnetList, sub)
}
}
d.Set(isLBSubnets, subnetList)
}
if lb.Listeners != nil {
listenerList := make([]string, 0)
for _, listener := range lb.Listeners {
if listener.ID != nil {
lis := *listener.ID
listenerList = append(listenerList, lis)
}
}
d.Set(isLBListeners, listenerList)
}
if lb.Pools != nil {
poolList := make([]string, 0)
for _, pool := range lb.Pools {
if pool.ID != nil {
p := *pool.ID
poolList = append(poolList, p)
}
}
d.Set(isLBPools, poolList)
}
d.Set(isLBResourceGroup, *lb.ResourceGroup.ID)
d.Set(isLBHostName, *lb.Hostname)
tags, err := GetTagsUsingCRN(meta, *lb.CRN)
if err != nil {
log.Printf(
"Error on get of resource vpc Load Balancer (%s) tags: %s", d.Id(), err)
}
d.Set(isLBTags, tags)
controller, err := getBaseController(meta)
if err != nil {
return err
}
d.Set(ResourceControllerURL, controller+"/vpc/network/loadBalancers")
d.Set(ResourceName, *lb.Name)
if lb.ResourceGroup != nil {
d.Set(ResourceGroupName, *lb.ResourceGroup.ID)
}
return nil
}
}
return fmt.Errorf("No Load balancer found with name %s", name)
}

func lbGetByName(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
listLoadBalancersOptions := &vpcv1.ListLoadBalancersOptions{}
lbs, response, err := sess.ListLoadBalancers(listLoadBalancersOptions)
if err != nil {
return fmt.Errorf("Error Fetching Load Balancers %s\n%s", err, response)
}
for _, lb := range lbs.LoadBalancers {
if *lb.Name == name {
d.SetId(*lb.ID)
d.Set("id", *lb.ID)
d.Set(isLBName, *lb.Name)
if *lb.IsPublic {
d.Set(isLBType, "public")
} else {
d.Set(isLBType, "private")
}
d.Set(isLBStatus, *lb.ProvisioningStatus)
d.Set(isLBOperatingStatus, *lb.OperatingStatus)
publicIpList := make([]string, 0)
if lb.PublicIps != nil {
for _, ip := range lb.PublicIps {
if ip.Address != nil {
pubip := *ip.Address
publicIpList = append(publicIpList, pubip)
}
}
}
d.Set(isLBPublicIPs, publicIpList)
privateIpList := make([]string, 0)
if lb.PrivateIps != nil {
for _, ip := range lb.PrivateIps {
if ip.Address != nil {
prip := *ip.Address
privateIpList = append(privateIpList, prip)
}
}
}
d.Set(isLBPrivateIPs, privateIpList)
if lb.Subnets != nil {
subnetList := make([]string, 0)
for _, subnet := range lb.Subnets {
if subnet.ID != nil {
sub := *subnet.ID
subnetList = append(subnetList, sub)
}
}
d.Set(isLBSubnets, subnetList)
}
if lb.Listeners != nil {
listenerList := make([]string, 0)
for _, listener := range lb.Listeners {
if listener.ID != nil {
lis := *listener.ID
listenerList = append(listenerList, lis)
}
}
d.Set(isLBListeners, listenerList)
}
if lb.Pools != nil {
poolList := make([]string, 0)
for _, pool := range lb.Pools {
if pool.ID != nil {
p := *pool.ID
poolList = append(poolList, p)
}
}
d.Set(isLBPools, poolList)
}
d.Set(isLBResourceGroup, *lb.ResourceGroup.ID)
d.Set(isLBHostName, *lb.Hostname)
tags, err := GetTagsUsingCRN(meta, *lb.CRN)
if err != nil {
log.Printf(
"Error on get of resource vpc Load Balancer (%s) tags: %s", d.Id(), err)
}
d.Set(isLBTags, tags)
controller, err := getBaseController(meta)
if err != nil {
return err
}
d.Set(ResourceControllerURL, controller+"/vpc-ext/network/loadBalancers")
d.Set(ResourceName, *lb.Name)
if lb.ResourceGroup != nil {
d.Set(ResourceGroupName, *lb.ResourceGroup.ID)
}
return nil
}
}
return fmt.Errorf("No Load balancer found with name %s", name)
}
50 changes: 50 additions & 0 deletions ibm/data_source_ibm_is_lb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ibm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccIBMISLBDatasource_basic(t *testing.T) {
name := fmt.Sprintf("tflb-name-%d", acctest.RandIntRange(10, 100))
vpcname := fmt.Sprintf("tflb-vpc-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tflb-subnet-name-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testDSCheckIBMISLBConfig(vpcname, subnetname, ISZoneName, ISCIDR, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ibm_is_lb.ds_lb", "name", name),
),
},
},
})
}

func testDSCheckIBMISLBConfig(vpcname, subnetname, zone, cidr, name string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_subnet" "testacc_subnet" {
name = "%s"
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
ipv4_cidr_block = "%s"
}
resource "ibm_is_lb" "testacc_lb" {
name = "%s"
subnets = [ibm_is_subnet.testacc_subnet.id]
}
data "ibm_is_lb" "ds_lb" {
name = ibm_is_lb.testacc_lb.name
}`, vpcname, subnetname, zone, cidr, name)
}
1 change: 1 addition & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func Provider() terraform.ResourceProvider {
"ibm_is_instance_profiles": dataSourceIBMISInstanceProfiles(),
"ibm_is_instance": dataSourceIBMISInstance(),
"ibm_is_instances": dataSourceIBMISInstances(),
"ibm_is_lb": dataSourceIBMISLB(),
"ibm_is_public_gateway": dataSourceIBMISPublicGateway(),
"ibm_is_region": dataSourceIBMISRegion(),
"ibm_is_ssh_key": dataSourceIBMISSSHKey(),
Expand Down
Loading

0 comments on commit c5a61c9

Please sign in to comment.