Skip to content

Commit 88d4bea

Browse files
committed
fix: add new source types to MultiSourceBuilder
1 parent 6c4280f commit 88d4bea

3 files changed

Lines changed: 42 additions & 30 deletions

File tree

server/builders.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

33
import (
4+
"fmt"
5+
46
"github.com/influxdata/chronograf"
57
"github.com/influxdata/chronograf/canned"
68
"github.com/influxdata/chronograf/filestore"
@@ -107,7 +109,7 @@ func (builder *MultiDashboardBuilder) Build(db chronograf.DashboardsStore) (*mul
107109

108110
// SourcesBuilder builds a MultiSourceStore
109111
type SourcesBuilder interface {
110-
Build(chronograf.SourcesStore) (*multistore.SourcesStore, error)
112+
Build(chronograf.SourcesStore, string) (*multistore.SourcesStore, error)
111113
}
112114

113115
// MultiSourceBuilder implements SourcesBuilder
@@ -129,29 +131,27 @@ type MultiSourceBuilder struct {
129131
}
130132

131133
// Build will return a MultiSourceStore
132-
func (fs *MultiSourceBuilder) Build(db chronograf.SourcesStore) (*multistore.SourcesStore, error) {
134+
func (fs *MultiSourceBuilder) Build(db chronograf.SourcesStore, defaultOrgID string) (*multistore.SourcesStore, error) {
133135
// These dashboards are those handled from a directory
134136
files := filestore.NewSources(fs.Path, fs.ID, fs.Logger)
135137

136138
stores := []chronograf.SourcesStore{db, files}
137139

138-
// TODO simon: also process fs.InfluxDBType!
139140
if fs.InfluxDBURL != "" {
140141
var influxdbType, username, password string
141142
var clusterID, accountID, mgmtToken, dbToken, tagsCSVPath string
142-
if fs.InfluxDBClusterID != "" && fs.InfluxDBAccountID != "" && fs.InfluxDBToken != "" && fs.InfluxDBMgmtToken != "" {
143+
if fs.InfluxDBType == chronograf.InfluxDBv3Core || fs.InfluxDBType == chronograf.InfluxDBv3Enterprise {
144+
// InfluxDB 3 Core, InfluxDB 3 Enterprise
145+
influxdbType = fs.InfluxDBType
146+
dbToken = fs.InfluxDBToken
147+
} else if fs.InfluxDBType == chronograf.InfluxDBv3CloudDedicated {
143148
// InfluxDB Cloud Dedicated
144-
influxdbType = chronograf.InfluxDBv3CloudDedicated
149+
influxdbType = fs.InfluxDBType
145150
clusterID = fs.InfluxDBClusterID
146151
accountID = fs.InfluxDBAccountID
147152
mgmtToken = fs.InfluxDBMgmtToken
148153
dbToken = fs.InfluxDBToken
149154
tagsCSVPath = fs.TagsCSVPath
150-
} else if fs.InfluxDBToken != "" {
151-
// TODO simon: this is not fully correct, it can be either v3 Core or v3 Enterprise
152-
// InfluxDB 3 Core/Enterprise
153-
influxdbType = chronograf.InfluxDBv3Core
154-
dbToken = fs.InfluxDBToken
155155
} else if fs.InfluxDBOrg == "" || fs.InfluxDBToken == "" {
156156
// v1 InfluxDB
157157
username = fs.InfluxDBUsername
@@ -164,24 +164,29 @@ func (fs *MultiSourceBuilder) Build(db chronograf.SourcesStore) (*multistore.Sou
164164
influxdbType = chronograf.InfluxDBv2
165165
}
166166

167-
influxStore := &memdb.SourcesStore{
168-
// TODO simon: validate the Source before adding, reuse ValidSourceRequest!
169-
Source: &chronograf.Source{
170-
ID: 0,
171-
Name: fs.InfluxDBURL,
172-
Type: influxdbType,
173-
Username: username,
174-
Password: password,
175-
ClusterID: clusterID,
176-
AccountID: accountID,
177-
ManagementToken: mgmtToken,
178-
DatabaseToken: dbToken,
179-
TagsCSVPath: tagsCSVPath,
180-
URL: fs.InfluxDBURL,
181-
Default: true,
182-
Version: "unknown", // a real version is re-fetched at runtime; use "unknown" version as a fallback, empty version would imply OSS 2.x
183-
}}
184-
stores = append([]chronograf.SourcesStore{influxStore}, stores...)
167+
source := chronograf.Source{
168+
ID: 0,
169+
Name: fs.InfluxDBURL,
170+
Type: influxdbType,
171+
Username: username,
172+
Password: password,
173+
ClusterID: clusterID,
174+
AccountID: accountID,
175+
ManagementToken: mgmtToken,
176+
DatabaseToken: dbToken,
177+
TagsCSVPath: tagsCSVPath,
178+
URL: fs.InfluxDBURL,
179+
Default: true,
180+
Version: "unknown", // a real version is re-fetched at runtime; use "unknown" version as a fallback, empty version would imply OSS 2.x
181+
}
182+
183+
if err := ValidSourceRequest(&source, defaultOrgID); err == nil {
184+
influxStore := &memdb.SourcesStore{Source: &source}
185+
stores = append([]chronograf.SourcesStore{influxStore}, stores...)
186+
} else {
187+
// Log the error and ignore
188+
fs.Logger.Error(fmt.Sprintf("Invalid %s source: %s", influxdbType, err))
189+
}
185190
}
186191
sources := &multistore.SourcesStore{
187192
Stores: stores,

server/builders_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestLayoutBuilder(t *testing.T) {
2020

2121
func TestSourcesStoresBuilder(t *testing.T) {
2222
var b server.SourcesBuilder = &server.MultiSourceBuilder{}
23-
sources, err := b.Build(nil)
23+
sources, err := b.Build(nil, "")
2424
if err != nil {
2525
t.Fatalf("MultiSourceBuilder can't build a MultiSourcesStore: %v", err)
2626
}

server/server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,13 @@ func openService(ctx context.Context, db kv.Store, builder builders, logger chro
824824
Error("Unable to construct a MultiOrganizationStore", err)
825825
os.Exit(1)
826826
}
827+
defaultOrg, err := organizations.DefaultOrganization(ctx)
828+
if err != nil {
829+
logger.
830+
WithField("component", "OrganizationsStore").
831+
Error("Unable to get default organization", err)
832+
os.Exit(1)
833+
}
827834

828835
kapacitors, err := builder.Kapacitors.Build(svc.ServersStore())
829836
if err != nil {
@@ -833,7 +840,7 @@ func openService(ctx context.Context, db kv.Store, builder builders, logger chro
833840
os.Exit(1)
834841
}
835842

836-
sources, err := builder.Sources.Build(svc.SourcesStore())
843+
sources, err := builder.Sources.Build(svc.SourcesStore(), defaultOrg.ID)
837844
if err != nil {
838845
logger.
839846
WithField("component", "SourcesStore").

0 commit comments

Comments
 (0)