-
Notifications
You must be signed in to change notification settings - Fork 3
/
backups.go
170 lines (148 loc) · 5.38 KB
/
backups.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
package datasources
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api"
backupapi "github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api/backup"
providerschema "github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/schema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &Backups{}
_ datasource.DataSourceWithConfigure = &Backups{}
)
// Backups is the Backups data source implementation.
type Backups struct {
*providerschema.Data
}
// NewBackups is a helper function to simplify the provider implementation.
func NewBackups() datasource.DataSource {
return &Backups{}
}
// Metadata returns the backup data source type name.
func (d *Backups) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_backups"
}
// Schema defines the schema for the Backups data source.
func (d *Backups) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = BackupSchema()
}
// Read refreshes the Terraform state with the latest data of backups.
func (d *Backups) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state providerschema.Backups
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
bucketId, clusterId, projectId, organizationId, err := state.Validate()
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Backups in Capella",
"Could not read Capella Backups in cluster "+clusterId+": "+err.Error(),
)
return
}
// Get all the cycles
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/buckets/%s/backup/cycles", d.HostURL, organizationId, projectId, clusterId, bucketId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := d.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
d.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Backup Cycles",
fmt.Sprintf("Could not read backup cycles in a cluster %s, unexpected error: %s", clusterId, api.ParseError(err)),
)
return
}
cyclesResp := backupapi.GetCyclesResponse{}
err = json.Unmarshal(response.Body, &cyclesResp)
if err != nil {
resp.Diagnostics.AddError(
"Error reading backup cycles",
"Could not read backup cycles, unexpected error: "+err.Error(),
)
return
}
// Loop through the cycles to fetch all backups within a cycle
for _, cycle := range cyclesResp.Data {
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/buckets/%s/backup/cycles/%s", d.HostURL, organizationId, projectId, clusterId, bucketId, cycle.CycleId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := d.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
d.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Capella Backups in a Cycle",
fmt.Sprintf("Could not read backups for a cycle %s in a bucket %s, unexpected error: %s", cycle.CycleId, bucketId, api.ParseError(err)),
)
return
}
backupsResp := backupapi.GetBackupsResponse{}
err = json.Unmarshal(response.Body, &backupsResp)
if err != nil {
resp.Diagnostics.AddError(
"Error reading backups",
"Could not read backups, unexpected error: "+err.Error(),
)
return
}
for i := range backupsResp.Data {
backup := backupsResp.Data[i]
backupStats := providerschema.NewBackupStats(*backup.BackupStats)
backupStatsObj, diags := types.ObjectValueFrom(ctx, backupStats.AttributeTypes(), backupStats)
if diags.HasError() {
resp.Diagnostics.AddError(
"Error Reading Backup Stats",
fmt.Sprintf("Could not read backup stats data in a backup record, unexpected error: %s", fmt.Errorf("error while backup stats conversion")),
)
return
}
scheduleInfo := providerschema.NewScheduleInfo(*backup.ScheduleInfo)
scheduleInfoObj, diags := types.ObjectValueFrom(ctx, scheduleInfo.AttributeTypes(), scheduleInfo)
if diags.HasError() {
resp.Diagnostics.AddError(
"Error Error Reading Backup Schedule Info",
fmt.Sprintf("Could not read backup schedule info in a backup record, unexpected error: %s", fmt.Errorf("error while backup schedule info conversion")),
)
return
}
newBackupData := providerschema.NewBackupData(&backup, organizationId, projectId, backupStatsObj, scheduleInfoObj)
state.Data = append(state.Data, *newBackupData)
}
}
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the cluster data source.
func (d *Backups) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*providerschema.Data)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *ProviderSourceData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.Data = data
}