|
| 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 | +} |
0 commit comments