-
Notifications
You must be signed in to change notification settings - Fork 0
/
typedef_client.go
152 lines (135 loc) · 4.49 KB
/
typedef_client.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
149
150
151
152
package client
import (
"encoding/json"
"fmt"
"github.com/atlanhq/atlan-go/atlan"
"github.com/atlanhq/atlan-go/atlan/model"
)
type TypeDefClient struct {
Client *AtlanClient
}
// NewTypeDefClient creates a new instance of TypeDefClient.
func NewTypeDefClient(client *AtlanClient) *TypeDefClient {
return &TypeDefClient{Client: client}
}
func buildTypeDefRequest(typedef model.TypeDef) (model.TypeDefResponse, error) {
var payload model.TypeDefResponse
switch t := typedef.(type) {
case *model.AtlanTagDef:
payload = model.TypeDefResponse{
AtlanTagDefs: []model.AtlanTagDef{*t},
EnumDefs: []model.EnumDef{},
StructDefs: []model.StructDef{},
EntityDefs: []model.EntityDef{},
RelationshipDefs: []model.RelationshipDef{},
CustomMetadataDefs: []model.CustomMetadataDef{},
}
case *model.CustomMetadataDef:
payload = model.TypeDefResponse{
AtlanTagDefs: []model.AtlanTagDef{},
EnumDefs: []model.EnumDef{},
StructDefs: []model.StructDef{},
EntityDefs: []model.EntityDef{},
RelationshipDefs: []model.RelationshipDef{},
CustomMetadataDefs: []model.CustomMetadataDef{*t},
}
case *model.EnumDef:
payload = model.TypeDefResponse{
AtlanTagDefs: []model.AtlanTagDef{},
EnumDefs: []model.EnumDef{*t},
StructDefs: []model.StructDef{},
EntityDefs: []model.EntityDef{},
RelationshipDefs: []model.RelationshipDef{},
CustomMetadataDefs: []model.CustomMetadataDef{},
}
default:
return model.TypeDefResponse{}, AtlanError{ErrorCode: errorCodes[UNABLE_TO_UPDATE_TYPEDEF_CATEGORY], Args: []interface{}{t}}
}
return payload, nil
}
func NewTypeDefResponse(rawJSON []byte) (*model.TypeDefResponse, error) {
var response model.TypeDefResponse
if err := json.Unmarshal(rawJSON, &response); err != nil {
//fmt.Println(response)
return nil, err
}
return &response, nil
}
func RefreshCaches(typedef model.TypeDef) error {
switch t := typedef.(type) {
case *model.AtlanTagDef:
return GetAtlanTagCache().RefreshCache()
case *model.CustomMetadataDef:
return GetCustomMetadataCache().RefreshCache()
case model.EnumDef:
//return EnumCache.RefreshCache()
default:
return AtlanError{ErrorCode: errorCodes[UNABLE_TO_UPDATE_TYPEDEF_CATEGORY], Args: []interface{}{t}}
}
return nil
}
func GetAll() (*model.TypeDefResponse, error) {
rawJSON, err := DefaultAtlanClient.CallAPI(&GET_ALL_TYPE_DEFS, nil, nil)
if err != nil {
return nil, AtlanError{
ErrorCode: errorCodes[CONNECTION_ERROR],
Args: []interface{}{"IOException"},
}
}
return NewTypeDefResponse(rawJSON)
}
// Get retrieves a TypeDefResponse object that contains a list of the specified category type definitions in Atlan.
func Get(typeCategory atlan.AtlanTypeCategory) (*model.TypeDefResponse, error) {
queryParams := map[string]string{"type": typeCategory.String()}
rawJSON, err := DefaultAtlanClient.CallAPI(&GET_ALL_TYPE_DEFS, queryParams, nil)
if err != nil {
return nil, err
}
return NewTypeDefResponse(rawJSON)
}
func (c *TypeDefClient) Create(typedef model.TypeDef) (*model.TypeDefResponse, error) {
payload, err := buildTypeDefRequest(typedef)
rawJSON, err := c.Client.CallAPI(&CREATE_TYPE_DEFS, nil, payload)
if err != nil {
return nil, err
}
RefreshCaches(typedef)
return NewTypeDefResponse(rawJSON)
}
func (c *TypeDefClient) Update(typedef model.TypeDef) (*model.TypeDefResponse, error) {
payload, err := buildTypeDefRequest(typedef)
rawJSON, err := c.Client.CallAPI(&UPDATE_TYPE_DEFS, nil, payload)
if err != nil {
return nil, err
}
RefreshCaches(typedef)
return NewTypeDefResponse(rawJSON)
}
func (c *TypeDefClient) Purge(name string, typedefType model.TypeDef) error {
var internalName string
switch t := typedefType.(type) {
case *model.CustomMetadataDef:
internalName, _ = GetCustomMetadataCache().GetIDForName(name)
case *model.EnumDef:
//internalName = name
case *model.AtlanTagDef:
internalName, _ = GetAtlanTagCache().GetIDForName(name)
default:
return fmt.Errorf("unsupported TypeDef type: %T", t)
}
if internalName == "" {
return NotFoundError{AtlanError{ErrorCode: errorCodes[TYPEDEF_NOT_FOUND_BY_NAME], Args: []interface{}{name}}}
}
c.Client.CallAPI(&DELETE_TYPE_DEF_BY_NAME, nil, nil)
switch t := typedefType.(type) {
case *model.CustomMetadataDef:
GetCustomMetadataCache().RefreshCache()
case *model.EnumDef:
//EnumCache.refreshCache()
case *model.AtlanTagDef:
RefreshCache()
default:
return fmt.Errorf("unsupported TypeDef type: %T", t)
}
return nil
}