-
Notifications
You must be signed in to change notification settings - Fork 19
/
model.go
148 lines (126 loc) · 3.36 KB
/
model.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
146
147
148
package bigquery
import (
"fmt"
"strings"
bq "cloud.google.com/go/bigquery"
"github.com/raystack/guardian/core/resource"
"github.com/raystack/guardian/domain"
)
const (
// ResourceTypeDataset is the resource type name for BigQuery dataset
ResourceTypeDataset = "dataset"
// ResourceTypeTable is the resource type name for BigQuery table
ResourceTypeTable = "table"
)
// Dataset is a reference to a BigQuery dataset
type Dataset struct {
ProjectID string
DatasetID string
Labels map[string]string
}
func (d *Dataset) FromDomain(r *domain.Resource) error {
if r.Type != ResourceTypeDataset {
return ErrInvalidResourceType
}
d.ProjectID = strings.TrimSuffix(r.URN, fmt.Sprintf(":%s", r.Name))
d.DatasetID = r.Name
return nil
}
func (d *Dataset) ToDomain() *domain.Resource {
r := &domain.Resource{
Type: ResourceTypeDataset,
Name: d.DatasetID,
URN: fmt.Sprintf("%s:%s", d.ProjectID, d.DatasetID),
}
if d.Labels != nil {
if r.Details == nil {
r.Details = make(map[string]interface{})
}
r.Details[resource.ReservedDetailsKeyMetadata] = map[string]interface{}{
"labels": d.Labels,
}
}
return r
}
// Table is a reference to a BigQuery table
type Table struct {
ProjectID string
DatasetID string
TableID string
Labels map[string]string
}
func (t *Table) FromDomain(r *domain.Resource) error {
if r.Type != ResourceTypeTable {
return ErrInvalidResourceType
}
datasetURN := strings.Split(strings.TrimSuffix(r.URN, fmt.Sprintf(".%s", r.Name)), ":")
if len(datasetURN) != 2 {
return ErrInvalidTableURN
}
t.ProjectID = datasetURN[0]
t.DatasetID = datasetURN[1]
t.TableID = r.Name
return nil
}
func (t *Table) ToDomain() *domain.Resource {
r := &domain.Resource{
Type: ResourceTypeTable,
Name: t.TableID,
URN: fmt.Sprintf("%s:%s.%s", t.ProjectID, t.DatasetID, t.TableID),
}
if t.Labels != nil {
if r.Details == nil {
r.Details = make(map[string]interface{})
}
r.Details[resource.ReservedDetailsKeyMetadata] = map[string]interface{}{
"labels": t.Labels,
}
}
return r
}
type datasetAccessEntry bq.AccessEntry
func (ae datasetAccessEntry) getEntityType() string {
switch bq.AccessEntry(ae).EntityType {
case bq.UserEmailEntity:
if strings.Contains(bq.AccessEntry(ae).Entity, "gserviceaccount.com") {
return AccountTypeServiceAccount
}
return AccountTypeUser
default:
return ""
}
}
// BigQueryResourceName is a string representation of bigquery resource's Relative Resource Name.
// Example: "projects/project-id/datasets/dataset_name/tables/table_name"
type BigQueryResourceName string
func (r BigQueryResourceName) ProjectID() string {
items := strings.Split(string(r), "/")
if len(items) >= 2 {
return items[1]
}
return ""
}
func (r BigQueryResourceName) DatasetID() string {
items := strings.Split(string(r), "/")
if len(items) >= 4 {
return items[3]
}
return ""
}
func (r BigQueryResourceName) TableID() string {
items := strings.Split(string(r), "/")
if len(items) >= 6 {
return items[5]
}
return ""
}
// BigQueryResourceID returns bigquery resource identifier in format of:
// For dataset type: "project-id:dataset_name"
// For table type: "project-id:dataset_name.table_name"
func (r BigQueryResourceName) BigQueryResourceID() string {
urn := fmt.Sprintf("%s:%s", r.ProjectID(), r.DatasetID())
if tableID := r.TableID(); tableID != "" {
urn = fmt.Sprintf("%s.%s", urn, tableID)
}
return urn
}