-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
iot_time_series_insights_access_policy_resource.go
209 lines (175 loc) · 6.82 KB
/
iot_time_series_insights_access_policy_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package iottimeseriesinsights
import (
"fmt"
"regexp"
"time"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/timeseriesinsights/2020-05-15/accesspolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/timeseriesinsights/2020-05-15/environments"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iottimeseriesinsights/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
func resourceIoTTimeSeriesInsightsAccessPolicy() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceIoTTimeSeriesInsightsAccessPolicyCreateUpdate,
Read: resourceIoTTimeSeriesInsightsAccessPolicyRead,
Update: resourceIoTTimeSeriesInsightsAccessPolicyCreateUpdate,
Delete: resourceIoTTimeSeriesInsightsAccessPolicyDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := accesspolicies.ParseAccessPolicyID(id)
return err
}),
DeprecationMessage: "Azure Time Series Insight will be retired on 2025-03-31. As a result the `azurerm_iot_time_series_insights_access_policy` resource has been deprecated and will be removed in v4.0 of the AzureRM Provider.",
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),
},
SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.StandardEnvironmentAccessPolicyV0ToV1{},
}),
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[-\w\._\(\)]+$`),
"IoT Time Series Insights Access Policy name must contain only word characters, periods, underscores, hyphens, and parentheses.",
),
},
"time_series_insights_environment_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: environments.ValidateEnvironmentID,
},
"principal_object_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"description": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"roles": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(accesspolicies.AccessPolicyRoleContributor),
string(accesspolicies.AccessPolicyRoleReader),
}, false),
},
},
},
}
}
func resourceIoTTimeSeriesInsightsAccessPolicyCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.AccessPolicies
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
environmentId, err := environments.ParseEnvironmentID(d.Get("time_series_insights_environment_id").(string))
if err != nil {
return err
}
id := accesspolicies.NewAccessPolicyID(subscriptionId, environmentId.ResourceGroupName, environmentId.EnvironmentName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_iot_time_series_insights_access_policy", id.ID())
}
}
payload := accesspolicies.AccessPolicyCreateOrUpdateParameters{
Properties: accesspolicies.AccessPolicyResourceProperties{
Description: utils.String(d.Get("description").(string)),
PrincipalObjectId: utils.String(d.Get("principal_object_id").(string)),
Roles: expandIoTTimeSeriesInsightsAccessPolicyRoles(d.Get("roles").(*pluginsdk.Set).List()),
},
}
if _, err := client.CreateOrUpdate(ctx, id, payload); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}
d.SetId(id.ID())
return resourceIoTTimeSeriesInsightsAccessPolicyRead(d, meta)
}
func resourceIoTTimeSeriesInsightsAccessPolicyRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.AccessPolicies
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := accesspolicies.ParseAccessPolicyID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
d.Set("name", id.AccessPolicyName)
d.Set("time_series_insights_environment_id", environments.NewEnvironmentID(id.SubscriptionId, id.ResourceGroupName, id.EnvironmentName).ID())
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
d.Set("description", props.Description)
d.Set("principal_object_id", props.PrincipalObjectId)
if err := d.Set("roles", flattenIoTTimeSeriesInsightsAccessPolicyRoles(props.Roles)); err != nil {
return fmt.Errorf("setting `roles`: %+v", err)
}
}
}
return nil
}
func resourceIoTTimeSeriesInsightsAccessPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.AccessPolicies
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := accesspolicies.ParseAccessPolicyID(d.Id())
if err != nil {
return err
}
if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
return nil
}
func expandIoTTimeSeriesInsightsAccessPolicyRoles(input []interface{}) *[]accesspolicies.AccessPolicyRole {
roles := make([]accesspolicies.AccessPolicyRole, 0)
for _, v := range input {
if v == nil {
continue
}
roles = append(roles, accesspolicies.AccessPolicyRole(v.(string)))
}
return &roles
}
func flattenIoTTimeSeriesInsightsAccessPolicyRoles(input *[]accesspolicies.AccessPolicyRole) []interface{} {
result := make([]interface{}, 0)
if input != nil {
for _, item := range *input {
result = append(result, string(item))
}
}
return result
}