/
backups_data_source.go
174 lines (160 loc) · 6.18 KB
/
backups_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
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 provider
import (
"context"
"fmt"
backupApi "github.com/c4pt0r/go-tidbcloud-sdk-v1/client/backup"
"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"
"math/rand"
"strconv"
)
type backupsDataSourceData struct {
Id types.String `tfsdk:"id"`
ProjectId string `tfsdk:"project_id"`
ClusterId string `tfsdk:"cluster_id"`
Page types.Int64 `tfsdk:"page"`
PageSize types.Int64 `tfsdk:"page_size"`
Items []backup `tfsdk:"items"`
Total types.Int64 `tfsdk:"total"`
}
type backup struct {
Id string `tfsdk:"id"`
Name string `tfsdk:"name"`
Description string `tfsdk:"description"`
Type string `tfsdk:"type"`
Size string `tfsdk:"size"`
CreateTimestamp string `tfsdk:"create_timestamp"`
Status string `tfsdk:"status"`
}
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &backupsDataSource{}
type backupsDataSource struct {
provider *tidbcloudProvider
}
func NewBackupsDataSource() datasource.DataSource {
return &backupsDataSource{}
}
func (d *backupsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_backups"
}
func (d *backupsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
var ok bool
if d.provider, ok = req.ProviderData.(*tidbcloudProvider); !ok {
resp.Diagnostics.AddError("Internal provider error",
fmt.Sprintf("Error in Configure: expected %T but got %T", tidbcloudProvider{}, req.ProviderData))
}
}
func (d *backupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "backups data source",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "data source ID",
Computed: true,
},
"project_id": schema.StringAttribute{
MarkdownDescription: "The ID of the project. You can get the project ID from [tidbcloud_projects datasource](../datasource/projects.md).",
Required: true,
},
"cluster_id": schema.StringAttribute{
MarkdownDescription: "The ID of your cluster.",
Required: true,
},
"page": schema.Int64Attribute{
MarkdownDescription: "Default:1 The number of pages.",
Optional: true,
Computed: true,
},
"page_size": schema.Int64Attribute{
MarkdownDescription: "Default:10 The size of a pages.",
Optional: true,
Computed: true,
},
"items": schema.ListNestedAttribute{
MarkdownDescription: "The items of backups",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the backup. It is generated by TiDB Cloud.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the backup. It is specified by the user when taking a manual type backup. It helps you add additional information to the backup.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the backup.",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "Enum: MANUAL/AUTO.The type of backup. TiDB Cloud only supports manual and auto backup. For more information, see [TiDB Cloud Documentation](https://docs.pingcap.com/tidbcloud/backup-and-restore#backup).",
Computed: true,
},
"size": schema.StringAttribute{
MarkdownDescription: "The bytes of the backup.",
Computed: true,
},
"create_timestamp": schema.StringAttribute{
MarkdownDescription: "The creation time of the backup in UTC. The time format follows the ISO8601 standard, which is YYYY-MM-DD (year-month-day) + T +HH:MM:SS (hour-minutes-seconds) + Z. For example, 2020-01-01T00:00:00Z.",
Computed: true,
},
"status": schema.StringAttribute{
MarkdownDescription: "Enum: PENDING/RUNNING/FAILED/SUCCESS.The status of backup.",
Computed: true,
},
},
},
},
"total": schema.Int64Attribute{
MarkdownDescription: "The total number of backups in the project.",
Computed: true,
},
},
}
}
func (d *backupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data backupsDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// set default value
var page int32 = 1
var pageSize int32 = 10
if !data.Page.IsNull() && !data.Page.IsUnknown() {
page = int32(data.Page.ValueInt64())
}
if !data.PageSize.IsNull() && !data.PageSize.IsUnknown() {
pageSize = int32(data.PageSize.ValueInt64())
}
tflog.Trace(ctx, "read backups data source")
listBackUpOfClusterOK, err := d.provider.client.ListBackUpOfCluster(backupApi.NewListBackUpOfClusterParams().WithProjectID(data.ProjectId).WithClusterID(data.ClusterId).WithPage(&page).WithPageSize(&pageSize))
if err != nil {
resp.Diagnostics.AddError("Read Error", fmt.Sprintf("Unable to call GetBackups, got error: %s", err))
return
}
data.Id = types.StringValue(strconv.FormatInt(rand.Int63(), 10))
data.Total = types.Int64Value(listBackUpOfClusterOK.Payload.Total)
var items []backup
for _, key := range listBackUpOfClusterOK.Payload.Items {
items = append(items, backup{
Id: key.ID,
Description: key.Description,
Name: key.Name,
Type: key.Type,
Size: key.Size,
CreateTimestamp: key.CreateTimestamp.String(),
Status: key.Status,
})
}
data.Items = items
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}