-
Notifications
You must be signed in to change notification settings - Fork 141
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
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1b99d91
add resrouce `tencentcloud_postgresql_instance`;
gailwang 5414c03
remove extra tf info
gailwang 797bdb8
add changelog
gailwang 36946cd
fix error with attributes test
gailwang 21fc827
fix spelling error
gailwang 2202d83
fix bugs; rename; add attributes
gailwang 5733051
fix bugs that test cannot read public_access_host
gailwang bd55a7b
add erb
gailwang 1da1305
add erb
gailwang bf46cc8
fix spelling error
gailwang ff868c8
remove extra info
gailwang c9de637
enchance descriptions
gailwang c349fac
remove extra file
gailwang 8d9d9e8
to fit the SDK changes
gailwang 34bc34d
remove parameter `speccode`
gailwang 8e1bcdd
Merge branch 'master' into master
gailwang 8afb421
go fmt
gailwang 2b03b87
fix document
gailwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
variable "availability_zone" { | ||
default = "ap-guangzhou-2" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
terraform { | ||
required_version = ">= 0.12" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Name of the postgresql instance to be query.", | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"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.", | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"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.", | ||
}, | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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.", | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"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") | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
gailwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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, "instance_list.#", "1"), | ||
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.id"), | ||
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.create_time"), | ||
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.id"), | ||
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.charge_type", "postpaid"), | ||
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.engine_version", "9.3.5"), | ||
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.project_id", "0"), | ||
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.memory", "2"), | ||
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.storage", "100"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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 | ||
} | ||
` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.