Skip to content

Commit afc4bf9

Browse files
committed
mutualize between ressource schema and data source schema
1 parent 5d578ed commit afc4bf9

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

internal/services/block/snapshot.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,7 @@ func ResourceSnapshot() *schema.Resource {
3333
Default: schema.DefaultTimeout(defaultBlockTimeout),
3434
},
3535
SchemaVersion: 0,
36-
Identity: &schema.ResourceIdentity{
37-
Version: 0,
38-
SchemaFunc: func() map[string]*schema.Schema {
39-
return map[string]*schema.Schema{
40-
"snapshot_id": {
41-
Type: schema.TypeString,
42-
RequiredForImport: true,
43-
Description: "Snapshot ID",
44-
},
45-
"zone": {
46-
Type: schema.TypeString,
47-
RequiredForImport: true,
48-
Description: "Zone ID",
49-
},
50-
}
51-
},
52-
},
36+
Identity: blockIdentity(),
5337
Schema: map[string]*schema.Schema{
5438
"name": {
5539
Type: schema.TypeString,
@@ -227,6 +211,7 @@ func ResourceBlockSnapshotRead(ctx context.Context, d *schema.ResourceData, m an
227211
if err = identity.Set("snapshot_id", snapshot.ID); err != nil {
228212
return diag.FromErr(err)
229213
}
214+
230215
if err = identity.Set("zone", snapshot.Zone); err != nil {
231216
return diag.FromErr(err)
232217
}
@@ -311,3 +296,23 @@ func ResourceBlockSnapshotDelete(ctx context.Context, d *schema.ResourceData, m
311296

312297
return nil
313298
}
299+
300+
func blockIdentity() *schema.ResourceIdentity {
301+
return &schema.ResourceIdentity{
302+
Version: 0,
303+
SchemaFunc: func() map[string]*schema.Schema {
304+
return map[string]*schema.Schema{
305+
"snapshot_id": {
306+
Type: schema.TypeString,
307+
RequiredForImport: true,
308+
Description: "Snapshot ID",
309+
},
310+
"zone": {
311+
Type: schema.TypeString,
312+
RequiredForImport: true,
313+
Description: "Zone ID",
314+
},
315+
}
316+
},
317+
}
318+
}

internal/services/block/snapshot_data_source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func DataSourceSnapshot() *schema.Resource {
2828
return &schema.Resource{
2929
ReadContext: DataSourceBlockSnapshotRead,
3030
Schema: dsSchema,
31+
Identity: blockIdentity(),
3132
}
3233
}
3334

internal/services/block/volume.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,7 @@ func ResourceVolume() *schema.Resource {
2626
Importer: &schema.ResourceImporter{
2727
StateContext: schema.ImportStatePassthroughContext,
2828
},
29-
Identity: &schema.ResourceIdentity{
30-
Version: 0,
31-
SchemaFunc: func() map[string]*schema.Schema {
32-
return map[string]*schema.Schema{
33-
"volume_id": {
34-
Type: schema.TypeString,
35-
RequiredForImport: true,
36-
Description: "Volume ID",
37-
},
38-
"zone": {
39-
Type: schema.TypeString,
40-
RequiredForImport: true,
41-
Description: "Zone",
42-
},
43-
}
44-
},
45-
},
29+
Identity: volumeIdentity(),
4630
Timeouts: &schema.ResourceTimeout{
4731
Create: schema.DefaultTimeout(defaultBlockTimeout),
4832
Read: schema.DefaultTimeout(defaultBlockTimeout),
@@ -100,6 +84,26 @@ func ResourceVolume() *schema.Resource {
10084
}
10185
}
10286

87+
func volumeIdentity() *schema.ResourceIdentity {
88+
return &schema.ResourceIdentity{
89+
Version: 0,
90+
SchemaFunc: func() map[string]*schema.Schema {
91+
return map[string]*schema.Schema{
92+
"volume_id": {
93+
Type: schema.TypeString,
94+
RequiredForImport: true,
95+
Description: "Volume ID",
96+
},
97+
"zone": {
98+
Type: schema.TypeString,
99+
RequiredForImport: true,
100+
Description: "Zone",
101+
},
102+
}
103+
},
104+
}
105+
}
106+
103107
func ResourceBlockVolumeCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
104108
api, zone, err := instancehelpers.InstanceAndBlockAPIWithZone(d, m)
105109
if err != nil {
@@ -211,6 +215,7 @@ func ResourceBlockVolumeRead(ctx context.Context, d *schema.ResourceData, m any)
211215
if err = identity.Set("volume_id", volume.ID); err != nil {
212216
return diag.FromErr(err)
213217
}
218+
214219
if err = identity.Set("zone", volume.Zone); err != nil {
215220
return diag.FromErr(err)
216221
}

internal/services/block/volume_data_source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func DataSourceVolume() *schema.Resource {
2828
return &schema.Resource{
2929
ReadContext: DataSourceBlockVolumeRead,
3030
Schema: dsSchema,
31+
Identity: volumeIdentity(),
3132
}
3233
}
3334

0 commit comments

Comments
 (0)