forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_analytics_stream_input_iothub_resource.go
220 lines (183 loc) · 7.58 KB
/
stream_analytics_stream_input_iothub_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
package streamanalytics
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/streamanalytics/mgmt/2020-03-01-preview/streamanalytics"
"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/streamanalytics/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 resourceStreamAnalyticsStreamInputIoTHub() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceStreamAnalyticsStreamInputIoTHubCreateUpdate,
Read: resourceStreamAnalyticsStreamInputIoTHubRead,
Update: resourceStreamAnalyticsStreamInputIoTHubCreateUpdate,
Delete: resourceStreamAnalyticsStreamInputIoTHubDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.StreamInputID(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.StringIsNotEmpty,
},
"stream_analytics_job_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"endpoint": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"iothub_namespace": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"eventhub_consumer_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"shared_access_policy_key": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"shared_access_policy_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"serialization": schemaStreamAnalyticsStreamInputSerialization(),
},
}
}
func resourceStreamAnalyticsStreamInputIoTHubCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).StreamAnalytics.InputsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for Azure Stream Analytics Stream Input IoTHub creation.")
resourceId := parse.NewStreamInputID(subscriptionId, d.Get("resource_group_name").(string), d.Get("stream_analytics_job_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.StreamingjobName, resourceId.InputName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %s", resourceId, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_stream_analytics_stream_input_iothub", resourceId.ID())
}
}
consumerGroupName := d.Get("eventhub_consumer_group_name").(string)
endpoint := d.Get("endpoint").(string)
iotHubNamespace := d.Get("iothub_namespace").(string)
sharedAccessPolicyKey := d.Get("shared_access_policy_key").(string)
sharedAccessPolicyName := d.Get("shared_access_policy_name").(string)
serializationRaw := d.Get("serialization").([]interface{})
serialization, err := expandStreamAnalyticsStreamInputSerialization(serializationRaw)
if err != nil {
return fmt.Errorf("expanding `serialization`: %+v", err)
}
props := streamanalytics.Input{
Name: utils.String(resourceId.InputName),
Properties: &streamanalytics.StreamInputProperties{
Type: streamanalytics.TypeStream,
Datasource: &streamanalytics.IoTHubStreamInputDataSource{
Type: streamanalytics.TypeBasicStreamInputDataSourceTypeMicrosoftDevicesIotHubs,
IoTHubStreamInputDataSourceProperties: &streamanalytics.IoTHubStreamInputDataSourceProperties{
ConsumerGroupName: utils.String(consumerGroupName),
SharedAccessPolicyKey: utils.String(sharedAccessPolicyKey),
SharedAccessPolicyName: utils.String(sharedAccessPolicyName),
Endpoint: utils.String(endpoint),
IotHubNamespace: utils.String(iotHubNamespace),
},
},
Serialization: serialization,
},
}
if d.IsNewResource() {
if _, err := client.CreateOrReplace(ctx, props, resourceId.ResourceGroup, resourceId.StreamingjobName, resourceId.InputName, "", ""); err != nil {
return fmt.Errorf("creating %s: %+v", resourceId, err)
}
d.SetId(resourceId.ID())
} else if _, err := client.Update(ctx, props, resourceId.ResourceGroup, resourceId.StreamingjobName, resourceId.InputName, ""); err != nil {
return fmt.Errorf("updating %s: %+v", resourceId, err)
}
return resourceStreamAnalyticsStreamInputIoTHubRead(d, meta)
}
func resourceStreamAnalyticsStreamInputIoTHubRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).StreamAnalytics.InputsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.StreamInputID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] %s was not found - removing from state!", id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
d.Set("name", id.InputName)
d.Set("stream_analytics_job_name", id.StreamingjobName)
d.Set("resource_group_name", id.ResourceGroup)
if props := resp.Properties; props != nil {
v, ok := props.AsStreamInputProperties()
if !ok {
return fmt.Errorf("converting Stream Input IoTHub to an Stream Input: %+v", err)
}
eventHub, ok := v.Datasource.AsIoTHubStreamInputDataSource()
if !ok {
return fmt.Errorf("converting Stream Input IoTHub to an IoTHub Stream Input: %+v", err)
}
d.Set("eventhub_consumer_group_name", eventHub.ConsumerGroupName)
d.Set("endpoint", eventHub.Endpoint)
d.Set("iothub_namespace", eventHub.IotHubNamespace)
d.Set("shared_access_policy_name", eventHub.SharedAccessPolicyName)
if err := d.Set("serialization", flattenStreamAnalyticsStreamInputSerialization(v.Serialization)); err != nil {
return fmt.Errorf("setting `serialization`: %+v", err)
}
}
return nil
}
func resourceStreamAnalyticsStreamInputIoTHubDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).StreamAnalytics.InputsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.StreamInputID(d.Id())
if err != nil {
return err
}
if _, err := client.Delete(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
return nil
}