-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
158 lines (144 loc) · 3.99 KB
/
provider.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
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-FileCopyrightText: 2024 Dominik Wombacher <[email protected]>
// SPDX-FileCopyrightText: 2019 The SourceHut API Contributors
//
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"fmt"
"os"
"git.sr.ht/~wombelix/sourcehut-go"
"git.sr.ht/~wombelix/sourcehut-go/git"
"git.sr.ht/~wombelix/sourcehut-go/meta"
"git.sr.ht/~wombelix/sourcehut-go/paste"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// All config
/* #nosec */
tokenKey = "token"
/* #nosec */
tokenEnv = "SRHT_TOKEN"
// Common key names
idKey = "id"
createdKey = "created"
createdTimestampKey = "created_unix"
userKey = "user"
canonicalUserKey = "canonical_user"
// Meta config
metaURLKey = "meta_url"
metaURLEnv = "SRHT_META_URL"
metaURLDef = "https://meta.sr.ht/api"
// Paste config
pasteURLKey = "paste_url"
pasteURLEnv = "SRHT_PASTE_URL"
pasteURLDef = "https://paste.sr.ht/api"
// Git config
gitURLKey = "git_url"
gitURLEnv = "SRHT_GIT_URL"
gitURLDef = "https://git.sr.ht/api"
)
func provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
metaURLKey: {
Type: schema.TypeString,
Optional: true,
Default: metaURLDef,
Description: fmt.Sprintf(
`The URL to the SourceHut Meta API endpoint. It is required if using
a private installation of SourceHut. The default is to use the cloud
paste service. It can be provided via the %s environment variable.`,
metaURLEnv),
},
pasteURLKey: {
Type: schema.TypeString,
Optional: true,
Default: pasteURLDef,
Description: fmt.Sprintf(
`The URL to the SourceHut Paste API endpoint. It is required if using
a private installation of SourceHut. The default is to use the cloud
paste service. It can be provided via the %s environment variable.`,
pasteURLEnv),
},
gitURLKey: {
Type: schema.TypeString,
Optional: true,
Default: gitURLDef,
Description: fmt.Sprintf(
`The URL to the SourceHut Paste API endpoint. It is required if using
a private installation of SourceHut. The default is to use the cloud
git service. It can be provided via the %s environment variable.`,
gitURLEnv),
},
tokenKey: {
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf(
`A SourceHut API personal access token. It is required to use most
resources. It can be provided via the %s environment variable.`,
tokenEnv),
},
},
ResourcesMap: map[string]*schema.Resource{
sshKeyName: resourceSSHKey(),
pgpKeyName: resourcePGPKey(),
repoName: resourceRepo(),
},
DataSourcesMap: map[string]*schema.Resource{
pasteName: dataSourcePaste(),
blobName: dataSourceBlob(),
userName: dataSourceUser(),
repoName: dataSourceRepo(),
},
ConfigureFunc: configureProvider,
}
}
func configureProvider(d *schema.ResourceData) (interface{}, error) {
srhtClient := sourcehut.NewClient(
sourcehut.Token(dataOrEnv(d, tokenKey, tokenEnv)),
sourcehut.UserAgent("git.sr.ht/~wombelix/terraform-provider-sourcehut"),
)
pasteClient, err := paste.NewClient(
paste.SrhtClient(srhtClient),
paste.Base(dataOrEnv(d, pasteURLKey, pasteURLEnv)),
)
if err != nil {
return nil, err
}
metaClient, err := meta.NewClient(
meta.SrhtClient(srhtClient),
meta.Base(dataOrEnv(d, metaURLKey, metaURLEnv)),
)
if err != nil {
return nil, err
}
gitClient, err := git.NewClient(
git.SrhtClient(srhtClient),
git.Base(dataOrEnv(d, gitURLKey, gitURLEnv)),
)
if err != nil {
return nil, err
}
return config{
srhtClient: srhtClient,
metaClient: metaClient,
pasteClient: pasteClient,
gitClient: gitClient,
}, nil
}
type config struct {
srhtClient sourcehut.Client
metaClient *meta.Client
pasteClient *paste.Client
gitClient *git.Client
}
func dataOrEnv(d *schema.ResourceData, key, env string) string {
var ret string
if v, ok := d.Get(key).(string); ok {
ret = v
}
if ret == "" {
ret = os.Getenv(env)
}
return ret
}