forked from turbot/steampipe-plugin-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
287 lines (257 loc) · 12.4 KB
/
plugin.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package salesforce
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"github.com/iancoleman/strcase"
"github.com/simpleforce/simpleforce"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
const pluginName = "steampipe-plugin-salesforce"
type contextKey string
func Plugin(ctx context.Context) *plugin.Plugin {
p := &plugin.Plugin{
Name: pluginName,
DefaultTransform: transform.From(getFieldFromSObjectMapByColumnName).NullIfZero(),
ConnectionConfigSchema: &plugin.ConnectionConfigSchema{
NewInstance: ConfigInstance,
},
SchemaMode: plugin.SchemaModeDynamic,
TableMapFunc: pluginTableDefinitions,
}
return p
}
type dynamicMap struct {
cols []*plugin.Column
keyColumns plugin.KeyColumnSlice
salesforceColumns map[string]string
}
func pluginTableDefinitions(ctx context.Context, td *plugin.TableMapData) (map[string]*plugin.Table, error) {
// If unable to connect to salesforce instance, log warning and abort dynamic table creation
client, err := connectRaw(ctx, td.ConnectionCache, td.Connection)
if err != nil {
// do not abort the plugin as static table needs to be generated
plugin.Logger(ctx).Warn("salesforce.pluginTableDefinitions", "connection_error: unable to generate dynamic tables because of invalid steampipe salesforce configuration", err)
}
staticTables := []string{"Account", "AccountContactRole", "Asset", "Contact", "Contract", "Lead", "Opportunity", "OpportunityContactRole", "Order", "Pricebook2", "Product2", "User", "PermissionSet", "PermissionSetAssignment", "ObjectPermissions"}
dynamicColumnsMap := map[string]dynamicMap{}
var mapLock sync.Mutex
config := GetConfig(td.Connection)
// If Salesforce client was obtained, don't generate dynamic columns for
// defined static tables
if client != nil {
var wgd sync.WaitGroup
wgd.Add(len(staticTables))
for _, st := range staticTables {
go func(staticTable string) {
defer wgd.Done()
dynamicCols, dynamicKeyColumns, salesforceCols := dynamicColumns(ctx, client, staticTable, config)
mapLock.Lock()
dynamicColumnsMap[staticTable] = dynamicMap{dynamicCols, dynamicKeyColumns, salesforceCols}
defer mapLock.Unlock()
}(st)
}
wgd.Wait()
}
// Initialize tables with static tables with static and dynamic columns(if credentials are set)
var tables map[string]*plugin.Table
// check the NamingConvention parameter value in config
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
tables = map[string]*plugin.Table{
"Account": SalesforceAccount(ctx, dynamicColumnsMap["Account"], config),
"AccountContactRole": SalesforceAccountContactRole(ctx, dynamicColumnsMap["AccountContactRole"], config),
"Asset": SalesforceAsset(ctx, dynamicColumnsMap["Asset"], config),
"Contact": SalesforceContact(ctx, dynamicColumnsMap["Contact"], config),
"Contract": SalesforceContract(ctx, dynamicColumnsMap["Contract"], config),
"Lead": SalesforceLead(ctx, dynamicColumnsMap["Lead"], config),
"ObjectPermissions": SalesforceObjectPermission(ctx, dynamicColumnsMap["ObjectPermissions"], config),
"Opportunity": SalesforceOpportunity(ctx, dynamicColumnsMap["Opportunity"], config),
"OpportunityContactRole": SalesforceOpportunityContactRole(ctx, dynamicColumnsMap["OpportunityContactRole"], config),
"Order": SalesforceOrder(ctx, dynamicColumnsMap["Order"], config),
"PermissionSet": SalesforcePermissionSet(ctx, dynamicColumnsMap["PermissionSet"], config),
"PermissionSetAssignment": SalesforcePermissionSetAssignment(ctx, dynamicColumnsMap["PermissionSetAssignment"], config),
"Pricebook2": SalesforcePricebook(ctx, dynamicColumnsMap["Pricebook2"], config),
"Product2": SalesforceProduct(ctx, dynamicColumnsMap["Product2"], config),
"User": SalesforceUser(ctx, dynamicColumnsMap["User"], config),
}
} else {
tables = map[string]*plugin.Table{
"salesforce_account": SalesforceAccount(ctx, dynamicColumnsMap["Account"], config),
"salesforce_account_contact_role": SalesforceAccountContactRole(ctx, dynamicColumnsMap["AccountContactRole"], config),
"salesforce_asset": SalesforceAsset(ctx, dynamicColumnsMap["Asset"], config),
"salesforce_contact": SalesforceContact(ctx, dynamicColumnsMap["Contact"], config),
"salesforce_contract": SalesforceContract(ctx, dynamicColumnsMap["Contract"], config),
"salesforce_lead": SalesforceLead(ctx, dynamicColumnsMap["Lead"], config),
"salesforce_object_permission": SalesforceObjectPermission(ctx, dynamicColumnsMap["ObjectPermissions"], config),
"salesforce_opportunity": SalesforceOpportunity(ctx, dynamicColumnsMap["Opportunity"], config),
"salesforce_opportunity_contact_role": SalesforceOpportunityContactRole(ctx, dynamicColumnsMap["OpportunityContactRole"], config),
"salesforce_order": SalesforceOrder(ctx, dynamicColumnsMap["Order"], config),
"salesforce_permission_set": SalesforcePermissionSet(ctx, dynamicColumnsMap["PermissionSet"], config),
"salesforce_permission_set_assignment": SalesforcePermissionSetAssignment(ctx, dynamicColumnsMap["PermissionSetAssignment"], config),
"salesforce_pricebook": SalesforcePricebook(ctx, dynamicColumnsMap["Pricebook2"], config),
"salesforce_product": SalesforceProduct(ctx, dynamicColumnsMap["Product2"], config),
"salesforce_user": SalesforceUser(ctx, dynamicColumnsMap["User"], config),
}
}
var re = regexp.MustCompile(`\d+`)
var substitution = ``
salesforceTables := []string{}
if config.Objects != nil && len(*config.Objects) > 0 {
for _, tableName := range *config.Objects {
var pluginTableName string
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
pluginTableName = strcase.ToSnake(re.ReplaceAllString(tableName, substitution))
} else {
pluginTableName = "salesforce_" + strcase.ToSnake(re.ReplaceAllString(tableName, substitution))
}
if _, ok := tables[pluginTableName]; !ok {
salesforceTables = append(salesforceTables, tableName)
}
}
}
if client == nil {
plugin.Logger(ctx).Warn("salesforce.pluginTableDefinitions", "client_not_found: unable to generate dynamic tables because of invalid steampipe salesforce configuration", err)
return tables, nil
}
var wg sync.WaitGroup
wg.Add(len(salesforceTables))
for _, sfTable := range salesforceTables {
var tableName string
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
tableName = sfTable
} else {
tableName = "salesforce_" + strcase.ToSnake(re.ReplaceAllString(sfTable, substitution))
}
if tables[tableName] != nil {
wg.Done()
continue
}
go func(name string) {
defer wg.Done()
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
tableName = name
} else {
tableName = "salesforce_" + strcase.ToSnake(re.ReplaceAllString(name, substitution))
}
plugin.Logger(ctx).Debug("salesforce.pluginTableDefinitions", "object_name", name, "table_name", tableName)
ctx = context.WithValue(ctx, contextKey("PluginTableName"), tableName)
ctx = context.WithValue(ctx, contextKey("SalesforceTableName"), name)
table := generateDynamicTables(ctx, client, config)
// Ignore if the requested Salesforce object is not present.
if table != nil {
tables[tableName] = table
}
}(sfTable)
}
wg.Wait()
return tables, nil
}
func generateDynamicTables(ctx context.Context, client *simpleforce.Client, config salesforceConfig) *plugin.Table {
// Get the query for the metric (required)
salesforceTableName := ctx.Value(contextKey("SalesforceTableName")).(string)
tableName := ctx.Value(contextKey("PluginTableName")).(string)
sObjectMeta := client.SObject(salesforceTableName).Describe()
if sObjectMeta == nil {
plugin.Logger(ctx).Error("salesforce.generateDynamicTables", fmt.Sprintf("Object %s not found in salesforce", salesforceTableName))
return nil
}
// Top columns
cols := []*plugin.Column{}
salesforceCols := map[string]string{}
// Key columns
keyColumns := plugin.KeyColumnSlice{}
salesforceObjectMetadata := *sObjectMeta
salesforceObjectMetadataAsByte, err := json.Marshal(salesforceObjectMetadata["fields"])
if err != nil {
plugin.Logger(ctx).Error("salesforce.generateDynamicTables", "json marshal error", err)
}
salesforceObjectFields := []map[string]interface{}{}
err = json.Unmarshal(salesforceObjectMetadataAsByte, &salesforceObjectFields)
if err != nil {
plugin.Logger(ctx).Error("salesforce.generateDynamicTables", "json unmarshal error", err)
}
for _, properties := range salesforceObjectFields {
if properties["name"] == nil {
continue
}
fieldName := properties["name"].(string)
compoundFieldName := properties["compoundFieldName"]
if compoundFieldName != nil && compoundFieldName.(string) != fieldName {
continue
}
if properties["soapType"] == nil {
continue
}
soapType := strings.Split((properties["soapType"]).(string), ":")
fieldType := soapType[len(soapType)-1]
// Column dynamic generation
// Don't convert to snake case since field names can have underscores in
// them, so it's impossible to convert from snake case back to camel case
// to match the original field name. Also, if we convert to snake case,
// custom fields like "TestField" and "Test_Field" will result in duplicates
var columnFieldName string
// keep the field name as it is if NamingConvention is set to api_native
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
columnFieldName = fieldName
} else if strings.HasSuffix(fieldName, "__c") {
columnFieldName = strings.ToLower(fieldName)
} else {
columnFieldName = strcase.ToSnake(fieldName)
}
column := plugin.Column{
Name: columnFieldName,
Description: fmt.Sprintf("%s.", properties["label"].(string)),
Transform: transform.FromP(getFieldFromSObjectMap, fieldName),
}
// Adding column type in the map to help in qual handling
salesforceCols[columnFieldName] = fieldType
// Set column type based on the `soapType` from salesforce schema
switch fieldType {
case "string", "ID":
column.Type = proto.ColumnType_STRING
keyColumns = append(keyColumns, &plugin.KeyColumn{Name: columnFieldName, Require: plugin.Optional, Operators: []string{"=", "<>"}})
case "date", "dateTime":
column.Type = proto.ColumnType_TIMESTAMP
keyColumns = append(keyColumns, &plugin.KeyColumn{Name: columnFieldName, Require: plugin.Optional, Operators: []string{"=", ">", ">=", "<=", "<"}})
case "boolean":
column.Type = proto.ColumnType_BOOL
keyColumns = append(keyColumns, &plugin.KeyColumn{Name: columnFieldName, Require: plugin.Optional, Operators: []string{"=", "<>"}})
case "double":
column.Type = proto.ColumnType_DOUBLE
keyColumns = append(keyColumns, &plugin.KeyColumn{Name: columnFieldName, Require: plugin.Optional, Operators: []string{"=", ">", ">=", "<=", "<"}})
case "int":
column.Type = proto.ColumnType_INT
keyColumns = append(keyColumns, &plugin.KeyColumn{Name: columnFieldName, Require: plugin.Optional, Operators: []string{"=", ">", ">=", "<=", "<"}})
default:
column.Type = proto.ColumnType_JSON
}
cols = append(cols, &column)
}
Table := plugin.Table{
Name: tableName,
Description: fmt.Sprintf("Represents Salesforce object %s.", salesforceObjectMetadata["name"]),
List: &plugin.ListConfig{
KeyColumns: keyColumns,
Hydrate: listSalesforceObjectsByTable(salesforceTableName, salesforceCols),
},
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn(checkNameScheme(config, cols)),
Hydrate: getSalesforceObjectbyID(salesforceTableName),
},
Columns: cols,
}
return &Table
}
// set GetConfig parameter based on NamingConvention value
// if the object is unavailable then there will be no dynamic columns, so GetConfig parameter should be id to avoid failure of static table creation
func checkNameScheme(config salesforceConfig, dynamicColumns []*plugin.Column) string {
if config.NamingConvention != nil && *config.NamingConvention == "api_native" && len(dynamicColumns) > 0 {
return "Id"
}
return "id"
}