forked from hashicorp/terraform-provider-azurerm
/
data_source_monitor_diagnostic_categories.go
89 lines (73 loc) · 2.42 KB
/
data_source_monitor_diagnostic_categories.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
package azurerm
import (
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)
func dataSourceArmMonitorDiagnosticCategories() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmMonitorDiagnosticCategoriesRead,
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
"logs": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
"metrics": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
},
}
}
func dataSourceArmMonitorDiagnosticCategoriesRead(d *schema.ResourceData, meta interface{}) error {
categoriesClient := meta.(*ArmClient).monitorDiagnosticSettingsCategoryClient
ctx := meta.(*ArmClient).StopContext
actualResourceId := d.Get("resource_id").(string)
// trim off the leading `/` since the CheckExistenceByID / List methods don't expect it
resourceId := strings.TrimPrefix(actualResourceId, "/")
// then retrieve the possible Diagnostics Categories for this Resource
categories, err := categoriesClient.List(ctx, resourceId)
if err != nil {
return fmt.Errorf("Error retrieving Diagnostics Categories for Resource %q: %+v", actualResourceId, err)
}
if categories.Value == nil {
return fmt.Errorf("Error retrieving Diagnostics Categories for Resource %q: `categories.Value` was nil", actualResourceId)
}
d.SetId(actualResourceId)
val := *categories.Value
metrics := make([]string, 0)
logs := make([]string, 0)
for _, v := range val {
if v.Name == nil {
continue
}
if category := v.DiagnosticSettingsCategory; category != nil {
switch category.CategoryType {
case insights.Logs:
logs = append(logs, *v.Name)
case insights.Metrics:
metrics = append(metrics, *v.Name)
default:
return fmt.Errorf("Unsupported category type %q", string(category.CategoryType))
}
}
}
if err := d.Set("logs", logs); err != nil {
return fmt.Errorf("Error setting `logs`: %+v", err)
}
if err := d.Set("metrics", metrics); err != nil {
return fmt.Errorf("Error setting `metrics`: %+v", err)
}
return nil
}