driftctl/pkg/terraform/resource_factory.go

36 lines
940 B
Go
Raw Normal View History

2021-03-29 16:10:50 +00:00
package terraform
import (
2021-04-29 15:17:55 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
2021-03-29 16:10:50 +00:00
)
type TerraformResourceFactory struct {
2021-05-04 13:03:17 +00:00
resourceSchemaRepository resource.SchemaRepositoryInterface
2021-03-29 16:10:50 +00:00
}
2021-05-21 14:09:45 +00:00
func NewTerraformResourceFactory(resourceSchemaRepository resource.SchemaRepositoryInterface) *TerraformResourceFactory {
2021-04-29 15:17:55 +00:00
return &TerraformResourceFactory{
resourceSchemaRepository: resourceSchemaRepository,
}
2021-03-29 16:10:50 +00:00
}
2021-05-04 13:03:17 +00:00
func (r *TerraformResourceFactory) CreateAbstractResource(ty, id string, data map[string]interface{}) *resource.AbstractResource {
attributes := resource.Attributes(data)
2021-05-11 08:53:39 +00:00
attributes.SanitizeDefaults()
2021-04-29 15:17:55 +00:00
2021-05-21 14:09:45 +00:00
schema, _ := r.resourceSchemaRepository.GetSchema(ty)
res := resource.AbstractResource{
2021-04-29 15:17:55 +00:00
Id: id,
Type: ty,
2021-05-04 13:03:17 +00:00
Attrs: &attributes,
2021-05-21 14:09:45 +00:00
Sch: schema,
2021-04-29 15:17:55 +00:00
}
schema, exist := r.resourceSchemaRepository.(*resource.SchemaRepository).GetSchema(ty)
if exist && schema.NormalizeFunc != nil {
schema.NormalizeFunc(&res)
}
return &res
2021-04-29 15:17:55 +00:00
}