forked from turbot/steampipe-plugin-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_salesforce_object.go
119 lines (100 loc) · 4.11 KB
/
table_salesforce_object.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
package salesforce
import (
"context"
"fmt"
"strings"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
//// LIST HYDRATE FUNCTION
func listSalesforceObjectsByTable(tableName string, salesforceCols map[string]string) func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
return func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("salesforce.listSalesforceObjectsByTable", "connection error", err)
return nil, err
}
if client == nil {
plugin.Logger(ctx).Error("salesforce.listSalesforceObjectsByTable", "client_not_found: unable to generate dynamic tables because of invalid steampipe salesforce configuration", err)
return nil, fmt.Errorf("salesforce.listSalesforceObjectsByTable: client_not_found, unable to query table %s because of invalid steampipe salesforce configuration", d.Table.Name)
}
query := generateQuery(d.Table.Columns, tableName)
condition := buildQueryFromQuals(d.Quals, d.Table.Columns, salesforceCols)
if condition != "" {
query = fmt.Sprintf("%s where %s", query, condition)
plugin.Logger(ctx).Debug("salesforce.listSalesforceObjectsByTable", "table_name", d.Table.Name, "query_condition", condition)
}
for {
result, err := client.Query(query)
if err != nil {
plugin.Logger(ctx).Error("salesforce.listSalesforceObjectsByTable", "query error", err)
return nil, err
}
AccountList := new([]map[string]interface{})
err = decodeQueryResult(ctx, result.Records, AccountList)
if err != nil {
plugin.Logger(ctx).Error("salesforce.listSalesforceObjectsByTable", "results decoding error", err)
return nil, err
}
for _, account := range *AccountList {
d.StreamListItem(ctx, account)
}
// Paging
if result.Done {
break
} else {
query = result.NextRecordsURL
}
}
return nil, nil
}
}
//// GET HYDRATE FUNCTION
func getSalesforceObjectbyID(tableName string) func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
return func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Info("salesforce.getSalesforceObjectbyID", "Table_Name", d.Table.Name)
config := GetConfig(d.Connection)
var id string
if config.NamingConvention != nil && *config.NamingConvention == "api_native" {
id = d.EqualsQualString("Id")
} else {
id = d.EqualsQualString("id")
}
if strings.TrimSpace(id) == "" {
return nil, nil
}
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("salesforce.getSalesforceObjectbyID", "connection error", err)
return nil, err
}
if client == nil {
plugin.Logger(ctx).Error("salesforce.getSalesforceObjectbyID", "client_not_found: unable to generate dynamic tables because of invalid steampipe salesforce configuration", err)
return nil, fmt.Errorf("salesforce.getSalesforceObjectbyID: client_not_found, unable to query table %s because of invalid steampipe salesforce configuration", d.Table.Name)
}
obj := client.SObject(tableName).Get(id)
if obj == nil {
// Object doesn't exist, handle the error
plugin.Logger(ctx).Warn("salesforce.getSalesforceObjectbyID", fmt.Sprintf("%s with id \"%s\" not found", tableName, id))
return nil, nil
}
object := new(map[string]interface{})
err = decodeQueryResult(ctx, obj, object)
if err != nil {
plugin.Logger(ctx).Error("salesforce.getSalesforceObjectbyID", "result decoding error", err)
return nil, err
}
return *object, nil
}
}
//// TRANSFORM FUNCTION
func getFieldFromSObjectMap(ctx context.Context, d *transform.TransformData) (interface{}, error) {
param := d.Param.(string)
ls := d.HydrateItem.(map[string]interface{})
return ls[param], nil
}
func getFieldFromSObjectMapByColumnName(ctx context.Context, d *transform.TransformData) (interface{}, error) {
salesforceColumnName := getSalesforceColumnName(d.ColumnName)
ls := d.HydrateItem.(map[string]interface{})
return ls[salesforceColumnName], nil
}