Skip to content

Commit f2e9e8a

Browse files
committed
Update docs, implement tests, finalize
1 parent ff92d8a commit f2e9e8a

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

docs/index.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ provider "restapi" {
6767
<a id="nestedblock--oauth_client_credentials"></a>
6868
### Nested Schema for `oauth_client_credentials`
6969

70+
NOTE: One of `oauth_client_id_environment_variable` and `oauth_client_secret_environment_variable` or `oauth_client_id` and `oauth_client_secret` MUST be set if this block is configured. If both are set environment variables take priority.
71+
7072
Required:
7173

72-
- `oauth_client_id` (String) client id
73-
- `oauth_client_secret` (String) client secret
7474
- `oauth_token_endpoint` (String) oauth token endpoint
7575

7676
Optional:
77-
77+
- `oauth_client_id_environment_variable` (String) client id
78+
- `oauth_client_secret_environment_variable` (String) client secret
79+
- `oauth_client_id` (String) client id
80+
- `oauth_client_secret` (String) client secret
7881
- `endpoint_params` (Map of String) Additional key/values to pass to the underlying Oauth client library (as EndpointParams)
7982
- `oauth_scopes` (List of String) scopes
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
provider "restapi" {
2+
alias = "restapi_oauth_env"
3+
uri = "https://graph.microsoft.com/beta/"
4+
write_returns_object = true
5+
debug = true
6+
7+
oauth_client_credentials {
8+
oauth_client_id_environment_variable = "ARM_CLIENT_ID"
9+
oauth_client_secret_environment_variable = "ARM_CLIENT_SECRET"
10+
oauth_token_endpoint = "https://login.microsoft.com/${var.tenantId}/oauth2/v2.0/token"
11+
oauth_scopes = ["https://graph.microsoft.com/.default"]
12+
endpoint_params = {"grant_type"="client_credentials"}
13+
}
14+
15+
headers = {
16+
"Content-Type" = "application/json"
17+
}
18+
19+
create_method = "PUT"
20+
update_method = "PATCH"
21+
destroy_method = "DELETE"
22+
}

restapi/api_client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type APIClient struct {
8686

8787
func GetEnvStringOrDefault(key, def string) string {
8888
if env := os.Getenv(key); env != "" {
89+
log.Printf("Got env for %s", key)
8990
return env
9091
}
9192
return def
@@ -219,8 +220,8 @@ func NewAPIClient(opt *apiClientOpt) (*APIClient, error) {
219220

220221
if resolvedClientID != "" && resolvedClientSecret != "" && opt.oauthTokenURL != "" {
221222
client.oauthConfig = &clientcredentials.Config{
222-
ClientID: opt.oauthClientID,
223-
ClientSecret: opt.oauthClientSecret,
223+
ClientID: resolvedClientID,
224+
ClientSecret: resolvedClientSecret,
224225
TokenURL: opt.oauthTokenURL,
225226
Scopes: opt.oauthScopes,
226227
EndpointParams: opt.oauthEndpointParams,

restapi/provider.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
278278
if v, ok := d.GetOk("oauth_client_credentials"); ok {
279279
oauthConfig := v.([]interface{})[0].(map[string]interface{})
280280

281+
if (oauthConfig["oauth_client_id_environment_variable"] == "" && oauthConfig["oauth_client_secret_environment_variable"] == "" && oauthConfig["oauth_client_id"] == "" && oauthConfig["oauth_client_secret"] == "") {
282+
return nil, fmt.Errorf("If configuring oauth, either `oauth_client_id_environment_variable`, `oauth_client_secret_environment_variable` OR `oauth_client_id` and `oauth_client_secret` must be specified")
283+
}
284+
if (oauthConfig["oauth_client_id_environment_variable"] != "" && oauthConfig["oauth_client_secret_environment_variable"] == "") {
285+
return nil, fmt.Errorf("`oauth_client_id_environment_variable` is configured, but `oauth_client_secret_environment_variable` is missing")
286+
}
287+
if (oauthConfig["oauth_client_id_environment_variable"] == "" && oauthConfig["oauth_client_secret_environment_variable"] != "") {
288+
return nil, fmt.Errorf("`oauth_client_secret_environment_variable` is configured, but `oauth_client_id_environment_variable` is missing")
289+
}
290+
if (oauthConfig["oauth_client_id"] != "" && oauthConfig["oauth_client_secret"] == "") {
291+
return nil, fmt.Errorf("`oauth_client_id` is configured, but `oauth_client_secret` is missing")
292+
}
293+
if (oauthConfig["oauth_client_id"] == "" && oauthConfig["oauth_client_secret"] != "") {
294+
return nil, fmt.Errorf("`oauth_client_secret` is configured, but `oauth_client_id` is missing")
295+
}
296+
281297
opt.oauthClientIDEnvVar = oauthConfig["oauth_client_id_environment_variable"].(string)
282298
opt.oauthClientSecretEnvVar = oauthConfig["oauth_client_secret_environment_variable"].(string)
283299
opt.oauthClientID = oauthConfig["oauth_client_id"].(string)

restapi/provider_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ func TestResourceProvider_Oauth(t *testing.T) {
6868
}
6969
}
7070

71+
func TestResourceProvider_Oauth_Env(t *testing.T) {
72+
rp := Provider()
73+
raw := map[string]interface{}{
74+
"uri": "http://foo.bar/baz",
75+
"oauth_client_credentials": map[string]interface{}{
76+
"oauth_client_id_environment_variable": "test",
77+
"oauth_client_secret_environment_variable": "secret",
78+
},
79+
}
80+
81+
/*
82+
XXX: This is expected to work even though we are not
83+
explicitly declaring the required url parameter since
84+
the test suite is run with the ENV entry set.
85+
*/
86+
err := rp.Configure(context.TODO(), terraform.NewResourceConfigRaw(raw))
87+
if err != nil {
88+
t.Fatalf("Provider failed with error: %v", err)
89+
}
90+
}
91+
7192
func TestResourceProvider_RequireTestPath(t *testing.T) {
7293
debug := false
7394
apiServerObjects := make(map[string]map[string]interface{})

0 commit comments

Comments
 (0)