Skip to content

Commit

Permalink
Merge pull request #908 from cloudskiff/better_generic_detail_fetcher
Browse files Browse the repository at this point in the history
Allow to customize read attributes in detail fetcher
  • Loading branch information
eliecharra committed Aug 4, 2021
2 parents 45b9af8 + 18b8f89 commit a8f9b17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/remote/common/details_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ func NewGenericDetailsFetcher(resType resource.ResourceType, provider terraform.
}

func (f *GenericDetailsFetcher) ReadDetails(res resource.Resource) (resource.Resource, error) {
attributes := map[string]string{}
if res.Schema().ResolveReadAttributesFunc != nil {
abstractResource := res.(*resource.AbstractResource)
attributes = res.Schema().ResolveReadAttributesFunc(abstractResource)
}
ctyVal, err := f.reader.ReadResource(terraform.ReadResourceArgs{
Ty: f.resType,
ID: res.TerraformId(),
Ty: f.resType,
ID: res.TerraformId(),
Attributes: attributes,
})
if err != nil {
return nil, remoteerror.NewResourceScanningError(err, res.TerraformType(), res.TerraformId())
Expand Down
11 changes: 11 additions & 0 deletions pkg/resource/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Schema struct {
Attributes map[string]AttributeSchema
NormalizeFunc func(res *AbstractResource)
HumanReadableAttributesFunc func(res *AbstractResource) map[string]string
ResolveReadAttributesFunc func(res *AbstractResource) map[string]string
}

func (s *Schema) IsComputedField(path []string) bool {
Expand All @@ -43,6 +44,7 @@ type SchemaRepositoryInterface interface {
UpdateSchema(typ string, schemasMutators map[string]func(attributeSchema *AttributeSchema))
SetNormalizeFunc(typ string, normalizeFunc func(res *AbstractResource))
SetHumanReadableAttributesFunc(typ string, humanReadableAttributesFunc func(res *AbstractResource) map[string]string)
SetResolveReadAttributesFunc(typ string, resolveReadAttributesFunc func(res *AbstractResource) map[string]string)
}

type SchemaRepository struct {
Expand Down Expand Up @@ -134,3 +136,12 @@ func (r *SchemaRepository) SetHumanReadableAttributesFunc(typ string, humanReada
}
(*metadata).HumanReadableAttributesFunc = humanReadableAttributesFunc
}

func (r *SchemaRepository) SetResolveReadAttributesFunc(typ string, resolveReadAttributesFunc func(res *AbstractResource) map[string]string) {
metadata, exist := r.GetSchema(typ)
if !exist {
logrus.WithFields(logrus.Fields{"type": typ}).Warning("Unable to add read resource attributes, no schema found")
return
}
(*metadata).ResolveReadAttributesFunc = resolveReadAttributesFunc
}

0 comments on commit a8f9b17

Please sign in to comment.