forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_huaweicloud_kms_data_key_v1.go
66 lines (57 loc) · 1.48 KB
/
data_source_huaweicloud_kms_data_key_v1.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
package huaweicloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/kms/v1/keys"
)
func dataSourceKmsDataKeyV1() *schema.Resource {
return &schema.Resource{
Read: resourceKmsDataKeyV1Read,
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Required: true,
},
"encryption_context": {
Type: schema.TypeString,
Optional: true,
},
"datakey_length": {
Type: schema.TypeString,
Required: true,
},
"plain_text": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cipher_text": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceKmsDataKeyV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
KmsDataKeyV1Client, err := config.kmsKeyV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud kms key client: %s", err)
}
req := &keys.DataEncryptOpts{
KeyID: d.Get("key_id").(string),
EncryptionContext: d.Get("encryption_context").(string),
DatakeyLength: d.Get("datakey_length").(string),
}
log.Printf("[DEBUG] KMS get data key for key: %s", d.Get("key_id").(string))
v, err := keys.DataEncryptGet(KmsDataKeyV1Client, req).ExtractDataKey()
if err != nil {
return err
}
d.SetId(time.Now().UTC().String())
d.Set("plain_text", v.PlainText)
d.Set("cipher_text", v.CipherText)
return nil
}