Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize read attributes in detail fetcher #908

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}