This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
/
data_source_yandex_compute_snapshot.go
124 lines (107 loc) · 2.77 KB
/
data_source_yandex_compute_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
package yandex
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
func dataSourceYandexComputeSnapshot() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexComputeSnapshotRead,
Schema: map[string]*schema.Schema{
"snapshot_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"storage_size": {
Type: schema.TypeInt,
Computed: true,
},
"disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"source_disk_id": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"product_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := config.Context()
err := checkOneOf(d, "snapshot_id", "name")
if err != nil {
return err
}
snapshotID := d.Get("snapshot_id").(string)
_, snapshotNameOk := d.GetOk("name")
if snapshotNameOk {
snapshotID, err = resolveObjectID(ctx, config, d, sdkresolvers.SnapshotResolver)
if err != nil {
return fmt.Errorf("failed to resolve data source snapshot by name: %v", err)
}
}
snapshot, err := config.sdk.Compute().Snapshot().Get(ctx, &compute.GetSnapshotRequest{
SnapshotId: snapshotID,
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("snapshot with ID %q", snapshotID))
}
createdAt, err := getTimestamp(snapshot.CreatedAt)
if err != nil {
return err
}
d.Set("snapshot_id", snapshot.Id)
d.Set("folder_id", snapshot.FolderId)
d.Set("created_at", createdAt)
d.Set("name", snapshot.Name)
d.Set("description", snapshot.Description)
d.Set("storage_size", toGigabytes(snapshot.StorageSize))
d.Set("disk_size", toGigabytes(snapshot.DiskSize))
d.Set("status", strings.ToLower(snapshot.Status.String()))
d.Set("source_disk_id", snapshot.GetSourceDiskId())
if err := d.Set("labels", snapshot.Labels); err != nil {
return err
}
if err := d.Set("product_ids", snapshot.ProductIds); err != nil {
return err
}
d.SetId(snapshot.Id)
return nil
}