|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cynosdb account_all_grant_privileges |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_account_all_grant_privileges" "account_all_grant_privileges" { |
| 8 | + cluster_id = "cynosdbmysql-bws8h88b" |
| 9 | + account { |
| 10 | + account_name = "keep_dts" |
| 11 | + host = "%" |
| 12 | + } |
| 13 | +} |
| 14 | +``` |
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 23 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudCynosdbAccountAllGrantPrivileges() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudCynosdbAccountAllGrantPrivilegesRead, |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "cluster_id": { |
| 32 | + Required: true, |
| 33 | + Type: schema.TypeString, |
| 34 | + Description: "Cluster ID.", |
| 35 | + }, |
| 36 | + "account": { |
| 37 | + Required: true, |
| 38 | + Type: schema.TypeList, |
| 39 | + MaxItems: 1, |
| 40 | + Description: "account information.", |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "account_name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: "Account.", |
| 47 | + }, |
| 48 | + "host": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Description: "Host, default `%`.", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + "privilege_statements": { |
| 57 | + Computed: true, |
| 58 | + Type: schema.TypeSet, |
| 59 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 60 | + Description: "Permission statement note: This field may return null, indicating that a valid value cannot be obtained.", |
| 61 | + }, |
| 62 | + "global_privileges": { |
| 63 | + Computed: true, |
| 64 | + Type: schema.TypeSet, |
| 65 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 66 | + Description: "Global permission note: This field may return null, indicating that a valid value cannot be obtained.", |
| 67 | + }, |
| 68 | + "database_privileges": { |
| 69 | + Computed: true, |
| 70 | + Type: schema.TypeList, |
| 71 | + Description: "Database permissions note: This field may return null, indicating that a valid value cannot be obtained.", |
| 72 | + Elem: &schema.Resource{ |
| 73 | + Schema: map[string]*schema.Schema{ |
| 74 | + "db": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "database.", |
| 78 | + }, |
| 79 | + "privileges": { |
| 80 | + Type: schema.TypeSet, |
| 81 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 82 | + Computed: true, |
| 83 | + Description: "Permission List.", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + "table_privileges": { |
| 89 | + Computed: true, |
| 90 | + Type: schema.TypeList, |
| 91 | + Description: "Database table permissions note: This field may return null, indicating that a valid value cannot be obtained.", |
| 92 | + Elem: &schema.Resource{ |
| 93 | + Schema: map[string]*schema.Schema{ |
| 94 | + "db": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Database name.", |
| 98 | + }, |
| 99 | + "table_name": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + Description: "Table Name.", |
| 103 | + }, |
| 104 | + "privileges": { |
| 105 | + Type: schema.TypeSet, |
| 106 | + Elem: &schema.Schema{ |
| 107 | + Type: schema.TypeString, |
| 108 | + }, |
| 109 | + Computed: true, |
| 110 | + Description: "Permission List.", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + "result_output_file": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Optional: true, |
| 118 | + Description: "Used to save results.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func dataSourceTencentCloudCynosdbAccountAllGrantPrivilegesRead(d *schema.ResourceData, meta interface{}) error { |
| 125 | + defer logElapsed("data_source.tencentcloud_cynosdb_account_all_grant_privileges.read")() |
| 126 | + defer inconsistentCheck(d, meta)() |
| 127 | + |
| 128 | + var ( |
| 129 | + logId = getLogId(contextNil) |
| 130 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 131 | + service = CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 132 | + accountAllGrantPrivileges *cynosdb.DescribeAccountAllGrantPrivilegesResponseParams |
| 133 | + clusterId string |
| 134 | + ) |
| 135 | + |
| 136 | + paramMap := make(map[string]interface{}) |
| 137 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 138 | + paramMap["ClusterId"] = helper.String(v.(string)) |
| 139 | + clusterId = v.(string) |
| 140 | + } |
| 141 | + |
| 142 | + if dMap, ok := helper.InterfacesHeadMap(d, "account"); ok { |
| 143 | + inputAccount := cynosdb.InputAccount{} |
| 144 | + if v, ok := dMap["account_name"]; ok { |
| 145 | + inputAccount.AccountName = helper.String(v.(string)) |
| 146 | + } |
| 147 | + if v, ok := dMap["host"]; ok { |
| 148 | + inputAccount.Host = helper.String(v.(string)) |
| 149 | + } |
| 150 | + paramMap["Account"] = &inputAccount |
| 151 | + } |
| 152 | + |
| 153 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 154 | + result, e := service.DescribeCynosdbAccountAllGrantPrivilegesByFilter(ctx, paramMap) |
| 155 | + if e != nil { |
| 156 | + return retryError(e) |
| 157 | + } |
| 158 | + |
| 159 | + accountAllGrantPrivileges = result |
| 160 | + return nil |
| 161 | + }) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + if accountAllGrantPrivileges.PrivilegeStatements != nil { |
| 168 | + _ = d.Set("privilege_statements", accountAllGrantPrivileges.PrivilegeStatements) |
| 169 | + } |
| 170 | + |
| 171 | + if accountAllGrantPrivileges.GlobalPrivileges != nil { |
| 172 | + _ = d.Set("global_privileges", accountAllGrantPrivileges.GlobalPrivileges) |
| 173 | + } |
| 174 | + |
| 175 | + if accountAllGrantPrivileges.DatabasePrivileges != nil { |
| 176 | + tmpList := []interface{}{} |
| 177 | + for _, databasePrivileges := range accountAllGrantPrivileges.DatabasePrivileges { |
| 178 | + databasePrivilegesMap := map[string]interface{}{} |
| 179 | + |
| 180 | + if databasePrivileges.Db != nil { |
| 181 | + databasePrivilegesMap["db"] = databasePrivileges.Db |
| 182 | + } |
| 183 | + |
| 184 | + if databasePrivileges.Privileges != nil { |
| 185 | + databasePrivilegesMap["privileges"] = databasePrivileges.Privileges |
| 186 | + } |
| 187 | + |
| 188 | + tmpList = append(tmpList, databasePrivilegesMap) |
| 189 | + } |
| 190 | + |
| 191 | + _ = d.Set("database_privileges", tmpList) |
| 192 | + } |
| 193 | + |
| 194 | + if accountAllGrantPrivileges.TablePrivileges != nil { |
| 195 | + tmpList := []interface{}{} |
| 196 | + for _, tablePrivileges := range accountAllGrantPrivileges.TablePrivileges { |
| 197 | + tablePrivilegesMap := map[string]interface{}{} |
| 198 | + |
| 199 | + if tablePrivileges.Db != nil { |
| 200 | + tablePrivilegesMap["db"] = tablePrivileges.Db |
| 201 | + } |
| 202 | + |
| 203 | + if tablePrivileges.TableName != nil { |
| 204 | + tablePrivilegesMap["table_name"] = tablePrivileges.TableName |
| 205 | + } |
| 206 | + |
| 207 | + if tablePrivileges.Privileges != nil { |
| 208 | + tablePrivilegesMap["privileges"] = tablePrivileges.Privileges |
| 209 | + } |
| 210 | + |
| 211 | + tmpList = append(tmpList, tablePrivilegesMap) |
| 212 | + } |
| 213 | + |
| 214 | + _ = d.Set("table_privileges", tmpList) |
| 215 | + } |
| 216 | + |
| 217 | + d.SetId(clusterId) |
| 218 | + output, ok := d.GetOk("result_output_file") |
| 219 | + if ok && output.(string) != "" { |
| 220 | + if e := writeToFile(output.(string), d); e != nil { |
| 221 | + return e |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return nil |
| 226 | +} |
0 commit comments