forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_migration_project_resource.go
175 lines (147 loc) · 5.84 KB
/
database_migration_project_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
package databasemigration
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/datamigration/mgmt/2018-04-19/datamigration"
"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/databasemigration/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/databasemigration/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 resourceDatabaseMigrationProject() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDatabaseMigrationProjectCreateUpdate,
Read: resourceDatabaseMigrationProjectRead,
Update: resourceDatabaseMigrationProjectCreateUpdate,
Delete: resourceDatabaseMigrationProjectDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ProjectID(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: validate.ProjectName,
},
"service_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ServiceName,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"location": azure.SchemaLocation(),
"source_platform": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
// Now that go sdk only export SQL as source platform type, we only allow it here.
string(datamigration.ProjectSourcePlatformSQL),
}, false),
},
"target_platform": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
// Now that go sdk only export SQL as source platform type, we only allow it here.
string(datamigration.ProjectTargetPlatformSQLDB),
}, false),
},
"tags": tags.Schema(),
},
}
}
func resourceDatabaseMigrationProjectCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DatabaseMigration.ProjectsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
id := parse.NewProjectID(subscriptionId, d.Get("resource_group_name").(string), d.Get("service_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_database_migration_project", id.ID())
}
}
location := azure.NormalizeLocation(d.Get("location").(string))
sourcePlatform := d.Get("source_platform").(string)
targetPlatform := d.Get("target_platform").(string)
t := d.Get("tags").(map[string]interface{})
parameters := datamigration.Project{
Location: utils.String(location),
ProjectProperties: &datamigration.ProjectProperties{
SourcePlatform: datamigration.ProjectSourcePlatform(sourcePlatform),
TargetPlatform: datamigration.ProjectTargetPlatform(targetPlatform),
},
Tags: tags.Expand(t),
}
if _, err := client.CreateOrUpdate(ctx, parameters, id.ResourceGroup, id.ServiceName, id.Name); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
d.SetId(id.ID())
return resourceDatabaseMigrationProjectRead(d, meta)
}
func resourceDatabaseMigrationProjectRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DatabaseMigration.ProjectsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ProjectID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] %s does not exist - removing from state", *id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
d.Set("name", id.Name)
d.Set("service_name", id.ServiceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
if prop := resp.ProjectProperties; prop != nil {
d.Set("source_platform", string(prop.SourcePlatform))
d.Set("target_platform", string(prop.TargetPlatform))
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceDatabaseMigrationProjectDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DatabaseMigration.ProjectsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ProjectID(d.Id())
if err != nil {
return err
}
deleteRunningTasks := false
if _, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.Name, &deleteRunningTasks); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
return nil
}