|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of redis backup |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_redis_backup" "backup" { |
| 8 | + instance_id = "crs-c1nl9rpv" |
| 9 | + begin_time = "2023-04-07 03:57:30" |
| 10 | + end_time = "2023-04-07 03:57:56" |
| 11 | + status = [2] |
| 12 | + instance_name = "Keep-terraform" |
| 13 | +} |
| 14 | +``` |
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudRedisBackup() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudRedisBackupRead, |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "instance_id": { |
| 32 | + Optional: true, |
| 33 | + Type: schema.TypeString, |
| 34 | + Description: "The ID of instance.", |
| 35 | + }, |
| 36 | + |
| 37 | + "begin_time": { |
| 38 | + Optional: true, |
| 39 | + Type: schema.TypeString, |
| 40 | + Description: "start time, such as 2017-02-08 19:09:26.Query the list of backups that the instance started backing up during the [beginTime, endTime] time period.", |
| 41 | + }, |
| 42 | + |
| 43 | + "end_time": { |
| 44 | + Optional: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "End time, such as 2017-02-08 19:09:26.Query the list of backups that the instance started backing up during the [beginTime, endTime] time period.", |
| 47 | + }, |
| 48 | + |
| 49 | + "status": { |
| 50 | + Optional: true, |
| 51 | + Type: schema.TypeSet, |
| 52 | + Elem: &schema.Schema{ |
| 53 | + Type: schema.TypeInt, |
| 54 | + }, |
| 55 | + Description: "Status of the backup task:1: Backup is in the process.2: The backup is normal.3: Backup to RDB file processing.4: RDB conversion completed.-1: The backup has expired.-2: Backup deleted.", |
| 56 | + }, |
| 57 | + |
| 58 | + "instance_name": { |
| 59 | + Optional: true, |
| 60 | + Type: schema.TypeString, |
| 61 | + Description: "Instance name, which supports fuzzy search based on instance name.", |
| 62 | + }, |
| 63 | + |
| 64 | + "backup_set": { |
| 65 | + Computed: true, |
| 66 | + Type: schema.TypeList, |
| 67 | + Description: "An array of backups for the instance.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "start_time": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Backup start time.", |
| 74 | + }, |
| 75 | + "backup_id": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Backup ID.", |
| 79 | + }, |
| 80 | + "backup_type": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "Backup type.1: User-initiated manual backup.0: System-initiated backup in the early morning.", |
| 84 | + }, |
| 85 | + "status": { |
| 86 | + Type: schema.TypeInt, |
| 87 | + Computed: true, |
| 88 | + Description: "Backup status.1: The backup is locked by another process.2: The backup is normal and not locked by any process.-1: The backup has expired.3: The backup is being exported.4: The backup export is successful.", |
| 89 | + }, |
| 90 | + "remark": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "Notes information for the backup.", |
| 94 | + }, |
| 95 | + "locked": { |
| 96 | + Type: schema.TypeInt, |
| 97 | + Computed: true, |
| 98 | + Description: "Whether the backup is locked.0: Not locked.1: Has been locked.", |
| 99 | + }, |
| 100 | + "backup_size": { |
| 101 | + Type: schema.TypeInt, |
| 102 | + Computed: true, |
| 103 | + Description: "Internal fields, which can be ignored by the user.", |
| 104 | + }, |
| 105 | + "full_backup": { |
| 106 | + Type: schema.TypeInt, |
| 107 | + Computed: true, |
| 108 | + Description: "Internal fields, which can be ignored by the user.", |
| 109 | + }, |
| 110 | + "instance_type": { |
| 111 | + Type: schema.TypeInt, |
| 112 | + Computed: true, |
| 113 | + Description: "Internal fields, which can be ignored by the user.", |
| 114 | + }, |
| 115 | + "instance_id": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Computed: true, |
| 118 | + Description: "The ID of instance.", |
| 119 | + }, |
| 120 | + "instance_name": { |
| 121 | + Type: schema.TypeString, |
| 122 | + Computed: true, |
| 123 | + Description: "The name of instance.", |
| 124 | + }, |
| 125 | + "region": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Computed: true, |
| 128 | + Description: "The region where the backup is located.", |
| 129 | + }, |
| 130 | + "end_time": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Computed: true, |
| 133 | + Description: "Backup end time.", |
| 134 | + }, |
| 135 | + "file_type": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Computed: true, |
| 138 | + Description: "Back up file types.", |
| 139 | + }, |
| 140 | + "expire_time": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Computed: true, |
| 143 | + Description: "Backup file expiration time.", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + |
| 149 | + "result_output_file": { |
| 150 | + Type: schema.TypeString, |
| 151 | + Optional: true, |
| 152 | + Description: "Used to save results.", |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func dataSourceTencentCloudRedisBackupRead(d *schema.ResourceData, meta interface{}) error { |
| 159 | + defer logElapsed("data_source.tencentcloud_redis_backup.read")() |
| 160 | + defer inconsistentCheck(d, meta)() |
| 161 | + |
| 162 | + logId := getLogId(contextNil) |
| 163 | + |
| 164 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 165 | + |
| 166 | + paramMap := make(map[string]interface{}) |
| 167 | + if v, ok := d.GetOk("instance_id"); ok { |
| 168 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 169 | + } |
| 170 | + |
| 171 | + if v, ok := d.GetOk("begin_time"); ok { |
| 172 | + paramMap["begin_time"] = helper.String(v.(string)) |
| 173 | + } |
| 174 | + |
| 175 | + if v, ok := d.GetOk("end_time"); ok { |
| 176 | + paramMap["end_time"] = helper.String(v.(string)) |
| 177 | + } |
| 178 | + |
| 179 | + if v, ok := d.GetOk("status"); ok { |
| 180 | + statusSet := v.(*schema.Set).List() |
| 181 | + statusList := []*int64{} |
| 182 | + for i := range statusSet { |
| 183 | + status := statusSet[i].(int) |
| 184 | + statusList = append(statusList, helper.IntInt64(status)) |
| 185 | + } |
| 186 | + paramMap["status"] = statusList |
| 187 | + } |
| 188 | + |
| 189 | + if v, ok := d.GetOk("instance_name"); ok { |
| 190 | + paramMap["InstanceName"] = helper.String(v.(string)) |
| 191 | + } |
| 192 | + |
| 193 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 194 | + |
| 195 | + var backupSet []*redis.RedisBackupSet |
| 196 | + |
| 197 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 198 | + result, e := service.DescribeRedisBackupByFilter(ctx, paramMap) |
| 199 | + if e != nil { |
| 200 | + return retryError(e) |
| 201 | + } |
| 202 | + backupSet = result |
| 203 | + return nil |
| 204 | + }) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + ids := make([]string, 0, len(backupSet)) |
| 210 | + tmpList := make([]map[string]interface{}, 0, len(backupSet)) |
| 211 | + |
| 212 | + if backupSet != nil { |
| 213 | + for _, redisBackupSet := range backupSet { |
| 214 | + redisBackupSetMap := map[string]interface{}{} |
| 215 | + |
| 216 | + if redisBackupSet.StartTime != nil { |
| 217 | + redisBackupSetMap["start_time"] = redisBackupSet.StartTime |
| 218 | + } |
| 219 | + |
| 220 | + if redisBackupSet.BackupId != nil { |
| 221 | + redisBackupSetMap["backup_id"] = redisBackupSet.BackupId |
| 222 | + } |
| 223 | + |
| 224 | + if redisBackupSet.BackupType != nil { |
| 225 | + redisBackupSetMap["backup_type"] = redisBackupSet.BackupType |
| 226 | + } |
| 227 | + |
| 228 | + if redisBackupSet.Status != nil { |
| 229 | + redisBackupSetMap["status"] = redisBackupSet.Status |
| 230 | + } |
| 231 | + |
| 232 | + if redisBackupSet.Remark != nil { |
| 233 | + redisBackupSetMap["remark"] = redisBackupSet.Remark |
| 234 | + } |
| 235 | + |
| 236 | + if redisBackupSet.Locked != nil { |
| 237 | + redisBackupSetMap["locked"] = redisBackupSet.Locked |
| 238 | + } |
| 239 | + |
| 240 | + if redisBackupSet.BackupSize != nil { |
| 241 | + redisBackupSetMap["backup_size"] = redisBackupSet.BackupSize |
| 242 | + } |
| 243 | + |
| 244 | + if redisBackupSet.FullBackup != nil { |
| 245 | + redisBackupSetMap["full_backup"] = redisBackupSet.FullBackup |
| 246 | + } |
| 247 | + |
| 248 | + if redisBackupSet.InstanceType != nil { |
| 249 | + redisBackupSetMap["instance_type"] = redisBackupSet.InstanceType |
| 250 | + } |
| 251 | + |
| 252 | + if redisBackupSet.InstanceId != nil { |
| 253 | + redisBackupSetMap["instance_id"] = redisBackupSet.InstanceId |
| 254 | + } |
| 255 | + |
| 256 | + if redisBackupSet.InstanceName != nil { |
| 257 | + redisBackupSetMap["instance_name"] = redisBackupSet.InstanceName |
| 258 | + } |
| 259 | + |
| 260 | + if redisBackupSet.Region != nil { |
| 261 | + redisBackupSetMap["region"] = redisBackupSet.Region |
| 262 | + } |
| 263 | + |
| 264 | + if redisBackupSet.EndTime != nil { |
| 265 | + redisBackupSetMap["end_time"] = redisBackupSet.EndTime |
| 266 | + } |
| 267 | + |
| 268 | + if redisBackupSet.FileType != nil { |
| 269 | + redisBackupSetMap["file_type"] = redisBackupSet.FileType |
| 270 | + } |
| 271 | + |
| 272 | + if redisBackupSet.ExpireTime != nil { |
| 273 | + redisBackupSetMap["expire_time"] = redisBackupSet.ExpireTime |
| 274 | + } |
| 275 | + |
| 276 | + ids = append(ids, *redisBackupSet.InstanceId) |
| 277 | + tmpList = append(tmpList, redisBackupSetMap) |
| 278 | + } |
| 279 | + |
| 280 | + _ = d.Set("backup_set", tmpList) |
| 281 | + } |
| 282 | + |
| 283 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 284 | + output, ok := d.GetOk("result_output_file") |
| 285 | + if ok && output.(string) != "" { |
| 286 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 287 | + return e |
| 288 | + } |
| 289 | + } |
| 290 | + return nil |
| 291 | +} |
0 commit comments