Skip to content

Commit

Permalink
updated datasource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwal-ibm committed Sep 5, 2024
1 parent a923360 commit 42b086b
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ func DataSourceIBMISLB() *schema.Resource {
},
},
},
"failsafe_policy_actions": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The supported `failsafe_policy.action` values for this load balancer's pools.",
Elem: &schema.Schema{Type: schema.TypeString},
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -455,6 +461,10 @@ func lbGetByName(d *schema.ResourceData, meta interface{}, name string) error {
}
d.Set(isLBListeners, listenerList)
}
if err = d.Set("failsafe_policy_actions", lb.FailsafePolicyActions); err != nil {
err = fmt.Errorf("Error setting failsafe_policy_actions: %s", err)
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_is_lb", "read", "set-failsafe_policy_actions")
}
listLoadBalancerPoolsOptions := &vpcv1.ListLoadBalancerPoolsOptions{}
listLoadBalancerPoolsOptions.SetLoadBalancerID(*lb.ID)
poolsResult, _, _ := sess.ListLoadBalancerPools(listLoadBalancerPoolsOptions)
Expand Down
102 changes: 102 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lb_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,62 @@ func DataSourceIBMISLBPool() *schema.Resource {
ExactlyOneOf: []string{"name", "identifier"},
Description: "The pool identifier.",
},
"failsafe_policy": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A load balancer failsafe policy action:- `forward`: Forwards requests to the `target` pool.- `fail`: Rejects requests with an HTTP `503` status code.The enumerated values for this property may[expand](https://cloud.ibm.com/apidocs/vpc#property-value-expansion) in the future.",
},
"healthy_member_threshold_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The healthy member count at which the failsafe policy action will be triggered. At present, this is always `0`, but may be modifiable in the future.",
},
"target": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If `action` is `forward`, the target pool to forward to.If `action` is `fail`, this property will be absent.The targets supported by this property may[expand](https://cloud.ibm.com/apidocs/vpc#property-value-expansion) in the future.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted, and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this load balancer pool.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name for this load balancer pool. The name is unique across all pools for the load balancer.",
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -256,6 +312,17 @@ func dataSourceIBMIsLbPoolRead(context context.Context, d *schema.ResourceData,
if err = d.Set("algorithm", loadBalancerPool.Algorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting algorithm: %s", err))
}
failsafePolicy := []map[string]interface{}{}
if loadBalancerPool.FailsafePolicy != nil {
modelMap, err := dataSourceIBMIsLbPoolLoadBalancerPoolFailsafePolicyToMap(loadBalancerPool.FailsafePolicy)
if err != nil {
return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_lb_pool", "read", "failsafe_policy-to-map").GetDiag()
}
failsafePolicy = append(failsafePolicy, modelMap)
}
if err = d.Set("failsafe_policy", failsafePolicy); err != nil {
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting failsafe_policy: %s", err), "(Data) ibm_is_lb_pool", "read", "set-failsafe_policy").GetDiag()
}
if err = d.Set("created_at", flex.DateTimeToString(loadBalancerPool.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
Expand Down Expand Up @@ -444,3 +511,38 @@ func dataSourceLoadBalancerPoolSessionPersistenceToMap(sessionPersistenceItem vp

return sessionPersistenceMap
}

func dataSourceIBMIsLbPoolLoadBalancerPoolFailsafePolicyToMap(model *vpcv1.LoadBalancerPoolFailsafePolicy) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["action"] = *model.Action
modelMap["healthy_member_threshold_count"] = flex.IntValue(model.HealthyMemberThresholdCount)
if model.Target != nil {
targetMap, err := dataSourceIBMIsLbPoolLoadBalancerPoolReferenceToMap(model.Target)
if err != nil {
return modelMap, err
}
modelMap["target"] = []map[string]interface{}{targetMap}
}
return modelMap, nil
}

func dataSourceIBMIsLbPoolLoadBalancerPoolReferenceToMap(model *vpcv1.LoadBalancerPoolReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Deleted != nil {
deletedMap, err := dataSourceIBMIsLbPoolDeletedToMap(model.Deleted)
if err != nil {
return modelMap, err
}
modelMap["deleted"] = []map[string]interface{}{deletedMap}
}
modelMap["href"] = *model.Href
modelMap["id"] = *model.ID
modelMap["name"] = *model.Name
return modelMap, nil
}

func dataSourceIBMIsLbPoolDeletedToMap(model *vpcv1.Deleted) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["more_info"] = *model.MoreInfo
return modelMap, nil
}
99 changes: 99 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lb_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -41,6 +42,62 @@ func DataSourceIBMISLBPools() *schema.Resource {
Computed: true,
Description: "The date and time that this pool was created.",
},
"failsafe_policy": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A load balancer failsafe policy action:- `forward`: Forwards requests to the `target` pool.- `fail`: Rejects requests with an HTTP `503` status code.The enumerated values for this property may[expand](https://cloud.ibm.com/apidocs/vpc#property-value-expansion) in the future.",
},
"healthy_member_threshold_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The healthy member count at which the failsafe policy action will be triggered. At present, this is always `0`, but may be modifiable in the future.",
},
"target": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If `action` is `forward`, the target pool to forward to.If `action` is `fail`, this property will be absent.The targets supported by this property may[expand](https://cloud.ibm.com/apidocs/vpc#property-value-expansion) in the future.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted, and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this load balancer pool.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name for this load balancer pool. The name is unique across all pools for the load balancer.",
},
},
},
},
},
},
},
"health_monitor": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -302,6 +359,13 @@ func dataSourceLoadBalancerPoolCollectionPoolsToMap(poolsItem vpcv1.LoadBalancer
if poolsItem.ProxyProtocol != nil {
poolsMap["proxy_protocol"] = poolsItem.ProxyProtocol
}
if poolsItem.FailsafePolicy != nil {
failsafePolicyMap, err := dataSourceIBMIsLbPoolsLoadBalancerPoolFailsafePolicyToMap(poolsItem.FailsafePolicy)
if err != nil {
return poolsMap
}
poolsMap["failsafe_policy"] = []map[string]interface{}{failsafePolicyMap}
}
if poolsItem.SessionPersistence != nil {
sessionPersistenceList := []map[string]interface{}{}
sessionPersistenceMap := dataSourceLoadBalancerPoolCollectionPoolsSessionPersistenceToMap(*poolsItem.SessionPersistence)
Expand Down Expand Up @@ -413,3 +477,38 @@ func dataSourceLoadBalancerPoolCollectionPoolsSessionPersistenceToMap(sessionPer

return sessionPersistenceMap
}

func dataSourceIBMIsLbPoolsLoadBalancerPoolFailsafePolicyToMap(model *vpcv1.LoadBalancerPoolFailsafePolicy) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["action"] = *model.Action
modelMap["healthy_member_threshold_count"] = flex.IntValue(model.HealthyMemberThresholdCount)
if model.Target != nil {
targetMap, err := dataSourceIBMIsLbPoolsLoadBalancerPoolReferenceToMap(model.Target)
if err != nil {
return modelMap, err
}
modelMap["target"] = []map[string]interface{}{targetMap}
}
return modelMap, nil
}

func dataSourceIBMIsLbPoolsLoadBalancerPoolReferenceToMap(model *vpcv1.LoadBalancerPoolReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Deleted != nil {
deletedMap, err := dataSourceIBMIsLbPoolsDeletedToMap(model.Deleted)
if err != nil {
return modelMap, err
}
modelMap["deleted"] = []map[string]interface{}{deletedMap}
}
modelMap["href"] = *model.Href
modelMap["id"] = *model.ID
modelMap["name"] = *model.Name
return modelMap, nil
}

func dataSourceIBMIsLbPoolsDeletedToMap(model *vpcv1.Deleted) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["more_info"] = *model.MoreInfo
return modelMap, nil
}
21 changes: 21 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ func TestAccIBMISLBDatasource_basic(t *testing.T) {
},
})
}
func TestAccIBMISLBDatasource_failsafe_policy(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))
routeMode := "false"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testDSCheckIBMISLBConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ibm_is_lb.ds_lb", "name", name),
resource.TestCheckResourceAttr(
"data.ibm_is_lb.ds_lb", "route_mode", routeMode),
),
},
},
})
}
func TestAccIBMISLBDatasource_ReservedIp(t *testing.T) {
name := fmt.Sprintf("tflb-name-%d", acctest.RandIntRange(10, 100))
vpcname := fmt.Sprintf("tflb-vpc-%d", acctest.RandIntRange(10, 100))
Expand Down
7 changes: 7 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func DataSourceIBMISLBS() *schema.Resource {
Computed: true,
Description: "The date and time that this pool was created.",
},
"failsafe_policy_actions": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The supported `failsafe_policy.action` values for this load balancer's pools.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dns": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -433,6 +439,7 @@ func getLbs(d *schema.ResourceData, meta interface{}) error {
}
lbInfo[isLBPools] = poolList
}
lbInfo["failsafe_policy_actions"] = lb.FailsafePolicyActions
if lb.Profile != nil {
lbProfile := make(map[string]interface{})
lbProfile[name] = *lb.Profile.Name
Expand Down
2 changes: 1 addition & 1 deletion ibm/service/vpc/resource_ibm_is_lb_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestAccIBMISLBPool_failsafe_policy(t *testing.T) {
healthType1 := "http"

alg2 := "least_connections"
protocol2 := "tcp"
protocol2 := "http"
proxyProtocol2 := "v2"
delay2 := "60"
retries2 := "3"
Expand Down

0 comments on commit 42b086b

Please sign in to comment.