Skip to content

feat(pgsql): [119540312] add new resource #2807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/2807.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
tencentcloud_postgresql_account
```

```release-note:new-data-source
tencentcloud_postgresql_account_privileges
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.984
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.970
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.960
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
Expand All @@ -70,7 +70,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.853
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.657
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993 h1:+iJM
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993 h1:WlPgXldQCxt7qi5Xrc6j6zTrsXWzN5BcOGs7Irq7fwQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993/go.mod h1:Z9U8zNtyuyKhjS0698wqsrG/kLx1TQ5CEixXBwVe7xY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995 h1:I1l5kl6rdQYDLEk8DqWjZgwMh0+nYnqbwq7KS4bqi/s=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.995/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860/go.mod h1:NZo1WplQcC314kMlCRUoy8NQju2BnolIJj7NAWgsuhY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.960 h1:Tpz3KVUfaJ7b98wQDYZSbmoKIn0hlH0oHPOzWMzE5Q8=
Expand Down Expand Up @@ -991,6 +993,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991 h
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.991/go.mod h1:1QZV9ZlcmYEVz/kA5jj8jHDjQsEKbT13WJBywZzaX2U=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873 h1:kqNFHpILFWPbOUAUrH1i6+IfkLWWEAZLYfj9RZ0WM+0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.873/go.mod h1:Mjkr/911Pw0VopTfXt5zt46TnV3IrX01mvjZ66RsRRg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995 h1:chLBYIeKYtDy2kB/IO4uFMLZzlYn/rDoBi7K34HWJu0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.995/go.mod h1:Qv44A5kdz3uX4TYhXz4WacYVSZ0BQL9aYtTdJEwDCQw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859 h1:VrE3qzwzWB5mV/ejTJuwZbqZ/CNYLoc8X+uFbWEEOnY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.859/go.mod h1:FeOl14rPWpNEu6r4bPEsTNHIB9qaRTg0dRhxhRwQ5Hw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.762 h1:rZDKucVVtTnmnbZFDyh6t47dHswkb2oSuOxOHTTkygA=
Expand Down
3 changes: 3 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ func Provider() *schema.Provider {
"tencentcloud_postgresql_regions": postgresql.DataSourceTencentCloudPostgresqlRegions(),
"tencentcloud_postgresql_db_instance_versions": postgresql.DataSourceTencentCloudPostgresqlDbInstanceVersions(),
"tencentcloud_postgresql_zones": postgresql.DataSourceTencentCloudPostgresqlZones(),
"tencentcloud_postgresql_account_privileges": postgresql.DataSourceTencentCloudPostgresqlAccountPrivileges(),
"tencentcloud_sqlserver_zone_config": sqlserver.DataSourceTencentCloudSqlserverZoneConfig(),
"tencentcloud_sqlserver_instances": sqlserver.DataSourceTencentCloudSqlserverInstances(),
"tencentcloud_sqlserver_backups": sqlserver.DataSourceTencentCloudSqlserverBackups(),
Expand Down Expand Up @@ -1488,6 +1489,8 @@ func Provider() *schema.Provider {
"tencentcloud_postgresql_modify_account_remark_operation": postgresql.ResourceTencentCloudPostgresqlModifyAccountRemarkOperation(),
"tencentcloud_postgresql_modify_switch_time_period_operation": postgresql.ResourceTencentCloudPostgresqlModifySwitchTimePeriodOperation(),
"tencentcloud_postgresql_instance_ha_config": postgresql.ResourceTencentCloudPostgresqlInstanceHAConfig(),
"tencentcloud_postgresql_account": postgresql.ResourceTencentCloudPostgresqlAccount(),
"tencentcloud_postgresql_account_privileges_operation": postgresql.ResourceTencentCloudPostgresqlAccountPrivilegesOperation(),
"tencentcloud_sqlserver_instance": sqlserver.ResourceTencentCloudSqlserverInstance(),
"tencentcloud_sqlserver_db": sqlserver.ResourceTencentCloudSqlserverDB(),
"tencentcloud_sqlserver_account": sqlserver.ResourceTencentCloudSqlserverAccount(),
Expand Down
2 changes: 2 additions & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ TencentDB for PostgreSQL(PostgreSQL)
tencentcloud_postgresql_regions
tencentcloud_postgresql_db_instance_versions
tencentcloud_postgresql_zones
tencentcloud_postgresql_account_privileges

Resource
tencentcloud_postgresql_instance
Expand All @@ -892,6 +893,7 @@ TencentDB for PostgreSQL(PostgreSQL)
tencentcloud_postgresql_modify_switch_time_period_operation
tencentcloud_postgresql_base_backup
tencentcloud_postgresql_instance_ha_config
tencentcloud_postgresql_account

TencentDB for Redis(crs)
Data Source
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package postgresql

import (
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
"log"
"strings"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
)

func DataSourceTencentCloudPostgresqlAccountPrivileges() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudPostgresqlAccountPrivilegesRead,
Schema: map[string]*schema.Schema{
"db_instance_id": {
Required: true,
Type: schema.TypeString,
Description: "Instance ID.",
},
"user_name": {
Required: true,
Type: schema.TypeString,
Description: "Instance username.",
},
"database_object_set": {
Required: true,
Type: schema.TypeList,
Description: "Instance database object info.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_type": {
Type: schema.TypeString,
Required: true,
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.",
},
"object_name": {
Type: schema.TypeString,
Required: true,
Description: "Database object Name.Note: This field may return null, indicating that no valid value can be obtained.",
},
"database_name": {
Type: schema.TypeString,
Optional: true,
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.",
},
"schema_name": {
Type: schema.TypeString,
Optional: true,
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.",
},
"table_name": {
Type: schema.TypeString,
Optional: true,
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.",
},
},
},
},
// computed
"privilege_set": {
Computed: true,
Type: schema.TypeList,
Description: "Privilege list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object": {
Type: schema.TypeList,
Computed: true,
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.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_type": {
Type: schema.TypeString,
Computed: true,
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.",
},
"object_name": {
Type: schema.TypeString,
Computed: true,
Description: "Database object Name. Note: This field may return null, indicating that no valid value can be obtained.",
},
"database_name": {
Type: schema.TypeString,
Computed: true,
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.",
},
"schema_name": {
Type: schema.TypeString,
Computed: true,
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.",
},
"table_name": {
Type: schema.TypeString,
Computed: true,
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.",
},
},
},
},
"privilege_set": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "Privileges the specific account has on database object. Note: This field may return null, indicating that no valid value can be obtained.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudPostgresqlAccountPrivilegesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_postgresql_account_privileges.read")()
defer tccommon.InconsistentCheck(d, meta)()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = postgresql.NewDescribeAccountPrivilegesRequest()
privilegeSet []*postgresql.DatabasePrivilege
dBInstanceId string
userName string
)

if v, ok := d.GetOk("db_instance_id"); ok {
request.DBInstanceId = &dBInstanceId
dBInstanceId = v.(string)
}

if v, ok := d.GetOk("user_name"); ok {
request.UserName = &userName
userName = v.(string)
}

if v, ok := d.GetOk("database_object_set"); ok {
for _, item := range v.([]interface{}) {
objectMap := item.(map[string]interface{})
databaseObject := postgresql.DatabaseObject{}
if v, ok := objectMap["object_type"]; ok {
databaseObject.ObjectType = helper.String(v.(string))
}

if v, ok := objectMap["object_name"]; ok {
databaseObject.ObjectName = helper.String(v.(string))
}

if v, ok := objectMap["database_name"]; ok {
databaseObject.DatabaseName = helper.String(v.(string))
}

if v, ok := objectMap["schema_name"]; ok {
databaseObject.SchemaName = helper.String(v.(string))
}

if v, ok := objectMap["table_name"]; ok {
databaseObject.TableName = helper.String(v.(string))
}

request.DatabaseObjectSet = append(request.DatabaseObjectSet, &databaseObject)
}
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().DescribeAccountPrivileges(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}

privilegeSet = result.Response.PrivilegeSet
return nil
})

if err != nil {
return err
}

tmpList := make([]map[string]interface{}, 0, len(privilegeSet))
for _, item := range privilegeSet {
databasePrivilegeMap := map[string]interface{}{}
if item.Object != nil {
objectMap := map[string]interface{}{}
if item.Object.ObjectType != nil {
objectMap["object_type"] = item.Object.ObjectType
}

if item.Object.ObjectName != nil {
objectMap["object_name"] = item.Object.ObjectName
}

if item.Object.DatabaseName != nil {
objectMap["database_name"] = item.Object.DatabaseName
}

if item.Object.SchemaName != nil {
objectMap["schema_name"] = item.Object.SchemaName
}

if item.Object.TableName != nil {
objectMap["table_name"] = item.Object.TableName
}

databasePrivilegeMap["object"] = []interface{}{objectMap}
}

if item.PrivilegeSet != nil {
privilegeList := make([]string, 0, len(item.PrivilegeSet))
for _, privilege := range item.PrivilegeSet {
privilegeList = append(privilegeList, *privilege)
}

databasePrivilegeMap["privilege_set"] = privilegeList
}

tmpList = append(tmpList, databasePrivilegeMap)
}

_ = d.Set("privilege_set", tmpList)

d.SetId(strings.Join([]string{dBInstanceId, userName}, tccommon.FILED_SP))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Use this data source to query detailed information of postgresql account privileges

Example Usage

```hcl
data "tencentcloud_postgresql_account_privileges" "example" {
db_instance_id = "postgres-3hk6b6tj"
user_name = "tf_example"
database_object_set {
object_name = "postgres"
object_type = "database"
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package postgresql_test

import (
"testing"

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// go test -i; go test -test.run TestAccTencentCloudPostgresqlAccountPrivilegesDataSource_basic -v
func TestAccTencentCloudPostgresqlAccountPrivilegesDataSource_basic(t *testing.T) {
// t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccPostgresqlAccountPrivilegesDataSource,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_postgresql_account_privileges.example"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "db_instance_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "user_name"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_account_privileges.example", "database_object_set.#"),
),
},
},
})
}

const testAccPostgresqlAccountPrivilegesDataSource = `
data "tencentcloud_postgresql_account_privileges" "example" {
db_instance_id = "postgres-3hk6b6tj"
user_name = "tf_example"
database_object_set {
object_name = "postgres"
object_type = "database"
}
}
`
Loading
Loading