-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_lb_listener_policy_rules.go
161 lines (144 loc) · 5.57 KB
/
data_source_ibm_is_lb_listener_policy_rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMISLBListenerPolicyRules() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsLbListenerPolicyRulesRead,
Schema: map[string]*schema.Schema{
isLBListenerPolicyRuleLBID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The load balancer identifier.",
},
isLBListenerPolicyRuleListenerID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The listener identifier.",
},
isLBListenerPolicyRulePolicyID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The policy identifier.",
},
"rules": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of rules.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"condition": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The condition of the rule.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this rule was created.",
},
"field": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The field. This is applicable to `header`, `query`, and `body` rule types.If the rule type is `header`, this property is required.If the rule type is `query`, this is optional. If specified and the rule condition is not`matches_regex`, the value must be percent-encoded.If the rule type is `body`, this is optional.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The rule's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The rule's unique identifier.",
},
"provisioning_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The provisioning status of this rule.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of the rule.Body rules are applied to form-encoded request bodies using the `UTF-8` character set.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Value to be matched for rule condition.If the rule type is `query` and the rule condition is not `matches_regex`, the value must be percent-encoded.",
},
},
},
},
},
}
}
func dataSourceIBMIsLbListenerPolicyRulesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listLoadBalancerListenerPolicyRulesOptions := &vpcv1.ListLoadBalancerListenerPolicyRulesOptions{}
listLoadBalancerListenerPolicyRulesOptions.SetLoadBalancerID(d.Get(isLBListenerPolicyRuleLBID).(string))
listLoadBalancerListenerPolicyRulesOptions.SetListenerID(d.Get(isLBListenerPolicyRuleListenerID).(string))
listLoadBalancerListenerPolicyRulesOptions.SetPolicyID(d.Get(isLBListenerPolicyRulePolicyID).(string))
loadBalancerListenerPolicyRuleCollection, response, err := vpcClient.ListLoadBalancerListenerPolicyRulesWithContext(context, listLoadBalancerListenerPolicyRulesOptions)
if err != nil {
log.Printf("[DEBUG] ListLoadBalancerListenerPolicyRulesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListLoadBalancerListenerPolicyRulesWithContext failed %s\n%s", err, response))
}
d.SetId(dataSourceIBMIsLbListenerPolicyRulesID(d))
if loadBalancerListenerPolicyRuleCollection.Rules != nil {
err = d.Set("rules", dataSourceLoadBalancerListenerPolicyRuleCollectionFlattenRules(loadBalancerListenerPolicyRuleCollection.Rules))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting rules %s", err))
}
}
return nil
}
// dataSourceIBMIsLbListenerPolicyRulesID returns a reasonable ID for the list.
func dataSourceIBMIsLbListenerPolicyRulesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceLoadBalancerListenerPolicyRuleCollectionFlattenRules(result []vpcv1.LoadBalancerListenerPolicyRule) (rules []map[string]interface{}) {
for _, rulesItem := range result {
rules = append(rules, dataSourceLoadBalancerListenerPolicyRuleCollectionRulesToMap(rulesItem))
}
return rules
}
func dataSourceLoadBalancerListenerPolicyRuleCollectionRulesToMap(rulesItem vpcv1.LoadBalancerListenerPolicyRule) (rulesMap map[string]interface{}) {
rulesMap = map[string]interface{}{}
if rulesItem.Condition != nil {
rulesMap["condition"] = rulesItem.Condition
}
if rulesItem.CreatedAt != nil {
rulesMap["created_at"] = rulesItem.CreatedAt.String()
}
if rulesItem.Field != nil {
rulesMap["field"] = rulesItem.Field
}
if rulesItem.Href != nil {
rulesMap["href"] = rulesItem.Href
}
if rulesItem.ID != nil {
rulesMap["id"] = rulesItem.ID
}
if rulesItem.ProvisioningStatus != nil {
rulesMap["provisioning_status"] = rulesItem.ProvisioningStatus
}
if rulesItem.Type != nil {
rulesMap["type"] = rulesItem.Type
}
if rulesItem.Value != nil {
rulesMap["value"] = rulesItem.Value
}
return rulesMap
}