|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of sqlserver datasource_backup_command |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_sqlserver_backup_commands" "backup_command" { |
| 8 | + backup_file_type = "FULL" |
| 9 | + data_base_name = "db_name" |
| 10 | + is_recovery = "No" |
| 11 | + local_path = "" |
| 12 | + } |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | + sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" |
| 25 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudSqlserverBackupCommands() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudSqlserverBackupCommandsRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "backup_file_type": { |
| 33 | + Required: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Backup file type. Full: full backup. FULL_LOG: full backup which needs log increments. FULL_DIFF: full backup which needs differential increments. LOG: log backup. DIFF: differential backup.", |
| 36 | + }, |
| 37 | + "data_base_name": { |
| 38 | + Required: true, |
| 39 | + Type: schema.TypeString, |
| 40 | + Description: "Database name.", |
| 41 | + }, |
| 42 | + "is_recovery": { |
| 43 | + Required: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "Whether restoration is required. No: not required. Yes: required.", |
| 46 | + }, |
| 47 | + "local_path": { |
| 48 | + Optional: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "Storage path of backup files. If this parameter is left empty, the default storage path will be D:.", |
| 51 | + }, |
| 52 | + "result_output_file": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + Description: "Used to save results.", |
| 56 | + }, |
| 57 | + "list": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Description: "Command list.", |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "command": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Create backup command.", |
| 67 | + }, |
| 68 | + "request_id": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Request ID.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func dataSourceTencentCloudSqlserverBackupCommandsRead(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer logElapsed("data_source.tencentcloud_sqlserver_backup_commands.read")() |
| 82 | + defer inconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + var ( |
| 85 | + logId = getLogId(contextNil) |
| 86 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 87 | + command []*sqlserver.DescribeBackupCommandResponseParams |
| 88 | + ) |
| 89 | + |
| 90 | + paramMap := make(map[string]interface{}) |
| 91 | + if v, ok := d.GetOk("backup_file_type"); ok { |
| 92 | + paramMap["BackupFileType"] = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("data_base_name"); ok { |
| 96 | + paramMap["DataBaseName"] = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("is_recovery"); ok { |
| 100 | + paramMap["IsRecovery"] = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOk("local_path"); ok { |
| 104 | + paramMap["LocalPath"] = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + service := SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 108 | + |
| 109 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 110 | + result, err := service.DescribeSqlserverBackupCommand(ctx, paramMap) |
| 111 | + if err != nil { |
| 112 | + return retryError(err) |
| 113 | + } |
| 114 | + |
| 115 | + command = result |
| 116 | + return nil |
| 117 | + }) |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + var list []map[string]interface{} |
| 124 | + var ids = make([]string, len(command)) |
| 125 | + for _, item := range command { |
| 126 | + mapping := map[string]interface{}{ |
| 127 | + "command": item.Command, |
| 128 | + "request_id": item.RequestId, |
| 129 | + } |
| 130 | + list = append(list, mapping) |
| 131 | + ids = append(ids, fmt.Sprintf("%s%s", *item.Command, *item.RequestId)) |
| 132 | + } |
| 133 | + |
| 134 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 135 | + if e := d.Set("list", list); e != nil { |
| 136 | + log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error()) |
| 137 | + return e |
| 138 | + } |
| 139 | + |
| 140 | + output, ok := d.GetOk("result_output_file") |
| 141 | + if ok && output.(string) != "" { |
| 142 | + if e := writeToFile(output.(string), command); e != nil { |
| 143 | + return e |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments