forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_time_series_insights_reference_data_set_resource.go
246 lines (209 loc) · 8.64 KB
/
iot_time_series_insights_reference_data_set_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package iottimeseriesinsights
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/timeseriesinsights/mgmt/2020-05-15/timeseriesinsights"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/iottimeseriesinsights/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/iottimeseriesinsights/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/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceIoTTimeSeriesInsightsReferenceDataSet() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceIoTTimeSeriesInsightsReferenceDataSetCreateUpdate,
Read: resourceIoTTimeSeriesInsightsReferenceDataSetRead,
Update: resourceIoTTimeSeriesInsightsReferenceDataSetCreateUpdate,
Delete: resourceIoTTimeSeriesInsightsReferenceDataSetDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ReferenceDataSetID(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,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[A-Za-z0-9]{3,63}`),
"IoT Time Series Insights Reference Data Set name must contain only alphanumeric characters and be between 3 and 63 characters.",
),
},
"time_series_insights_environment_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.TimeSeriesInsightsEnvironmentID,
},
"data_string_comparison_behavior": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(timeseriesinsights.Ordinal),
ValidateFunc: validation.StringInSlice([]string{
string(timeseriesinsights.Ordinal),
string(timeseriesinsights.OrdinalIgnoreCase),
}, false),
},
"key_property": {
Type: pluginsdk.TypeSet,
Required: true,
ForceNew: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"type": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(timeseriesinsights.ReferenceDataKeyPropertyTypeBool),
string(timeseriesinsights.ReferenceDataKeyPropertyTypeDateTime),
string(timeseriesinsights.ReferenceDataKeyPropertyTypeDouble),
string(timeseriesinsights.ReferenceDataKeyPropertyTypeString),
}, false),
},
},
},
},
"location": azure.SchemaLocation(),
"tags": tags.Schema(),
},
}
}
func resourceIoTTimeSeriesInsightsReferenceDataSetCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
environmentID := d.Get("time_series_insights_environment_id").(string)
id, err := parse.EnvironmentID(environmentID)
if err != nil {
return err
}
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing IoT Time Series Insights Reference Data Set %q (Resource Group %q): %s", name, id.ResourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_iot_time_series_insights_reference_data_set", *existing.ID)
}
}
dataset := timeseriesinsights.ReferenceDataSetCreateOrUpdateParameters{
Location: &location,
Tags: tags.Expand(t),
ReferenceDataSetCreationProperties: ×eriesinsights.ReferenceDataSetCreationProperties{
DataStringComparisonBehavior: timeseriesinsights.DataStringComparisonBehavior(d.Get("data_string_comparison_behavior").(string)),
KeyProperties: expandIoTTimeSeriesInsightsReferenceDataSetKeyProperties(d.Get("key_property").(*pluginsdk.Set).List()),
},
}
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, dataset); err != nil {
return fmt.Errorf("creating/updating IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name)
if err != nil {
return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("cannot read IoT Time Series Insights Reference Data Set %q (Resource Group %q) ID", name, id.ResourceGroup)
}
d.SetId(*resp.ID)
return resourceIoTTimeSeriesInsightsReferenceDataSetRead(d, meta)
}
func resourceIoTTimeSeriesInsightsReferenceDataSetRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ReferenceDataSetID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.EnvironmentName, id.Name)
if err != nil || resp.ID == nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("time_series_insights_environment_id", strings.Split(d.Id(), "/referenceDataSets")[0])
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.ReferenceDataSetResourceProperties; props != nil {
d.Set("data_string_comparison_behavior", string(props.DataStringComparisonBehavior))
if err := d.Set("key_property", flattenIoTTimeSeriesInsightsReferenceDataSetKeyProperties(props.KeyProperties)); err != nil {
return fmt.Errorf("setting `key_property`: %+v", err)
}
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceIoTTimeSeriesInsightsReferenceDataSetDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ReferenceDataSetID(d.Id())
if err != nil {
return err
}
response, err := client.Delete(ctx, id.ResourceGroup, id.EnvironmentName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("deleting IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
return nil
}
func expandIoTTimeSeriesInsightsReferenceDataSetKeyProperties(input []interface{}) *[]timeseriesinsights.ReferenceDataSetKeyProperty {
properties := make([]timeseriesinsights.ReferenceDataSetKeyProperty, 0)
for _, v := range input {
if v == nil {
continue
}
attr := v.(map[string]interface{})
properties = append(properties, timeseriesinsights.ReferenceDataSetKeyProperty{
Type: timeseriesinsights.ReferenceDataKeyPropertyType(attr["type"].(string)),
Name: utils.String(attr["name"].(string)),
})
}
return &properties
}
func flattenIoTTimeSeriesInsightsReferenceDataSetKeyProperties(input *[]timeseriesinsights.ReferenceDataSetKeyProperty) []interface{} {
if input == nil {
return []interface{}{}
}
properties := make([]interface{}, 0)
for _, property := range *input {
attr := make(map[string]interface{})
attr["type"] = string(property.Type)
if name := property.Name; name != nil {
attr["name"] = *property.Name
}
properties = append(properties, attr)
}
return properties
}