forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_scheduled_query_rules_log_data_source.go
153 lines (134 loc) · 4.36 KB
/
monitor_scheduled_query_rules_log_data_source.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
package monitor
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-07-01-preview/insights"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/monitor/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tags"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func dataSourceMonitorScheduledQueryRulesLog() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceMonitorScheduledQueryRulesLogRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"location": azure.SchemaLocationForDataSource(),
"authorized_resource_ids": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"criteria": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dimension": {
Type: pluginsdk.TypeSet,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"metric_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
"data_source_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"tags": tags.SchemaDataSource(),
},
}
}
func dataSourceMonitorScheduledQueryRulesLogRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Monitor.ScheduledQueryRulesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id := parse.NewScheduledQueryRulesID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroup, id.ScheduledQueryRuleName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("[DEBUG] Scheduled Query Rule %q was not found in Resource Group %q: %+v", id.ScheduledQueryRuleName, id.ResourceGroup, err)
}
return fmt.Errorf("getting Monitor %s: %+v", id, err)
}
d.SetId(id.ID())
d.Set("name", id.ScheduledQueryRuleName)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("description", resp.Description)
if resp.Enabled == insights.EnabledTrue {
d.Set("enabled", true)
} else {
d.Set("enabled", false)
}
action, ok := resp.Action.(insights.LogToMetricAction)
if !ok {
return fmt.Errorf("wrong action type in %s: %T", id, resp.Action)
}
if err = d.Set("criteria", flattenAzureRmScheduledQueryRulesLogCriteria(action.Criteria)); err != nil {
return fmt.Errorf("setting `criteria`: %+v", err)
}
if schedule := resp.Schedule; schedule != nil {
if schedule.FrequencyInMinutes != nil {
d.Set("frequency", schedule.FrequencyInMinutes)
}
if schedule.TimeWindowInMinutes != nil {
d.Set("time_window", schedule.TimeWindowInMinutes)
}
}
if source := resp.Source; source != nil {
if source.AuthorizedResources != nil {
d.Set("authorized_resource_ids", utils.FlattenStringSlice(source.AuthorizedResources))
}
if source.DataSourceID != nil {
d.Set("data_source_id", source.DataSourceID)
}
}
return tags.FlattenAndSet(d, resp.Tags)
}