-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
mssql_job_agent_resource.go
162 lines (132 loc) · 4.92 KB
/
mssql_job_agent_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package mssql
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
func resourceMsSqlJobAgent() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceMsSqlJobAgentCreateUpdate,
Read: resourceMsSqlJobAgentRead,
Update: resourceMsSqlJobAgentCreateUpdate,
Delete: resourceMsSqlJobAgentDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.JobAgentID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(60 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(60 * time.Minute),
Delete: pluginsdk.DefaultTimeout(60 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ValidateMsSqlJobAgentName,
},
"database_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateSqlDatabaseID,
},
"location": commonschema.Location(),
"tags": tags.Schema(),
},
}
}
func resourceMsSqlJobAgentCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.JobAgentsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for Job Agent creation.")
location := azure.NormalizeLocation(d.Get("location").(string))
databaseId := d.Get("database_id").(string)
dbId, err := commonids.ParseSqlDatabaseID(databaseId)
if err != nil {
return err
}
id := parse.NewJobAgentID(dbId.SubscriptionId, dbId.ResourceGroupName, dbId.ServerName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("failed to check for presence of existing %s: %s", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_mssql_job_agent", id.ID())
}
}
params := sql.JobAgent{
Name: &id.Name,
Location: utils.String(location),
JobAgentProperties: &sql.JobAgentProperties{
DatabaseID: &databaseId,
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ServerName, id.Name, params)
if err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}
d.SetId(id.ID())
return resourceMsSqlJobAgentRead(d, meta)
}
func resourceMsSqlJobAgentRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.JobAgentsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.JobAgentID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("reading MsSql Job Agent %s (MsSql Server Name %q / Resource Group %q): %s", id.Name, id.ServerName, id.ResourceGroup, err)
}
d.Set("name", resp.Name)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("database_id", resp.DatabaseID)
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceMsSqlJobAgentDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.JobAgentsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.JobAgentID(d.Id())
if err != nil {
return err
}
future, err := client.Delete(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
return fmt.Errorf("deleting Job Agent %s: %+v", id.Name, err)
}
return future.WaitForCompletionRef(ctx, client.Client)
}