Skip to content

Commit 561e6c0

Browse files
committed
New Data Source: alicloud_gpdb_data_backups.
1 parent 070bb9e commit 561e6c0

4 files changed

+408
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
package alicloud
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/PaesslerAG/jsonpath"
8+
util "github.com/alibabacloud-go/tea-utils/service"
9+
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
12+
)
13+
14+
func dataSourceAliCloudGpdbDataBackups() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceAliCloudGpdbDataBackupRead,
17+
Schema: map[string]*schema.Schema{
18+
"ids": {
19+
Type: schema.TypeList,
20+
Optional: true,
21+
ForceNew: true,
22+
Elem: &schema.Schema{Type: schema.TypeString},
23+
Computed: true,
24+
},
25+
"backup_mode": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
},
29+
"db_instance_id": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
"data_backup_id": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
},
37+
"data_type": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
},
41+
"end_time": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
},
45+
"page_number": {
46+
Type: schema.TypeInt,
47+
Optional: true,
48+
},
49+
"page_size": {
50+
Type: schema.TypeInt,
51+
Optional: true,
52+
},
53+
"start_time": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
},
57+
"status": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
},
61+
"backups": {
62+
Type: schema.TypeList,
63+
Computed: true,
64+
Elem: &schema.Resource{
65+
Schema: map[string]*schema.Schema{
66+
"backup_end_time": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
"backup_end_time_local": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
},
74+
"backup_method": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
},
78+
"backup_mode": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
"backup_set_id": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
"backup_size": {
87+
Type: schema.TypeInt,
88+
Computed: true,
89+
},
90+
"backup_start_time": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
},
94+
"backup_start_time_local": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
},
98+
"bakset_name": {
99+
Type: schema.TypeString,
100+
Computed: true,
101+
},
102+
"consistent_time": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
},
106+
"db_instance_id": {
107+
Type: schema.TypeString,
108+
Computed: true,
109+
},
110+
"data_type": {
111+
Type: schema.TypeString,
112+
Computed: true,
113+
},
114+
"status": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
},
118+
},
119+
},
120+
},
121+
"output_file": {
122+
Type: schema.TypeString,
123+
Optional: true,
124+
},
125+
},
126+
}
127+
}
128+
129+
func dataSourceAliCloudGpdbDataBackupRead(d *schema.ResourceData, meta interface{}) error {
130+
client := meta.(*connectivity.AliyunClient)
131+
132+
var request map[string]interface{}
133+
var response map[string]interface{}
134+
var query map[string]interface{}
135+
action := "DescribeDataBackups"
136+
conn, err := client.NewGpdbClient()
137+
if err != nil {
138+
return WrapError(err)
139+
}
140+
141+
var objects []map[string]interface{}
142+
for {
143+
request = make(map[string]interface{})
144+
query = make(map[string]interface{})
145+
146+
if v, ok := d.GetOk("page_number"); ok && v.(int) > 0 {
147+
request["PageNumber"] = v.(int)
148+
} else {
149+
request["PageNumber"] = 1
150+
}
151+
if v, ok := d.GetOk("page_size"); ok && v.(int) > 0 {
152+
request["PageSize"] = v.(int)
153+
} else {
154+
request["PageSize"] = PageSizeLarge
155+
}
156+
157+
if v, ok := d.GetOk("backup_mode"); ok {
158+
request["BackupMode"] = v
159+
}
160+
request["DBInstanceId"] = d.Get("db_instance_id")
161+
if v, ok := d.GetOk("data_backup_id"); ok {
162+
request["BackupId"] = v
163+
}
164+
if v, ok := d.GetOk("data_type"); ok {
165+
request["DataType"] = v
166+
}
167+
request["EndTime"] = d.Get("end_time")
168+
request["StartTime"] = d.Get("start_time")
169+
if v, ok := d.GetOk("status"); ok {
170+
request["BackupStatus"] = v
171+
}
172+
runtime := util.RuntimeOptions{}
173+
runtime.SetAutoretry(true)
174+
wait := incrementalWait(3*time.Second, 5*time.Second)
175+
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
176+
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-05-03"), StringPointer("AK"), query, request, &runtime)
177+
178+
if err != nil {
179+
if NeedRetry(err) {
180+
wait()
181+
return resource.RetryableError(err)
182+
}
183+
return resource.NonRetryableError(err)
184+
}
185+
addDebug(action, response, request)
186+
return nil
187+
})
188+
if err != nil {
189+
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
190+
}
191+
192+
idsMap := make(map[string]string)
193+
if v, ok := d.GetOk("ids"); ok {
194+
for _, vv := range v.([]interface{}) {
195+
if vv == nil {
196+
continue
197+
}
198+
idsMap[vv.(string)] = vv.(string)
199+
}
200+
}
201+
202+
resp, _ := jsonpath.Get("$.Items[*]", response)
203+
204+
result, _ := resp.([]interface{})
205+
for _, v := range result {
206+
item := v.(map[string]interface{})
207+
if len(idsMap) > 0 {
208+
if _, ok := idsMap[fmt.Sprint(item["DBInstanceId"])]; !ok {
209+
continue
210+
}
211+
}
212+
objects = append(objects, item)
213+
}
214+
215+
if len(result) < request["PageSize"].(int) {
216+
break
217+
}
218+
request["PageNumber"] = request["PageNumber"].(int) + 1
219+
220+
}
221+
222+
ids := make([]string, 0)
223+
s := make([]map[string]interface{}, 0)
224+
for _, objectRaw := range objects {
225+
mapping := map[string]interface{}{}
226+
227+
mapping["status"] = objectRaw["BackupStatus"]
228+
mapping["backup_end_time"] = objectRaw["BackupEndTime"]
229+
mapping["backup_end_time_local"] = objectRaw["BackupEndTimeLocal"]
230+
mapping["backup_method"] = objectRaw["BackupMethod"]
231+
mapping["backup_mode"] = objectRaw["BackupMode"]
232+
mapping["backup_set_id"] = objectRaw["BackupSetId"]
233+
mapping["backup_size"] = objectRaw["BackupSize"]
234+
mapping["backup_start_time"] = objectRaw["BackupStartTime"]
235+
mapping["backup_start_time_local"] = objectRaw["BackupStartTimeLocal"]
236+
mapping["bakset_name"] = objectRaw["BaksetName"]
237+
mapping["consistent_time"] = objectRaw["ConsistentTime"]
238+
mapping["db_instance_id"] = objectRaw["DBInstanceId"]
239+
mapping["data_type"] = objectRaw["DataType"]
240+
241+
ids = append(ids, fmt.Sprint(mapping["db_instance_id"]))
242+
s = append(s, mapping)
243+
}
244+
245+
d.SetId(dataResourceIdHash(ids))
246+
if err := d.Set("ids", ids); err != nil {
247+
return WrapError(err)
248+
}
249+
if err := d.Set("backups", s); err != nil {
250+
return WrapError(err)
251+
}
252+
253+
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
254+
writeToFile(output.(string), s)
255+
}
256+
return nil
257+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package alicloud
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
)
10+
11+
func TestAccAlicloudGpdbDatabackupDataSource(t *testing.T) {
12+
rand := acctest.RandIntRange(1000000, 9999999)
13+
14+
idsConf := dataSourceTestAccConfig{
15+
existConfig: testAccCheckAlicloudGpdbDatabackupSourceConfig(rand, map[string]string{
16+
"db_instance_id": `"${data.alicloud_gpdb_instances.default.ids.0}"`,
17+
"ids": `["${data.alicloud_gpdb_instances.default.ids.0}"]`,
18+
}),
19+
fakeConfig: testAccCheckAlicloudGpdbDatabackupSourceConfig(rand, map[string]string{
20+
"db_instance_id": `"${data.alicloud_gpdb_instances.default.ids.0}"`,
21+
"ids": `["${data.alicloud_gpdb_instances.default.ids.0}_fake"]`,
22+
}),
23+
}
24+
25+
allConf := dataSourceTestAccConfig{
26+
existConfig: testAccCheckAlicloudGpdbDatabackupSourceConfig(rand, map[string]string{
27+
"db_instance_id": `"${data.alicloud_gpdb_instances.default.ids.0}"`,
28+
"ids": `["${data.alicloud_gpdb_instances.default.ids.0}"]`,
29+
}),
30+
fakeConfig: testAccCheckAlicloudGpdbDatabackupSourceConfig(rand, map[string]string{
31+
"db_instance_id": `"${data.alicloud_gpdb_instances.default.ids.0}"`,
32+
"ids": `["${data.alicloud_gpdb_instances.default.ids.0}_fake"]`,
33+
}),
34+
}
35+
36+
GpdbDatabackupCheckInfo.dataSourceTestCheck(t, rand, idsConf, allConf)
37+
}
38+
39+
var existGpdbDatabackupMapFunc = func(rand int) map[string]string {
40+
return map[string]string{
41+
"backups.#": "1",
42+
"backups.0.backup_method": CHECKSET,
43+
"backups.0.status": CHECKSET,
44+
"backups.0.backup_size": CHECKSET,
45+
"backups.0.backup_mode": CHECKSET,
46+
"backups.0.backup_set_id": CHECKSET,
47+
"backups.0.backup_end_time_local": CHECKSET,
48+
"backups.0.db_instance_id": CHECKSET,
49+
"backups.0.backup_end_time": CHECKSET,
50+
"backups.0.consistent_time": CHECKSET,
51+
"backups.0.backup_start_time": CHECKSET,
52+
"backups.0.data_type": CHECKSET,
53+
"backups.0.backup_start_time_local": CHECKSET,
54+
}
55+
}
56+
57+
var fakeGpdbDatabackupMapFunc = func(rand int) map[string]string {
58+
return map[string]string{
59+
"backups.#": "0",
60+
}
61+
}
62+
63+
var GpdbDatabackupCheckInfo = dataSourceAttr{
64+
resourceId: "data.alicloud_gpdb_data_backups.default",
65+
existMapFunc: existGpdbDatabackupMapFunc,
66+
fakeMapFunc: fakeGpdbDatabackupMapFunc,
67+
}
68+
69+
func testAccCheckAlicloudGpdbDatabackupSourceConfig(rand int, attrMap map[string]string) string {
70+
var pairs []string
71+
for k, v := range attrMap {
72+
pairs = append(pairs, k+" = "+v)
73+
}
74+
config := fmt.Sprintf(`
75+
variable "name" {
76+
default = "tf-testAccGpdbDatabackup%d"
77+
}
78+
data "alicloud_gpdb_instances" "default" {
79+
name_regex = "^default-NODELETING$"
80+
}
81+
82+
data "alicloud_gpdb_data_backups" "default" {
83+
%s
84+
}
85+
`, rand, strings.Join(pairs, "\n "))
86+
return config
87+
}

alicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func Provider() terraform.ResourceProvider {
164164
},
165165
},
166166
DataSourcesMap: map[string]*schema.Resource{
167+
"alicloud_gpdb_data_backups": dataSourceAliCloudGpdbDataBackups(),
167168
"alicloud_gpdb_log_backups": dataSourceAliCloudGpdbLogbackups(),
168169
"alicloud_governance_baselines": dataSourceAliCloudGovernanceBaselines(),
169170
"alicloud_vpn_gateway_zones": dataSourceAliCloudVPNGatewayZones(),

0 commit comments

Comments
 (0)