Skip to content

Commit

Permalink
Alter BlobManager interface
Browse files Browse the repository at this point in the history
Allow constructor to return an error so we can move repetetive work
there.

[161569370](https://www.pivotaltracker.com/story/show/161569370)

Co-authored-by: Christopher Brown <[email protected]>
  • Loading branch information
2 people authored and Your Name committed Nov 27, 2018
1 parent aa9dc4e commit 1cc0187
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
27 changes: 6 additions & 21 deletions agent/blobstore/blob_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ type BlobManager struct {
workdir string
}

func NewBlobManager(fs boshsys.FileSystem, workdir string) BlobManager {
return BlobManager{
func NewBlobManager(workdir string) (*BlobManager, error) {
bm := &BlobManager{
workdir: workdir,
}
if err := bm.createDirStructure(); err != nil {
return nil, err
}
return bm, nil
}

func (m BlobManager) Fetch(blobID string) (boshsys.File, int, error) {
if err := m.createDirStructure(); err != nil {
return nil, 500, err
}

file, err := os.Open(m.blobPath(blobID))
if err != nil {
return nil, statusForErr(err), bosherr.WrapError(err, "Reading blob")
Expand All @@ -36,10 +36,6 @@ func (m BlobManager) Fetch(blobID string) (boshsys.File, int, error) {
}

func (m BlobManager) Write(blobID string, r io.Reader) error {
if err := m.createDirStructure(); err != nil {
return err
}

blobPath := m.blobPath(blobID)
file, err := os.OpenFile(blobPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
Expand All @@ -55,10 +51,6 @@ func (m BlobManager) Write(blobID string, r io.Reader) error {
}

func (m BlobManager) GetPath(blobID string, digest boshcrypto.Digest) (string, error) {
if err := m.createDirStructure(); err != nil {
return "", err
}

if !m.BlobExists(blobID) {
return "", bosherr.Errorf("Blob '%s' not found", blobID)
}
Expand All @@ -82,17 +74,10 @@ func (m BlobManager) GetPath(blobID string, digest boshcrypto.Digest) (string, e
}

func (m BlobManager) Delete(blobID string) error {
if err := m.createDirStructure(); err != nil {
return err
}
return os.RemoveAll(m.blobPath(blobID))
}

func (m BlobManager) BlobExists(blobID string) bool {
if err := m.createDirStructure(); err != nil {
return false
}

_, err := os.Stat(m.blobPath(blobID))
return !os.IsNotExist(err)
}
Expand Down
3 changes: 2 additions & 1 deletion agent/blobstore/blob_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var _ = Describe("Blob Manager", func() {
basePath, err = ioutil.TempDir("", "blobmanager")
Expect(err).NotTo(HaveOccurred())

blobManager = NewBlobManager(fs, basePath)
blobManager, err = boshagentblobstore.NewBlobManager(basePath)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
Expand Down
7 changes: 5 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ func (app *app) Setup(opts Options) error {
return bosherr.WrapError(err, "Running bootstrap")
}

blobManager := boshagentblobstore.NewBlobManager(app.platform.GetFs(), app.dirProvider.BlobsDir())
blobstore, err := app.setupBlobstore(settingsService.GetSettings().GetBlobstore(), blobManager)
blobManager, err := boshagentblobstore.NewBlobManager(app.dirProvider.BlobsDir())
if err != nil {
return bosherr.WrapError(err, "Getting blob manager")
}

blobstore, err := app.setupBlobstore(settingsService.GetSettings().GetBlobstore(), blobManager)
if err != nil {
return bosherr.WrapError(err, "Getting blobstore")
}
Expand Down
3 changes: 2 additions & 1 deletion mbus/https_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var _ = Describe("HTTPSHandler", func() {
Expect(err).NotTo(HaveOccurred())

serverURL = "https://user:pass@localhost:6900"
blobManager = boshagentblobstore.NewBlobManager(nil, tmpdir)
blobManager, err = boshagentblobstore.NewBlobManager(tmpdir)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
Expand Down

0 comments on commit 1cc0187

Please sign in to comment.