/
data_akamai_botman_bot_detection.go
61 lines (52 loc) · 1.58 KB
/
data_akamai_botman_bot_detection.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
package botman
import (
"context"
"encoding/json"
"errors"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/botman"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/hash"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceBotDetection() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBotDetectionRead,
Schema: map[string]*schema.Schema{
"detection_name": {
Type: schema.TypeString,
Optional: true,
},
"json": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceBotDetectionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
logger := meta.Log("botman", "dataSourceBotDetectionRead")
detectionName, err := tf.GetStringValue("detection_name", d)
if err != nil && !errors.Is(err, tf.ErrNotFound) {
return diag.FromErr(err)
}
request := botman.GetBotDetectionListRequest{
DetectionName: detectionName,
}
response, err := getBotDetectionList(ctx, request, m)
if err != nil {
logger.Errorf("calling 'GetBotDetectionList': %s", err.Error())
return diag.FromErr(err)
}
jsonBody, err := json.Marshal(response)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("json", string(jsonBody)); err != nil {
return diag.Errorf("%s: %s", tf.ErrValueSet, err.Error())
}
d.SetId(hash.GetSHAString(string(jsonBody)))
return nil
}