2020-12-09 15:31:34 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-03-19 18:20:57 +00:00
|
|
|
"sort"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2021-03-19 18:20:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
})
|
2021-03-19 18:20:57 +00:00
|
|
|
return res
|
|
|
|
}
|