driftctl/pkg/resource/resource.go

59 lines
1.2 KiB
Go
Raw Normal View History

package resource
import (
"encoding/json"
"sort"
)
type Resource interface {
TerraformId() string
TerraformType() string
}
type SerializableResource struct {
Resource
}
type SerializedResource struct {
Id string `json:"id"`
Type string `json:"type"`
}
func (u SerializedResource) TerraformId() string {
return u.Id
}
func (u SerializedResource) TerraformType() string {
return u.Type
}
func (s *SerializableResource) UnmarshalJSON(bytes []byte) error {
var res SerializedResource
if err := json.Unmarshal(bytes, &res); err != nil {
return err
}
s.Resource = res
return nil
}
func (s SerializableResource) MarshalJSON() ([]byte, error) {
return json.Marshal(SerializedResource{Id: s.TerraformId(), Type: s.TerraformType()})
}
type NormalizedResource interface {
NormalizeForState() (Resource, error)
NormalizeForProvider() (Resource, error)
}
func IsSameResource(rRs, lRs Resource) bool {
return rRs.TerraformType() == lRs.TerraformType() && rRs.TerraformId() == lRs.TerraformId()
}
func Sort(res []Resource) []Resource {
2021-03-22 10:17:50 +00:00
sort.SliceStable(res, func(i, j int) bool {
2021-03-22 10:35:23 +00:00
return res[i].TerraformType() < res[j].TerraformType() || res[i].TerraformId() < res[j].TerraformId()
2021-03-22 10:17:50 +00:00
})
return res
}