-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement loading from URI for Profile, Device, & Provision Wat…
…cher files (#1471) Closes #1467 Signed-off-by: Elizabeth J Lee <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,027 additions
and
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
// | ||
// # Copyright (C) 2023 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package provision | ||
|
||
import ( | ||
"github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/utils" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
"net/url" | ||
"path" | ||
"strings" | ||
) | ||
|
||
type FileType int | ||
|
||
const ( | ||
YAML FileType = iota | ||
JSON | ||
OTHER | ||
) | ||
|
||
func GetFileType(fullPath string) FileType { | ||
if strings.HasSuffix(fullPath, yamlExt) || strings.HasSuffix(fullPath, ymlExt) { | ||
return YAML | ||
} else if strings.HasSuffix(fullPath, ".json") { | ||
return JSON | ||
} else { | ||
return OTHER | ||
} | ||
} | ||
|
||
func GetFullAndRedactedURI(baseURI *url.URL, file, description string, lc logger.LoggingClient) (string, string) { | ||
basePath, _ := path.Split(baseURI.Path) | ||
newPath, err := url.JoinPath(basePath, file) | ||
if err != nil { | ||
lc.Error("could not join URI path for %s %s/%s: %v", description, basePath, file, err) | ||
return "", "" | ||
} | ||
var fullURI url.URL | ||
err = utils.DeepCopy(baseURI, &fullURI) | ||
if err != nil { | ||
lc.Error("could not copy URI for %s %s: %v", description, newPath, err) | ||
return "", "" | ||
} | ||
fullURI.User = baseURI.User | ||
fullURI.Path = newPath | ||
return fullURI.String(), fullURI.Redacted() | ||
} |
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,57 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
// | ||
// # Copyright (C) 2023 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package provision | ||
|
||
import ( | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"net/url" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func Test_GetFileType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
expectedFileType FileType | ||
}{ | ||
{"valid get Yaml file type", path.Join("..", "..", "example", "cmd", "device-simple", "res", "devices", "simple-device.yml"), YAML}, | ||
{"valid get Json file type", path.Join("..", "..", "example", "cmd", "device-simple", "res", "devices", "simple-device.json"), JSON}, | ||
{"valid get other file type", path.Join("..", "..", "example", "cmd", "device-simple", "res", "devices", "simple-device.bogus"), OTHER}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actualType := GetFileType(tt.path) | ||
assert.Equal(t, tt.expectedFileType, actualType) | ||
}) | ||
} | ||
} | ||
|
||
func Test_GetFullAndRedactedURI(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
baseURI string | ||
file string | ||
expectedURI string | ||
expectedRedacted string | ||
}{ | ||
{"valid no secret uri", "https://raw.githubusercontent.com/edgexfoundry/device-virtual-go/main/cmd/res/devices/devices.yaml", "device-simple.yaml", "https://raw.githubusercontent.com/edgexfoundry/device-virtual-go/main/cmd/res/devices/device-simple.yaml", "https://raw.githubusercontent.com/edgexfoundry/device-virtual-go/main/cmd/res/devices/device-simple.yaml"}, | ||
{"valid query secret uri", "https://raw.githubusercontent.com/edgexfoundry/device-simple/main/devices/index.json?edgexSecretName=githubCredentials", "device-simple.yaml", "https://raw.githubusercontent.com/edgexfoundry/device-simple/main/devices/device-simple.yaml?edgexSecretName=githubCredentials", "https://raw.githubusercontent.com/edgexfoundry/device-simple/main/devices/device-simple.yaml?edgexSecretName=githubCredentials"}, | ||
{"valid query secret uri", "https://myuser:[email protected]/edgexfoundry/device-simple/main/devices/index.json", "device-simple.yaml", "https://myuser:[email protected]/edgexfoundry/device-simple/main/devices/device-simple.yaml", "https://myuser:[email protected]/edgexfoundry/device-simple/main/devices/device-simple.yaml"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
testURI, err := url.Parse(tt.baseURI) | ||
require.NoError(t, err) | ||
lc := logger.MockLogger{} | ||
actualURI, actualRedacted := GetFullAndRedactedURI(testURI, tt.file, "test", lc) | ||
assert.Equal(t, tt.expectedURI, actualURI) | ||
assert.Equal(t, tt.expectedRedacted, actualRedacted) | ||
}) | ||
} | ||
} |
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.