-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_volcengine_tls_alarm.go
240 lines (229 loc) · 8.4 KB
/
resource_volcengine_tls_alarm.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package alarm
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
)
/*
Import
tls alarm can be imported using the id and project id, e.g.
```
$ terraform import volcengine_tls_alarm.default projectId:fc************
```
*/
func ResourceVolcengineTlsAlarm() *schema.Resource {
return &schema.Resource{
Read: resourceVolcengineTlsAlarmRead,
Create: resourceVolcengineTlsAlarmCreate,
Update: resourceVolcengineTlsAlarmUpdate,
Delete: resourceVolcengineTlsAlarmDelete,
Importer: &schema.ResourceImporter{
State: importTlsAlarmApply,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"alarm_name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the alarm.",
},
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The project id.",
},
"status": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether to enable the alert policy. The default value is true, that is, on.",
},
"trigger_period": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "Continuous cycle. The alarm will be issued after the trigger condition is continuously met for TriggerPeriod periods; the minimum value is 1, the maximum value is 10, and the default value is 1.",
},
"alarm_period": {
Type: schema.TypeInt,
Optional: true,
ExactlyOneOf: []string{"alarm_period_detail", "alarm_period"},
Description: "Period for sending alarm notifications. When the number of continuous alarm triggers reaches the specified limit (TriggerPeriod), Log Service will send alarm notifications according to the specified period.",
},
"alarm_notify_group": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of notification groups corresponding to the alarm.",
},
"user_define_msg": {
Type: schema.TypeString,
Optional: true,
Description: "Customize the alarm notification content.",
},
"query_request": {
Type: schema.TypeSet,
Required: true,
MaxItems: 3,
MinItems: 1,
Set: func(i interface{}) int {
if i == nil {
return hashcode.String("")
}
m := i.(map[string]interface{})
var (
buf bytes.Buffer
)
buf.WriteString(fmt.Sprintf("%v#%v#%v#%v#%v", m["topic_id"], m["query"], m["number"], m["start_time_offset"], m["end_time_offset"]))
return hashcode.String(buf.String())
},
Description: "Search and analyze sentences, 1~3 can be configured.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"topic_id": {
Type: schema.TypeString,
Required: true,
Description: "The id of the topic.",
},
"query": {
Type: schema.TypeString,
Required: true,
Description: "Query statement, the maximum supported length is 1024.",
},
"number": {
Type: schema.TypeInt,
Required: true,
Description: "Alarm object sequence number; increments from 1.",
},
"start_time_offset": {
Type: schema.TypeInt,
Required: true,
Description: "The start time of the query range is relative to the current historical time, in minutes. The value is non-positive, the maximum value is 0, and the minimum value is -1440.",
},
"end_time_offset": {
Type: schema.TypeInt,
Required: true,
Description: "The end time of the query range is relative to the current historical time. The unit is minutes. The value is not positive and must be greater than StartTimeOffset. The maximum value is 0 and the minimum value is -1440.",
},
},
},
},
"condition": {
Required: true,
Type: schema.TypeString,
Description: "Alarm trigger condition.",
},
"request_cycle": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "The execution period of the alarm task.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Description: "Execution cycle type.\nPeriod: Periodic execution, which means executing once every certain period of time.\nFixed: Regular execution, which means executing at a fixed time point every day.",
},
"time": {
Type: schema.TypeInt,
Required: true,
Description: "The cycle of alarm task execution, or the time point of periodic execution. The unit is minutes, and the value range is 1~1440.",
},
},
},
},
"alarm_period_detail": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: []string{"alarm_period_detail", "alarm_period"},
Description: "Period for sending alarm notifications. When the number of continuous alarm triggers reaches the specified limit (TriggerPeriod), Log Service will send alarm notifications according to the specified period.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sms": {
Type: schema.TypeInt,
Required: true,
Description: "SMS alarm cycle, the unit is minutes, and the value range is 10~1440.",
},
"phone": {
Type: schema.TypeInt,
Required: true,
Description: "Telephone alarm cycle, the unit is minutes, and the value range is 10~1440.",
},
"email": {
Type: schema.TypeInt,
Required: true,
Description: "Email alarm period, the unit is minutes, and the value range is 1~1440.",
},
"general_webhook": {
Type: schema.TypeInt,
Required: true,
Description: "Customize the webhook alarm period, the unit is minutes, and the value range is 1~1440.",
},
},
},
},
"alarm_id": {
Type: schema.TypeString,
Computed: true,
Description: "The alarm id.",
},
},
}
}
func resourceVolcengineTlsAlarmCreate(d *schema.ResourceData, meta interface{}) error {
TlsAlarmService := NewVolcengineTlsAlarmService(meta.(*ve.SdkClient))
if err := ve.DefaultDispatcher().Create(TlsAlarmService, d, ResourceVolcengineTlsAlarm()); err != nil {
return fmt.Errorf("error on creating tls Alarm %q, %w", d.Id(), err)
}
return resourceVolcengineTlsAlarmRead(d, meta)
}
func resourceVolcengineTlsAlarmRead(d *schema.ResourceData, meta interface{}) error {
TlsAlarmService := NewVolcengineTlsAlarmService(meta.(*ve.SdkClient))
if err := ve.DefaultDispatcher().Read(TlsAlarmService, d, ResourceVolcengineTlsAlarm()); err != nil {
return fmt.Errorf("error on reading tls Alarm %q, %w", d.Id(), err)
}
return nil
}
func resourceVolcengineTlsAlarmUpdate(d *schema.ResourceData, meta interface{}) error {
TlsAlarmService := NewVolcengineTlsAlarmService(meta.(*ve.SdkClient))
if err := ve.DefaultDispatcher().Update(TlsAlarmService, d, ResourceVolcengineTlsAlarm()); err != nil {
return fmt.Errorf("error on updating tls Alarm %q, %w", d.Id(), err)
}
return resourceVolcengineTlsAlarmRead(d, meta)
}
func resourceVolcengineTlsAlarmDelete(d *schema.ResourceData, meta interface{}) error {
TlsAlarmService := NewVolcengineTlsAlarmService(meta.(*ve.SdkClient))
if err := ve.DefaultDispatcher().Delete(TlsAlarmService, d, ResourceVolcengineTlsAlarm()); err != nil {
return fmt.Errorf("error on deleting tls Alarm %q, %w", d.Id(), err)
}
return nil
}
func importTlsAlarmApply(data *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
var err error
items := strings.Split(data.Id(), ":")
if len(items) != 2 {
return []*schema.ResourceData{data}, fmt.Errorf("import id must be of the form project_id:alarm_id")
}
err = data.Set("project_id", items[0])
if err != nil {
return []*schema.ResourceData{data}, err
}
err = data.Set("alarm_id", items[1])
if err != nil {
return []*schema.ResourceData{data}, err
}
return []*schema.ResourceData{data}, nil
}