Skip to content

Commit ff92d8a

Browse files
committed
Working commit
1 parent fd4e68b commit ff92d8a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

restapi/api_client.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type apiClientOpt struct {
4343
xssiPrefix string
4444
useCookies bool
4545
rateLimit float64
46+
oauthClientIDEnvVar string
47+
oauthClientSecretEnvVar string
4648
oauthClientID string
4749
oauthClientSecret string
4850
oauthScopes []string
@@ -82,6 +84,13 @@ type APIClient struct {
8284
oauthConfig *clientcredentials.Config
8385
}
8486

87+
func GetEnvStringOrDefault(key, def string) string {
88+
if env := os.Getenv(key); env != "" {
89+
return env
90+
}
91+
return def
92+
}
93+
8594
// NewAPIClient makes a new api client for RESTful calls
8695
func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
8796
if opt.debug {
@@ -205,7 +214,10 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
205214
debug: opt.debug,
206215
}
207216

208-
if opt.oauthClientID != "" && opt.oauthClientSecret != "" && opt.oauthTokenURL != "" {
217+
resolvedClientID := GetEnvStringOrDefault(opt.oauthClientIDEnvVar, opt.oauthClientID)
218+
resolvedClientSecret := GetEnvStringOrDefault(opt.oauthClientSecretEnvVar, opt.oauthClientSecret)
219+
220+
if resolvedClientID != "" && resolvedClientSecret != "" && opt.oauthTokenURL != "" {
209221
client.oauthConfig = &clientcredentials.Config{
210222
ClientID: opt.oauthClientID,
211223
ClientSecret: opt.oauthClientSecret,

restapi/provider.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,25 @@ func Provider() *schema.Provider {
135135
Description: "Configuration for oauth client credential flow using the https://pkg.go.dev/golang.org/x/oauth2 implementation",
136136
Elem: &schema.Resource{
137137
Schema: map[string]*schema.Schema{
138+
"oauth_client_id_environment_variable": {
139+
Type: schema.TypeString,
140+
Description: "Name of client id environment variable, when using an environment variable to specify the client id",
141+
Optional: true,
142+
},
143+
"oauth_client_secret_environment_variable": {
144+
Type: schema.TypeString,
145+
Description: "Name of client secret environment variable, when using an environment variable to specify the client id",
146+
Optional: true,
147+
},
138148
"oauth_client_id": {
139149
Type: schema.TypeString,
140150
Description: "client id",
141-
Required: true,
151+
Optional: true,
142152
},
143153
"oauth_client_secret": {
144154
Type: schema.TypeString,
145155
Description: "client secret",
146-
Required: true,
156+
Optional: true,
147157
},
148158
"oauth_token_endpoint": {
149159
Type: schema.TypeString,
@@ -214,6 +224,7 @@ func Provider() *schema.Provider {
214224
"restapi_object": dataSourceRestAPI(),
215225
},
216226
ConfigureFunc: configureProvider,
227+
217228
}
218229
}
219230

@@ -267,6 +278,8 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
267278
if v, ok := d.GetOk("oauth_client_credentials"); ok {
268279
oauthConfig := v.([]interface{})[0].(map[string]interface{})
269280

281+
opt.oauthClientIDEnvVar = oauthConfig["oauth_client_id_environment_variable"].(string)
282+
opt.oauthClientSecretEnvVar = oauthConfig["oauth_client_secret_environment_variable"].(string)
270283
opt.oauthClientID = oauthConfig["oauth_client_id"].(string)
271284
opt.oauthClientSecret = oauthConfig["oauth_client_secret"].(string)
272285
opt.oauthTokenURL = oauthConfig["oauth_token_endpoint"].(string)

0 commit comments

Comments
 (0)