|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of mongodb instance_backups |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_mongodb_instance_backups" "instance_backups" { |
| 8 | + instance_id = "cmgo-9d0p6umb" |
| 9 | + backup_method = 0 |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudMongodbInstanceBackups() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudMongodbInstanceBackupsRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "Instance ID, the format is: cmgo-9d0p6umb.Same as the instance ID displayed in the cloud database console page.", |
| 32 | + }, |
| 33 | + |
| 34 | + "backup_method": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeInt, |
| 37 | + Description: "Backup mode, currently supported: 0-logic backup, 1-physical backup, 2-all backups.The default is logical backup.", |
| 38 | + }, |
| 39 | + |
| 40 | + "backup_list": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "backup list.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "instance_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Instance ID.", |
| 50 | + }, |
| 51 | + "backup_type": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "Backup mode type.", |
| 55 | + }, |
| 56 | + "backup_name": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Backup mode name.", |
| 60 | + }, |
| 61 | + "backup_desc": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "Remark of backup.", |
| 65 | + }, |
| 66 | + "backup_size": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Computed: true, |
| 69 | + Description: "Size of backup(KN).", |
| 70 | + }, |
| 71 | + "start_time": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "start time of backup.", |
| 75 | + }, |
| 76 | + "end_time": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "end time of backup.", |
| 80 | + }, |
| 81 | + "status": { |
| 82 | + Type: schema.TypeInt, |
| 83 | + Computed: true, |
| 84 | + Description: "Backup status.", |
| 85 | + }, |
| 86 | + "backup_method": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "Backup method.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + |
| 95 | + "result_output_file": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Optional: true, |
| 98 | + Description: "Used to save results.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func dataSourceTencentCloudMongodbInstanceBackupsRead(d *schema.ResourceData, meta interface{}) error { |
| 105 | + defer logElapsed("data_source.tencentcloud_mongodb_instance_backups.read")() |
| 106 | + defer inconsistentCheck(d, meta)() |
| 107 | + |
| 108 | + logId := getLogId(contextNil) |
| 109 | + |
| 110 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 111 | + |
| 112 | + paramMap := make(map[string]interface{}) |
| 113 | + if v, ok := d.GetOk("instance_id"); ok { |
| 114 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOkExists("backup_method"); ok { |
| 118 | + paramMap["backup_method"] = helper.IntInt64(v.(int)) |
| 119 | + } |
| 120 | + |
| 121 | + service := MongodbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 122 | + |
| 123 | + var backupList []*mongodb.BackupInfo |
| 124 | + |
| 125 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 126 | + result, e := service.DescribeMongodbInstanceBackupsByFilter(ctx, paramMap) |
| 127 | + if e != nil { |
| 128 | + return retryError(e) |
| 129 | + } |
| 130 | + backupList = result |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + ids := make([]string, 0, len(backupList)) |
| 138 | + tmpList := make([]map[string]interface{}, 0, len(backupList)) |
| 139 | + |
| 140 | + if backupList != nil { |
| 141 | + for _, backupInfo := range backupList { |
| 142 | + backupInfoMap := map[string]interface{}{} |
| 143 | + |
| 144 | + if backupInfo.InstanceId != nil { |
| 145 | + backupInfoMap["instance_id"] = backupInfo.InstanceId |
| 146 | + } |
| 147 | + |
| 148 | + if backupInfo.BackupType != nil { |
| 149 | + backupInfoMap["backup_type"] = backupInfo.BackupType |
| 150 | + } |
| 151 | + |
| 152 | + if backupInfo.BackupName != nil { |
| 153 | + backupInfoMap["backup_name"] = backupInfo.BackupName |
| 154 | + } |
| 155 | + |
| 156 | + if backupInfo.BackupDesc != nil { |
| 157 | + backupInfoMap["backup_desc"] = backupInfo.BackupDesc |
| 158 | + } |
| 159 | + |
| 160 | + if backupInfo.BackupSize != nil { |
| 161 | + backupInfoMap["backup_size"] = backupInfo.BackupSize |
| 162 | + } |
| 163 | + |
| 164 | + if backupInfo.StartTime != nil { |
| 165 | + backupInfoMap["start_time"] = backupInfo.StartTime |
| 166 | + } |
| 167 | + |
| 168 | + if backupInfo.EndTime != nil { |
| 169 | + backupInfoMap["end_time"] = backupInfo.EndTime |
| 170 | + } |
| 171 | + |
| 172 | + if backupInfo.Status != nil { |
| 173 | + backupInfoMap["status"] = backupInfo.Status |
| 174 | + } |
| 175 | + |
| 176 | + if backupInfo.BackupMethod != nil { |
| 177 | + backupInfoMap["backup_method"] = backupInfo.BackupMethod |
| 178 | + } |
| 179 | + |
| 180 | + ids = append(ids, *backupInfo.InstanceId) |
| 181 | + tmpList = append(tmpList, backupInfoMap) |
| 182 | + } |
| 183 | + |
| 184 | + _ = d.Set("backup_list", tmpList) |
| 185 | + } |
| 186 | + |
| 187 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 188 | + output, ok := d.GetOk("result_output_file") |
| 189 | + if ok && output.(string) != "" { |
| 190 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 191 | + return e |
| 192 | + } |
| 193 | + } |
| 194 | + return nil |
| 195 | +} |
0 commit comments