|
| 1 | +/* |
| 2 | +Use this data source to query the list of SQL Server account DB privileges. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_sqlserver_account_db_attachments" "test"{ |
| 8 | + instance_id = tencentcloud_sqlserver_instance.test.id |
| 9 | + account_name = tencentcloud_sqlserver_account_db_attachment.test.account_name |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudSqlserverAccountDBAttachments() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentSqlserverAccountDBAttachmentsRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "SQL Server instance ID that the account belongs to.", |
| 32 | + }, |
| 33 | + "account_name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Default: "", |
| 37 | + Description: "Name of the SQL Server account to be queried.", |
| 38 | + }, |
| 39 | + "db_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Default: "", |
| 43 | + Description: "Name of the DB to be queried.", |
| 44 | + }, |
| 45 | + "result_output_file": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + Description: "Used to store results.", |
| 49 | + }, |
| 50 | + // Computed values |
| 51 | + "list": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Computed: true, |
| 54 | + Description: "A list of SQL Server account. Each element contains the following attributes:", |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "instance_id": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "SQL Server instance ID that the account belongs to.", |
| 61 | + }, |
| 62 | + "account_name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "SQL Server account name.", |
| 66 | + }, |
| 67 | + "db_name": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "SQL Server DB name.", |
| 71 | + }, |
| 72 | + "privilege": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Privilege of the account on DB. Valid value are `ReadOnly`, `ReadWrite`.", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func dataSourceTencentSqlserverAccountDBAttachmentsRead(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer logElapsed("data_source.tencentcloud_sqlserver_account_db_attachments.read")() |
| 86 | + |
| 87 | + logId := getLogId(contextNil) |
| 88 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 89 | + |
| 90 | + instanceId := d.Get("instance_id").(string) |
| 91 | + accountName := d.Get("account_name").(string) |
| 92 | + dbName := d.Get("db_name").(string) |
| 93 | + |
| 94 | + sqlserverService := SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 95 | + |
| 96 | + attachments, err := sqlserverService.DescribeAccountDBAttachments(ctx, instanceId, accountName, dbName) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("api[DescribeAccountDBAttachments]fail, return %s", err.Error()) |
| 100 | + } |
| 101 | + |
| 102 | + var list []map[string]interface{} |
| 103 | + var ids = make([]string, len(attachments)) |
| 104 | + |
| 105 | + for _, item := range attachments { |
| 106 | + mapping := map[string]interface{}{ |
| 107 | + "instance_id": instanceId, |
| 108 | + "account_name": item["account_name"], |
| 109 | + "db_name": item["db_name"], |
| 110 | + "privilege": item["privilege"], |
| 111 | + } |
| 112 | + |
| 113 | + list = append(list, mapping) |
| 114 | + ids = append(ids, fmt.Sprintf("%s%s%s%s%s", instanceId, FILED_SP, accountName, FILED_SP, dbName)) |
| 115 | + } |
| 116 | + |
| 117 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 118 | + if e := d.Set("list", list); e != nil { |
| 119 | + log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error()) |
| 120 | + return e |
| 121 | + } |
| 122 | + output, ok := d.GetOk("result_output_file") |
| 123 | + if ok && output.(string) != "" { |
| 124 | + return writeToFile(output.(string), list) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments