-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathgrant_privileges_identifier_commons.go
130 lines (108 loc) · 3.92 KB
/
grant_privileges_identifier_commons.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
package resources
import (
"fmt"
"strings"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)
type OnSchemaGrantKind string
const (
OnSchemaSchemaGrantKind OnSchemaGrantKind = "OnSchema"
OnAllSchemasInDatabaseSchemaGrantKind OnSchemaGrantKind = "OnAllSchemasInDatabase"
OnFutureSchemasInDatabaseSchemaGrantKind OnSchemaGrantKind = "OnFutureSchemasInDatabase"
)
type OnSchemaObjectGrantKind string
const (
OnObjectSchemaObjectGrantKind OnSchemaObjectGrantKind = "OnObject"
OnAllSchemaObjectGrantKind OnSchemaObjectGrantKind = "OnAll"
OnFutureSchemaObjectGrantKind OnSchemaObjectGrantKind = "OnFuture"
)
type OnSchemaGrantData struct {
Kind OnSchemaGrantKind
SchemaName *sdk.DatabaseObjectIdentifier
DatabaseName *sdk.AccountObjectIdentifier
}
func (d *OnSchemaGrantData) String() string {
var parts []string
parts = append(parts, string(d.Kind))
switch d.Kind {
case OnSchemaSchemaGrantKind:
parts = append(parts, d.SchemaName.FullyQualifiedName())
case OnAllSchemasInDatabaseSchemaGrantKind, OnFutureSchemasInDatabaseSchemaGrantKind:
parts = append(parts, d.DatabaseName.FullyQualifiedName())
}
return helpers.EncodeResourceIdentifier(parts...)
}
type OnSchemaObjectGrantData struct {
Kind OnSchemaObjectGrantKind
Object *sdk.Object
OnAllOrFuture *BulkOperationGrantData
}
func (d *OnSchemaObjectGrantData) String() string {
var parts []string
parts = append(parts, string(d.Kind))
switch d.Kind {
case OnObjectSchemaObjectGrantKind:
parts = append(parts, fmt.Sprintf("%s|%s", d.Object.ObjectType, d.Object.Name.FullyQualifiedName()))
case OnAllSchemaObjectGrantKind, OnFutureSchemaObjectGrantKind:
parts = append(parts, d.OnAllOrFuture.String())
}
return helpers.EncodeResourceIdentifier(parts...)
}
type BulkOperationGrantKind string
const (
InDatabaseBulkOperationGrantKind BulkOperationGrantKind = "InDatabase"
InSchemaBulkOperationGrantKind BulkOperationGrantKind = "InSchema"
)
type BulkOperationGrantData struct {
ObjectNamePlural sdk.PluralObjectType
Kind BulkOperationGrantKind
Database *sdk.AccountObjectIdentifier
Schema *sdk.DatabaseObjectIdentifier
}
func (d *BulkOperationGrantData) String() string {
var parts []string
parts = append(parts, d.ObjectNamePlural.String())
parts = append(parts, string(d.Kind))
switch d.Kind {
case InDatabaseBulkOperationGrantKind:
parts = append(parts, d.Database.FullyQualifiedName())
case InSchemaBulkOperationGrantKind:
parts = append(parts, d.Schema.FullyQualifiedName())
}
return helpers.EncodeResourceIdentifier(parts...)
}
func getBulkOperationGrantData(in *sdk.GrantOnSchemaObjectIn) *BulkOperationGrantData {
bulkOperationGrantData := &BulkOperationGrantData{
ObjectNamePlural: in.PluralObjectType,
}
if in.InDatabase != nil {
bulkOperationGrantData.Kind = InDatabaseBulkOperationGrantKind
bulkOperationGrantData.Database = in.InDatabase
}
if in.InSchema != nil {
bulkOperationGrantData.Kind = InSchemaBulkOperationGrantKind
bulkOperationGrantData.Schema = in.InSchema
}
return bulkOperationGrantData
}
func getGrantOnSchemaObjectIn(allOrFuture map[string]any) (*sdk.GrantOnSchemaObjectIn, error) {
grantOnSchemaObjectIn := &sdk.GrantOnSchemaObjectIn{
PluralObjectType: sdk.PluralObjectType(strings.ToUpper(allOrFuture["object_type_plural"].(string))),
}
if inDatabase, ok := allOrFuture["in_database"].(string); ok && len(inDatabase) > 0 {
databaseId, err := sdk.ParseAccountObjectIdentifier(inDatabase)
if err != nil {
return nil, err
}
grantOnSchemaObjectIn.InDatabase = sdk.Pointer(databaseId)
}
if inSchema, ok := allOrFuture["in_schema"].(string); ok && len(inSchema) > 0 {
schemaId, err := sdk.ParseDatabaseObjectIdentifier(inSchema)
if err != nil {
return nil, err
}
grantOnSchemaObjectIn.InSchema = sdk.Pointer(schemaId)
}
return grantOnSchemaObjectIn, nil
}