This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
forked from rancher/terraform-provider-rancher2
/
data_source_rancher2_cluster_alert_rule.go
131 lines (121 loc) · 3.03 KB
/
data_source_rancher2_cluster_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
123
124
125
126
127
128
129
130
131
package rancher2
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceRancher2ClusterAlertRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ClusterAlertRuleRead,
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule cluster ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule name",
},
"event_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: eventRuleFields(),
},
Description: "Alert event rule",
},
"metric_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: metricRuleFields(),
},
Description: "Alert metric rule",
},
"node_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: nodeRuleFields(),
},
Description: "Alert node rule",
},
"system_service_rule": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: systemServiceRuleFields(),
},
Description: "Alert system service 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 dataSourceRancher2ClusterAlertRuleRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}
clusterID := d.Get("cluster_id").(string)
name := d.Get("name").(string)
filters := map[string]interface{}{
"clusterId": clusterID,
"name": name,
}
listOpts := NewListOpts(filters)
alertRules, err := client.ClusterAlertRule.List(listOpts)
if err != nil {
return err
}
count := len(alertRules.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] cluster alert rule with name \"%s\" on cluster ID \"%s\" not found", name, clusterID)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d cluster alert rule with name \"%s\" on cluster ID \"%s\"", count, name, clusterID)
}
return flattenClusterAlertRule(d, &alertRules.Data[0])
}