@@ -10,6 +10,7 @@ import (
1010 "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1111 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1212 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
13+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1314 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1415 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1516 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
@@ -41,10 +42,11 @@ func ResourceSnapshot() *schema.Resource {
4142 },
4243 "volume_id" : {
4344 Type : schema .TypeString ,
44- Required : true ,
45+ Optional : true ,
4546 ValidateDiagFunc : verify .IsUUIDorUUIDWithLocality (),
4647 Description : "ID of the volume from which creates a snapshot" ,
4748 DiffSuppressFunc : dsf .Locality ,
49+ ConflictsWith : []string {"import" },
4850 },
4951 "tags" : {
5052 Type : schema .TypeList ,
@@ -54,6 +56,34 @@ func ResourceSnapshot() *schema.Resource {
5456 Optional : true ,
5557 Description : "The tags associated with the snapshot" ,
5658 },
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+ },
5787 "zone" : zonal .Schema (),
5888 "project_id" : account .ProjectIDSchema (),
5989 },
@@ -66,15 +96,33 @@ func ResourceBlockSnapshotCreate(ctx context.Context, d *schema.ResourceData, m
6696 return diag .FromErr (err )
6797 }
6898
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+ }
78126 }
79127
80128 d .SetId (zonal .NewIDString (zone , snapshot .ID ))
0 commit comments