Skip to content
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
15 changes: 15 additions & 0 deletions tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ resource "tencentcloud_instance" "default" {

// End of SQLServer

// PostgreSQL

const defaultPGSQLName = "keep-postgresql"
const CommonPresetPGSQL = `
data "tencentcloud_postgresql_instances" "foo" {
name = "` + defaultPGSQLName + `"
}

locals {
pgsql_id = data.tencentcloud_postgresql_instances.foo.instance_list.0.id
}
`

// End of PostgreSQL

const defaultCVMName = "keep-cvm"
const presetCVM = `
data "tencentcloud_instances" "instance" {
Expand Down
56 changes: 7 additions & 49 deletions tencentcloud/data_source_tc_postgresql_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,27 @@ func TestAccTencentCloudDataPostgresqlInstances(t *testing.T) {
{
Config: testAccTencentCloudDataPostgresqlInstanceBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlInstanceExists("tencentcloud_postgresql_instance.test"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.#", "1"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.id"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.create_time"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.charge_type", "POSTPAID_BY_HOUR"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.engine_version", "10.4"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.project_id", "0"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.memory", "2"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.storage", "10"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.charge_type"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.engine_version"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.project_id"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.memory"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.storage"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.private_access_ip"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.private_access_port"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.public_access_switch"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.charset"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.tags.tf", "test"),
),
},
},
})
}

const testAccTencentCloudDataPostgresqlInstanceBasic = `
data "tencentcloud_availability_zones_by_product" "pg" {
product = "postgres"
}

resource "tencentcloud_vpc" "vpc" {
cidr_block = "10.0.0.0/24"
name = "test-pg-vpc"
}

resource "tencentcloud_subnet" "subnet" {
availability_zone = local.az
cidr_block = "10.0.0.0/24"
name = "sub1"
vpc_id = tencentcloud_vpc.vpc.id
}

locals {
az = data.tencentcloud_availability_zones_by_product.pg.zones.0.name
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id
}


resource "tencentcloud_postgresql_instance" "test" {
name = "tf_postsql_instance"
availability_zone = local.az
charge_type = "POSTPAID_BY_HOUR"
engine_version = "10.4"
root_password = "1qaA2k1wgvfa!_3ZZZ"
charset = "UTF8"
project_id = 0
memory = 2
storage = 10
vpc_id = local.vpc_id
subnet_id = local.subnet_id

tags = {
tf = "test"
}
}
const testAccTencentCloudDataPostgresqlInstanceBasic = CommonPresetPGSQL + `

data "tencentcloud_postgresql_instances" "id_test"{
id = tencentcloud_postgresql_instance.test.id
id = local.pgsql_id
}
`
163 changes: 163 additions & 0 deletions tencentcloud/data_source_tc_postgresql_xlogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
Provide a datasource to query PostgreSQL Xlogs.

Example Usage

```hcl
data "tencentcloud_postgresql_xlogs" "foo" {
instance_id = "postgres-xxxxxxxx"
start_time = "2022-01-01 00:00:00"
end_time = "2022-01-07 01:02:03"
}
```


*/
package tencentcloud

import (
"context"
"time"

postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func datasourceTencentCloudPostgresqlXlogs() *schema.Resource {
return &schema.Resource{
Read: datasourceTencentCloudPostgresqlXlogsRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "PostgreSQL instance id.",
},
"start_time": {
Type: schema.TypeString,
Optional: true,
Description: "Xlog start time, format `yyyy-MM-dd hh:mm:ss`, start time cannot before 7 days ago.",
},
"end_time": {
Type: schema.TypeString,
Optional: true,
Description: "Xlog end time, format `yyyy-MM-dd hh:mm:ss`.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used for save results.",
},
"list": {
Type: schema.TypeList,
Computed: true,
Description: "List of Xlog query result.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
Description: "Xlog id.",
},
"start_time": {
Type: schema.TypeString,
Computed: true,
Description: "Xlog file created start time.",
},
"end_time": {
Type: schema.TypeString,
Computed: true,
Description: "Xlog file created end time.",
},
"internal_addr": {
Type: schema.TypeString,
Computed: true,
Description: "Xlog internal download address.",
},
"external_addr": {
Type: schema.TypeString,
Computed: true,
Description: "Xlog external download address.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "Xlog file size.",
},
},
},
},
},
}
}

func datasourceTencentCloudPostgresqlXlogsRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("datasource.tencentcloud_postgresql_xlogs.read")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
client := meta.(*TencentCloudClient).apiV3Conn
service := PostgresqlService{client}

request := postgresql.NewDescribeDBXlogsRequest()

id := d.Get("instance_id").(string)
defaultEndTime := time.Now()

if endTime, ok := d.GetOk("end_time"); ok && endTime != "" {
request.EndTime = helper.String(endTime.(string))
} else {
endTime := defaultEndTime.Format("2006-01-02 15:04:05")
request.EndTime = &endTime
}

if startTime, ok := d.GetOk("start_time"); ok && startTime != "" {
request.StartTime = helper.String(startTime.(string))
} else {
defaultStartTime := defaultEndTime.AddDate(0, 0, -7)
startTime := defaultStartTime.Format("2006-01-02 15:04:05")
request.StartTime = &startTime
}

request.DBInstanceId = &id

result, err := service.DescribeDBXlogs(ctx, request)

if err != nil {
d.SetId("")
return err
}

list := make([]interface{}, 0, len(result))

for i := range result {
item := result[i]
xlog := map[string]interface{}{
"id": item.Id,
"start_time": item.StartTime,
"end_time": item.EndTime,
"internal_addr": item.InternalAddr,
"external_addr": item.ExternalAddr,
"size": item.Size,
}

list = append(list, xlog)
}

d.SetId("postgres-xlog-" + id)

if err := d.Set("list", list); err != nil {
return err
}

if output, ok := d.GetOk("result_output_file"); ok {
return writeToFile(output.(string), list)
}

return nil
}
52 changes: 52 additions & 0 deletions tencentcloud/data_source_tc_postgresql_xlogs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tencentcloud

import (
"fmt"
"testing"
"time"

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

func TestAccTencentCloudDataSourcePostgresqlXlogs(t *testing.T) {
t.Parallel()

startTime := time.Now().AddDate(0, 0, -7).Format("2006-01-02 15:04:05")
endTime := time.Now().Format("2006-01-02 15:04:05")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePostgresqlXlogsBasic(startTime, endTime),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_xlogs.foo", "start_time", startTime),
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_xlogs.foo", "end_time", endTime),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.#"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.start_time"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.end_time"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.internal_addr"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.external_addr"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.size"),
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.bar", "list.#"),
),
},
},
})
}

func testAccDataSourcePostgresqlXlogsBasic(startTime, endTime string) string {
return fmt.Sprintf(`
%s
data "tencentcloud_postgresql_xlogs" "foo" {
instance_id = local.pgsql_id
start_time = "%s"
end_time = "%s"
}

data "tencentcloud_postgresql_xlogs" "bar" {
instance_id = local.pgsql_id
}
`, CommonPresetPGSQL, startTime, endTime)
}
2 changes: 2 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ PostgreSQL
Data Source
tencentcloud_postgresql_instances
tencentcloud_postgresql_specinfos
tencentcloud_postgresql_xlogs

Resource
tencentcloud_postgresql_instance
Expand Down Expand Up @@ -844,6 +845,7 @@ func Provider() terraform.ResourceProvider {
"tencentcloud_elasticsearch_instances": dataSourceTencentCloudElasticsearchInstances(),
"tencentcloud_postgresql_instances": dataSourceTencentCloudPostgresqlInstances(),
"tencentcloud_postgresql_specinfos": dataSourceTencentCloudPostgresqlSpecinfos(),
"tencentcloud_postgresql_xlogs": datasourceTencentCloudPostgresqlXlogs(),
"tencentcloud_sqlserver_zone_config": dataSourceTencentSqlserverZoneConfig(),
"tencentcloud_sqlserver_instances": dataSourceTencentCloudSqlserverInstances(),
"tencentcloud_sqlserver_backups": dataSourceTencentCloudSqlserverBackups(),
Expand Down
Loading