-
Notifications
You must be signed in to change notification settings - Fork 117
/
datasource_bigip_waf_pb_suggestions.go
108 lines (101 loc) · 3.05 KB
/
datasource_bigip_waf_pb_suggestions.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
package bigip
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/f5devcentral/go-bigip"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceBigipWafPb() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBigipWafPbRead,
Schema: map[string]*schema.Schema{
"policy_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the WAF policy",
},
"partition": {
Type: schema.TypeString,
Required: true,
Description: "Partition where the WAF policy is deployed",
},
"minimum_learning_score": {
Type: schema.TypeInt,
Required: true,
Description: "The minimum learning for suggestions",
ValidateFunc: validation.IntBetween(1, 100),
},
"policy_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "System generated id of the WAF policy",
},
"json": {
Type: schema.TypeString,
Computed: true,
Description: "The return payload of the queried PB suggestions",
},
},
}
}
type ExportPb struct {
PolicyReference map[string]string `json:"policyReference,omitempty"`
Inline bool `json:"inline,omitempty"`
Filter string `json:"filter,omitempty"`
}
func dataSourceBigipWafPbRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
d.SetId("")
policyName := d.Get("policy_name").(string)
partition := d.Get("partition").(string)
score := d.Get("minimum_learning_score").(int)
policyId, err := client.GetWafPolicyId(policyName, partition)
if err != nil {
return diag.FromErr(fmt.Errorf("error retrieving policy %s on partition %s", policyName, partition))
}
policyLink := fmt.Sprintf("https://localhost/mgmt/tm/asm/policies/%s", policyId)
payload := ExportPb{
PolicyReference: map[string]string{"link": policyLink},
Inline: true,
Filter: fmt.Sprintf("score gt %d", score),
}
export, err := client.PostPbExport(payload)
if err != nil {
return diag.FromErr(fmt.Errorf("error exporting pb suggestions: %v", err))
}
task, err := client.GetWafPbExportResult(export.Task_id)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG]Initial response export status %v", task.Status)
for task.Status != "COMPLETED" && task.Status != "FAILURE" {
pbtask, err := client.GetWafPbExportResult(export.Task_id)
if err != nil {
return diag.FromErr(err)
}
task = pbtask
if task.Status == "FAILURE" || task.Status == "COMPLETED" {
break
}
time.Sleep(3 * time.Second)
}
if task.Status == "FAILURE" {
return diag.FromErr(fmt.Errorf("export task failed"))
}
if task.Status == "COMPLETED" {
pbJson, err := json.Marshal(task.Result)
if err != nil {
return diag.FromErr(err)
}
_ = d.Set("policy_id", policyId)
_ = d.Set("json", string(pbJson))
d.SetId(policyName)
}
return nil
}