Skip to content

Commit edeee77

Browse files
committed
feat(cts): add this data source to check whether data can be transferred to the OBS bucket
1 parent 71f229d commit edeee77

File tree

5 files changed

+358
-0
lines changed

5 files changed

+358
-0
lines changed

docs/data-sources/cts_check_bucket.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
subcategory: "Cloud Trace Service (CTS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cts_check_bucket"
5+
description: |-
6+
Use this data source to check whether data can be transferred to the OBS bucket.
7+
---
8+
9+
# huaweicloud_cts_check_bucket
10+
11+
Use this data source to check whether data can be transferred to the OBS bucket.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_cts_check_bucket" "test" {
17+
bucket_name = "my_bucket"
18+
bucket_location = "cn-north-4"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `region` - (Optional, String) Specifies the region in which to query the resource.
27+
If omitted, the provider-level region will be used.
28+
29+
* `bucket_name` - (Required, String) Specifies the OBS bucket name.
30+
31+
* `bucket_location` - (Required, String) Specifies the OBS bucket location.
32+
33+
* `is_support_trace_files_encryption` - (Optional, Bool) Specifies whether trace files are encrypted during transfer to
34+
an OBS bucket.
35+
36+
* `kms_id` - (Optional, String) Specifies the Key ID used for encrypting transferred trace files.
37+
38+
## Attribute Reference
39+
40+
In addition to all arguments above, the following attributes are exported:
41+
42+
* `id` - The data source ID.
43+
44+
* `buckets` - The OBS bucket information.
45+
The [buckets](#BucketsAttr) structure is documented below.
46+
47+
<a name="BucketsAttr"></a>
48+
The `buckets` block supports:
49+
50+
* `bucket_name` - The OBS bucket name.
51+
52+
* `bucket_location` - The OBS bucket location.
53+
54+
* `is_support_trace_files_encryption` - Whether trace files are encrypted during transfer to an OBS bucket.
55+
56+
* `kms_id` - The Key ID used for encrypting transferred trace files.
57+
58+
* `check_bucket_response` - The check result of the OBS bucket.
59+
The [check_bucket_response](#CheckBucketResponse) structure is documented below.
60+
61+
<a name="CheckBucketResponse"></a>
62+
The `check_bucket_response` block supports:
63+
64+
* `error_code` - The error code.
65+
66+
* `error_message` - The error message.
67+
68+
* `response_code` - The returned HTTP status code.
69+
70+
* `success` - Whether the transfer is successful.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ func Provider() *schema.Provider {
628628
"huaweicloud_codearts_inspector_host_groups": codeartsinspector.DataSourceCodeartsInspectorHostGroups(),
629629
"huaweicloud_codearts_inspector_hosts": codeartsinspector.DataSourceCodeartsInspectorHosts(),
630630

631+
"huaweicloud_cts_check_bucket": cts.DataSourceCheckBucket(),
631632
"huaweicloud_cts_notifications": cts.DataSourceNotifications(),
632633
"huaweicloud_cts_traces": cts.DataSourceCtsTraces(),
633634
"huaweicloud_cts_trackers": cts.DataSourceCtsTrackers(),

huaweicloud/services/acceptance/acceptance.go

+8
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ var (
300300
HW_CFW_PREDEFINED_ADDRESS_GROUP2 = os.Getenv("HW_CFW_PREDEFINED_ADDRESS_GROUP2")
301301
HW_CFW_IPS_CUSTOM_RULE = os.Getenv("HW_CFW_IPS_CUSTOM_RULE")
302302

303+
HW_CTS_KMS_ID = os.Getenv("HW_CTS_KMS_ID")
303304
HW_CTS_START_TIME = os.Getenv("HW_CTS_START_TIME")
304305
HW_CTS_END_TIME = os.Getenv("HW_CTS_END_TIME")
305306

@@ -1569,6 +1570,13 @@ func TestAccPreCheckCCConnectionRouteRegionName(t *testing.T) {
15691570
}
15701571
}
15711572

1573+
// lintignore:AT003
1574+
func TestAccPreCheckCtsKmsId(t *testing.T) {
1575+
if HW_CTS_KMS_ID == "" {
1576+
t.Skip("HW_CTS_KMS_ID must be set for CTS acceptance tests")
1577+
}
1578+
}
1579+
15721580
// lintignore:AT003
15731581
func TestAccPreCheckCtsTimeRange(t *testing.T) {
15741582
if HW_CTS_START_TIME == "" || HW_CTS_END_TIME == "" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cts
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceCheckBucket_basic(t *testing.T) {
13+
dataSource := "data.huaweicloud_cts_check_bucket.test"
14+
rName := acceptance.RandomAccResourceNameWithDash()
15+
dc := acceptance.InitDataSourceCheck(dataSource)
16+
baseConfig := testDataSourceCheckBucket_base(rName)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acceptance.TestAccPreCheck(t)
21+
acceptance.TestAccPreCheckCtsKmsId(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testDataSourceCheckBucket_basic(baseConfig),
27+
Check: resource.ComposeTestCheckFunc(
28+
dc.CheckResourceExists(),
29+
resource.TestCheckResourceAttr(dataSource, "buckets.0.bucket_name", rName),
30+
resource.TestCheckResourceAttr(dataSource, "buckets.0.check_bucket_response.0.response_code", "200"),
31+
resource.TestCheckResourceAttr(dataSource, "buckets.0.check_bucket_response.0.success", "true"),
32+
),
33+
},
34+
{
35+
Config: testDataSourceCheckBucket_KmsId(baseConfig),
36+
Check: resource.ComposeTestCheckFunc(
37+
dc.CheckResourceExists(),
38+
resource.TestCheckResourceAttr(dataSource, "buckets.0.bucket_name", rName),
39+
resource.TestCheckResourceAttr(dataSource, "buckets.0.check_bucket_response.0.response_code", "200"),
40+
resource.TestCheckResourceAttr(dataSource, "buckets.0.check_bucket_response.0.success", "true"),
41+
resource.TestCheckResourceAttr(dataSource, "buckets.0.is_support_trace_files_encryption", "true"),
42+
resource.TestCheckResourceAttr(dataSource, "buckets.0.kms_id", acceptance.HW_CTS_KMS_ID),
43+
),
44+
},
45+
},
46+
})
47+
}
48+
49+
func testDataSourceCheckBucket_basic(config string) string {
50+
return fmt.Sprintf(`
51+
%s
52+
53+
data "huaweicloud_cts_check_bucket" "test" {
54+
bucket_name = huaweicloud_obs_bucket.bucket.bucket
55+
bucket_location = huaweicloud_obs_bucket.bucket.region
56+
}
57+
`, config)
58+
}
59+
60+
func testDataSourceCheckBucket_KmsId(config string) string {
61+
return fmt.Sprintf(`
62+
%[1]s
63+
64+
data "huaweicloud_cts_check_bucket" "test" {
65+
bucket_name = huaweicloud_obs_bucket.bucket.bucket
66+
bucket_location = huaweicloud_obs_bucket.bucket.region
67+
kms_id = "%[2]s"
68+
is_support_trace_files_encryption = true
69+
}
70+
`, config, acceptance.HW_CTS_KMS_ID)
71+
}
72+
73+
func testDataSourceCheckBucket_base(rName string) string {
74+
return fmt.Sprintf(`
75+
resource "huaweicloud_obs_bucket" "bucket" {
76+
bucket = "%s"
77+
acl = "public-read"
78+
force_destroy = true
79+
}
80+
`, rName)
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package cts
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/tidwall/gjson"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
func DataSourceCheckBucket() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceCtsCheckBucketRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
29+
},
30+
"bucket_name": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: `Specifies the OBS bucket name.`,
34+
},
35+
"bucket_location": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
Description: `Specifies the OBS bucket location.`,
39+
},
40+
"kms_id": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Description: `Specifies the Key ID used for encrypting transferred trace files.`,
44+
},
45+
"is_support_trace_files_encryption": {
46+
Type: schema.TypeBool,
47+
Optional: true,
48+
Description: `Specifies whether trace files are encrypted during transfer to an OBS bucket.`,
49+
},
50+
"buckets": {
51+
Type: schema.TypeList,
52+
Computed: true,
53+
Description: `The OBS bucket information.`,
54+
Elem: &schema.Resource{
55+
Schema: map[string]*schema.Schema{
56+
"bucket_name": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Description: `The OBS bucket name.`,
60+
},
61+
"bucket_location": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: `The OBS bucket location.`,
65+
},
66+
"kms_id": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: `The Key ID used for encrypting transferred trace files.`,
70+
},
71+
"is_support_trace_files_encryption": {
72+
Type: schema.TypeBool,
73+
Computed: true,
74+
Description: `Whether trace files are encrypted during transfer to an OBS bucket.`,
75+
},
76+
"check_bucket_response": {
77+
Type: schema.TypeList,
78+
Computed: true,
79+
Description: `The check result of the OBS bucket.`,
80+
Elem: &schema.Resource{
81+
Schema: map[string]*schema.Schema{
82+
"error_code": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: `The error code.`,
86+
},
87+
"error_message": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: `The error message.`,
91+
},
92+
"response_code": {
93+
Type: schema.TypeInt,
94+
Computed: true,
95+
Description: `The returned HTTP status code.`,
96+
},
97+
"success": {
98+
Type: schema.TypeBool,
99+
Computed: true,
100+
Description: `Whether the transfer is successful.`,
101+
},
102+
},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
}
110+
}
111+
112+
type CheckBucketDSWrapper struct {
113+
*schemas.ResourceDataWrapper
114+
Config *config.Config
115+
}
116+
117+
func newCheckBucketDSWrapper(d *schema.ResourceData, meta interface{}) *CheckBucketDSWrapper {
118+
return &CheckBucketDSWrapper{
119+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
120+
Config: meta.(*config.Config),
121+
}
122+
}
123+
124+
func dataSourceCtsCheckBucketRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
125+
wrapper := newCheckBucketDSWrapper(d, meta)
126+
checkObsBucketsRst, err := wrapper.CheckObsBuckets()
127+
if err != nil {
128+
return diag.FromErr(err)
129+
}
130+
131+
id, err := uuid.GenerateUUID()
132+
if err != nil {
133+
return diag.FromErr(err)
134+
}
135+
d.SetId(id)
136+
137+
err = wrapper.checkObsBucketsToSchema(checkObsBucketsRst)
138+
if err != nil {
139+
return diag.FromErr(err)
140+
}
141+
142+
return nil
143+
}
144+
145+
// @API CTS POST /v3/{domain_id}/checkbucket
146+
func (w *CheckBucketDSWrapper) CheckObsBuckets() (*gjson.Result, error) {
147+
client, err := w.NewClient(w.Config, "cts")
148+
if err != nil {
149+
return nil, err
150+
}
151+
152+
uri := "/v3/{domain_id}/checkbucket"
153+
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
154+
params := map[string]any{
155+
"bucket_location": w.Get("bucket_location"),
156+
"bucket_name": w.Get("bucket_name"),
157+
"is_support_trace_files_encryption": w.Get("is_support_trace_files_encryption"),
158+
"kms_id": w.Get("kms_id"),
159+
}
160+
params = utils.RemoveNil(params)
161+
reqBody := map[string]any{
162+
"buckets": []map[string]any{params},
163+
}
164+
return httphelper.New(client).
165+
Method("POST").
166+
URI(uri).
167+
Body(reqBody).
168+
Request().
169+
Result()
170+
}
171+
172+
func (w *CheckBucketDSWrapper) checkObsBucketsToSchema(body *gjson.Result) error {
173+
d := w.ResourceData
174+
mErr := multierror.Append(nil,
175+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
176+
d.Set("buckets", schemas.SliceToList(body.Get("buckets"),
177+
func(buckets gjson.Result) any {
178+
return map[string]any{
179+
"bucket_name": buckets.Get("bucket_name").Value(),
180+
"bucket_location": buckets.Get("bucket_location").Value(),
181+
"kms_id": buckets.Get("kms_id").Value(),
182+
"is_support_trace_files_encryption": buckets.Get("is_support_trace_files_encryption").Value(),
183+
"check_bucket_response": schemas.SliceToList(buckets.Get("check_bucket_response"),
184+
func(checkBucketResponse gjson.Result) any {
185+
return map[string]any{
186+
"error_code": checkBucketResponse.Get("error_code").Value(),
187+
"error_message": checkBucketResponse.Get("error_message").Value(),
188+
"response_code": checkBucketResponse.Get("response_code").Value(),
189+
"success": checkBucketResponse.Get("success").Value(),
190+
}
191+
},
192+
),
193+
}
194+
},
195+
)),
196+
)
197+
return mErr.ErrorOrNil()
198+
}

0 commit comments

Comments
 (0)