-
Notifications
You must be signed in to change notification settings - Fork 221
/
data_source_rancher2_project_alert_rule.go
122 lines (112 loc) · 2.82 KB
/
data_source_rancher2_project_alert_rule.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
package rancher2
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceRancher2ProjectAlertRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ProjectAlertRuleRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule project ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule name",
},
"metric_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: metricRuleFields(),
},
Description: "Alert metric rule",
},
"pod_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: podRuleFields(),
},
Description: "Alert pod rule",
},
"workload_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: workloadRuleFields(),
},
Description: "Alert workload rule",
},
"group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Alert rule group ID",
},
"group_interval_seconds": {
Type: schema.TypeInt,
Computed: true,
Description: "Alert rule interval seconds",
},
"group_wait_seconds": {
Type: schema.TypeInt,
Computed: true,
Description: "Alert rule wait seconds",
},
"inherited": {
Type: schema.TypeBool,
Computed: true,
Description: "Alert rule inherited",
},
"repeat_interval_seconds": {
Type: schema.TypeInt,
Computed: true,
Description: "Alert rule repeat interval seconds",
},
"severity": {
Type: schema.TypeString,
Computed: true,
Description: "Alert rule severity",
},
"annotations": {
Type: schema.TypeMap,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func dataSourceRancher2ProjectAlertRuleRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}
projectID := d.Get("project_id").(string)
name := d.Get("name").(string)
filters := map[string]interface{}{
"projectId": projectID,
"name": name,
}
listOpts := NewListOpts(filters)
alertRules, err := client.ProjectAlertRule.List(listOpts)
if err != nil {
return err
}
count := len(alertRules.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] project alert rule with name \"%s\" on project ID \"%s\" not found", name, projectID)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d project alert rule with name \"%s\" on project ID \"%s\"", count, name, projectID)
}
return flattenProjectAlertRule(d, &alertRules.Data[0])
}