driftctl/pkg/remote/common/details_fetcher.go

55 lines
1.6 KiB
Go
Raw Normal View History

2021-06-30 12:57:50 +00:00
package common
import (
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
2021-06-30 12:57:50 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
2021-07-06 15:08:47 +00:00
"github.com/sirupsen/logrus"
2021-06-30 12:57:50 +00:00
)
type DetailsFetcher interface {
2021-08-09 14:03:04 +00:00
ReadDetails(*resource.Resource) (*resource.Resource, error)
2021-06-30 12:57:50 +00:00
}
2021-07-01 15:11:25 +00:00
type GenericDetailsFetcher struct {
2021-06-30 12:57:50 +00:00
resType resource.ResourceType
reader terraform.ResourceReader
deserializer *resource.Deserializer
}
2021-07-01 15:11:25 +00:00
func NewGenericDetailsFetcher(resType resource.ResourceType, provider terraform.ResourceReader, deserializer *resource.Deserializer) *GenericDetailsFetcher {
return &GenericDetailsFetcher{
2021-06-30 12:57:50 +00:00
resType: resType,
reader: provider,
deserializer: deserializer,
}
}
2021-08-09 14:03:04 +00:00
func (f *GenericDetailsFetcher) ReadDetails(res *resource.Resource) (*resource.Resource, error) {
attributes := map[string]string{}
if res.Schema().ResolveReadAttributesFunc != nil {
2021-08-09 14:03:04 +00:00
attributes = res.Schema().ResolveReadAttributesFunc(res)
}
2021-06-30 12:57:50 +00:00
ctyVal, err := f.reader.ReadResource(terraform.ReadResourceArgs{
Ty: f.resType,
ID: res.TerraformId(),
Attributes: attributes,
2021-06-30 12:57:50 +00:00
})
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceScanningError(err, res.TerraformType(), res.TerraformId())
2021-06-30 12:57:50 +00:00
}
2021-07-06 15:08:47 +00:00
if ctyVal.IsNull() {
logrus.WithFields(logrus.Fields{
"type": f.resType,
"id": res.TerraformId(),
}).Debug("Got null while reading resource details")
return nil, nil
}
2021-06-30 12:57:50 +00:00
deserializedRes, err := f.deserializer.DeserializeOne(string(f.resType), *ctyVal)
if err != nil {
return nil, err
}
return deserializedRes, nil
}