forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_learning_synapse_spark_resource.go
206 lines (173 loc) · 6.97 KB
/
machine_learning_synapse_spark_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
package machinelearning
import (
"fmt"
"log"
"regexp"
"time"
"github.com/Azure/azure-sdk-for-go/services/machinelearningservices/mgmt/2021-07-01/machinelearningservices"
"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/location"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/machinelearning/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/machinelearning/validate"
synapseValidate "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/synapse/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 resourceSynapseSpark() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSynapseSparkCreate,
Read: resourceSynapseSparkRead,
Delete: resourceSynapseSparkDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ComputeID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * 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-Z][a-zA-Z0-9-]{2,16}$`),
"It can include letters, digits and dashes. It must start with a letter, end with a letter or digit, and be between 2 and 16 characters in length."),
},
"machine_learning_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.WorkspaceID,
},
"location": azure.SchemaLocation(),
"synapse_spark_pool_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: synapseValidate.SparkPoolID,
},
"description": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},
"identity": SystemAssignedUserAssigned{}.Schema(),
"local_auth_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"tags": tags.ForceNewSchema(),
},
}
}
func resourceSynapseSparkCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MachineLearning.MachineLearningComputeClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
workspaceID, _ := parse.WorkspaceID(d.Get("machine_learning_workspace_id").(string))
id := parse.NewComputeID(subscriptionId, workspaceID.ResourceGroup, workspaceID.Name, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing Machine Learning Compute (%q): %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_machine_learning_synapse_spark", id.ID())
}
}
identity, err := SystemAssignedUserAssigned{}.Expand(d.Get("identity").([]interface{}))
if err != nil {
return err
}
parameters := machinelearningservices.ComputeResource{
Properties: &machinelearningservices.SynapseSpark{
Properties: nil,
ComputeLocation: utils.String(d.Get("location").(string)),
Description: utils.String(d.Get("description").(string)),
ResourceID: utils.String(d.Get("synapse_spark_pool_id").(string)),
DisableLocalAuth: utils.Bool(!d.Get("local_auth_enabled").(bool)),
},
Identity: identity,
Location: utils.String(location.Normalize(d.Get("location").(string))),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.WorkspaceName, id.Name, parameters)
if err != nil {
return fmt.Errorf("creating Machine Learning Compute (%q): %+v", id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of Machine Learning Compute (%q): %+v", id, err)
}
d.SetId(id.ID())
return resourceSynapseSparkRead(d, meta)
}
func resourceSynapseSparkRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MachineLearning.MachineLearningComputeClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ComputeID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Machine Learning Compute %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Machine Learning Compute (%q): %+v", id, err)
}
d.Set("name", id.Name)
workspaceId := parse.NewWorkspaceID(subscriptionId, id.ResourceGroup, id.WorkspaceName)
d.Set("machine_learning_workspace_id", workspaceId.ID())
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
identity, err := SystemAssignedUserAssigned{}.Flatten(resp.Identity)
if err != nil {
return err
}
d.Set("identity", identity)
if props, ok := resp.Properties.AsSynapseSpark(); ok && props != nil {
if props.DisableLocalAuth != nil {
d.Set("local_auth_enabled", !*props.DisableLocalAuth)
}
d.Set("description", props.Description)
d.Set("synapse_spark_pool_id", props.ResourceID)
} else {
return fmt.Errorf("compute resource %s is not a SynapseSpark Compute", id)
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceSynapseSparkDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MachineLearning.MachineLearningComputeClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ComputeID(d.Id())
if err != nil {
return err
}
future, err := client.Delete(ctx, id.ResourceGroup, id.WorkspaceName, id.Name, machinelearningservices.UnderlyingResourceActionDetach)
if err != nil {
return fmt.Errorf("deleting Machine Learning Compute (%q): %+v", id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of the Machine Learning Compute (%q): %+v", id, err)
}
return nil
}