-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dli_package.go
283 lines (246 loc) · 8.28 KB
/
resource_huaweicloud_dli_package.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package dli
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/dli/v2/spark/resources"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API DLI POST /v2.0/{project_id}/resources
// @API DLI GET /v2.0/{project_id}/resources/{resource_name}
// @API DLI PUT /v2.0/{project_id}/resources/owner
// @API DLI DELETE /v2.0/{project_id}/resources/{resource_name}
// @API DLI GET /v3/{project_id}/dli_package_resource/{resource_id}/tags
// @API DLI POST /v3/{project_id}/dli_package_resource/{resource_id}/tags/create
// @API DLI POST /v3/{project_id}/dli_package_resource/{resource_id}/tags/delete
func ResourceDliPackageV2() *schema.Resource {
return &schema.Resource{
CreateContext: ResourceDliDependentPackageV2Create,
ReadContext: ResourceDliDependentPackageV2Read,
UpdateContext: ResourceDliDependentPackageV2Update,
DeleteContext: ResourceDliDependentPackageV2Delete,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"group_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"object_path": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"is_async": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"owner": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": common.TagsSchema(),
"object_name": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildDliDependentPackageCreateOpts(d *schema.ResourceData) resources.CreateGroupAndUploadOpts {
result := resources.CreateGroupAndUploadOpts{
Paths: []string{d.Get("object_path").(string)},
Kind: d.Get("type").(string),
Group: d.Get("group_name").(string),
IsAsync: d.Get("is_async").(bool),
Tags: utils.ExpandResourceTags(d.Get("tags").(map[string]interface{})),
}
return result
}
func ResourceDliDependentPackageV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
c, err := cfg.DliV2Client(region)
if err != nil {
return diag.Errorf("error creating DLI v2 client: %s", err)
}
opt := buildDliDependentPackageCreateOpts(d)
resp, err := resources.CreateGroupAndUpload(c, opt)
if err != nil {
return diag.Errorf("error uploading package to OBS bucket: %s", err)
}
// If the object list of resources.Resources is not empty, it means that the object has been uploaded successfully,
// and the element with an index of zero is the object name.
groupName, ok := d.GetOk("group_name")
if ok && len(resp.Resources) < 1 || !ok && len(resp.ResourceNames) < 1 {
return diag.Errorf("failed to upload package (%s).", d.Get("object_path").(string))
}
// object_path is not unique and cannot be used for ID setting, because the object can exist in multiple groups.
if ok {
d.SetId(fmt.Sprintf("%s#%s", groupName.(string), resp.Resources[0]))
} else {
d.SetId(resp.ResourceNames[0])
}
// If the owner of the configuration is not the creator, update it.
pkg, err := GetDliDependentPackageInfo(c, d.Id())
if err != nil {
return diag.Errorf("error getting the package: %s", err)
}
if owner, ok := d.GetOk("owner"); ok && owner.(string) != pkg.Owner {
if err = updateOwner(c, groupName.(string), owner.(string), pkg.ResourceName); err != nil {
return diag.FromErr(err)
}
}
return ResourceDliDependentPackageV2Read(ctx, d, meta)
}
func getGroupNameAndPackageName(id string) (groupName, packageName string, err error) {
names := strings.Split(id, "#")
if len(names) == 1 {
return "", names[0], nil
}
if len(names) == 2 {
return names[0], names[1], nil
}
err = fmt.Errorf("invalid format for resource ID, want '<group_name>#<object_name>' or '<object_name>', but got '%s'", id)
return
}
func GetDliDependentPackageInfo(c *golangsdk.ServiceClient, id string) (*resources.Resource, error) {
groupName, packageName, err := getGroupNameAndPackageName(id)
if err != nil {
return nil, fmt.Errorf("error parsing resource ID (%s): %s", id, err)
}
opt := resources.ResourceLocatedOpts{
Group: groupName,
}
resp, err := resources.Get(c, packageName, opt)
if err != nil {
return nil, err
}
return resp, nil
}
func ResourceDliDependentPackageV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
c, err := cfg.DliV2Client(region)
if err != nil {
return diag.Errorf("error creating DLI v2 client: %s", err)
}
id := d.Id()
resp, err := GetDliDependentPackageInfo(c, id)
if err != nil {
return common.CheckDeletedDiag(d, err, "DLI package")
}
mErr := multierror.Append(nil,
d.Set("object_name", resp.ResourceName),
d.Set("type", resp.ResourceType),
d.Set("status", resp.Status),
d.Set("created_at", time.Unix(int64(resp.CreateTime)/1000, 0).Format("2006-01-02 15:04:05")),
d.Set("updated_at", time.Unix(int64(resp.CreateTime)/1000, 0).Format("2006-01-02 15:04:05")),
d.Set("owner", resp.Owner),
)
v3Client, err := cfg.DliV3Client(region)
if err != nil {
return diag.Errorf("error creating DLI v3 client: %s", err)
}
err = utils.SetResourceTagsToState(d, v3Client, "dli_package_resource", getASCIIFormationId(id))
if err != nil {
return diag.FromErr(err)
}
return diag.FromErr(mErr.ErrorOrNil())
}
func updateOwner(c *golangsdk.ServiceClient, groupName, owner, resourceName string) error {
opt := resources.UpdateOpts{
ResourceName: resourceName,
GroupName: groupName,
NewOwner: owner,
}
resp, err := resources.UpdateOwner(c, opt)
if err != nil {
return fmt.Errorf("error updating package owner: %s", err)
}
if !resp.IsSuccess {
return fmt.Errorf("unable to update the package: %s", resp.Message)
}
return nil
}
func getASCIIFormationId(id string) string {
// The url.QueryEscape function is used to convert special characters in the URL to corresponding hexadecimal format.
return url.QueryEscape(id)
}
func ResourceDliDependentPackageV2Update(ctx context.Context, d *schema.ResourceData,
meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
c, err := cfg.DliV2Client(region)
if err != nil {
return diag.Errorf("error creating DLI v2 client: %s", err)
}
if err = updateOwner(c, d.Get("group_name").(string), d.Get("owner").(string), d.Get("object_name").(string)); err != nil {
return diag.FromErr(err)
}
if d.HasChange("tags") {
v3Client, err := cfg.DliV3Client(region)
if err != nil {
return diag.Errorf("error creating DLI v3 client: %s", err)
}
resourceId := getASCIIFormationId(d.Id())
oldTags, newTags := d.GetChange("tags")
err = updateResourceTags(v3Client, resourceId, "dli_package_resource", oldTags, newTags)
if err != nil {
return diag.Errorf("error updating tags of the package (%s): %s", resourceId, err)
}
}
return ResourceDliDependentPackageV2Read(ctx, d, meta)
}
func ResourceDliDependentPackageV2Delete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*config.Config)
c, err := config.DliV2Client(config.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DLI v2 client: %s", err)
}
groupName, packageName, err := getGroupNameAndPackageName(d.Id())
if err != nil {
return diag.Errorf("error parsing resource ID (%s): %s", d.Id(), err)
}
opt := resources.ResourceLocatedOpts{
Group: groupName,
}
err = resources.Delete(c, packageName, opt).ExtractErr()
if err != nil {
return diag.Errorf("error deleting DLI dependent package (%s): %s", packageName, err)
}
return nil
}