/
resource_huaweicloud_sfs_turbo_dir.go
186 lines (162 loc) · 5.22 KB
/
resource_huaweicloud_sfs_turbo_dir.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
package sfs
import (
"context"
"fmt"
"strings"
"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/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API SFSTurbo POST /v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir
// @API SFSTurbo GET /v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir
// @API SFSTurbo DELETE /v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir
func ResourceSfsTurboDir() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSfsTurboDirCreate,
ReadContext: resourceSfsTurboDirRead,
DeleteContext: resourceSfsTurboDirDelete,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"share_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the SFS Turbo ID.`,
},
"path": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the valid full path of SFS Turbo directory.`,
},
"mode": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Specifies the SFS Turbo directory permissions.`,
},
"uid": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Specifies the user ID of the SFS Turbo directory.`,
},
"gid": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Specifies the group ID of the SFS Turbo directory.`,
},
},
}
}
func resourceSfsTurboDirCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir"
)
sfsClient, err := cfg.SfsV1Client(region)
if err != nil {
return diag.Errorf("error creating SFS v1 client: %s", err)
}
createPath := sfsClient.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", sfsClient.ProjectID)
createPath = strings.ReplaceAll(createPath, "{share_id}", d.Get("share_id").(string))
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: utils.RemoveNil(buildCreateSfsTurboDirBodyParams(d)),
OkCodes: []int{
204,
},
}
_, err = sfsClient.Request("POST", createPath, &createOpt)
if err != nil {
return diag.Errorf("error creating SFS Turbo directory: %s", err)
}
path := d.Get("path").(string)
d.SetId(path)
return resourceSfsTurboDirRead(ctx, d, meta)
}
func buildCreateSfsTurboDirBodyParams(d *schema.ResourceData) map[string]interface{} {
params := map[string]interface{}{
"path": d.Get("path"),
"mode": d.Get("mode"),
"uid": d.Get("uid"),
"gid": d.Get("gid"),
}
return params
}
func resourceSfsTurboDirRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
mErr *multierror.Error
httpUrl = "v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir"
)
sfsClient, err := cfg.SfsV1Client(region)
if err != nil {
return diag.Errorf("error creating SFS v1 client: %s", err)
}
getPath := sfsClient.Endpoint + httpUrl
getPath = strings.ReplaceAll(getPath, "{project_id}", sfsClient.ProjectID)
getPath = strings.ReplaceAll(getPath, "{share_id}", d.Get("share_id").(string))
getPath += fmt.Sprintf("?path=%s", d.Id())
getOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getResp, err := sfsClient.Request("GET", getPath, &getOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving SFS Turbo directory")
}
getRespBody, err := utils.FlattenResponse(getResp)
if err != nil {
return diag.FromErr(err)
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("path", utils.PathSearch("path", getRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildDeleteSfsTurboDirBodyParams(d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{
"path": d.Id(),
}
}
func resourceSfsTurboDirDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/sfs-turbo/shares/{share_id}/fs/dir"
)
sfsClient, err := cfg.SfsV1Client(region)
if err != nil {
return diag.Errorf("error creating SFS v1 client: %s", err)
}
deletePath := sfsClient.Endpoint + httpUrl
deletePath = strings.ReplaceAll(deletePath, "{project_id}", sfsClient.ProjectID)
deletePath = strings.ReplaceAll(deletePath, "{share_id}", d.Get("share_id").(string))
deleteOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: buildDeleteSfsTurboDirBodyParams(d),
}
_, err = sfsClient.Request("DELETE", deletePath, &deleteOpt)
if err != nil {
if utils.IsResourceNotFound(err) {
return nil
}
return diag.Errorf("error deleting SFS Turbo directory: %s", err)
}
return nil
}