-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_dbaas_pgsql_backups.go
122 lines (113 loc) · 3.44 KB
/
data_source_dbaas_pgsql_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
package ionoscloud
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
dbaasService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dbaas"
)
func dataSourceDbaasPgSqlBackups() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDbaasPgSqlReadBackups,
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
},
"cluster_backups": {
Type: schema.TypeList,
Description: "list of backups",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "The unique ID of the resource.",
Computed: true,
},
"cluster_id": {
Type: schema.TypeString,
Description: "The unique ID of the cluster",
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Description: "Size of all base backups including the wal size in MB.",
Computed: true,
},
"location": {
Type: schema.TypeString,
Description: "The S3 location where the backups will be stored.",
Computed: true,
},
"version": {
Type: schema.TypeString,
Description: "The PostgreSQL version this backup was created from.",
Computed: true,
},
"is_active": {
Type: schema.TypeBool,
Description: "Whether a cluster currently backs up data to this backup.",
Computed: true,
},
"earliest_recovery_target_time": {
Type: schema.TypeString,
Description: "The oldest available timestamp to which you can restore.",
Computed: true,
},
"metadata": {
Type: schema.TypeList,
Description: "Metadata of the resource",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"created_date": {
Type: schema.TypeString,
Description: "The ISO 8601 creation timestamp.",
Computed: true,
},
},
},
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceDbaasPgSqlReadBackups(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).PsqlClient
id, idOk := d.GetOk("cluster_id")
idStr := id.(string)
if !idOk {
diags := diag.FromErr(fmt.Errorf("cluster_id has to be provided in order to search for backups"))
return diags
}
/* search by ID */
clusterBackups, resp, err := client.GetClusterBackups(ctx, idStr)
if resp != nil {
log.Printf("operation %s", resp.Operation)
if resp.Response != nil {
log.Printf("[DEBUG] response status code : %d\n", resp.StatusCode)
}
}
if err != nil {
diags := diag.FromErr(fmt.Errorf("an error occurred while fetching backup for cluster with ID %s: %w", idStr, err))
return diags
}
if len(*clusterBackups.Items) == 0 {
diags := diag.FromErr(fmt.Errorf("could not find backups for cluster with ID %s: %w", idStr, err))
return diags
}
if diags := dbaasService.SetPgSqlClusterBackupData(d, &clusterBackups); diags != nil {
return diags
}
return nil
}