forked from raystack/guardian
/
resource.go
145 lines (125 loc) · 3.43 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package model
import (
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/goto/guardian/domain"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Resource is the database model for resource
type Resource struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
ParentID *string `gorm:"type:uuid"`
ProviderType string `gorm:"uniqueIndex:resource_index"`
ProviderURN string `gorm:"uniqueIndex:resource_index"`
Type string `gorm:"uniqueIndex:resource_index"`
URN string `gorm:"uniqueIndex:resource_index"`
Name string
Details datatypes.JSON
Labels datatypes.JSON
GlobalURN *string `gorm:"uniqueIndex:resource_global_urn"`
Children []Resource `gorm:"ForeignKey:ParentID;References:ID"`
Provider Provider `gorm:"ForeignKey:ProviderType,ProviderURN;References:Type,URN"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index"`
IsDeleted bool
}
// TableName overrides the table name
func (Resource) TableName() string {
return "resources"
}
func (r *Resource) BeforeCreate(tx *gorm.DB) error {
tx.Statement.AddClause(clause.OnConflict{
Columns: []clause.Column{
{Name: "provider_type"},
{Name: "provider_urn"},
{Name: "type"},
{Name: "urn"},
},
DoUpdates: clause.AssignmentColumns([]string{"name", "details", "updated_at", "is_deleted", "parent_id", "global_urn"}),
})
return nil
}
// FromDomain uses *domain.Resource values as the model values
func (m *Resource) FromDomain(r *domain.Resource) error {
details, err := json.Marshal(r.Details)
if err != nil {
return err
}
labels, err := json.Marshal(r.Labels)
if err != nil {
return err
}
if r.ID != "" {
uuid, err := uuid.Parse(r.ID)
if err == nil {
m.ID = uuid
}
}
m.ParentID = r.ParentID
m.ProviderType = r.ProviderType
m.ProviderURN = r.ProviderURN
m.Type = r.Type
m.URN = r.URN
m.Name = r.Name
m.Details = datatypes.JSON(details)
m.Labels = datatypes.JSON(labels)
m.CreatedAt = r.CreatedAt
m.UpdatedAt = r.UpdatedAt
m.IsDeleted = r.IsDeleted
if r.GlobalURN != "" {
m.GlobalURN = &r.GlobalURN
}
if r.Children != nil && len(r.Children) > 0 {
m.Children = make([]Resource, len(r.Children))
for i, child := range r.Children {
if err := m.Children[i].FromDomain(child); err != nil {
return fmt.Errorf("parsing child: %w", err)
}
}
}
return nil
}
// ToDomain transforms model into *domain.Provider
func (m *Resource) ToDomain() (*domain.Resource, error) {
r := &domain.Resource{
ID: m.ID.String(),
ParentID: m.ParentID,
ProviderType: m.ProviderType,
ProviderURN: m.ProviderURN,
Type: m.Type,
URN: m.URN,
Name: m.Name,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
IsDeleted: m.IsDeleted,
}
if m.GlobalURN != nil {
r.GlobalURN = *m.GlobalURN
}
if m.Details != nil {
if err := json.Unmarshal(m.Details, &r.Details); err != nil {
return nil, err
}
}
if m.Labels != nil {
if err := json.Unmarshal(m.Labels, &r.Labels); err != nil {
return nil, err
}
}
if m.Children != nil && len(m.Children) > 0 {
r.Children = make([]*domain.Resource, len(m.Children))
for i, child := range m.Children {
child, err := child.ToDomain()
if err != nil {
return nil, fmt.Errorf("parsing child: %w", err)
}
r.Children[i] = child
}
}
return r, nil
}