/
data_source_backup.go
202 lines (169 loc) · 5.97 KB
/
data_source_backup.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package db
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/clients"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/services/db"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/services/db/v1/backups"
)
var (
_ datasource.DataSource = &BackupDataSource{}
_ datasource.DataSourceWithConfigure = &BackupDataSource{}
)
func NewBackupDataSource() datasource.DataSource {
return &BackupDataSource{}
}
type BackupDataSource struct {
config clients.Config
}
type BackupDataSourceModel struct {
ID types.String `tfsdk:"id"`
Region types.String `tfsdk:"region"`
BackupID types.String `tfsdk:"backup_id"`
Created types.String `tfsdk:"created"`
Datastore types.List `tfsdk:"datastore"`
DbmsID types.String `tfsdk:"dbms_id"`
DbmsType types.String `tfsdk:"dbms_type"`
Description types.String `tfsdk:"description"`
LocationRef types.String `tfsdk:"location_ref"`
Meta types.String `tfsdk:"meta"`
Name types.String `tfsdk:"name"`
Size types.Float64 `tfsdk:"size"`
Updated types.String `tfsdk:"updated"`
WalSize types.Float64 `tfsdk:"wal_size"`
}
func (d *BackupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "vkcs_db_backup"
}
func (d *BackupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the resource.",
},
"region": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The region in which to obtain the service client. If omitted, the `region` argument of the provider is used.",
},
"backup_id": schema.StringAttribute{
Required: true,
Description: "The UUID of the backup.",
},
"created": schema.StringAttribute{
Computed: true,
Description: "Backup creation timestamp",
},
// TODO: change type to SingleNestedAttribute #BREAKING_CHANGE
"datastore": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Required: true,
Description: "Type of the datastore.",
},
"version": schema.StringAttribute{
Required: true,
Description: "Version of the datastore.",
},
},
},
Computed: true,
Description: "Object that represents datastore of backup",
},
"dbms_id": schema.StringAttribute{
Computed: true,
Description: "ID of the backed up instance or cluster",
},
"dbms_type": schema.StringAttribute{
Computed: true,
Description: "Type of dbms of the backup, can be \"instance\" or \"cluster\".",
},
"description": schema.StringAttribute{
Optional: true,
Description: "The description of the backup",
},
"location_ref": schema.StringAttribute{
Computed: true,
Description: "Location of backup data on backup storage",
},
"meta": schema.StringAttribute{
Computed: true,
Description: "Metadata of the backup",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the backup.",
},
"size": schema.Float64Attribute{
Computed: true,
Description: "Backup's volume size",
},
"updated": schema.StringAttribute{
Computed: true,
Description: "Timestamp of backup's last update",
},
"wal_size": schema.Float64Attribute{
Computed: true,
Description: "Backup's WAL volume size",
},
},
Description: "Use this data source to get the information on a db backup resource.",
}
}
func (d *BackupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.config = req.ProviderData.(clients.Config)
}
func (d *BackupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data BackupDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
region := data.Region.ValueString()
if region == "" {
region = d.config.GetRegion()
}
client, err := d.config.DatabaseV1Client(region)
if err != nil {
resp.Diagnostics.AddError("Error creating VKCS Databases API client", err.Error())
return
}
tflog.Debug(ctx, "Calling Databases API to get the backup")
backup, err := backups.Get(client, data.BackupID.ValueString()).Extract()
if err != nil {
resp.Diagnostics.AddError("Error calling VKCS Databases API", err.Error())
return
}
tflog.Debug(ctx, "Called Databases API to get the backup", map[string]interface{}{"backup": fmt.Sprintf("%#v", backup)})
data.ID = types.StringValue(backup.ID)
data.Region = types.StringValue(region)
data.Created = types.StringValue(backup.Created)
data.Datastore = flattenBackupDatastore(ctx, *backup.Datastore, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if backup.InstanceID != "" {
data.DbmsID = types.StringValue(backup.InstanceID)
data.DbmsType = types.StringValue(db.DBMSTypeInstance)
} else {
data.DbmsID = types.StringValue(backup.ClusterID)
data.DbmsType = types.StringValue(db.DBMSTypeInstance)
}
data.Description = types.StringValue(backup.Description)
data.LocationRef = types.StringValue(backup.LocationRef)
data.Meta = types.StringValue(backup.Meta)
data.Name = types.StringValue(backup.Name)
data.Size = types.Float64Value(backup.Size)
data.Updated = types.StringValue(backup.Updated)
data.WalSize = types.Float64Value(backup.WalSize)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}