Skip to content

Commit c8fd8cf

Browse files
authored
Merge pull request #514 from gailwang/master
Add new resource `tencentcloud_audit`
2 parents db03453 + 5b00e98 commit c8fd8cf

26 files changed

+2752
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
## 1.42.3 (Unreleased)
1+
## 1.43.0 (Unreleased)
22

33
FEATURES:
44

55
* **New Resource**: `tencentcloud_image`
6+
* **New Resource**: `tencentcloud_audit`
7+
* **New Data Source**: `tencentcloud_audits`
8+
* **New Data Source**: `tencentcloud_audit_cos_regions`
9+
* **New Data Source**: `tencentcloud_audit_key_alias`
610

711
ENHANCEMENTS:
812

examples/tencentcloud-audit/main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
data "tencentcloud_audit_cos_regions" "example" {
2+
}
3+
4+
data "tencentcloud_audits" "example" {
5+
}
6+
7+
data "tencentcloud_audit_key_alias" "example" {
8+
region = "ap-hongkong"
9+
}
10+
11+
resource "tencentcloud_audit" "example_kms" {
12+
name = "example_kms"
13+
cos_bucket = "test"
14+
cos_region = "ap-hongkong"
15+
enable_kms_encry = true
16+
log_file_prefix = "exampleprefix"
17+
key_id = data.tencentcloud_audit_key_alias.example.audit_key_alias_list.0.key_id
18+
audit_switch = true
19+
read_write_attribute = 3
20+
}
21+
22+
data "tencentcloud_audits" "name_example" {
23+
name = "example_kms"
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/mattn/go-colorable v0.1.6 // indirect
1616
github.com/mitchellh/go-homedir v1.1.0
1717
github.com/pkg/errors v0.9.1
18-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible
18+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.15
1919
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
2020
github.com/zclconf/go-cty v1.4.2 // indirect
2121
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
458458
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
459459
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
460460
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
461-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible h1:ZlFfQuJYC+g2bEjlZUvIUQUifHhzWM69GTzFh0qzLl4=
462-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
461+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.15 h1:tH8E3LWSRu4g0egqRNDWzHp6MRqRaNMOnxrgAnQfvYU=
462+
github.com/tencentcloud/tencentcloud-sdk-go v1.0.15/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
463463
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
464464
github.com/tetafro/godot v0.3.7/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
465465
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
1717
ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819"
1818
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
19+
audit "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319"
1920
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
2021
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
2122
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
@@ -70,6 +71,7 @@ type TencentCloudClient struct {
7071
sqlserverConn *sqlserver.Client
7172
postgreConn *postgre.Client
7273
ckafkaConn *ckafka.Client
74+
auditConn *audit.Client
7375
}
7476

7577
// NewClientProfile returns a new ClientProfile
@@ -443,3 +445,16 @@ func (me *TencentCloudClient) UseCkafkaClient() *ckafka.Client {
443445

444446
return me.ckafkaConn
445447
}
448+
449+
// UseAuditClient returns audit client for service
450+
func (me *TencentCloudClient) UseAuditClient() *audit.Client {
451+
if me.auditConn != nil {
452+
return me.auditConn
453+
}
454+
455+
cpf := me.NewClientProfile(300)
456+
me.auditConn, _ = audit.NewClient(me.Credential, me.Region, cpf)
457+
me.auditConn.WithHttpTransport(&LogRoundTripper{})
458+
459+
return me.auditConn
460+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Use this data source to query the cos region list supported by the audit.
3+
4+
Example Usage
5+
```hcl
6+
data "tencentcloud_audit_cos_regions" "foo" {
7+
}
8+
```
9+
*/
10+
package tencentcloud
11+
12+
import (
13+
"context"
14+
"log"
15+
16+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
18+
audit "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319"
19+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
20+
)
21+
22+
func dataSourceTencentCloudAuditCosRegions() *schema.Resource {
23+
return &schema.Resource{
24+
Read: dataSourceTencentCloudAuditCosRegionsRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"result_output_file": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Description: "Used to save results.",
31+
},
32+
"audit_cos_region_list": {
33+
Type: schema.TypeList,
34+
Computed: true,
35+
Description: "List of available regions supported by audit cos.",
36+
Elem: &schema.Resource{
37+
Schema: map[string]*schema.Schema{
38+
"cos_region": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "Cos region.",
42+
},
43+
"cos_region_name": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "Cos region chinese name.",
47+
},
48+
},
49+
},
50+
},
51+
},
52+
}
53+
}
54+
55+
func dataSourceTencentCloudAuditCosRegionsRead(d *schema.ResourceData, meta interface{}) error {
56+
defer logElapsed("data_source.tencentcloud_audit_cos_regions.read")()
57+
58+
logId := getLogId(contextNil)
59+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
60+
auditService := AuditService{
61+
client: meta.(*TencentCloudClient).apiV3Conn,
62+
}
63+
64+
var regions []*audit.CosRegionInfo
65+
var errRet error
66+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
67+
regions, errRet = auditService.DescribeAuditCosRegions(ctx)
68+
if errRet != nil {
69+
return retryError(errRet, InternalError)
70+
}
71+
return nil
72+
})
73+
if err != nil {
74+
return err
75+
}
76+
77+
regionList := make([]map[string]interface{}, 0, len(regions))
78+
ids := make([]string, 0, len(regions))
79+
for _, region := range regions {
80+
mapping := map[string]interface{}{
81+
"cos_region": region.CosRegion,
82+
"cos_region_name": region.CosRegionName,
83+
}
84+
regionList = append(regionList, mapping)
85+
ids = append(ids, *region.CosRegion)
86+
}
87+
d.SetId(helper.DataResourceIdsHash(ids))
88+
err = d.Set("audit_cos_region_list", regionList)
89+
if err != nil {
90+
log.Printf("[CRITAL]%s audit cos read regions list fail, reason:%s\n ", logId, err.Error())
91+
return err
92+
}
93+
94+
output, ok := d.GetOk("result_output_file")
95+
if ok && output.(string) != "" {
96+
if e := writeToFile(output.(string), regionList); e != nil {
97+
return e
98+
}
99+
}
100+
return nil
101+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudAuditCosRegionsDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() {
12+
testAccPreCheck(t)
13+
},
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccTencentCloudAuditCosRegionsDataSource,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_audit_cos_regions.all"),
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_cos_regions.all", "audit_cos_region_list.#"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_cos_regions.all", "audit_cos_region_list.0.cos_region"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_cos_regions.all", "audit_cos_region_list.0.cos_region_name"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccTencentCloudAuditCosRegionsDataSource = `
30+
data "tencentcloud_audit_cos_regions" "all" {
31+
}
32+
`
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Use this data source to query the key alias list specified with region supported by the audit.
3+
4+
Example Usage
5+
```hcl
6+
data "tencentcloud_audit_key_alias" "all" {
7+
region = "ap-hongkong"
8+
}
9+
```
10+
*/
11+
package tencentcloud
12+
13+
import (
14+
"context"
15+
"log"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
audit "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudAuditKeyAlias() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudAuditKeyAliasRead,
26+
27+
Schema: map[string]*schema.Schema{
28+
"region": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "Region.",
32+
},
33+
"result_output_file": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Description: "Used to save results.",
37+
},
38+
"audit_key_alias_list": {
39+
Type: schema.TypeList,
40+
Computed: true,
41+
Description: "List of available key alias supported by audit.",
42+
Elem: &schema.Resource{
43+
Schema: map[string]*schema.Schema{
44+
"key_id": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "Key id.",
48+
},
49+
"key_alias": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "Key alias.",
53+
},
54+
},
55+
},
56+
},
57+
},
58+
}
59+
}
60+
61+
func dataSourceTencentCloudAuditKeyAliasRead(d *schema.ResourceData, meta interface{}) error {
62+
defer logElapsed("data_source.tencentcloud_audit_cmq_regions.read")()
63+
64+
logId := getLogId(contextNil)
65+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
66+
auditService := AuditService{
67+
client: meta.(*TencentCloudClient).apiV3Conn,
68+
}
69+
70+
region := d.Get("region").(string)
71+
var keyAlias []*audit.KeyMetadata
72+
var errRet error
73+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
74+
keyAlias, errRet = auditService.DescribeKeyAlias(ctx, region)
75+
if errRet != nil {
76+
return retryError(errRet, InternalError)
77+
}
78+
return nil
79+
})
80+
if err != nil {
81+
return err
82+
}
83+
84+
keyList := make([]map[string]interface{}, 0, len(keyAlias))
85+
ids := make([]string, 0, len(keyAlias))
86+
for _, keyData := range keyAlias {
87+
mapping := map[string]interface{}{
88+
"key_id": keyData.KeyId,
89+
"key_alias": keyData.Alias,
90+
}
91+
keyList = append(keyList, mapping)
92+
ids = append(ids, *keyData.KeyId)
93+
}
94+
d.SetId(helper.DataResourceIdsHash(ids))
95+
err = d.Set("audit_key_alias_list", keyList)
96+
if err != nil {
97+
log.Printf("[CRITAL]%s audit read key alias list fail, reason:%s\n ", logId, err.Error())
98+
return err
99+
}
100+
101+
output, ok := d.GetOk("result_output_file")
102+
if ok && output.(string) != "" {
103+
if e := writeToFile(output.(string), keyList); e != nil {
104+
return e
105+
}
106+
}
107+
return nil
108+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudAuditKeyAliassDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() {
12+
testAccPreCheck(t)
13+
},
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccTencentCloudAuditKeyAliasDataSource,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_audit_key_alias.all"),
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_key_alias.all", "audit_key_alias_list.#"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_key_alias.all", "audit_key_alias_list.0.key_id"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_audit_key_alias.all", "audit_key_alias_list.0.key_alias"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccTencentCloudAuditKeyAliasDataSource = `
30+
data "tencentcloud_audit_key_alias" "all" {
31+
region = "ap-hongkong"
32+
}
33+
`

0 commit comments

Comments
 (0)