/
data_source_edgecenter_snapshot.go
174 lines (156 loc) · 5.02 KB
/
data_source_edgecenter_snapshot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package edgecenter
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
)
func dataSourceSnapshot() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSnapshotRead,
Description: "A snapshot is a feature that allows you to capture the current state of the instance or volume at a specific point in time",
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The name of the snapshot. Use only with uniq name.",
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The ID of the snapshot.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the snapshot.",
},
"creator_task_id": {
Type: schema.TypeString,
Computed: true,
Description: "The task that created this entity.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the snapshot.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the snapshot, GiB.",
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The ID of the volume this snapshot was made from.",
RequiredWith: []string{"name"},
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The datetime when the snapshot was last updated.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The datetime when the snapshot was created.",
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Description: "The metadata",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start snapshot reading")
var diags diag.Diagnostics
clientV2, err := InitCloudClient(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
snapshotID := d.Get("snapshot_id").(string)
volumeID := d.Get("volume_id").(string)
log.Printf("[DEBUG] Snapshot id = %s", snapshotID)
var snapshot *edgecloudV2.Snapshot
switch {
case snapshotID != "":
snapshot, _, err = clientV2.Snapshots.Get(ctx, snapshotID)
if err != nil {
return diag.Errorf("cannot get snapshot with ID %s. Error: %s", snapshotID, err.Error())
}
default:
name := d.Get("name").(string)
snapshotsOpts := &edgecloudV2.SnapshotListOptions{VolumeID: volumeID}
allSnapshots, _, err := clientV2.Snapshots.List(ctx, snapshotsOpts)
if err != nil {
return diag.Errorf("cannot get snapshots. Error: %s", err.Error())
}
var foundSnapshots []*edgecloudV2.Snapshot
for _, s := range allSnapshots {
snapshot := s
if name == snapshot.Name {
foundSnapshots = append(foundSnapshots, &snapshot)
}
}
if len(foundSnapshots) == 0 {
return diag.Errorf("snapshot with name %s does not exist", name)
} else if len(foundSnapshots) > 1 {
return diag.Errorf("multiple snapshots found with name %s. Use snapshot_id instead of name.", name)
}
snapshot = foundSnapshots[0]
}
setSnapshotData(d, snapshot)
log.Println("[DEBUG] Finish snapshot reading")
return diags
}
func setSnapshotData(d *schema.ResourceData, snapshot *edgecloudV2.Snapshot) {
d.SetId(snapshot.ID)
d.Set("name", snapshot.Name)
d.Set("updated_at", snapshot.UpdatedAt)
d.Set("created_at", snapshot.CreatedAt)
d.Set("status", snapshot.Status)
d.Set("creator_task_id", snapshot.CreatorTaskID)
d.Set("size", snapshot.Size)
d.Set("volume_id", snapshot.VolumeID)
d.Set("description", snapshot.Description)
d.Set("snapshot_id", snapshot.ID)
d.Set("metadata", snapshot.Metadata)
}