Skip to content

add resrouce tencentcloud_postgresql_instance; #449

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 18 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions examples/tencentcloud-postgresql/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

provider "tencentcloud" {
region = "ap-guangzhou"
}

resource "tencentcloud_vpc" "foo" {
name = "example"
cidr_block = "10.0.0.0/16"
}

resource "tencentcloud_subnet" "foo" {
name = "example"
availability_zone = var.availability_zone
vpc_id = tencentcloud_vpc.foo.id
cidr_block = "10.0.0.0/24"
is_multicast = false
}

resource "tencentcloud_postgresql_instance" "example" {
name = "tf_postsql_instance_111"
availability_zone = var.availability_zone
charge_type = "postpaid"
vpc_id = tencentcloud_vpc.foo.id
subnet_id = tencentcloud_subnet.foo.id
engine_version = "9.3.5"
root_password = "1qaA2k1wgvfa3ZZZ"
charset = "UTF8"
spec_code = "cdb.pg.z1.2g"
project_id = 0
memory = 2
storage = 10
}

data "tencentcloud_postgresql_instances" "id_example" {
id = tencentcloud_postgresql_instance.example.id
}

data "tencentcloud_postgresql_instances" "project_example" {
project_id = 0
}

data "tencentcloud_postgresql_instances" "charge_example" {
name = tencentcloud_postgresql_instance.example.name
}

data "tencentcloud_postgresql_speccodes" "example" {
availability_zone = var.availability_zone
}
3 changes: 3 additions & 0 deletions examples/tencentcloud-postgresql/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "availability_zone" {
default = "ap-guangzhou-2"
}
3 changes: 3 additions & 0 deletions examples/tencentcloud-postgresql/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/tencentcloud/tencentcloud-sdk-go v3.0.187+incompatible
github.com/tencentcloud/tencentcloud-sdk-go v3.0.189+incompatible
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
github.com/zclconf/go-cty v1.4.2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.187+incompatible h1:9hr93p945INwxa67N68EUJuIkUE2iGnMq/YZ6x3JHik=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.187+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.189+incompatible h1:eLVHwxLW2HFvr48l5sqROircYlq6Lp39EXhyCkK6Jlw=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.189+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
github.com/tetafro/godot v0.3.7/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=
Expand Down
15 changes: 15 additions & 0 deletions tencentcloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529"
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20180408"
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
postrge "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
Expand Down Expand Up @@ -64,6 +65,7 @@ type TencentCloudClient struct {
cdnConn *cdn.Client
monitorConn *monitor.Client
esConn *es.Client
postgreConn *postrge.Client
}

// NewClientProfile returns a new ClientProfile
Expand Down Expand Up @@ -398,3 +400,16 @@ func (me *TencentCloudClient) UseEsClient() *es.Client {

return me.esConn
}

// UsePostgreClient returns es client for service
func (me *TencentCloudClient) UsePostgresqlClient() *postrge.Client {
if me.postgreConn != nil {
return me.postgreConn
}

cpf := me.NewClientProfile(300)
me.postgreConn, _ = postrge.NewClient(me.Credential, me.Region, cpf)
me.postgreConn.WithHttpTransport(&LogRoundTripper{})

return me.postgreConn
}
208 changes: 208 additions & 0 deletions tencentcloud/data_source_tc_postgresql_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*
Use this data source to query postgresql instances

Example Usage

```hcl
data "tencentcloud_postgresql_instances" "name"{
name = "test"
}

data "tencentcloud_postgresql_instances" "project"{
project_id = 0
}

data "tencentcloud_postgresql_instances" "id"{
id = "postgres-h9t4fde1"
}
```
*/
package tencentcloud

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func dataSourceTencentCloudPostgresqlInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudPostgresqlInstanceRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the postgresql instance to be queey.",
},
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the postgresql instance to be query.",
},
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Project id of the postgresql instance to be query.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"instance_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of postgresql instances. Each element contains the following attributes.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of the postgresql instance.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the postgresql instance.",
},
"charge_type": {
Type: schema.TypeString,
Computed: true,
Description: "Pay type of the postgresql instance.",
},
"auto_renew_flag": {
Type: schema.TypeInt,
Computed: true,
Description: "Auto renew flag.",
},
"engine_version": {
Type: schema.TypeString,
Computed: true,
Description: "Version of the postgresql db engine.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of VPC.",
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of subnet.",
},
"storage": {
Type: schema.TypeInt,
Computed: true,
Description: "Disk size (in GB).",
},
"memory": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory size (in GB).",
},
"project_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Project ID, default value is 0.",
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Description: "Availability zone.",
},
"public_access_switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether to enable the access to an instance from public network or not.",
},
"public_access_host": {
Type: schema.TypeString,
Computed: true,
Description: "host for public access.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Create time of the postgresql instance.",
},
},
},
},
},
}
}

func dataSourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_postgresql_instances.read")

logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}

filter := make([]*postgresql.Filter, 0)
if v, ok := d.GetOk("name"); ok {
filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-name"), Values: []*string{helper.String(v.(string))}})
}
if v, ok := d.GetOk("id"); ok {
filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-id"), Values: []*string{helper.String(v.(string))}})
}
if v, ok := d.GetOk("project_id"); ok {
filter = append(filter, &postgresql.Filter{Name: helper.String("db-project-id"), Values: []*string{helper.String(v.(string))}})
}

instanceList, err := service.DescribePostgresqlInstances(ctx, filter)

if err != nil {
instanceList, err = service.DescribePostgresqlInstances(ctx, filter)
}

if err != nil {
return err
}

ids := make([]string, 0, len(instanceList))
list := make([]map[string]interface{}, 0, len(instanceList))

for _, v := range instanceList {
listItem := make(map[string]interface{})
listItem["id"] = *v.DBInstanceId
listItem["name"] = *v.DBInstanceName
listItem["charge_type"] = *v.PayType
listItem["auto_renew_flag"] = int(*v.AutoRenew)
listItem["project_id"] = int(*v.ProjectId)
listItem["storage"] = int(*v.DBInstanceStorage)
listItem["memory"] = int(*v.DBInstanceMemory)
listItem["availability_zone"] = *v.Zone
listItem["create_time"] = *v.CreateTime
listItem["vpc_id"] = *v.VpcId
listItem["subnet_id"] = *v.SubnetId
listItem["engine_version"] = *v.DBVersion
listItem["public_access_switch"] = false
listItem["public_access_host"] = ""
for _, netInfo := range v.DBInstanceNetInfo {
if *netInfo.NetType == "public" {
if *netInfo.Status == "open" {
listItem["public_access_switch"] = true
}
listItem["public_access_host"] = *netInfo.Address
}
}
list = append(list, listItem)
ids = append(ids, *v.DBInstanceId)
}

d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("instance_list", list); e != nil {
log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error())
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
return writeToFile(output.(string), list)
}
return nil
}
60 changes: 60 additions & 0 deletions tencentcloud/data_source_tc_postgresql_instances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tencentcloud

import (
"testing"

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

var testDataPostgresqlInstancesName = "data.tencentcloud_postgresql_instances.id_test"

func TestAccTencentCloudDataPostgresqlInstances(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLBDestroy,
Steps: []resource.TestStep{
{
Config: testAccTencentCloudDataPostgresqlInstanceBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlInstanceExists("tencentcloud_postgresql_instance.test"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#", "1"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "list.0.id"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "list.0.create_time"),
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "list.0.id"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.0.charge_type", "POSTPAID_BY_HOUR"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#.engine_version", "9.3.5"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#.project_id", "0"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#.mem_size", "2"),
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#.volume_size", "1"),
//resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "list.#.volume_size", "1"),
),
},
},
})
}

const testAccTencentCloudDataPostgresqlInstanceBasic = `
variable "availability_zone"{
default = "ap-guangzhou-2"
}

resource "tencentcloud_postgresql_instance" "test" {
name = "tf_postsql_instance"
availability_zone = var.availability_zone
charge_type = "postpaid"
vpc_id = "` + defaultVpcId + `"
subnet_id = "subnet-pyio7yog"
engine_version = "9.3.5"
root_password = "1qaA2k1wgvfa3ZZZ"
charset = "UTF8"
spec_code = "cdb.pg.z1.2g"
project_id = 0
memory = 2
storage = 100
}

data "tencentcloud_postgresql_instances" "id_test"{
id = tencentcloud_postgresql_instance.test.id
}
`
Loading