@@ -10,6 +10,7 @@ import (
10
10
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
11
11
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
12
12
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
13
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
13
14
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
14
15
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
15
16
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
@@ -41,10 +42,11 @@ func ResourceSnapshot() *schema.Resource {
41
42
},
42
43
"volume_id" : {
43
44
Type : schema .TypeString ,
44
- Required : true ,
45
+ Optional : true ,
45
46
ValidateDiagFunc : verify .IsUUIDorUUIDWithLocality (),
46
47
Description : "ID of the volume from which creates a snapshot" ,
47
48
DiffSuppressFunc : dsf .Locality ,
49
+ ConflictsWith : []string {"import" },
48
50
},
49
51
"tags" : {
50
52
Type : schema .TypeList ,
@@ -54,6 +56,34 @@ func ResourceSnapshot() *schema.Resource {
54
56
Optional : true ,
55
57
Description : "The tags associated with the snapshot" ,
56
58
},
59
+ "import" : {
60
+ Type : schema .TypeList ,
61
+ ForceNew : true ,
62
+ MaxItems : 1 ,
63
+ Elem : & schema.Resource {
64
+ Schema : map [string ]* schema.Schema {
65
+ "bucket" : {
66
+ Type : schema .TypeString ,
67
+ Required : true ,
68
+ ForceNew : true ,
69
+ Description : "Bucket containing qcow" ,
70
+ DiffSuppressFunc : dsf .Locality ,
71
+ StateFunc : func (i interface {}) string {
72
+ return regional .ExpandID (i .(string )).ID
73
+ },
74
+ },
75
+ "key" : {
76
+ Type : schema .TypeString ,
77
+ Required : true ,
78
+ ForceNew : true ,
79
+ Description : "Key of the qcow file in the specified bucket" ,
80
+ },
81
+ },
82
+ },
83
+ Optional : true ,
84
+ Description : "Import snapshot from a qcow" ,
85
+ ConflictsWith : []string {"volume_id" },
86
+ },
57
87
"zone" : zonal .Schema (),
58
88
"project_id" : account .ProjectIDSchema (),
59
89
},
@@ -66,15 +96,33 @@ func ResourceBlockSnapshotCreate(ctx context.Context, d *schema.ResourceData, m
66
96
return diag .FromErr (err )
67
97
}
68
98
69
- snapshot , err := api .CreateSnapshot (& block.CreateSnapshotRequest {
70
- Zone : zone ,
71
- ProjectID : d .Get ("project_id" ).(string ),
72
- Name : types .ExpandOrGenerateString (d .Get ("name" ).(string ), "snapshot" ),
73
- VolumeID : locality .ExpandID (d .Get ("volume_id" )),
74
- Tags : types .ExpandStrings (d .Get ("tags" )),
75
- }, scw .WithContext (ctx ))
76
- if err != nil {
77
- return diag .FromErr (err )
99
+ var snapshot * block.Snapshot
100
+
101
+ if _ , isImported := d .GetOk ("import" ); ! isImported {
102
+ snapshot , err = api .CreateSnapshot (& block.CreateSnapshotRequest {
103
+ Zone : zone ,
104
+ ProjectID : d .Get ("project_id" ).(string ),
105
+ Name : types .ExpandOrGenerateString (d .Get ("name" ).(string ), "snapshot" ),
106
+ VolumeID : locality .ExpandID (d .Get ("volume_id" )),
107
+ Tags : types .ExpandStrings (d .Get ("tags" )),
108
+ }, scw .WithContext (ctx ))
109
+ if err != nil {
110
+ return diag .FromErr (err )
111
+ }
112
+ } else {
113
+ req := & block.ImportSnapshotFromObjectStorageRequest {
114
+ Zone : zone ,
115
+ ProjectID : d .Get ("project_id" ).(string ),
116
+ Name : types .ExpandOrGenerateString (d .Get ("name" ), "snapshot" ),
117
+ Bucket : regional .ExpandID (d .Get ("import.0.bucket" )).ID ,
118
+ Key : d .Get ("import.0.key" ).(string ),
119
+ Tags : types .ExpandStrings (d .Get ("tags" )),
120
+ }
121
+
122
+ snapshot , err = api .ImportSnapshotFromObjectStorage (req , scw .WithContext (ctx ))
123
+ if err != nil {
124
+ return diag .FromErr (err )
125
+ }
78
126
}
79
127
80
128
d .SetId (zonal .NewIDString (zone , snapshot .ID ))
0 commit comments