This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_yandex_iam_service_account_static_access_key.go
146 lines (117 loc) · 3.78 KB
/
resource_yandex_iam_service_account_static_access_key.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
135
136
137
138
139
140
141
142
143
144
145
146
package yandex
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/encryption"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1/awscompatibility"
)
func resourceYandexIAMServiceAccountStaticAccessKey() *schema.Resource {
return &schema.Resource{
Create: resourceYandexIAMServiceAccountStaticAccessKeyCreate,
Read: resourceYandexIAMServiceAccountStaticAccessKeyRead,
Delete: resourceYandexIAMServiceAccountStaticAccessKeyDelete,
Schema: map[string]*schema.Schema{
"service_account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
// There is no Update method for IAM SA Key resource,
// so "description" attr set as 'ForceNew:true'
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"pgp_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"access_key": {
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"key_fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"encrypted_secret_key": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceYandexIAMServiceAccountStaticAccessKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := context.WithTimeout(config.ContextWithClientTraceID(), d.Timeout(schema.TimeoutCreate))
defer cancel()
resp, err := config.sdk.IAM().AWSCompatibility().AccessKey().Create(ctx, &awscompatibility.CreateAccessKeyRequest{
ServiceAccountId: d.Get("service_account_id").(string),
Description: d.Get("description").(string),
})
if err != nil {
return fmt.Errorf("error creating service account key: %s", err)
}
d.SetId(resp.AccessKey.Id)
// Data only available on create.
if v, ok := d.GetOk("pgp_key"); ok {
encryptionKey, err := encryption.RetrieveGPGKey(v.(string))
if err != nil {
return err
}
fingerprint, encrypted, err := encryption.EncryptValue(encryptionKey, resp.Secret, "Yandex Service Account Static Access Key")
if err != nil {
return err
}
d.Set("key_fingerprint", fingerprint)
d.Set("encrypted_secret_key", encrypted)
} else {
d.Set("secret_key", resp.Secret)
}
return resourceYandexIAMServiceAccountStaticAccessKeyRead(d, meta)
}
func resourceYandexIAMServiceAccountStaticAccessKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := context.WithTimeout(config.ContextWithClientTraceID(), d.Timeout(schema.TimeoutRead))
defer cancel()
sak, err := config.sdk.IAM().AWSCompatibility().AccessKey().Get(ctx, &awscompatibility.GetAccessKeyRequest{
AccessKeyId: d.Id(),
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account Static Access Key %q", d.Id()))
}
createdAt, err := getTimestamp(sak.CreatedAt)
if err != nil {
return err
}
d.Set("service_account_id", sak.ServiceAccountId)
d.Set("created_at", createdAt)
d.Set("description", sak.Description)
d.Set("access_key", sak.KeyId)
return nil
}
func resourceYandexIAMServiceAccountStaticAccessKeyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := context.WithTimeout(config.ContextWithClientTraceID(), d.Timeout(schema.TimeoutDelete))
defer cancel()
_, err := config.sdk.IAM().AWSCompatibility().AccessKey().Delete(ctx, &awscompatibility.DeleteAccessKeyRequest{
AccessKeyId: d.Id(),
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account Static Access Key %q", d.Id()))
}
d.SetId("")
return nil
}