Skip to content

Commit 73fe32b

Browse files
committed
feat(file): add support v1beta1
1 parent a6a7a13 commit 73fe32b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

internal/services/file/file.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package file
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
file "github.com/scaleway/scaleway-sdk-go/api/file/v1alpha1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
13+
)
14+
15+
func ResourceFile() *schema.Resource {
16+
return &schema.Resource{
17+
CreateContext: ResourceFileCreate,
18+
ReadContext: ResourceFileRead,
19+
UpdateContext: ResourceFileUpdate,
20+
DeleteContext: ResourceFileDelete,
21+
Importer: &schema.ResourceImporter{
22+
StateContext: schema.ImportStatePassthroughContext,
23+
},
24+
Timeouts: &schema.ResourceTimeout{
25+
Create: schema.DefaultTimeout(defaultFileTimeout),
26+
Read: schema.DefaultTimeout(defaultFileTimeout),
27+
Delete: schema.DefaultTimeout(defaultFileTimeout),
28+
Default: schema.DefaultTimeout(defaultFileTimeout),
29+
},
30+
SchemaVersion: 0,
31+
Schema: map[string]*schema.Schema{
32+
"name": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
Optional: true,
36+
Description: "The name of the filesystem",
37+
},
38+
"size": {
39+
Type: schema.TypeInt,
40+
Required: true,
41+
Description: "The Filesystem size in bytes, with a granularity of 100 GB (10^11 bytes). Must be compliant with the minimum (100 GB) and maximum (10 TB) allowed size.",
42+
},
43+
"tags": {
44+
Type: schema.TypeList,
45+
Elem: &schema.Schema{
46+
Type: schema.TypeString,
47+
},
48+
Optional: true,
49+
Description: "The list of tags assigned to the filesystem",
50+
},
51+
"project_id": account.ProjectIDSchema(),
52+
"organization_id": account.OrganizationIDSchema(),
53+
"region": regional.Schema(),
54+
"status": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "The Current status of the filesystem (e.g. creating, available, ...)",
58+
},
59+
"number_of_attachements": {
60+
Type: schema.TypeInt,
61+
Computed: true,
62+
Description: "The current number of attachments (mounts) that the filesystem has",
63+
},
64+
"created_at": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "The creation date of the filesystem",
68+
},
69+
"updated_at": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "The last update date of the properties of the filesystem",
73+
},
74+
},
75+
}
76+
}
77+
78+
func ResourceFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
79+
api, region, err := fileAPIWithZone(d, m)
80+
if err != nil {
81+
return diag.FromErr(err)
82+
}
83+
84+
req := &file.CreateFileSystemRequest{
85+
Region: region,
86+
Name: types.ExpandOrGenerateString(d.Get("name").(string), "file"),
87+
ProjectID: d.Get("project_id").(string),
88+
Size: *types.ExpandUint64Ptr(d.Get("size")),
89+
Tags: types.ExpandStrings(d.Get("tags")),
90+
}
91+
92+
file, err := api.CreateFileSystem(req, scw.WithContext(ctx))
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
97+
d.SetId(regional.NewIDString(region, file.ID))
98+
99+
//TODO waitForFile
100+
101+
return ResourceFileRead(ctx, d, m)
102+
}
103+
104+
func ResourceFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
105+
106+
return nil
107+
}
108+
109+
func ResourceFileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
110+
111+
return ResourceFileRead(ctx, d, m)
112+
}
113+
114+
func ResourceFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
115+
116+
return nil
117+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package file
2+
3+
import (
4+
"time"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
file "github.com/scaleway/scaleway-sdk-go/api/file/v1alpha1"
8+
"github.com/scaleway/scaleway-sdk-go/scw"
9+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
11+
)
12+
13+
const (
14+
defaultFileTimeout = 5 * time.Minute
15+
defaultFileRetryInterval = 5 * time.Second
16+
)
17+
18+
func fileAPIWithZone(d *schema.ResourceData, m interface{}) (*file.API, scw.Region, error) {
19+
fileAPI := file.NewAPI(meta.ExtractScwClient(m))
20+
21+
region, err := meta.ExtractRegion(d, m)
22+
if err != nil {
23+
return nil, "", err
24+
}
25+
26+
return fileAPI, region, nil
27+
}
28+
29+
func NewAPIWithRegionAndID(m interface{}, regionID string) (*file.API, scw.Region, string, error) {
30+
fileAPI := file.NewAPI(meta.ExtractScwClient(m))
31+
32+
region, ID, err := regional.ParseID(regionID)
33+
if err != nil {
34+
return nil, "", "", err
35+
}
36+
37+
return fileAPI, region, ID, nil
38+
}

0 commit comments

Comments
 (0)