Skip to content

Commit a37eb29

Browse files
committed
Create, Read and Update function
1 parent d635bf1 commit a37eb29

File tree

1 file changed

+92
-22
lines changed

1 file changed

+92
-22
lines changed

internal/services/file/file.go

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
file "github.com/scaleway/scaleway-sdk-go/api/file/v1alpha1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1011
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1314
)
1415

15-
func ResourceFile() *schema.Resource {
16+
func ResourceFileSystem() *schema.Resource {
1617
return &schema.Resource{
17-
CreateContext: ResourceFileCreate,
18-
ReadContext: ResourceFileRead,
19-
UpdateContext: ResourceFileUpdate,
20-
DeleteContext: ResourceFileDelete,
18+
CreateContext: ResourceFileSystemCreate,
19+
ReadContext: ResourceFileSystemRead,
20+
UpdateContext: ResourceFileSystemUpdate,
21+
DeleteContext: ResourceFileSystemDelete,
2122
Importer: &schema.ResourceImporter{
2223
StateContext: schema.ImportStatePassthroughContext,
2324
},
2425
Timeouts: &schema.ResourceTimeout{
25-
Create: schema.DefaultTimeout(defaultFileTimeout),
26-
Read: schema.DefaultTimeout(defaultFileTimeout),
27-
Delete: schema.DefaultTimeout(defaultFileTimeout),
28-
Default: schema.DefaultTimeout(defaultFileTimeout),
26+
Create: schema.DefaultTimeout(defaultFileSystemTimeout),
27+
Read: schema.DefaultTimeout(defaultFileSystemTimeout),
28+
Delete: schema.DefaultTimeout(defaultFileSystemTimeout),
29+
Default: schema.DefaultTimeout(defaultFileSystemTimeout),
2930
},
3031
SchemaVersion: 0,
3132
Schema: map[string]*schema.Schema{
@@ -56,7 +57,7 @@ func ResourceFile() *schema.Resource {
5657
Computed: true,
5758
Description: "The Current status of the filesystem (e.g. creating, available, ...)",
5859
},
59-
"number_of_attachements": {
60+
"number_of_attachments": {
6061
Type: schema.TypeInt,
6162
Computed: true,
6263
Description: "The current number of attachments (mounts) that the filesystem has",
@@ -75,18 +76,18 @@ func ResourceFile() *schema.Resource {
7576
}
7677
}
7778

78-
func ResourceFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
79-
api, region, err := fileAPIWithZone(d, m)
79+
func ResourceFileSystemCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
80+
api, region, err := fileSystemAPIWithZone(d, m)
8081
if err != nil {
8182
return diag.FromErr(err)
8283
}
8384

8485
req := &file.CreateFileSystemRequest{
85-
Region: region,
86-
Name: types.ExpandOrGenerateString(d.Get("name").(string), "file"),
86+
Region: region,
87+
Name: types.ExpandOrGenerateString(d.Get("name").(string), "file"),
8788
ProjectID: d.Get("project_id").(string),
88-
Size: *types.ExpandUint64Ptr(d.Get("size")),
89-
Tags: types.ExpandStrings(d.Get("tags")),
89+
Size: *types.ExpandUint64Ptr(d.Get("size")),
90+
Tags: types.ExpandStrings(d.Get("tags")),
9091
}
9192

9293
file, err := api.CreateFileSystem(req, scw.WithContext(ctx))
@@ -96,22 +97,91 @@ func ResourceFileCreate(ctx context.Context, d *schema.ResourceData, m interface
9697

9798
d.SetId(regional.NewIDString(region, file.ID))
9899

99-
//TODO waitForFile
100+
_, err = waitForFileSystem(ctx, api, region, file.ID, d.Timeout(schema.TimeoutCreate))
101+
if err != nil {
102+
return diag.FromErr(err)
103+
}
100104

101-
return ResourceFileRead(ctx, d, m)
105+
return ResourceFileSystemRead(ctx, d, m)
102106
}
103107

104-
func ResourceFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
108+
func ResourceFileSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
109+
api, region, id, err := NewAPIWithRegionAndID(m, d.Id())
110+
if err != nil {
111+
return diag.FromErr(err)
112+
}
113+
114+
fileSystem, err := waitForFileSystem(ctx, api, region, id, d.Timeout(schema.TimeoutRead))
115+
if err != nil {
116+
if httperrors.Is404(err) {
117+
d.SetId("")
118+
119+
return nil
120+
}
121+
122+
return diag.FromErr(err)
123+
}
124+
125+
_ = d.Set("name", fileSystem.Name)
126+
_ = d.Set("project_id", fileSystem.ProjectID)
127+
_ = d.Set("region", fileSystem.Region)
128+
_ = d.Set("organization_id", fileSystem.OrganizationID)
129+
_ = d.Set("status", fileSystem.Status)
130+
_ = d.Set("size", fileSystem.Size)
131+
_ = d.Set("tags", fileSystem.Tags)
132+
_ = d.Set("created_at", fileSystem.CreatedAt)
133+
_ = d.Set("updated_at", fileSystem.UpdatedAt)
134+
_ = d.Set("number_of_attachments", fileSystem.NumberOfAttachments)
105135

106136
return nil
107137
}
108138

109-
func ResourceFileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
139+
func ResourceFileSystemUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
140+
api, region, id, err := NewAPIWithRegionAndID(m, d.Id())
141+
if err != nil {
142+
return diag.FromErr(err)
143+
}
144+
145+
fileSystem, err := waitForFileSystem(ctx, api, region, id, d.Timeout(schema.TimeoutUpdate))
146+
if err != nil {
147+
if httperrors.Is404(err) {
148+
d.SetId("")
149+
150+
return nil
151+
}
152+
153+
return diag.FromErr(err)
154+
}
155+
156+
req := &file.UpdateFileSystemRequest{
157+
Region: region,
158+
FilesystemID: fileSystem.ID,
159+
}
160+
161+
if d.HasChange("name") {
162+
req.Name = types.ExpandUpdatedStringPtr(d.Get("name"))
163+
}
164+
165+
// // Region: region to target. If none is passed will use default region from the config.
166+
// Region scw.Region `json:"-"`
167+
168+
// // FilesystemID: UUID of the filesystem.
169+
// FilesystemID string `json:"-"`
170+
171+
// // Name: when defined, is the new name of the filesystem.
172+
// Name *string `json:"name,omitempty"`
173+
174+
// // Size: size in bytes, with a granularity of 100 GB (10^11 bytes).
175+
// // Must be compliant with the minimum (100 GB) and maximum (10 TB) allowed size.
176+
// Size *uint64 `json:"size,omitempty"`
177+
178+
// // Tags: list of tags assigned to the filesystem.
179+
// Tags *[]string `json:"tags,omitempty"`
110180

111-
return ResourceFileRead(ctx, d, m)
181+
return ResourceFileSystemRead(ctx, d, m)
112182
}
113183

114-
func ResourceFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
184+
func ResourceFileSystemDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
115185

116186
return nil
117187
}

0 commit comments

Comments
 (0)