forked from hashicorp/terraform-provider-aws
/
resource_aws_guardduty_detector.go
114 lines (95 loc) · 3.35 KB
/
resource_aws_guardduty_detector.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/guardduty"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsGuardDutyDetector() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGuardDutyDetectorCreate,
Read: resourceAwsGuardDutyDetectorRead,
Update: resourceAwsGuardDutyDetectorUpdate,
Delete: resourceAwsGuardDutyDetectorDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"finding_publishing_frequency": {
Type: schema.TypeString,
Optional: true,
Default: "SIX_HOURS",
},
},
}
}
func resourceAwsGuardDutyDetectorCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
input := guardduty.CreateDetectorInput{
Enable: aws.Bool(d.Get("enable").(bool)),
FindingPublishingFrequency: aws.String(d.Get("finding_publishing_frequency").(string)),
}
log.Printf("[DEBUG] Creating GuardDuty Detector: %s", input)
output, err := conn.CreateDetector(&input)
if err != nil {
return fmt.Errorf("Creating GuardDuty Detector failed: %s", err.Error())
}
d.SetId(*output.DetectorId)
return resourceAwsGuardDutyDetectorRead(d, meta)
}
func resourceAwsGuardDutyDetectorRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
input := guardduty.GetDetectorInput{
DetectorId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Reading GuardDuty Detector: %s", input)
gdo, err := conn.GetDetector(&input)
if err != nil {
if isAWSErr(err, guardduty.ErrCodeBadRequestException, "The request is rejected because the input detectorId is not owned by the current account.") {
log.Printf("[WARN] GuardDuty detector %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Reading GuardDuty Detector '%s' failed: %s", d.Id(), err.Error())
}
d.Set("account_id", meta.(*AWSClient).accountid)
d.Set("enable", *gdo.Status == guardduty.DetectorStatusEnabled)
d.Set("finding_publishing_frequency", gdo.FindingPublishingFrequency)
return nil
}
func resourceAwsGuardDutyDetectorUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
input := guardduty.UpdateDetectorInput{
DetectorId: aws.String(d.Id()),
Enable: aws.Bool(d.Get("enable").(bool)),
FindingPublishingFrequency: aws.String(d.Get("finding_publishing_frequency").(string)),
}
log.Printf("[DEBUG] Update GuardDuty Detector: %s", input)
_, err := conn.UpdateDetector(&input)
if err != nil {
return fmt.Errorf("Updating GuardDuty Detector '%s' failed: %s", d.Id(), err.Error())
}
return resourceAwsGuardDutyDetectorRead(d, meta)
}
func resourceAwsGuardDutyDetectorDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
input := guardduty.DeleteDetectorInput{
DetectorId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Delete GuardDuty Detector: %s", input)
_, err := conn.DeleteDetector(&input)
if err != nil {
return fmt.Errorf("Deleting GuardDuty Detector '%s' failed: %s", d.Id(), err.Error())
}
return nil
}