-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_user.go
103 lines (95 loc) · 2.24 KB
/
data_source_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
// SPDX-FileCopyrightText: 2024 Dominik Wombacher <[email protected]>
// SPDX-FileCopyrightText: 2019 The SourceHut API Contributors
//
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// Datasource Name
userName = "sourcehut_user"
// Keys
emailKey = "email"
urlKey = "url"
locationKey = "location"
bioKey = "bio"
pgpKeyKey = "preferred_pgp_key"
)
// dataSourceUser returns a data source for getting information about the
// authenticated users account.
func dataSourceUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserRead,
Schema: map[string]*schema.Schema{
userKey: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the authenticated user (eg. 'example').",
},
canonicalUserKey: {
Type: schema.TypeString,
Computed: true,
Description: "The canonical name of the authenticated user (eg. '~example').",
},
emailKey: {
Type: schema.TypeString,
Computed: true,
Description: "The users email.",
},
urlKey: {
Type: schema.TypeString,
Computed: true,
Description: "The users URL.",
},
locationKey: {
Type: schema.TypeString,
Computed: true,
Description: "The users location.",
},
bioKey: {
Type: schema.TypeString,
Computed: true,
Description: "The users bio.",
},
pgpKeyKey: {
Type: schema.TypeString,
Computed: true,
Description: "The users preferred PGP key.",
},
},
}
}
func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
user, err := config.metaClient.GetUser()
if err != nil {
return err
}
d.SetId(user.Name)
err = d.Set(userKey, user.Name)
if err != nil {
return err
}
err = d.Set(canonicalUserKey, user.CanonicalName)
if err != nil {
return err
}
err = d.Set(emailKey, user.Email)
if err != nil {
return err
}
err = d.Set(urlKey, user.URL)
if err != nil {
return err
}
err = d.Set(locationKey, user.Location)
if err != nil {
return err
}
err = d.Set(bioKey, user.Bio)
if err != nil {
return err
}
return d.Set(pgpKeyKey, user.UsePGPKey)
}