-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_opencti.go
75 lines (70 loc) · 1.83 KB
/
data_opencti.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
package opencti
import (
"context"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
"github.com/blackstork-io/fabric/plugin"
)
func makeOpenCTIDataSource() *plugin.DataSource {
return &plugin.DataSource{
Config: hcldec.ObjectSpec{
"graphql_url": &hcldec.AttrSpec{
Name: "graphql_url",
Type: cty.String,
Required: true,
},
"auth_token": &hcldec.AttrSpec{
Name: "auth_token",
Type: cty.String,
Required: false,
},
},
Args: hcldec.ObjectSpec{
"graphql_query": &hcldec.AttrSpec{
Name: "graphql_query",
Type: cty.String,
Required: true,
},
},
DataFunc: fetchOpenCTIData,
}
}
func fetchOpenCTIData(ctx context.Context, params *plugin.RetrieveDataParams) (plugin.Data, hcl.Diagnostics) {
url := params.Config.GetAttr("graphql_url")
if url.IsNull() || url.AsString() == "" {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Failed to parse config",
Detail: "graphql_url is required",
}}
}
authToken := params.Config.GetAttr("auth_token")
if authToken.IsNull() {
authToken = cty.StringVal("")
}
query := params.Args.GetAttr("graphql_query")
if query.IsNull() || query.AsString() == "" {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Failed to parse arguments",
Detail: "graphql_query is required",
}}
}
if err := validateQuery(query.AsString()); err != nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Invalid GraphQL query",
Detail: err.Error(),
}}
}
result, err := executeQuery(ctx, url.AsString(), query.AsString(), authToken.AsString())
if err != nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Failed to execute query",
Detail: err.Error(),
}}
}
return result, nil
}