forked from GoogleCloudPlatform/terraform-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update ancestry manager to use cfg (GoogleCloudPlatform#283)
* update ancestry manager to use cfg * fix tests, fork function * move credential test * remove split functionality, change original interface * abstract config out to retriever class * align coments with names * clean up
- Loading branch information
1 parent
638f7e2
commit c37093e
Showing
8 changed files
with
181 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cnvconfig | ||
|
||
import ( | ||
"context" | ||
|
||
converter "github.com/GoogleCloudPlatform/terraform-google-conversion/google" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func GetConfig(ctx context.Context, project string, offline bool) (*converter.Config, error) { | ||
cfg := &converter.Config{ | ||
Project: project, | ||
} | ||
|
||
// Search for default credentials | ||
cfg.Credentials = multiEnvSearch([]string{ | ||
"GOOGLE_CREDENTIALS", | ||
"GOOGLE_CLOUD_KEYFILE_JSON", | ||
"GCLOUD_KEYFILE_JSON", | ||
}) | ||
|
||
cfg.AccessToken = multiEnvSearch([]string{ | ||
"GOOGLE_OAUTH_ACCESS_TOKEN", | ||
}) | ||
|
||
if !offline { | ||
converter.ConfigureBasePaths(cfg) | ||
if err := cfg.LoadAndValidate(ctx); err != nil { | ||
return nil, errors.Wrap(err, "load and validate config") | ||
} | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cnvconfig | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
converter "github.com/GoogleCloudPlatform/terraform-google-conversion/google" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type configAttrGetter func(cfg *converter.Config) string | ||
|
||
func getCredentials(cfg *converter.Config) string { | ||
return cfg.Credentials | ||
} | ||
func getAccessToken(cfg *converter.Config) string { | ||
return cfg.AccessToken | ||
} | ||
|
||
func TestNewConverterCredentials(t *testing.T) { | ||
ctx := context.Background() | ||
offline := true | ||
cases := []struct { | ||
name string | ||
envKey string | ||
envValue string | ||
getConfigValue configAttrGetter | ||
}{ | ||
{ | ||
name: "GOOGLE_CREDENTIALS", | ||
envKey: "GOOGLE_CREDENTIALS", | ||
envValue: "whatever", | ||
getConfigValue: getCredentials, | ||
}, | ||
{ | ||
name: "GOOGLE_CLOUD_KEYFILE_JSON", | ||
envKey: "GOOGLE_CLOUD_KEYFILE_JSON", | ||
envValue: "whatever", | ||
getConfigValue: getCredentials, | ||
}, | ||
{ | ||
name: "GCLOUD_KEYFILE_JSON", | ||
envKey: "GCLOUD_KEYFILE_JSON", | ||
envValue: "whatever", | ||
getConfigValue: getCredentials, | ||
}, | ||
{ | ||
name: "GOOGLE_OAUTH_ACCESS_TOKEN", | ||
envKey: "GOOGLE_OAUTH_ACCESS_TOKEN", | ||
envValue: "whatever", | ||
getConfigValue: getAccessToken, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
originalValue, isSet := os.LookupEnv(c.envKey) | ||
err := os.Setenv(c.envKey, c.envValue) | ||
if err != nil { | ||
t.Fatalf("error setting env var %s=%s: %s", c.envKey, c.envValue, err) | ||
} | ||
|
||
cfg, err := GetConfig(ctx, "project", offline) | ||
if err != nil { | ||
t.Fatalf("error building converter: %s", err) | ||
} | ||
|
||
assert.EqualValues(t, c.getConfigValue(cfg), c.envValue) | ||
|
||
if isSet { | ||
err = os.Setenv(c.envKey, originalValue) | ||
if err != nil { | ||
t.Fatalf("error setting env var %s=%s: %s", c.envKey, originalValue, err) | ||
} | ||
} else { | ||
err = os.Unsetenv(c.envKey) | ||
if err != nil { | ||
t.Fatalf("error unsetting env var %s: %s", c.envKey, err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.