Merge pull request #908 from cloudskiff/better_generic_detail_fetcher

Allow to customize read attributes in detail fetcher
main
Elie 2021-08-04 14:36:39 +02:00 committed by GitHub
commit a8f9b1764e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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())

View File

@ -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 {
@ -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 {
@ -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
}