corso/src/pkg/storage/storage.go
neha_gupta 9f334f7d30
remove getting s3 config from envvars (#3757)
<!-- PR description-->

S3 configs like BUCKET and PREFIX are common env vars and can cause overkills. Remove these S3 configs to avoid confusion.

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #<issue>

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
2023-07-07 06:15:18 +00:00

84 lines
1.9 KiB
Go

package storage
import (
"fmt"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/internal/common"
)
type storageProvider int
//go:generate stringer -type=storageProvider -linecomment
const (
ProviderUnknown storageProvider = iota // Unknown Provider
ProviderS3 // S3
)
// storage parsing errors
var (
errMissingRequired = clues.New("missing required storage configuration")
)
// Storage defines a storage provider, along with any configuration
// required to set up or communicate with that provider.
type Storage struct {
Provider storageProvider
Config map[string]string
// TODO: These are AWS S3 specific -> move these out
SessionTags map[string]string
Role string
SessionName string
SessionDuration string
}
// NewStorage aggregates all the supplied configurations into a single configuration.
func NewStorage(p storageProvider, cfgs ...common.StringConfigurer) (Storage, error) {
cs, err := common.UnionStringConfigs(cfgs...)
return Storage{
Provider: p,
Config: cs,
}, err
}
// NewStorageUsingRole supports specifying an AWS IAM role the storage provider
// should assume.
func NewStorageUsingRole(
p storageProvider,
roleARN string,
sessionName string,
sessionTags map[string]string,
duration string,
cfgs ...common.StringConfigurer,
) (Storage, error) {
cs, err := common.UnionStringConfigs(cfgs...)
return Storage{
Provider: p,
Config: cs,
Role: roleARN,
SessionTags: sessionTags,
SessionName: sessionName,
SessionDuration: duration,
}, err
}
// Helper for parsing the values in a config object.
// If the value is nil or not a string, returns an empty string.
func orEmptyString(v any) string {
defer func() {
r := recover()
if r != nil {
fmt.Printf("panic recovery casting %v to string\n", v)
}
}()
if v == nil {
return ""
}
return v.(string)
}