forked from DrFaust92/terraform-provider-bitbucket
/
data_current_user.go
129 lines (106 loc) · 2.83 KB
/
data_current_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
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
package bitbucket
import (
"context"
"encoding/json"
"io"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type PaginatedUserEmails struct {
Values []UserEmail `json:"values,omitempty"`
Page int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
Next string `json:"next,omitempty"`
}
type UserEmail struct {
Email string `json:"email"`
IsPrimary bool `json:"is_primary"`
IsConfirmed bool `json:"is_confirmed"`
}
func dataCurrentUser() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataReadCurrentUser,
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"uuid": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_confirmed": {
Type: schema.TypeBool,
Computed: true,
},
"is_primary": {
Type: schema.TypeBool,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataReadCurrentUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(Clients).genClient
httpClient := m.(Clients).httpClient
usersApi := c.ApiClient.UsersApi
curUser, _, err := usersApi.UserGet(c.AuthContext)
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Current User: %#v", curUser)
curUserEmails, err := httpClient.Get("2.0/user/emails")
if err != nil {
return diag.FromErr(err)
}
emailBody, readerr := io.ReadAll(curUserEmails.Body)
if readerr != nil {
return diag.FromErr(readerr)
}
log.Printf("[DEBUG] Current User Emails Response JSON: %v", string(emailBody))
var emails PaginatedUserEmails
decodeerr := json.Unmarshal(emailBody, &emails)
if decodeerr != nil {
return diag.FromErr(decodeerr)
}
log.Printf("[DEBUG] Current User Emails Response Decoded: %#v", emails)
d.SetId(curUser.Uuid)
d.Set("uuid", curUser.Uuid)
d.Set("username", curUser.Username)
d.Set("display_name", curUser.DisplayName)
d.Set("email", flattenUserEmails(emails.Values))
return nil
}
func flattenUserEmails(userEmails []UserEmail) []interface{} {
if len(userEmails) == 0 {
return nil
}
var tfList []interface{}
for _, btRaw := range userEmails {
log.Printf("[DEBUG] User Email Response Decoded: %#v", btRaw)
branchType := map[string]interface{}{
"email": btRaw.Email,
"is_confirmed": btRaw.IsConfirmed,
"is_primary": btRaw.IsPrimary,
}
tfList = append(tfList, branchType)
}
return tfList
}