diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2018-05-03 21:50:37 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2018-05-03 22:57:25 -0400 |
| commit | 6124d3dfd274ac24e5da0817ae172aca58c22d4e (patch) | |
| tree | 22acb5a6e7bbf59dd1337953445e036b06da94b5 /data | |
| parent | 195a6dc75ddbc6f4899176d006c6dfba39e08f1a (diff) | |
Adding datasourceReachable function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'data')
| -rw-r--r-- | data/datasource.go | 11 | ||||
| -rw-r--r-- | data/datasource_test.go | 27 |
2 files changed, 38 insertions, 0 deletions
diff --git a/data/datasource.go b/data/datasource.go index e4336fb5..6370bd5f 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -257,6 +257,17 @@ func (d *Data) Datasource(alias string, args ...string) (interface{}, error) { return nil, errors.Errorf("Datasources of type %s not yet supported", source.Type) } +// DatasourceReachable - Determines if the named datasource is reachable with +// the given arguments. Reads from the datasource, and discards the returned data. +func (d *Data) DatasourceReachable(alias string, args ...string) bool { + source, ok := d.Sources[alias] + if !ok { + return false + } + _, err := d.ReadSource(source, args...) + return err == nil +} + // Include - func (d *Data) Include(alias string, args ...string) (string, error) { source, ok := d.Sources[alias] diff --git a/data/datasource_test.go b/data/datasource_test.go index dbf1f0af..ab71a060 100644 --- a/data/datasource_test.go +++ b/data/datasource_test.go @@ -170,6 +170,33 @@ func TestDatasource(t *testing.T) { assert.Equal(t, "", actual) } +func TestDatasourceReachable(t *testing.T) { + fname := "foo.json" + fs := memfs.Create() + _ = fs.Mkdir("/tmp", 0777) + f, _ := vfs.Create(fs, "/tmp/"+fname) + _, _ = f.Write([]byte("{}")) + + sources := map[string]*Source{ + "foo": { + Alias: "foo", + URL: &url.URL{Scheme: "file", Path: "/tmp/" + fname}, + Ext: "json", + Type: "application/json", + FS: fs, + }, + "bar": { + Alias: "bar", + URL: &url.URL{Scheme: "file", Path: "/bogus"}, + FS: fs, + }, + } + data := &Data{Sources: sources} + + assert.True(t, data.DatasourceReachable("foo")) + assert.False(t, data.DatasourceReachable("bar")) +} + func TestDatasourceExists(t *testing.T) { sources := map[string]*Source{ "foo": {Alias: "foo"}, |
