-
Notifications
You must be signed in to change notification settings - Fork 133
/
data_source_tc_ssm_ssh_key_pair_value.go
134 lines (114 loc) · 3.72 KB
/
data_source_tc_ssm_ssh_key_pair_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package ssm
import (
"context"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"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 DataSourceTencentCloudSsmSshKeyPairValue() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudSsmSshKeyPairValueRead,
Schema: map[string]*schema.Schema{
"secret_name": {
Optional: true,
Computed: true,
Type: schema.TypeString,
Description: "Secret name.",
},
"ssh_key_id": {
Optional: true,
Computed: true,
Type: schema.TypeString,
Description: "The key pair ID is the unique identifier of the key pair in the cloud server.",
},
"public_key": {
Computed: true,
Type: schema.TypeString,
Description: "Public key plain text, encoded using base64.",
},
"private_key": {
Computed: true,
Type: schema.TypeString,
Description: "Private key plain text, encoded using base64.",
},
"project_id": {
Computed: true,
Type: schema.TypeInt,
Description: "The project ID to which this key pair belongs.",
},
"ssh_key_name": {
Computed: true,
Type: schema.TypeString,
Description: "SSH key name.",
},
"ssh_key_description": {
Computed: true,
Type: schema.TypeString,
Description: "Description of the SSH key pair. Users can modify the description information of the key pair in the CVM console.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudSsmSshKeyPairValueRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_ssm_ssh_key_pair_value.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = SsmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
sshKeyPairValue *ssm.GetSSHKeyPairValueResponseParams
sshKeyID string
)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("secret_name"); ok {
paramMap["SecretName"] = helper.String(v.(string))
}
if v, ok := d.GetOk("ssh_key_id"); ok {
paramMap["SSHKeyId"] = helper.String(v.(string))
}
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeSsmSshKeyPairValueByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
sshKeyPairValue = result
return nil
})
if err != nil {
return err
}
if sshKeyPairValue.SSHKeyID != nil {
_ = d.Set("ssh_key_id", sshKeyPairValue.SSHKeyID)
sshKeyID = *sshKeyPairValue.SSHKeyID
}
if sshKeyPairValue.SSHKeyName != nil {
_ = d.Set("ssh_key_name", sshKeyPairValue.SSHKeyName)
}
if sshKeyPairValue.PublicKey != nil {
_ = d.Set("public_key", sshKeyPairValue.PublicKey)
}
if sshKeyPairValue.PrivateKey != nil {
_ = d.Set("private_key", sshKeyPairValue.PrivateKey)
}
if sshKeyPairValue.ProjectID != nil {
_ = d.Set("project_id", sshKeyPairValue.ProjectID)
}
if sshKeyPairValue.SSHKeyDescription != nil {
_ = d.Set("ssh_key_description", sshKeyPairValue.SSHKeyDescription)
}
d.SetId(sshKeyID)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), d); e != nil {
return e
}
}
return nil
}