-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
173 lines (149 loc) · 5.57 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2020 IBM Corp.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"emperror.dev/errors"
"encoding/json"
"github.com/spf13/cobra"
"io"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"os"
"strings"
dcclient "fybrik.io/fybrik/pkg/connectors/datacatalog/clients"
"fybrik.io/fybrik/pkg/logging"
"fybrik.io/fybrik/pkg/model/datacatalog"
"fybrik.io/fybrik/pkg/taxonomy/validate"
"github.com/rs/zerolog"
)
var version string
const (
requestJsonOption = "request-payload"
requestOperationOption = "operation-type"
credentialPathOption = "creds"
catalogconnectorUrlOption = "url"
)
var (
requestFile string
requestOperation string
credentialPath string
catalogconnectorUrl string
)
type Request struct {
log zerolog.Logger
operationType string
}
var request Request
var DataCatalogGetAssetResponseTaxonomy = "resources/taxonomy/datacatalog.json#/definitions/GetAssetResponse"
var DataCatalogCreateAssetResponseTaxonomy = "resources/taxonomy/datacatalog.json#/definitions/CreateAssetResponse"
func newDataCatalog() (dcclient.DataCatalog, error) {
providerName := "egeria"
return dcclient.NewDataCatalog(
providerName,
catalogconnectorUrl)
}
func ValidateAssetResponse(response interface{}, taxonomyFile string, log *zerolog.Logger) error {
var allErrs []*field.Error
// Convert GetAssetRequest Go struct to JSON
responseJSON, err := json.Marshal(response)
if err != nil {
return err
}
log.Info().Msg("responseJSON:" + string(responseJSON))
// Validate Fybrik module against taxonomy
allErrs, err = validate.TaxonomyCheck(responseJSON, taxonomyFile)
if err != nil {
return err
}
// Return any error
if len(allErrs) != 0 {
return apierrors.NewInvalid(
schema.GroupKind{Group: "app.fybrik.io", Kind: "infrastructure"}, "", allErrs)
}
return nil
}
func handleRead(requestJsonFile *os.File, catalog dcclient.DataCatalog, log *zerolog.Logger) error {
byteValue, _ := io.ReadAll(requestJsonFile)
var dataCatalogReq datacatalog.GetAssetRequest
var err error
if err = json.Unmarshal(byteValue, &dataCatalogReq); err != nil {
return errors.Wrap(err, "dataCatalog request unmarshal failed")
}
var response *datacatalog.GetAssetResponse
if response, err = catalog.GetAssetInfo(&dataCatalogReq, credentialPath); err != nil {
return errors.Wrap(err, "failed to receive the catalog connector response")
}
err = ValidateAssetResponse(response, DataCatalogGetAssetResponseTaxonomy, log)
if err != nil {
return errors.Wrap(err, "failed to validate the catalog connector response")
}
log.Info().Msg("RESPONSE VALIDATION PASS")
return nil
}
func handleWrite(requestJsonFile *os.File, catalog dcclient.DataCatalog, log *zerolog.Logger) error {
byteValue, _ := io.ReadAll(requestJsonFile)
var dataCatalogReq datacatalog.CreateAssetRequest
var err error
if err = json.Unmarshal(byteValue, &dataCatalogReq); err != nil {
return errors.Wrap(err, "dataCatalog request unmarshal failed")
}
var response *datacatalog.CreateAssetResponse
if response, err = catalog.CreateAsset(&dataCatalogReq, credentialPath); err != nil {
log.Error().Err(err).Msg("failed to receive the catalog connector response")
return err
}
err = ValidateAssetResponse(response, DataCatalogCreateAssetResponseTaxonomy, log)
if err != nil {
return errors.Wrap(err, "failed to validate the catalog connector response")
}
log.Info().Msg("RESPONSE VALIDATION PASS")
return nil
}
// RootCmd defines the root cli command
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "catalog-connector-client",
Short: "Data catalog connector client",
SilenceErrors: true,
SilenceUsage: true,
Version: strings.TrimSpace(version),
RunE: func(cmd *cobra.Command, args []string) error {
// Initialize DataCatalog interface
catalog, err := newDataCatalog()
if err != nil {
return errors.Wrap(err, "unable to create data catalog facade")
}
defer catalog.Close()
// Open our requestJsonFile
requestJsonFile, err := os.Open(requestFile)
// if we os.Open returns an error then handle it
if err != nil {
return errors.Wrap(err, "error opening "+requestFile)
}
request.log.Info().Msg("Successfully Opened " + requestFile)
defer requestJsonFile.Close()
if requestOperation == "get-asset" {
request.operationType = "get-asset"
return handleRead(requestJsonFile, catalog, &request.log)
} else if requestOperation == "create-asset" {
request.operationType = "create-asset"
return handleWrite(requestJsonFile, catalog, &request.log)
}
return errors.New("Unsupported operation")
},
}
cmd.PersistentFlags().StringVar(&requestFile, requestJsonOption, "resources/read-request.json", "Json file containing the payload of the request")
cmd.PersistentFlags().StringVar(&requestOperation, requestOperationOption, "get-asset", "Request operation. valid options are get-asset or create-asset")
cmd.PersistentFlags().StringVar(&credentialPath, credentialPathOption, "/v1/kubernetes-secrets/my-secret?namespace=default", "Credential path")
cmd.PersistentFlags().StringVar(&catalogconnectorUrl, catalogconnectorUrlOption, "http://localhost:8080", "Catalog connector Url")
cmd.MarkFlagsRequiredTogether(requestJsonOption, requestOperationOption, credentialPathOption, catalogconnectorUrlOption)
return cmd
}
func main() {
request.log = logging.LogInit(logging.CONTROLLER, "DataCatalogConnectorClient")
if err := RootCmd().Execute(); err != nil {
request.log.Error().Err(err).Msg("request failed")
os.Exit(1)
}
}