Skip to content

Commit

Permalink
CIS: GLB resource changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KumarGanesanIBM committed Oct 29, 2020
1 parent ee2ab66 commit b429c0f
Show file tree
Hide file tree
Showing 9 changed files with 640 additions and 259 deletions.
27 changes: 27 additions & 0 deletions ibm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
cisedgefunctionv1 "github.com/IBM/networking-go-sdk/edgefunctionsapiv1"
cisglbhealthcheckv1 "github.com/IBM/networking-go-sdk/globalloadbalancermonitorv1"
cisglbpoolv0 "github.com/IBM/networking-go-sdk/globalloadbalancerpoolsv0"
cisglbv1 "github.com/IBM/networking-go-sdk/globalloadbalancerv1"
cissslv1 "github.com/IBM/networking-go-sdk/sslcertificateapiv1"
tg "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
cisratelimitv1 "github.com/IBM/networking-go-sdk/zoneratelimitsv1"
Expand Down Expand Up @@ -191,6 +192,7 @@ type ClientSession interface {
IAMNamespaceAPI() (*ns.IbmCloudFunctionsNamespaceAPIV1, error)
CisZonesV1ClientSession() (*ciszonesv1.ZonesV1, error)
CisDNSRecordClientSession() (*cisdnsrecordsv1.DnsRecordsV1, error)
CisGLBClientSession() (*cisglbv1.GlobalLoadBalancerV1, error)
CisGLBPoolClientSession() (*cisglbpoolv0.GlobalLoadBalancerPoolsV0, error)
CisGLBHealthCheckClientSession() (*cisglbhealthcheckv1.GlobalLoadBalancerMonitorV1, error)
CisRLClientSession() (*cisratelimitv1.ZoneRateLimitsV1, error)
Expand Down Expand Up @@ -325,6 +327,10 @@ type clientSession struct {
cisGLBPoolErr error
cisGLBPoolClient *cisglbpoolv0.GlobalLoadBalancerPoolsV0

// CIS GLB service options
cisGLBErr error
cisGLBClient *cisglbv1.GlobalLoadBalancerV1

// CIS GLB health check service options
cisGLBHealthCheckErr error
cisGLBHealthCheckClient *cisglbhealthcheckv1.GlobalLoadBalancerMonitorV1
Expand Down Expand Up @@ -547,6 +553,11 @@ func (sess clientSession) CisGLBPoolClientSession() (*cisglbpoolv0.GlobalLoadBal
return sess.cisGLBPoolClient, sess.cisGLBPoolErr
}

// CIS GLB
func (sess clientSession) CisGLBClientSession() (*cisglbv1.GlobalLoadBalancerV1, error) {
return sess.cisGLBClient, sess.cisGLBErr
}

// CIS GLB Health Check/Monitor
func (sess clientSession) CisGLBHealthCheckClientSession() (*cisglbhealthcheckv1.GlobalLoadBalancerMonitorV1, error) {
return sess.cisGLBHealthCheckClient, sess.cisGLBHealthCheckErr
Expand Down Expand Up @@ -626,6 +637,7 @@ func (c *Config) ClientSession() (interface{}, error) {
session.iamNamespaceErr = errEmptyBluemixCredentials
session.cisDNSErr = errEmptyBluemixCredentials
session.cisGLBPoolErr = errEmptyBluemixCredentials
session.cisGLBErr = errEmptyBluemixCredentials
session.cisGLBHealthCheckErr = errEmptyBluemixCredentials
session.cisZonesErr = errEmptyBluemixCredentials
session.cisRLErr = errEmptyBluemixCredentials
Expand Down Expand Up @@ -999,6 +1011,20 @@ func (c *Config) ClientSession() (interface{}, error) {
session.cisGLBPoolErr)
}

// IBM Network CIS Global load balancer
cisGLBOpt := &cisglbv1.GlobalLoadBalancerV1Options{
URL: cisEndPoint,
Authenticator: authenticator,
Crn: core.StringPtr(""),
ZoneIdentifier: core.StringPtr(""),
}
session.cisGLBClient, session.cisGLBErr = cisglbv1.NewGlobalLoadBalancerV1(cisGLBOpt)
if session.cisGLBErr != nil {
session.cisGLBErr =
fmt.Errorf("Error occured while configuring CIS GLB service: %s",
session.cisGLBErr)
}

// IBM Network CIS Global load balancer health check/monitor
cisGLBHealthCheckOpt := &cisglbhealthcheckv1.GlobalLoadBalancerMonitorV1Options{
URL: cisEndPoint,
Expand Down Expand Up @@ -1072,6 +1098,7 @@ func (c *Config) ClientSession() (interface{}, error) {
fmt.Errorf("Error occured while configuring CIS SSL certificate service: %s",
session.cisSSLErr)
}

return session, nil
}

Expand Down
225 changes: 225 additions & 0 deletions ibm/data_source_ibm_cis_global_load_balancers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package ibm

import (
"log"
"time"

"github.com/IBM/go-sdk-core/v3/core"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const cisGLB = "cis_glb"

func dataSourceIBMCISGlbs() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Description: "CIS instance crn",
Required: true,
},
cisDomainID: {
Type: schema.TypeString,
Description: "Associated CIS domain",
Required: true,
DiffSuppressFunc: suppressDomainIDDiff,
},
cisGLB: {
Type: schema.TypeList,
Description: "Collection of GLB detail",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "identifier with zone id",
Computed: true,
},
cisGLBID: {
Type: schema.TypeString,
Description: "global load balancer id",
Computed: true,
},
cisGLBName: {
Type: schema.TypeString,
Description: "name",
Computed: true,
},
cisGLBFallbackPoolID: {
Type: schema.TypeString,
Description: "fallback pool ID",
Computed: true,
},
cisGLBDefaultPoolIDs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of default Pool IDs",
},
cisGLBDesc: {
Type: schema.TypeString,
Computed: true,
Description: "Description for the load balancer instance",
},
cisGLBTTL: {
Type: schema.TypeInt,
Computed: true,
Description: "TTL value",
},
cisGLBProxied: {
Type: schema.TypeBool,
Computed: true,
Description: "set to true if proxy needs to be enabled",
},
cisGLBSessionAffinity: {
Type: schema.TypeString,
Computed: true,
Description: "Session affinity info",
},
cisGLBEnabled: {
Type: schema.TypeBool,
Computed: true,
Description: "set to true of LB needs to enabled",
},
cisGLBCreatedOn: {
Type: schema.TypeString,
Computed: true,
Description: "Load balancer creation date",
},
cisGLBModifiedOn: {
Type: schema.TypeString,
Computed: true,
Description: "Load balancer modified date",
},
cisGLBPopPools: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisGLBPopPoolsPop: {
Type: schema.TypeString,
Computed: true,
Description: "pop pools region",
},

cisGLBPopPoolsPoolIDs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
cisGLBRegionPools: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisGLBRegionPoolsRegion: {
Type: schema.TypeString,
Computed: true,
},
cisGLBRegionPoolsPoolIDs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
},
},
},
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(10 * time.Minute),
},
Read: dataSourceCISGlbsRead,
Importer: &schema.ResourceImporter{},
}
}

func dataSourceCISGlbsRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisGLBClientSession()
if err != nil {
return err
}

crn := d.Get(cisID).(string)
zoneID := d.Get(cisDomainID).(string)
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneIdentifier = core.StringPtr(zoneID)

opt := cisClient.NewListAllLoadBalancersOptions()

result, resp, err := cisClient.ListAllLoadBalancers(opt)
if err != nil {
log.Printf("[WARN] List all GLB failed: %v\n", resp)
return err
}
glbs := result.Result

glbList := make([]map[string]interface{}, 0)
for _, glbObj := range glbs {
glbOutput := map[string]interface{}{}
glbOutput["id"] = convertCisToTfThreeVar(*glbObj.ID, zoneID, crn)
glbOutput[cisGLBID] = *glbObj.ID
glbOutput[cisGLBName] = *glbObj.Name
glbOutput[cisGLBDefaultPoolIDs] = convertCisToTfTwoVarSlice(glbObj.DefaultPools, crn)
glbOutput[cisGLBDesc] = *glbObj.Description
glbOutput[cisGLBFallbackPoolID] = convertCisToTfTwoVar(*glbObj.FallbackPool, crn)
glbOutput[cisGLBTTL] = *glbObj.TTL
glbOutput[cisGLBProxied] = *glbObj.Proxied
glbOutput[cisGLBEnabled] = *glbObj.Enabled
glbOutput[cisGLBSessionAffinity] = *glbObj.SessionAffinity
glbOutput[cisGLBCreatedOn] = *glbObj.CreatedOn
glbOutput[cisGLBModifiedOn] = *glbObj.ModifiedOn
flattenPopPools := flattenDataSourcePopPools(
glbObj.PopPools, cisGLBPopPoolsPop, crn)
glbOutput[cisGLBPopPools] = flattenPopPools
flattenRegionPools := flattenDataSourceRegionPools(
glbObj.RegionPools, cisGLBRegionPoolsRegion, crn)
glbOutput[cisGLBRegionPools] = flattenRegionPools
glbList = append(glbList, glbOutput)
}
d.SetId(dataSourceCISGlbsCheckID(d))
d.Set(cisGLB, glbList)
return nil
}

// dataSourceCISGlbCheckID returns a reasonable ID glb list
func dataSourceCISGlbsCheckID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}

func flattenDataSourcePopPools(pools interface{}, geoType string, cisID string) []interface{} {
result := make([]interface{}, 0)
for k, v := range pools.(map[string]interface{}) {
poolIds := convertCisToTfTwoVarSlice(expandStringList(v.([]interface{})), cisID)
pool := map[string]interface{}{
cisGLBPopPoolsPop: k,
cisGLBPopPoolsPoolIDs: poolIds,
}
result = append(result, pool)
}
return result
}

func flattenDataSourceRegionPools(pools interface{}, geoType string, cisID string) []interface{} {
result := make([]interface{}, 0)
for k, v := range pools.(map[string]interface{}) {
poolIds := convertCisToTfTwoVarSlice(expandStringList(v.([]interface{})), cisID)
pool := map[string]interface{}{
cisGLBRegionPoolsRegion: k,
cisGLBRegionPoolsPoolIDs: poolIds,
}
result = append(result, pool)
}
return result
}
34 changes: 34 additions & 0 deletions ibm/data_source_ibm_cis_global_load_balancers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ibm

import (
"fmt"
"testing"

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

func TestAccIBMCisGLBDataSource_basic(t *testing.T) {
node := "data.ibm_cis_global_load_balancers.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMCisGLBDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(node, "cis_glb.0.id"),
resource.TestCheckResourceAttrSet(node, "cis_glb.0.glb_id"),
),
},
},
})
}

func testAccCheckIBMCisGLBDataSourceConfig() string {
// status filter defaults to empty
return testAccCheckCisGlbConfigCisDSBasic("test", cisDomainStatic) + fmt.Sprintf(`
data "ibm_cis_global_load_balancers" "test" {
cis_id = ibm_cis_global_load_balancer.test.cis_id
domain_id = ibm_cis_global_load_balancer.test.domain_id
}`)
}
1 change: 1 addition & 0 deletions ibm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Provider() terraform.ResourceProvider {
"ibm_certificate_manager_certificate": dataIBMCertificateManagerCertificate(),
"ibm_cis": dataSourceIBMCISInstance(),
"ibm_cis_dns_records": dataSourceIBMCISDNSRecords(),
"ibm_cis_global_load_balancers": dataSourceIBMCISGlbs(),
"ibm_cis_origin_pools": dataSourceIBMCISOriginPools(),
"ibm_cis_healthchecks": dataSourceIBMCISHealthChecks(),
"ibm_cis_domain": dataSourceIBMCISDomain(),
Expand Down
Loading

0 comments on commit b429c0f

Please sign in to comment.