Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show servers to query language #1906

Merged
merged 2 commits into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (*DropSeriesStatement) node() {}
func (*DropUserStatement) node() {}
func (*GrantStatement) node() {}
func (*ShowContinuousQueriesStatement) node() {}
func (*ShowServersStatement) node() {}
func (*ShowDatabasesStatement) node() {}
func (*ShowFieldKeysStatement) node() {}
func (*ShowRetentionPoliciesStatement) node() {}
Expand Down Expand Up @@ -161,6 +162,7 @@ func (*DropSeriesStatement) stmt() {}
func (*DropUserStatement) stmt() {}
func (*GrantStatement) stmt() {}
func (*ShowContinuousQueriesStatement) stmt() {}
func (*ShowServersStatement) stmt() {}
func (*ShowDatabasesStatement) stmt() {}
func (*ShowFieldKeysStatement) stmt() {}
func (*ShowMeasurementsStatement) stmt() {}
Expand Down Expand Up @@ -1174,6 +1176,17 @@ func (s *ShowContinuousQueriesStatement) RequiredPrivileges() ExecutionPrivilege
return ExecutionPrivileges{{Name: "", Privilege: ReadPrivilege}}
}

// ShowServersStatement represents a command for listing all servers.
type ShowServersStatement struct{}

// String returns a string representation of the show servers command.
func (s *ShowServersStatement) String() string { return "SHOW SERVERS" }

// RequiredPrivileges returns the privilege required to execute a ShowServersStatement
func (s *ShowServersStatement) RequiredPrivileges() ExecutionPrivileges {
return ExecutionPrivileges{{Name: "", Privilege: AllPrivileges}}
}

// ShowDatabasesStatement represents a command for listing all databases in the cluster.
type ShowDatabasesStatement struct{}

Expand Down
11 changes: 10 additions & 1 deletion influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func (p *Parser) parseShowStatement() (Statement, error) {
return p.parseShowContinuousQueriesStatement()
case DATABASES:
return p.parseShowDatabasesStatement()
case SERVERS:
return p.parseShowServersStatement()
case FIELD:
tok, pos, lit := p.scanIgnoreWhitespace()
if tok == KEYS {
Expand Down Expand Up @@ -129,7 +131,7 @@ func (p *Parser) parseShowStatement() (Statement, error) {
return p.parseShowUsersStatement()
}

return nil, newParseError(tokstr(tok, lit), []string{"CONTINUOUS", "DATABASES", "FIELD", "MEASUREMENTS", "RETENTION", "SERIES", "TAG", "USERS"}, pos)
return nil, newParseError(tokstr(tok, lit), []string{"CONTINUOUS", "DATABASES", "FIELD", "MEASUREMENTS", "RETENTION", "SERIES", "SERVERS", "TAG", "USERS"}, pos)
}

// parseCreateStatement parses a string and returns a create statement.
Expand Down Expand Up @@ -947,6 +949,13 @@ func (p *Parser) parseShowContinuousQueriesStatement() (*ShowContinuousQueriesSt
return stmt, nil
}

// parseShowServersStatement parses a string and returns a ShowServersStatement.
// This function assumes the "SHOW SERVERS" tokens have already been consumed.
func (p *Parser) parseShowServersStatement() (*ShowServersStatement, error) {
stmt := &ShowServersStatement{}
return stmt, nil
}

// parseShowDatabasesStatement parses a string and returns a ShowDatabasesStatement.
// This function assumes the "SHOW DATABASE" tokens have already been consumed.
func (p *Parser) parseShowDatabasesStatement() (*ShowDatabasesStatement, error) {
Expand Down
8 changes: 7 additions & 1 deletion influxql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ func TestParser_ParseStatement(t *testing.T) {
},
},

// SHOW SERVERS
{
s: `SHOW SERVERS`,
stmt: &influxql.ShowServersStatement{},
},

// SHOW DATABASES
{
s: `SHOW DATABASES`,
Expand Down Expand Up @@ -738,7 +744,7 @@ func TestParser_ParseStatement(t *testing.T) {
{s: `SHOW CONTINUOUS`, err: `found EOF, expected QUERIES at line 1, char 17`},
{s: `SHOW RETENTION`, err: `found EOF, expected POLICIES at line 1, char 16`},
{s: `SHOW RETENTION POLICIES`, err: `found EOF, expected identifier at line 1, char 25`},
{s: `SHOW FOO`, err: `found FOO, expected CONTINUOUS, DATABASES, FIELD, MEASUREMENTS, RETENTION, SERIES, TAG, USERS at line 1, char 6`},
{s: `SHOW FOO`, err: `found FOO, expected CONTINUOUS, DATABASES, FIELD, MEASUREMENTS, RETENTION, SERIES, SERVERS, TAG, USERS at line 1, char 6`},
{s: `DROP CONTINUOUS`, err: `found EOF, expected QUERY at line 1, char 17`},
{s: `DROP CONTINUOUS QUERY`, err: `found EOF, expected identifier at line 1, char 23`},
{s: `CREATE CONTINUOUS`, err: `found EOF, expected QUERY at line 1, char 19`},
Expand Down
2 changes: 2 additions & 0 deletions influxql/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
REVOKE
SELECT
SERIES
SERVERS
SHOW
SLIMIT
SOFFSET
Expand Down Expand Up @@ -203,6 +204,7 @@ var tokens = [...]string{
REVOKE: "REVOKE",
SELECT: "SELECT",
SERIES: "SERIES",
SERVERS: "SERVERS",
SHOW: "SHOW",
SLIMIT: "SLIMIT",
SOFFSET: "SOFFSET",
Expand Down
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,8 @@ func (s *Server) ExecuteQuery(q *influxql.Query, database string, user *User) Re
res = s.executeDropDatabaseStatement(stmt, user)
case *influxql.ShowDatabasesStatement:
res = s.executeShowDatabasesStatement(stmt, user)
case *influxql.ShowServersStatement:
res = s.executeShowServersStatement(stmt, user)
case *influxql.CreateUserStatement:
res = s.executeCreateUserStatement(stmt, user)
case *influxql.DropUserStatement:
Expand Down Expand Up @@ -2078,6 +2080,14 @@ func (s *Server) executeShowDatabasesStatement(q *influxql.ShowDatabasesStatemen
return &Result{Series: []*influxql.Row{row}}
}

func (s *Server) executeShowServersStatement(q *influxql.ShowServersStatement, user *User) *Result {
row := &influxql.Row{Columns: []string{"id", "url"}}
for _, node := range s.DataNodes() {
row.Values = append(row.Values, []interface{}{node.ID, node.URL.String()})
}
return &Result{Series: []*influxql.Row{row}}
}

func (s *Server) executeCreateUserStatement(q *influxql.CreateUserStatement, user *User) *Result {
isAdmin := false
if q.Privilege != nil {
Expand Down