From 4cbf29fa5bd46cef0c2bb398a3e3977bb9dc6091 Mon Sep 17 00:00:00 2001 From: Elie Date: Wed, 30 Jun 2021 14:57:50 +0200 Subject: [PATCH] Add generic detail fetcher --- pkg/remote/common/detail_fetcher.go | 40 +++++++++++++++++++++++++++++ pkg/remote/common/library.go | 4 --- 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 pkg/remote/common/detail_fetcher.go diff --git a/pkg/remote/common/detail_fetcher.go b/pkg/remote/common/detail_fetcher.go new file mode 100644 index 00000000..940a4ec6 --- /dev/null +++ b/pkg/remote/common/detail_fetcher.go @@ -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 +} diff --git a/pkg/remote/common/library.go b/pkg/remote/common/library.go index 79781bf6..9e3c24ca 100644 --- a/pkg/remote/common/library.go +++ b/pkg/remote/common/library.go @@ -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