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
119 lines (92 loc) · 3.09 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
package yandex
import (
"context"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/terraform/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,
},
"access_key": {
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceYandexIAMServiceAccountStaticAccessKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := context.WithTimeout(context.Background(), 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 create service account key: %s", err)
}
d.SetId(resp.AccessKey.Id)
// One-time generated value
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(context.Background(), 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 Key %q", d.Id()))
}
ts, err := ptypes.Timestamp(sak.CreatedAt)
if err != nil {
return fmt.Errorf("error while convert CreatedAt timestamp: %s", err)
}
d.Set("access_key", sak.KeyId)
d.Set("description", sak.Description)
d.Set("service_account_id", sak.ServiceAccountId)
d.Set("created_at", ts.Format(time.RFC3339))
return nil
}
func resourceYandexIAMServiceAccountStaticAccessKeyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout(schema.TimeoutRead))
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 Key %q", d.Id()))
}
d.SetId("")
return nil
}