-
Notifications
You must be signed in to change notification settings - Fork 533
/
data_source_alicloud_kms_ciphertext.go
97 lines (83 loc) · 2.57 KB
/
data_source_alicloud_kms_ciphertext.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
package alicloud
import (
"strconv"
"time"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudKmsCiphertext() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudKmsCiphertextRead,
Schema: map[string]*schema.Schema{
"plaintext": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"key_id": {
Type: schema.TypeString,
Required: true,
},
"encryption_context": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ciphertext_blob": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAlicloudKmsCiphertextRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
// Since a ciphertext has no ID, we create an ID based on
// current unix time.
d.SetId(strconv.FormatInt(time.Now().Unix(), 16))
action := "Encrypt"
request := make(map[string]interface{})
request["Plaintext"] = d.Get("plaintext")
request["KeyId"] = d.Get("key_id")
request["RegionId"] = client.RegionId
if context := d.Get("encryption_context"); context != nil {
cm := context.(map[string]interface{})
contextJson, err := convertMaptoJsonString(cm)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_kms_ciphertext", action, AlibabaCloudSdkGoERROR)
}
request["EncryptionContext"] = contextJson
}
var response map[string]interface{}
conn, err := client.NewKmsClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-01-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_kms_ciphertext", action, AlibabaCloudSdkGoERROR)
}
resp, err := jsonpath.Get("$.CiphertextBlob", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.CiphertextBlob", response)
}
d.Set("ciphertext_blob", resp)
return nil
}