/
resource_aviatrix_cloudwatch_agent.go
131 lines (110 loc) · 3.58 KB
/
resource_aviatrix_cloudwatch_agent.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
package aviatrix
import (
"fmt"
"strings"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v2/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixCloudwatchAgent() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixCloudwatchAgentCreate,
Read: resourceAviatrixCloudwatchAgentRead,
Delete: resourceAviatrixCloudwatchAgentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"cloudwatch_role_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "CloudWatch role ARN.",
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of AWS region.",
},
"log_group_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "AVIATRIX-CLOUDWATCH-LOG",
Description: "Log group name.",
},
"excluded_gateways": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Description: "List of excluded gateways.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Enabled or not.",
},
},
}
}
func marshalCloudwatchAgentInput(d *schema.ResourceData) *goaviatrix.CloudwatchAgent {
cloudwatchAgent := &goaviatrix.CloudwatchAgent{
RoleArn: d.Get("cloudwatch_role_arn").(string),
Region: d.Get("region").(string),
LogGroupName: d.Get("log_group_name").(string),
}
var excludedGateways []string
for _, v := range d.Get("excluded_gateways").(*schema.Set).List() {
excludedGateways = append(excludedGateways, v.(string))
}
if len(excludedGateways) != 0 {
cloudwatchAgent.ExcludedGatewaysInput = strings.Join(excludedGateways, ",")
}
return cloudwatchAgent
}
func resourceAviatrixCloudwatchAgentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
_, err := client.GetCloudwatchAgentStatus()
if err != goaviatrix.ErrNotFound {
return fmt.Errorf("the cloudwatch_agent is already enabled, please import to manage with Terraform")
}
cloudwatchAgent := marshalCloudwatchAgentInput(d)
if err := client.EnableCloudwatchAgent(cloudwatchAgent); err != nil {
return fmt.Errorf("could not enable cloudwatch agent: %v", err)
}
d.SetId("cloudwatch_agent")
return nil
}
func resourceAviatrixCloudwatchAgentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
if d.Id() != "cloudwatch_agent" {
return fmt.Errorf("invalid ID, expected ID \"cloudwatch_agent\", instead got %s", d.Id())
}
cloudwatchAgentStatus, err := client.GetCloudwatchAgentStatus()
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("could not get cloudwatch agent status: %v", err)
}
d.Set("cloudwatch_role_arn", cloudwatchAgentStatus.RoleArn)
d.Set("region", cloudwatchAgentStatus.Region)
d.Set("log_group_name", cloudwatchAgentStatus.LogGroupName)
if len(cloudwatchAgentStatus.ExcludedGateways) != 0 {
d.Set("excluded_gateways", cloudwatchAgentStatus.ExcludedGateways)
}
d.Set("status", cloudwatchAgentStatus.Status)
d.SetId("cloudwatch_agent")
return nil
}
func resourceAviatrixCloudwatchAgentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
if err := client.DisableCloudwatchAgent(); err != nil {
return fmt.Errorf("could not disable cloudwatch agent: %v", err)
}
return nil
}