-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_sharedfilesystem_snapshot_v2.go
124 lines (101 loc) · 3.01 KB
/
data_source_viettelidc_sharedfilesystem_snapshot_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package viettelidc
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/snapshots"
)
func dataSourceSharedFilesystemSnapshotV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSharedFilesystemSnapshotV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"share_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"share_proto": {
Type: schema.TypeString,
Computed: true,
},
"share_size": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceSharedFilesystemSnapshotV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
sfsClient, err := config.SharedfilesystemV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack sharedfilesystem sfsClient: %s", err)
}
sfsClient.Microversion = minManilaShareMicroversion
listOpts := snapshots.ListOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
ProjectID: d.Get("project_id").(string),
Status: d.Get("status").(string),
}
allPages, err := snapshots.ListDetail(sfsClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to query snapshots: %s", err)
}
allSnapshots, err := snapshots.ExtractSnapshots(allPages)
if err != nil {
return diag.Errorf("Unable to retrieve snapshots: %s", err)
}
if len(allSnapshots) < 1 {
return diag.Errorf("Your query returned no results. Please change your search criteria and try again")
}
var share snapshots.Snapshot
if len(allSnapshots) > 1 {
log.Printf("[DEBUG] Multiple results found: %#v", allSnapshots)
return diag.Errorf("Your query returned more than one result. Please try a more specific search criteria")
}
share = allSnapshots[0]
dataSourceSharedFilesystemSnapshotV2Attributes(d, &share, GetRegion(d, config))
return nil
}
func dataSourceSharedFilesystemSnapshotV2Attributes(d *schema.ResourceData, snapshot *snapshots.Snapshot, region string) {
d.SetId(snapshot.ID)
d.Set("name", snapshot.Name)
d.Set("region", region)
d.Set("project_id", snapshot.ProjectID)
d.Set("description", snapshot.Description)
d.Set("size", snapshot.Size)
d.Set("status", snapshot.Status)
d.Set("share_id", snapshot.ShareID)
d.Set("share_proto", snapshot.ShareProto)
d.Set("share_size", snapshot.ShareSize)
}