-
Notifications
You must be signed in to change notification settings - Fork 152
/
databases.go
164 lines (139 loc) · 4.64 KB
/
databases.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
package v1
import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/stdlib/influxdata/influxdb"
)
const (
DatabasesKind = "databases"
DatabasesRemoteKind = "influxdata/influxdb/v1.databasesRemote"
)
type DatabasesOpSpec struct {
Org *influxdb.NameOrID
Host *string
Token *string
}
func init() {
databasesSignature := runtime.MustLookupBuiltinType("influxdata/influxdb/v1", "databases")
runtime.RegisterPackageValue("influxdata/influxdb/v1", DatabasesKind, flux.MustValue(flux.FunctionValue(DatabasesKind, createDatabasesOpSpec, databasesSignature)))
flux.RegisterOpSpec(DatabasesKind, newDatabasesOp)
plan.RegisterProcedureSpec(DatabasesKind, newDatabasesProcedure, DatabasesKind)
execute.RegisterSource(DatabasesRemoteKind, createDatabasesSource)
plan.RegisterPhysicalRules(DatabasesRemoteRule{})
}
func createDatabasesOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
spec := new(DatabasesOpSpec)
if o, ok, err := influxdb.GetNameOrID(args, "org", "orgID"); err != nil {
return nil, err
} else if ok {
spec.Org = &o
}
if h, ok, err := args.GetString("host"); err != nil {
return nil, err
} else if ok {
spec.Host = &h
}
if token, ok, err := args.GetString("token"); err != nil {
return nil, err
} else if ok {
spec.Token = &token
}
return spec, nil
}
func newDatabasesOp() flux.OperationSpec {
return new(DatabasesOpSpec)
}
func (s *DatabasesOpSpec) Kind() flux.OperationKind {
return DatabasesKind
}
type DatabasesProcedureSpec struct {
plan.DefaultCost
Org *influxdb.NameOrID
Host *string
Token *string
}
func newDatabasesProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*DatabasesOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &DatabasesProcedureSpec{
Org: spec.Org,
Host: spec.Host,
Token: spec.Token,
}, nil
}
func (s *DatabasesProcedureSpec) Kind() plan.ProcedureKind {
return DatabasesKind
}
func (s *DatabasesProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(DatabasesProcedureSpec)
*ns = *s
return ns
}
func (s *DatabasesProcedureSpec) SetOrg(org *influxdb.NameOrID) { s.Org = org }
func (s *DatabasesProcedureSpec) SetHost(host *string) { s.Host = host }
func (s *DatabasesProcedureSpec) SetToken(token *string) { s.Token = token }
func (s *DatabasesProcedureSpec) GetOrg() *influxdb.NameOrID { return s.Org }
func (s *DatabasesProcedureSpec) GetHost() *string { return s.Host }
func (s *DatabasesProcedureSpec) GetToken() *string { return s.Token }
func (s *DatabasesProcedureSpec) PostPhysicalValidate(id plan.NodeID) error {
// This condition should never be met.
// Customized planner rules within each binary should have
// filled in either a default host or registered a from procedure
// for when no host is specified.
// We mark this as an internal error because it is a programming
// error if this one ever gets hit.
if s.Host == nil {
return errors.New(codes.Internal, "databases requires a remote host to be specified")
}
return nil
}
type DatabasesRemoteProcedureSpec struct {
plan.DefaultCost
*DatabasesProcedureSpec
}
func (s *DatabasesRemoteProcedureSpec) Kind() plan.ProcedureKind {
return DatabasesRemoteKind
}
func (s *DatabasesRemoteProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(DatabasesRemoteProcedureSpec)
*ns = *s
ns.DatabasesProcedureSpec = s.DatabasesProcedureSpec.Copy().(*DatabasesProcedureSpec)
return ns
}
func (s *DatabasesRemoteProcedureSpec) PostPhysicalValidate(id plan.NodeID) error {
if s.Org == nil {
return errors.New(codes.Invalid, "reading from a remote host requires an organization to be set")
}
return nil
}
func createDatabasesSource(ps plan.ProcedureSpec, id execute.DatasetID, a execute.Administration) (execute.Source, error) {
spec := ps.(*DatabasesRemoteProcedureSpec)
return influxdb.CreateSource(id, spec, a)
}
func (s *DatabasesRemoteProcedureSpec) BuildQuery() *ast.File {
query := &ast.CallExpression{
Callee: &ast.MemberExpression{
Object: &ast.Identifier{Name: "v1"},
Property: &ast.Identifier{Name: "databases"},
},
}
return &ast.File{
Name: "query.flux",
Package: &ast.PackageClause{
Name: &ast.Identifier{Name: "main"},
},
Imports: []*ast.ImportDeclaration{{
Path: &ast.StringLiteral{Value: "influxdata/influxdb/v1"},
}},
Body: []ast.Statement{
&ast.ExpressionStatement{Expression: query},
},
}
}