forked from puppetlabs-toy-chest/wash
-
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.
Create MetadataJSONFile helper for exposing metadata as a JSON file
This is a common pattern, so provide a helper in the `plugin` module and use it for AWS, Docker, and GCP. Signed-off-by: Michael Smith <[email protected]>
- Loading branch information
1 parent
264e95d
commit a9efac8
Showing
9 changed files
with
102 additions
and
97 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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
package plugin | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
) | ||
|
||
// MetadataJSONFile represents a metadata.json file that contains another entry's metadata. | ||
type MetadataJSONFile struct { | ||
EntryBase | ||
other Entry | ||
} | ||
|
||
// NewMetadataJSONFile creates a new MetadataJSONFile. If caching Metadata on the `other` entry is | ||
// disabled, it will use that to compute the file size upfront. | ||
func NewMetadataJSONFile(ctx context.Context, other Entry) (*MetadataJSONFile, error) { | ||
meta := &MetadataJSONFile{ | ||
EntryBase: NewEntry("metadata.json"), | ||
other: other, | ||
} | ||
|
||
if other.getTTLOf(MetadataOp) < 0 { | ||
// Content is presumably easy to get, so use it to determine size. | ||
content, err := meta.Open(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
meta.Attributes().SetSize(uint64(content.Size())) | ||
} | ||
|
||
return meta, nil | ||
} | ||
|
||
// Schema defines the schema of a metadata.json file. | ||
func (m *MetadataJSONFile) Schema() *EntrySchema { | ||
return NewEntrySchema(m, "metadata.json").IsSingleton() | ||
} | ||
|
||
// Open returns the metadata of the `other` entry as its content. | ||
func (m *MetadataJSONFile) Open(ctx context.Context) (SizedReader, error) { | ||
meta, err := CachedMetadata(ctx, m.other) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
prettyMeta, err := json.MarshalIndent(meta, "", " ") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bytes.NewReader(prettyMeta), 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,24 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type basicEntry struct { | ||
EntryBase | ||
} | ||
|
||
func (e *basicEntry) Schema() *EntrySchema { | ||
return NewEntrySchema(e, "basic") | ||
} | ||
|
||
func TestMetadataJSONFile(t *testing.T) { | ||
basic := basicEntry{NewEntry("foo")} | ||
inst, err := NewMetadataJSONFile(context.Background(), &basic) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "metadata.json", inst.Name()) | ||
assert.Implements(t, (*Readable)(nil), inst) | ||
} |