Skip to content
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

feat: support pts #2139

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .changelog/2139.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
tencentcloud_pts_tmp_key
```
2 changes: 2 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ Performance Testing Service(PTS)
tencentcloud_pts_file
tencentcloud_pts_job
tencentcloud_pts_cron_job
tencentcloud_pts_tmp_key

TencentCloud Automation Tools(TAT)
Data Source
Expand Down Expand Up @@ -2865,6 +2866,7 @@ func Provider() *schema.Provider {
"tencentcloud_pts_file": resourceTencentCloudPtsFile(),
"tencentcloud_pts_job": resourceTencentCloudPtsJob(),
"tencentcloud_pts_cron_job": resourceTencentCloudPtsCronJob(),
"tencentcloud_pts_tmp_key": resourceTencentCloudPtsTmpKey(),
"tencentcloud_tat_command": resourceTencentCloudTatCommand(),
"tencentcloud_tat_invoker": resourceTencentCloudTatInvoker(),
"tencentcloud_tat_invoker_config": resourceTencentCloudTatInvokerConfig(),
Expand Down
164 changes: 164 additions & 0 deletions tencentcloud/resource_tc_pts_tmp_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Provides a resource to create a pts tmp_key

Example Usage

```hcl
resource "tencentcloud_pts_tmp_key" "tmp_key" {
project_id = "project-1b0zqmhg"
scenario_id = "scenario-abc"
}
```
*/
package tencentcloud

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
pts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts/v20210728"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func resourceTencentCloudPtsTmpKey() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudPtsTmpKeyCreate,
Read: resourceTencentCloudPtsTmpKeyRead,
Delete: resourceTencentCloudPtsTmpKeyDelete,

Schema: map[string]*schema.Schema{
"project_id": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Project ID.",
},

"scenario_id": {
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Scenario ID.",
},

"start_time": {
Computed: true,
Type: schema.TypeInt,
Description: "The timestamp of the moment when the temporary access credential was obtained (in seconds).",
},
"expired_time": {
Computed: true,
Type: schema.TypeInt,
Description: "Timestamp of temporary access credential timeout (in seconds).",
},
"credentials": {
Type: schema.TypeList,
Computed: true,
Description: "Temporary access credentials.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tmp_secret_id": {
Type: schema.TypeString,
Computed: true,
Description: "Temporary secret ID.",
},
"tmp_secret_key": {
Type: schema.TypeString,
Computed: true,
Description: "Temporary secret key.",
},
"token": {
Type: schema.TypeString,
Computed: true,
Description: "Temporary token.",
},
},
},
},
},
}
}

func resourceTencentCloudPtsTmpKeyCreate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_pts_tmp_key.create")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

var (
request = pts.NewGenerateTmpKeyRequest()
response = pts.NewGenerateTmpKeyResponse()
projectId string
)
if v, ok := d.GetOk("project_id"); ok {
projectId = v.(string)
request.ProjectId = helper.String(v.(string))
}

if v, ok := d.GetOk("scenario_id"); ok {
request.ScenarioId = helper.String(v.(string))
}

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePtsClient().GenerateTmpKey(request)
if e != nil {
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate pts tmpKey failed, reason:%+v", logId, err)
return err
}

// d.SetId(projectId + FILED_SP + scenarioId)
d.SetId(projectId)

if response != nil || response.Response != nil {
credentials := response.Response.Credentials
if credentials != nil {
credentialsMap := map[string]interface{}{}
if credentials.TmpSecretId != nil {
credentialsMap["tmp_secret_id"] = credentials.TmpSecretId
}

if credentials.TmpSecretKey != nil {
credentialsMap["tmp_secret_key"] = credentials.TmpSecretKey
}

if credentials.Token != nil {
credentialsMap["token"] = credentials.Token
}

_ = d.Set("credentials", []interface{}{credentialsMap})
}

if response.Response.StartTime != nil {
_ = d.Set("start_time", response.Response.StartTime)
}

if response.Response.ExpiredTime != nil {
_ = d.Set("expired_time", response.Response.ExpiredTime)
}
}

return resourceTencentCloudPtsTmpKeyRead(d, meta)
}

func resourceTencentCloudPtsTmpKeyRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_pts_tmp_key.read")()
defer inconsistentCheck(d, meta)()

return nil
}

func resourceTencentCloudPtsTmpKeyDelete(d *schema.ResourceData, meta interface{}) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个资源不支持删除吗

defer logElapsed("resource.tencentcloud_pts_tmp_key.delete")()
defer inconsistentCheck(d, meta)()

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

import (
"testing"

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

// go test -test.run TestAccTencentCloudPtsTmpKeyResource_basic -v
func TestAccTencentCloudPtsTmpKeyResource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccPtsTmpKey,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "id"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "project_id"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "scenario_id"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "start_time"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "expired_time"),
resource.TestCheckResourceAttr("tencentcloud_pts_tmp_key.tmp_key", "credentials.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "credentials.0.tmp_secret_id"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "credentials.0.tmp_secret_key"),
resource.TestCheckResourceAttrSet("tencentcloud_pts_tmp_key.tmp_key", "credentials.0.token"),
),
},
},
})
}

const testAccPtsTmpKeyVar = `
variable "project_id" {
default = "` + defaultPtsProjectId + `"
}
variable "scenario_id" {
default = "` + defaultScenarioId + `"
}

`

const testAccPtsTmpKey = testAccPtsTmpKeyVar + `
resource "tencentcloud_pts_tmp_key" "tmp_key" {
project_id = var.project_id
scenario_id = var.scenario_id
}
`
42 changes: 42 additions & 0 deletions website/docs/r/pts_tmp_key.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Performance Testing Service(PTS)"
layout: "tencentcloud"
page_title: "TencentCloud: tencentcloud_pts_tmp_key"
sidebar_current: "docs-tencentcloud-resource-pts_tmp_key"
description: |-
Provides a resource to create a pts tmp_key
---

# tencentcloud_pts_tmp_key

Provides a resource to create a pts tmp_key

## Example Usage

```hcl
resource "tencentcloud_pts_tmp_key" "tmp_key" {
project_id = "project-1b0zqmhg"
scenario_id = "scenario-abc"
}
```

## Argument Reference

The following arguments are supported:

* `project_id` - (Required, String, ForceNew) Project ID.
* `scenario_id` - (Optional, String, ForceNew) Scenario ID.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - ID of the resource.
* `credentials` - Temporary access credentials.
* `tmp_secret_id` - Temporary secret ID.
* `tmp_secret_key` - Temporary secret key.
* `token` - Temporary token.
* `expired_time` - Timestamp of temporary access credential timeout (in seconds).
* `start_time` - The timestamp of the moment when the temporary access credential was obtained (in seconds).


3 changes: 3 additions & 0 deletions website/tencentcloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,9 @@
<li>
<a href="/docs/providers/tencentcloud/r/pts_scenario.html">tencentcloud_pts_scenario</a>
</li>
<li>
<a href="/docs/providers/tencentcloud/r/pts_tmp_key.html">tencentcloud_pts_tmp_key</a>
</li>
</ul>
</li>
</ul>
Expand Down