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
/
data_source_yandex_iam_user.go
84 lines (69 loc) · 1.81 KB
/
data_source_yandex_iam_user.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
package yandex
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/grpc/codes"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1"
)
func dataSourceYandexIAMUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexLoginRead,
Schema: map[string]*schema.Schema{
"login": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"user_id"},
},
"user_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"login"},
},
"default_email": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexLoginRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := context.Background()
var user *iam.UserAccount
if v, ok := d.GetOk("login"); ok {
login := v.(string)
resp, err := config.sdk.IAM().YandexPassportUserAccount().GetByLogin(ctx, &iam.GetUserAccountByLoginRequest{
Login: login,
})
if err != nil {
if isStatusWithCode(err, codes.NotFound) {
return fmt.Errorf("login not found: %s", login)
}
return err
}
user = resp
} else if v, ok := d.GetOk("user_id"); ok {
userID := v.(string)
resp, err := config.sdk.IAM().UserAccount().Get(ctx, &iam.GetUserAccountRequest{
UserAccountId: userID,
})
if err != nil {
return fmt.Errorf("failed to find user with ID \"%s\": %s", userID, err)
}
user = resp
} else {
return fmt.Errorf("one of 'login' or 'user_id' must be set")
}
d.Set("user_id", user.Id)
if user.UserAccount != nil {
if yaUser := user.GetYandexPassportUserAccount(); yaUser != nil {
d.Set("default_email", yaUser.DefaultEmail)
d.Set("login", yaUser.Login)
}
}
d.SetId(user.Id)
return nil
}