forked from hashicorp/terraform-provider-aws
/
resource_aws_securityhub_standards_subscription.go
93 lines (72 loc) · 2.63 KB
/
resource_aws_securityhub_standards_subscription.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsSecurityHubStandardsSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSecurityHubStandardsSubscriptionCreate,
Read: resourceAwsSecurityHubStandardsSubscriptionRead,
Delete: resourceAwsSecurityHubStandardsSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"standards_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
},
}
}
func resourceAwsSecurityHubStandardsSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn
log.Printf("[DEBUG] Enabling Security Hub standard %s", d.Get("standards_arn"))
resp, err := conn.BatchEnableStandards(&securityhub.BatchEnableStandardsInput{
StandardsSubscriptionRequests: []*securityhub.StandardsSubscriptionRequest{
{
StandardsArn: aws.String(d.Get("standards_arn").(string)),
},
},
})
if err != nil {
return fmt.Errorf("Error enabling Security Hub standard: %s", err)
}
standardsSubscription := resp.StandardsSubscriptions[0]
d.SetId(*standardsSubscription.StandardsSubscriptionArn)
return resourceAwsSecurityHubStandardsSubscriptionRead(d, meta)
}
func resourceAwsSecurityHubStandardsSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn
log.Printf("[DEBUG] Reading Security Hub standard %s", d.Id())
resp, err := conn.GetEnabledStandards(&securityhub.GetEnabledStandardsInput{
StandardsSubscriptionArns: []*string{aws.String(d.Id())},
})
if err != nil {
return fmt.Errorf("Error reading Security Hub standard %s: %s", d.Id(), err)
}
if len(resp.StandardsSubscriptions) == 0 {
log.Printf("[WARN] Security Hub standard (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
standardsSubscription := resp.StandardsSubscriptions[0]
d.Set("standards_arn", standardsSubscription.StandardsArn)
return nil
}
func resourceAwsSecurityHubStandardsSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn
log.Printf("[DEBUG] Disabling Security Hub standard %s", d.Id())
_, err := conn.BatchDisableStandards(&securityhub.BatchDisableStandardsInput{
StandardsSubscriptionArns: []*string{aws.String(d.Id())},
})
if err != nil {
return fmt.Errorf("Error disabling Security Hub standard %s: %s", d.Id(), err)
}
return nil
}