Skip to content

Commit c480269

Browse files
committed
add
1 parent 7db62e1 commit c480269

23 files changed

+3094
-174
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.984
4747
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
4848
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.970
49-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993
49+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995
5050
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.960
5151
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
5252
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
@@ -70,7 +70,7 @@ require (
7070
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844
7171
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.853
7272
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991
73-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873
73+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995
7474
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859
7575
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.762
7676
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.657

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.991 h1:0Xg2
931931
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.991/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
932932
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993 h1:+iJMmF0q1MPyhLs0+J7CcJ47w/vq6ICsCxnV4gc0dKw=
933933
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
934+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995 h1:I1l5kl6rdQYDLEk8DqWjZgwMh0+nYnqbwq7KS4bqi/s=
935+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
934936
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
935937
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860/go.mod h1:NZo1WplQcC314kMlCRUoy8NQju2BnolIJj7NAWgsuhY=
936938
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.960 h1:Tpz3KVUfaJ7b98wQDYZSbmoKIn0hlH0oHPOzWMzE5Q8=
@@ -989,6 +991,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991 h
989991
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991/go.mod h1:1QZV9ZlcmYEVz/kA5jj8jHDjQsEKbT13WJBywZzaX2U=
990992
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873 h1:kqNFHpILFWPbOUAUrH1i6+IfkLWWEAZLYfj9RZ0WM+0=
991993
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873/go.mod h1:Mjkr/911Pw0VopTfXt5zt46TnV3IrX01mvjZ66RsRRg=
994+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995 h1:chLBYIeKYtDy2kB/IO4uFMLZzlYn/rDoBi7K34HWJu0=
995+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995/go.mod h1:Qv44A5kdz3uX4TYhXz4WacYVSZ0BQL9aYtTdJEwDCQw=
992996
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859 h1:VrE3qzwzWB5mV/ejTJuwZbqZ/CNYLoc8X+uFbWEEOnY=
993997
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859/go.mod h1:FeOl14rPWpNEu6r4bPEsTNHIB9qaRTg0dRhxhRwQ5Hw=
994998
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.762 h1:rZDKucVVtTnmnbZFDyh6t47dHswkb2oSuOxOHTTkygA=

tencentcloud/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ func Provider() *schema.Provider {
679679
"tencentcloud_postgresql_regions": postgresql.DataSourceTencentCloudPostgresqlRegions(),
680680
"tencentcloud_postgresql_db_instance_versions": postgresql.DataSourceTencentCloudPostgresqlDbInstanceVersions(),
681681
"tencentcloud_postgresql_zones": postgresql.DataSourceTencentCloudPostgresqlZones(),
682+
"tencentcloud_postgresql_account_privileges": postgresql.DataSourceTencentCloudPostgresqlAccountPrivileges(),
682683
"tencentcloud_sqlserver_zone_config": sqlserver.DataSourceTencentCloudSqlserverZoneConfig(),
683684
"tencentcloud_sqlserver_instances": sqlserver.DataSourceTencentCloudSqlserverInstances(),
684685
"tencentcloud_sqlserver_backups": sqlserver.DataSourceTencentCloudSqlserverBackups(),
@@ -1487,6 +1488,8 @@ func Provider() *schema.Provider {
14871488
"tencentcloud_postgresql_modify_account_remark_operation": postgresql.ResourceTencentCloudPostgresqlModifyAccountRemarkOperation(),
14881489
"tencentcloud_postgresql_modify_switch_time_period_operation": postgresql.ResourceTencentCloudPostgresqlModifySwitchTimePeriodOperation(),
14891490
"tencentcloud_postgresql_instance_ha_config": postgresql.ResourceTencentCloudPostgresqlInstanceHAConfig(),
1491+
"tencentcloud_postgresql_account": postgresql.ResourceTencentCloudPostgresqlAccount(),
1492+
"tencentcloud_postgresql_account_privileges_operation": postgresql.ResourceTencentCloudPostgresqlAccountPrivilegesOperation(),
14901493
"tencentcloud_sqlserver_instance": sqlserver.ResourceTencentCloudSqlserverInstance(),
14911494
"tencentcloud_sqlserver_db": sqlserver.ResourceTencentCloudSqlserverDB(),
14921495
"tencentcloud_sqlserver_account": sqlserver.ResourceTencentCloudSqlserverAccount(),

tencentcloud/provider.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ TencentDB for PostgreSQL(PostgreSQL)
872872
tencentcloud_postgresql_regions
873873
tencentcloud_postgresql_db_instance_versions
874874
tencentcloud_postgresql_zones
875+
tencentcloud_postgresql_account_privileges
875876

876877
Resource
877878
tencentcloud_postgresql_instance
@@ -892,6 +893,8 @@ TencentDB for PostgreSQL(PostgreSQL)
892893
tencentcloud_postgresql_modify_switch_time_period_operation
893894
tencentcloud_postgresql_base_backup
894895
tencentcloud_postgresql_instance_ha_config
896+
tencentcloud_postgresql_account
897+
tencentcloud_postgresql_account_privileges_operation
895898

896899
TencentDB for Redis(crs)
897900
Data Source
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package postgresql
2+
3+
import (
4+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
5+
"log"
6+
"strings"
7+
8+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
13+
)
14+
15+
func DataSourceTencentCloudPostgresqlAccountPrivileges() *schema.Resource {
16+
return &schema.Resource{
17+
Read: dataSourceTencentCloudPostgresqlAccountPrivilegesRead,
18+
Schema: map[string]*schema.Schema{
19+
"db_instance_id": {
20+
Required: true,
21+
Type: schema.TypeString,
22+
Description: "Instance ID.",
23+
},
24+
"user_name": {
25+
Required: true,
26+
Type: schema.TypeString,
27+
Description: "Instance username.",
28+
},
29+
"database_object_set": {
30+
Required: true,
31+
Type: schema.TypeList,
32+
Description: "Instance database object info.",
33+
Elem: &schema.Resource{
34+
Schema: map[string]*schema.Schema{
35+
"object_type": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
Description: "Supported database object types: account, database, schema, sequence, procedure, type, function, table, view, matview, column. Note: This field may return null, indicating that no valid value can be obtained.",
39+
},
40+
"object_name": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
Description: "Database object Name.Note: This field may return null, indicating that no valid value can be obtained.",
44+
},
45+
"database_name": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Database name to which the database object belongs. This parameter is mandatory when ObjectType is not database.Note: This field may return null, indicating that no valid value can be obtained.",
49+
},
50+
"schema_name": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Schema name to which the database object belongs. This parameter is mandatory when ObjectType is not database or schema.Note: This field may return null, indicating that no valid value can be obtained.",
54+
},
55+
"table_name": {
56+
Type: schema.TypeString,
57+
Optional: true,
58+
Description: "Table name to which the database object belongs. This parameter is mandatory when ObjectType is column.Note: This field may return null, indicating that no valid value can be obtained.",
59+
},
60+
},
61+
},
62+
},
63+
// computed
64+
"privilege_set": {
65+
Computed: true,
66+
Type: schema.TypeList,
67+
Description: "Privilege list.",
68+
Elem: &schema.Resource{
69+
Schema: map[string]*schema.Schema{
70+
"object": {
71+
Type: schema.TypeList,
72+
Computed: true,
73+
Description: "Database object.If ObjectType is database, DatabaseName/SchemaName/TableName can be null.If ObjectType is schema, SchemaName/TableName can be null.If ObjectType is table, TableName can be null.If ObjectType is column, DatabaseName/SchemaName/TableName can't be null.In all other cases, DatabaseName/SchemaName/TableName can be null. Note: This field may return null, indicating that no valid value can be obtained.",
74+
Elem: &schema.Resource{
75+
Schema: map[string]*schema.Schema{
76+
"object_type": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Supported database object types: account, database, schema, sequence, procedure, type, function, table, view, matview, column. Note: This field may return null, indicating that no valid value can be obtained.",
80+
},
81+
"object_name": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "Database object Name. Note: This field may return null, indicating that no valid value can be obtained.",
85+
},
86+
"database_name": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "Database name to which the database object belongs. This parameter is mandatory when ObjectType is not database. Note: This field may return null, indicating that no valid value can be obtained.",
90+
},
91+
"schema_name": {
92+
Type: schema.TypeString,
93+
Computed: true,
94+
Description: "Schema name to which the database object belongs. This parameter is mandatory when ObjectType is not database or schema. Note: This field may return null, indicating that no valid value can be obtained.",
95+
},
96+
"table_name": {
97+
Type: schema.TypeString,
98+
Computed: true,
99+
Description: "Table name to which the database object belongs. This parameter is mandatory when ObjectType is column. Note: This field may return null, indicating that no valid value can be obtained.",
100+
},
101+
},
102+
},
103+
},
104+
"privilege_set": {
105+
Type: schema.TypeSet,
106+
Elem: &schema.Schema{Type: schema.TypeString},
107+
Computed: true,
108+
Description: "Privileges the specific account has on database object. Note: This field may return null, indicating that no valid value can be obtained.",
109+
},
110+
},
111+
},
112+
},
113+
"result_output_file": {
114+
Type: schema.TypeString,
115+
Optional: true,
116+
Description: "Used to save results.",
117+
},
118+
},
119+
}
120+
}
121+
122+
func dataSourceTencentCloudPostgresqlAccountPrivilegesRead(d *schema.ResourceData, meta interface{}) error {
123+
defer tccommon.LogElapsed("data_source.tencentcloud_postgresql_account_privileges.read")()
124+
defer tccommon.InconsistentCheck(d, meta)()
125+
126+
var (
127+
logId = tccommon.GetLogId(tccommon.ContextNil)
128+
request = postgresql.NewDescribeAccountPrivilegesRequest()
129+
privilegeSet []*postgresql.DatabasePrivilege
130+
dBInstanceId string
131+
userName string
132+
)
133+
134+
if v, ok := d.GetOk("db_instance_id"); ok {
135+
request.DBInstanceId = &dBInstanceId
136+
dBInstanceId = v.(string)
137+
}
138+
139+
if v, ok := d.GetOk("user_name"); ok {
140+
request.UserName = &userName
141+
userName = v.(string)
142+
}
143+
144+
if v, ok := d.GetOk("database_object_set"); ok {
145+
for _, item := range v.([]interface{}) {
146+
objectMap := item.(map[string]interface{})
147+
databaseObject := postgresql.DatabaseObject{}
148+
if v, ok := objectMap["object_type"]; ok {
149+
databaseObject.ObjectType = helper.String(v.(string))
150+
}
151+
152+
if v, ok := objectMap["object_name"]; ok {
153+
databaseObject.ObjectName = helper.String(v.(string))
154+
}
155+
156+
if v, ok := objectMap["database_name"]; ok {
157+
databaseObject.DatabaseName = helper.String(v.(string))
158+
}
159+
160+
if v, ok := objectMap["schema_name"]; ok {
161+
databaseObject.SchemaName = helper.String(v.(string))
162+
}
163+
164+
if v, ok := objectMap["table_name"]; ok {
165+
databaseObject.TableName = helper.String(v.(string))
166+
}
167+
168+
request.DatabaseObjectSet = append(request.DatabaseObjectSet, &databaseObject)
169+
}
170+
}
171+
172+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
173+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().DescribeAccountPrivileges(request)
174+
if e != nil {
175+
return tccommon.RetryError(e)
176+
} else {
177+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
178+
}
179+
180+
privilegeSet = result.Response.PrivilegeSet
181+
return nil
182+
})
183+
184+
if err != nil {
185+
return err
186+
}
187+
188+
tmpList := make([]map[string]interface{}, 0, len(privilegeSet))
189+
for _, item := range privilegeSet {
190+
databasePrivilegeMap := map[string]interface{}{}
191+
if item.Object != nil {
192+
objectMap := map[string]interface{}{}
193+
if item.Object.ObjectType != nil {
194+
objectMap["object_type"] = item.Object.ObjectType
195+
}
196+
197+
if item.Object.ObjectName != nil {
198+
objectMap["object_name"] = item.Object.ObjectName
199+
}
200+
201+
if item.Object.DatabaseName != nil {
202+
objectMap["database_name"] = item.Object.DatabaseName
203+
}
204+
205+
if item.Object.SchemaName != nil {
206+
objectMap["schema_name"] = item.Object.SchemaName
207+
}
208+
209+
if item.Object.TableName != nil {
210+
objectMap["table_name"] = item.Object.TableName
211+
}
212+
213+
databasePrivilegeMap["object"] = []interface{}{objectMap}
214+
}
215+
216+
if item.PrivilegeSet != nil {
217+
privilegeList := make([]string, 0, len(item.PrivilegeSet))
218+
for _, privilege := range item.PrivilegeSet {
219+
privilegeList = append(privilegeList, *privilege)
220+
}
221+
222+
databasePrivilegeMap["privilege_set"] = privilegeList
223+
}
224+
225+
tmpList = append(tmpList, databasePrivilegeMap)
226+
}
227+
228+
_ = d.Set("privilege_set", tmpList)
229+
230+
d.SetId(strings.Join([]string{dBInstanceId, userName}, tccommon.FILED_SP))
231+
output, ok := d.GetOk("result_output_file")
232+
if ok && output.(string) != "" {
233+
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
234+
return e
235+
}
236+
}
237+
238+
return nil
239+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Use this data source to query detailed information of postgresql account privileges
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_postgresql_account_privileges" "example" {
7+
db_instance_id = "postgres-3hk6b6tj"
8+
user_name = "tf_example"
9+
database_object_set {
10+
object_name = "postgres"
11+
object_type = "database"
12+
}
13+
}
14+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package postgresql_test
2+
3+
import (
4+
"testing"
5+
6+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
// go test -i; go test -test.run TestAccTencentCloudPostgresqlAccountPrivilegesDataSource_basic -v
12+
func TestAccTencentCloudPostgresqlAccountPrivilegesDataSource_basic(t *testing.T) {
13+
// t.Parallel()
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() {
16+
tcacctest.AccPreCheck(t)
17+
},
18+
Providers: tcacctest.AccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccPostgresqlAccountPrivilegesDataSource,
22+
Check: resource.ComposeTestCheckFunc(
23+
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_postgresql_account_privileges.example"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "db_instance_id"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "user_name"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "database_object_set.#"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
const testAccPostgresqlAccountPrivilegesDataSource = `
34+
data "tencentcloud_postgresql_account_privileges" "example" {
35+
db_instance_id = "postgres-3hk6b6tj"
36+
user_name = "tf_example"
37+
database_object_set {
38+
object_name = "postgres"
39+
object_type = "database"
40+
}
41+
}
42+
`

0 commit comments

Comments
 (0)