/
file_storage_snapshots_data_source.go
135 lines (106 loc) · 3.02 KB
/
file_storage_snapshots_data_source.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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"github.com/hashicorp/terraform/helper/schema"
oci_file_storage "github.com/oracle/oci-go-sdk/filestorage"
)
func FileStorageSnapshotsDataSource() *schema.Resource {
return &schema.Resource{
Read: readFileStorageSnapshots,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"file_system_id": {
Type: schema.TypeString,
Required: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
},
"snapshots": {
Type: schema.TypeList,
Computed: true,
Elem: GetDataSourceItemSchema(FileStorageSnapshotResource()),
},
},
}
}
func readFileStorageSnapshots(d *schema.ResourceData, m interface{}) error {
sync := &FileStorageSnapshotsDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).fileStorageClient
return ReadResource(sync)
}
type FileStorageSnapshotsDataSourceCrud struct {
D *schema.ResourceData
Client *oci_file_storage.FileStorageClient
Res *oci_file_storage.ListSnapshotsResponse
}
func (s *FileStorageSnapshotsDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *FileStorageSnapshotsDataSourceCrud) Get() error {
request := oci_file_storage.ListSnapshotsRequest{}
if fileSystemId, ok := s.D.GetOkExists("file_system_id"); ok {
tmp := fileSystemId.(string)
request.FileSystemId = &tmp
}
if id, ok := s.D.GetOkExists("id"); ok {
tmp := id.(string)
request.Id = &tmp
}
if state, ok := s.D.GetOkExists("state"); ok {
request.LifecycleState = oci_file_storage.ListSnapshotsLifecycleStateEnum(state.(string))
}
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "file_storage")
response, err := s.Client.ListSnapshots(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
request.Page = s.Res.OpcNextPage
for request.Page != nil {
listResponse, err := s.Client.ListSnapshots(context.Background(), request)
if err != nil {
return err
}
s.Res.Items = append(s.Res.Items, listResponse.Items...)
request.Page = listResponse.OpcNextPage
}
return nil
}
func (s *FileStorageSnapshotsDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
s.D.SetId(GenerateDataSourceID())
resources := []map[string]interface{}{}
for _, r := range s.Res.Items {
snapshot := map[string]interface{}{
"file_system_id": *r.FileSystemId,
}
if r.Id != nil {
snapshot["id"] = *r.Id
}
if r.Name != nil {
snapshot["name"] = *r.Name
}
snapshot["state"] = r.LifecycleState
if r.TimeCreated != nil {
snapshot["time_created"] = r.TimeCreated.String()
}
resources = append(resources, snapshot)
}
if f, fOk := s.D.GetOkExists("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources, FileStorageSnapshotsDataSource().Schema["snapshots"].Elem.(*schema.Resource).Schema)
}
if err := s.D.Set("snapshots", resources); err != nil {
return err
}
return nil
}