forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal_dashboard_resource.go
146 lines (122 loc) · 4.71 KB
/
portal_dashboard_resource.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
package portal
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/portal/mgmt/2019-01-01-preview/portal"
"github.com/hashicorp/go-azure-helpers/lang/response"
"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/portal/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/portal/validate"
"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 resourceDashboard() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDashboardCreateUpdate,
Read: resourceDashboardRead,
Update: resourceDashboardCreateUpdate,
Delete: resourceDashboardDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.DashboardID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.DashboardName,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"location": azure.SchemaLocation(),
"tags": tags.Schema(),
"dashboard_properties": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
StateFunc: utils.NormalizeJson,
},
},
}
}
func resourceDashboardCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Portal.DashboardsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
t := d.Get("tags").(map[string]interface{})
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
dashboardProps := d.Get("dashboard_properties").(string)
// TODO: requires import support
dashboard := portal.Dashboard{
Location: &location,
Tags: tags.Expand(t),
}
var dashboardProperties portal.DashboardProperties
if err := json.Unmarshal([]byte(dashboardProps), &dashboardProperties); err != nil {
return fmt.Errorf("parsing JSON: %+v", err)
}
dashboard.DashboardProperties = &dashboardProperties
if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, dashboard); err != nil {
return fmt.Errorf("creating/updating Dashboard %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(parse.NewDashboardID(subscriptionId, resourceGroup, name).ID())
return resourceDashboardRead(d, meta)
}
func resourceDashboardRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Portal.DashboardsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.DashboardID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Dashboard %q was not found in Resource Group %q - removing from state", id.Name, id.ResourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Dashboard %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*resp.Location))
}
props, jsonErr := json.Marshal(resp.DashboardProperties)
if jsonErr != nil {
return fmt.Errorf("parsing JSON for Dashboard Properties: %+v", jsonErr)
}
d.Set("dashboard_properties", string(props))
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceDashboardDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Portal.DashboardsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.DashboardID(d.Id())
if err != nil {
return err
}
resp, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("deleting Dashboard %q (Resource Group %q): %+v", id.Name, id.Name, err)
}
}
return nil
}