Skip to content

Commit

Permalink
feat/ssm (#2172)
Browse files Browse the repository at this point in the history
* feat/ssm

* feat/ssm
  • Loading branch information
SevenEarth authored Oct 8, 2023
1 parent 333cb18 commit d8c42e1
Show file tree
Hide file tree
Showing 19 changed files with 1,129 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .changelog/2172.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```release-note:new-resource
tencentcloud_ssm_rotate_product_secret
```

```release-note:new-data-source
tencentcloud_ssm_rotation_detail
```

```release-note:new-data-source
tencentcloud_ssm_rotation_history
```

```release-note:new-data-source
tencentcloud_ssm_service_status
```

```release-note:new-data-source
tencentcloud_ssm_ssh_key_pair_value
```
118 changes: 118 additions & 0 deletions tencentcloud/data_source_tc_ssm_rotation_detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Use this data source to query detailed information of ssm rotation_detail
Example Usage
```hcl
data "tencentcloud_ssm_rotation_detail" "example" {
secret_name = "tf_example"
}
```
*/
package tencentcloud

import (
"context"

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

func dataSourceTencentCloudSsmRotationDetail() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudSsmRotationDetailRead,
Schema: map[string]*schema.Schema{
"secret_name": {
Required: true,
Type: schema.TypeString,
Description: "Secret name.",
},
"enable_rotation": {
Computed: true,
Type: schema.TypeBool,
Description: "Whether to allow rotation.",
},
"frequency": {
Computed: true,
Type: schema.TypeInt,
Description: "The rotation frequency, in days, defaults to 1 day.",
},
"latest_rotate_time": {
Computed: true,
Type: schema.TypeString,
Description: "Time of last rotation.",
},
"next_rotate_begin_time": {
Computed: true,
Type: schema.TypeString,
Description: "The time to start the next rotation.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudSsmRotationDetailRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_ssm_rotation_detail.read")()
defer inconsistentCheck(d, meta)()

var (
logId = getLogId(contextNil)
ctx = context.WithValue(context.TODO(), logIdKey, logId)
service = SsmService{client: meta.(*TencentCloudClient).apiV3Conn}
rotationDetail *ssm.DescribeRotationDetailResponseParams
secretName string
)

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("secret_name"); ok {
paramMap["SecretName"] = helper.String(v.(string))
secretName = v.(string)
}

err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeSsmRotationDetailByFilter(ctx, paramMap)
if e != nil {
return retryError(e)
}

rotationDetail = result
return nil
})

if err != nil {
return err
}

if rotationDetail.EnableRotation != nil {
_ = d.Set("enable_rotation", rotationDetail.EnableRotation)
}

if rotationDetail.Frequency != nil {
_ = d.Set("frequency", rotationDetail.Frequency)
}

if rotationDetail.LatestRotateTime != nil {
_ = d.Set("latest_rotate_time", rotationDetail.LatestRotateTime)
}

if rotationDetail.NextRotateBeginTime != nil {
_ = d.Set("next_rotate_begin_time", rotationDetail.NextRotateBeginTime)
}

d.SetId(secretName)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), d); e != nil {
return e
}
}

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

import (
"testing"

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

// go test -i; go test -test.run TestAccTencentCloudNeedFixSsmRotationDetailDataSource_basic -v
func TestAccTencentCloudNeedFixSsmRotationDetailDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccSsmRotationDetailDataSource,
Check: resource.ComposeTestCheckFunc(
testAccCheckTencentCloudDataSourceID("data.tencentcloud_ssm_rotation_detail.example"),
),
},
},
})
}

const testAccSsmRotationDetailDataSource = `
data "tencentcloud_ssm_rotation_detail" "example" {
secret_name = "tf_example"
}
`
91 changes: 91 additions & 0 deletions tencentcloud/data_source_tc_ssm_rotation_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Use this data source to query detailed information of ssm rotation_history
Example Usage
```hcl
data "tencentcloud_ssm_rotation_history" "example" {
secret_name = "keep_terraform"
}
```
*/
package tencentcloud

import (
"context"

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

func dataSourceTencentCloudSsmRotationHistory() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudSsmRotationHistoryRead,
Schema: map[string]*schema.Schema{
"secret_name": {
Required: true,
Type: schema.TypeString,
Description: "Secret name.",
},
"version_ids": {
Computed: true,
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The number of version numbers. The maximum number of version numbers that can be displayed to users is 10.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudSsmRotationHistoryRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_ssm_rotation_history.read")()
defer inconsistentCheck(d, meta)()

var (
logId = getLogId(contextNil)
ctx = context.WithValue(context.TODO(), logIdKey, logId)
service = SsmService{client: meta.(*TencentCloudClient).apiV3Conn}
versionIDs []*string
secretName string
)

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("secret_name"); ok {
paramMap["SecretName"] = helper.String(v.(string))
secretName = v.(string)
}

err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeSsmRotationHistoryByFilter(ctx, paramMap)
if e != nil {
return retryError(e)
}

versionIDs = result
return nil
})

if err != nil {
return err
}

if versionIDs != nil {
_ = d.Set("version_ids", versionIDs)
}

d.SetId(secretName)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), d); e != nil {
return e
}
}

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

import (
"testing"

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

// go test -i; go test -test.run TestAccTencentCloudNeedFixSsmRotationHistoryDataSource_basic -v
func TestAccTencentCloudNeedFixSsmRotationHistoryDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccSsmRotationHistoryDataSource,
Check: resource.ComposeTestCheckFunc(
testAccCheckTencentCloudDataSourceID("data.tencentcloud_ssm_rotation_history.example"),
),
},
},
})
}

const testAccSsmRotationHistoryDataSource = `
data "tencentcloud_ssm_rotation_history" "example" {
secret_name = "keep_terraform"
}
`
96 changes: 96 additions & 0 deletions tencentcloud/data_source_tc_ssm_service_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Use this data source to query detailed information of ssm service_status
Example Usage
```hcl
data "tencentcloud_ssm_service_status" "example" {}
```
*/
package tencentcloud

import (
"context"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ssm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm/v20190923"
)

func dataSourceTencentCloudSsmServiceStatus() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudSsmServiceStatusRead,
Schema: map[string]*schema.Schema{
"service_enabled": {
Computed: true,
Type: schema.TypeBool,
Description: "True means the service has been activated, false means the service has not been activated yet.",
},
"invalid_type": {
Computed: true,
Type: schema.TypeInt,
Description: "Service unavailability type: 0-Not purchased, 1-Normal, 2-Service suspended due to arrears, 3-Resource release.",
},
"access_key_escrow_enabled": {
Computed: true,
Type: schema.TypeBool,
Description: "True means that the user can already use the key safe hosting function, false means that the user cannot use the key safe hosting function temporarily.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudSsmServiceStatusRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_ssm_service_status.read")()
defer inconsistentCheck(d, meta)()

var (
logId = getLogId(contextNil)
ctx = context.WithValue(context.TODO(), logIdKey, logId)
service = SsmService{client: meta.(*TencentCloudClient).apiV3Conn}
serviceStatus *ssm.GetServiceStatusResponseParams
)

err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeSsmServiceStatusByFilter(ctx)
if e != nil {
return retryError(e)
}

serviceStatus = result
return nil
})

if err != nil {
return err
}

if serviceStatus.ServiceEnabled != nil {
_ = d.Set("service_enabled", serviceStatus.ServiceEnabled)
}

if serviceStatus.InvalidType != nil {
_ = d.Set("invalid_type", serviceStatus.InvalidType)
}

if serviceStatus.AccessKeyEscrowEnabled != nil {
_ = d.Set("access_key_escrow_enabled", serviceStatus.AccessKeyEscrowEnabled)
}

d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), d); e != nil {
return e
}
}

return nil
}
Loading

0 comments on commit d8c42e1

Please sign in to comment.