Add generic detail fetcher

main
Elie 2021-06-30 14:57:50 +02:00
parent d7d6807965
commit 4cbf29fa5b
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
2 changed files with 40 additions and 4 deletions

View File

@ -0,0 +1,40 @@
package common
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type DetailsFetcher interface {
ReadDetails(resource.Resource) (resource.Resource, error)
}
type GenericDetailFetcher struct {
resType resource.ResourceType
reader terraform.ResourceReader
deserializer *resource.Deserializer
}
func NewGenericDetailFetcher(resType resource.ResourceType, provider terraform.ResourceReader, deserializer *resource.Deserializer) *GenericDetailFetcher {
return &GenericDetailFetcher{
resType: resType,
reader: provider,
deserializer: deserializer,
}
}
func (f *GenericDetailFetcher) ReadDetails(res resource.Resource) (resource.Resource, error) {
ctyVal, err := f.reader.ReadResource(terraform.ReadResourceArgs{
Ty: f.resType,
ID: res.TerraformId(),
})
if err != nil {
return nil, err
}
deserializedRes, err := f.deserializer.DeserializeOne(string(f.resType), *ctyVal)
if err != nil {
return nil, err
}
return deserializedRes, nil
}

View File

@ -9,10 +9,6 @@ type Enumerator interface {
Enumerate() ([]resource.Resource, error)
}
type DetailsFetcher interface {
ReadDetails(resource.Resource) (resource.Resource, error)
}
type RemoteLibrary struct {
enumerators []Enumerator
detailsFetchers map[resource.ResourceType]DetailsFetcher