-
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.
Refactor S3 client to make it unit-testable
- Loading branch information
Elliot Smith
committed
Feb 19, 2024
1 parent
6d1fe8b
commit 6e82be8
Showing
2 changed files
with
56 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package objectstore | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/s3/types" | ||
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared" | ||
) | ||
|
||
/** | ||
* For saving static copy of original LPA to S3 | ||
*/ | ||
type StaticLpaStorage struct { | ||
client *S3Client | ||
} | ||
|
||
func (sls *StaticLpaStorage) Save(lpa *shared.Lpa) error { | ||
// save a copy of the original to permanent storage, | ||
// but only if the key doesn't already exist | ||
objectKey := fmt.Sprintf("%s/donor-executed-lpa.json", lpa.Uid) | ||
_, err := sls.client.Get(objectKey) | ||
|
||
if err == nil { | ||
// no error and 200 => bad (object already exists) | ||
return fmt.Errorf("Could not save donor executed LPA as key %s already exists", objectKey) | ||
} | ||
|
||
// error which is a 404 => good (object should not already exist) | ||
var nsk *types.NoSuchKey | ||
if errors.As(err, &nsk) { | ||
_, err = sls.client.Put(objectKey, lpa) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func NewStaticLpaStorage(client *S3Client) *StaticLpaStorage { | ||
return &StaticLpaStorage{ | ||
client: client, | ||
} | ||
} |
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