2020-12-09 15:31:34 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2021-03-29 16:10:50 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 13:24:20 +00:00
|
|
|
AWS string = "aws"
|
|
|
|
GITHUB string = "github"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
type ProviderLibrary struct {
|
|
|
|
providers map[string]TerraformProvider
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
func NewProviderLibrary() *ProviderLibrary {
|
|
|
|
logrus.Debug("New provider library created")
|
|
|
|
return &ProviderLibrary{
|
|
|
|
make(map[string]TerraformProvider),
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
func (p *ProviderLibrary) AddProvider(name string, provider TerraformProvider) {
|
|
|
|
p.providers[name] = provider
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
func (p *ProviderLibrary) Provider(name string) TerraformProvider {
|
|
|
|
return p.providers[name]
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
func (p *ProviderLibrary) Cleanup() {
|
|
|
|
logrus.Debug("Closing providers")
|
|
|
|
for providerKey, provider := range p.providers {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"key": providerKey,
|
|
|
|
}).Debug("Closing provider")
|
|
|
|
provider.Cleanup()
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
func (p *ProviderLibrary) GetProviderForResourceType(resType string) (TerraformProvider, error) {
|
|
|
|
|
|
|
|
var name string
|
|
|
|
|
|
|
|
if strings.HasPrefix(resType, AWS) {
|
|
|
|
name = AWS
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(resType, GITHUB) {
|
|
|
|
name = GITHUB
|
|
|
|
}
|
|
|
|
|
|
|
|
if name != "" {
|
|
|
|
return p.Provider(name), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Unable to resolve provider for resource")
|
|
|
|
}
|