forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinel_alert_rule_data_source.go
65 lines (53 loc) · 2.16 KB
/
sentinel_alert_rule_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
package sentinel
import (
"fmt"
"time"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
loganalyticsParse "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/loganalytics/parse"
loganalyticsValidate "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/loganalytics/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/sentinel/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func dataSourceSentinelAlertRule() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceSentinelAlertRuleRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: loganalyticsValidate.LogAnalyticsWorkspaceID,
},
},
}
}
func dataSourceSentinelAlertRuleRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Sentinel.AlertRulesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
workspaceID, err := loganalyticsParse.LogAnalyticsWorkspaceID(d.Get("log_analytics_workspace_id").(string))
if err != nil {
return err
}
id := parse.NewAlertRuleID(workspaceID.SubscriptionId, workspaceID.ResourceGroup, workspaceID.WorkspaceName, name)
resp, err := client.Get(ctx, workspaceID.ResourceGroup, OperationalInsightsResourceProvider, workspaceID.WorkspaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Sentinel Alert Rule %q was not found", id)
}
return fmt.Errorf("retrieving Sentinel Alert Rule %q: %+v", id, err)
}
d.SetId(id.ID())
return nil
}