// NewSourcesStore creates a new SourcesStore from an existing
// chronograf.SourceStore and an role string
196519
func NewSourcesStore(s chronograf.SourcesStore, role string) *SourcesStore {
	return &SourcesStore{
		store: s,
		role:  role,
	}
}
109002
func NewSource(ctx *plan.Context, p *plan.Source) (*Source, error) {

	if p.Stmt == nil {
		return nil, fmt.Errorf("must have from for Source")
	}
	if p.Conn == nil {
		return nil, fmt.Errorf("Must have existing connection on Plan")
	}

	scanner, hasScanner := p.Conn.(schema.ConnScanner)

	// Some sources require context so we seed it here
	if sourceContext, needsContext := p.Conn.(RequiresContext); needsContext {
		sourceContext.SetContext(ctx)
	}

	if !hasScanner {
		e, hasSourceExec := p.Conn.(ExecutorSource)
		if hasSourceExec {
			s := &Source{
				TaskBase:   NewTaskBase(ctx),
				ExecSource: e,
				p:          p,
			}
			return s, nil
		}
		u.Warnf("source %T does not implement datasource.Scanner", p.Conn)
		return nil, fmt.Errorf("%T Must Implement Scanner for %q", p.Conn, p.Stmt.String())
	}
	s := &Source{
		TaskBase: NewTaskBase(ctx),
		Scanner:  scanner,
		p:        p,
	}
	return s, nil
}
1352858
func NewSource(opts ...SrcOption) (*Source, error) {
	s := &Source{
		records: make(chan record, 100),
	}
	for _, opt := range opts {
		err := opt(s)
		if err != nil {
			return nil, err
		}
	}
	go s.run()
	return s, nil
}
1286546
func (s *SourcesStore) Add(ctx context.Context, d chronograf.Source) (chronograf.Source, error) {
	err := validRole(ctx)
	if err != nil {
		return chronograf.Source{}, err
	}

	return s.store.Add(ctx, d)
}
560708
func MapToResource(resourceMap interface{}, asPointer bool) interface{} {
	b, _ := json.Marshal(&resourceMap)
	m := resourceMap.(map[string]interface{})
	t := m["resourceType"]

	if t == nil {
		return nil
	}

	var x interface{} = StructForResourceName(t.(string))

	if x == nil {
		return nil
	}

	json.Unmarshal(b, &x)

	if asPointer {
		return &x
	}
	return x
}
469318
func NewService(policyRepo policyReader, resourceRepo resourceReader, sessionRepo sessionReader) Service {
	return &service{
		policyRepo:   policyRepo,
		resourceRepo: resourceRepo,
		sessionRepo:  sessionRepo,
	}
}
808505
func NewSources(dir string, ids chronograf.ID, logger chronograf.Logger) chronograf.SourcesStore {
	return &Sources{
		Dir:     dir,
		Load:    load,
		Create:  create,
		ReadDir: ioutil.ReadDir,
		Remove:  os.Remove,
		IDs:     ids,
		Logger:  logger,
	}
}
1086008
func NewStoreKeyErr(key string) error {
	return &types.ErrStoreKey{
		Goof: goof.WithField("storeKey", key, "missing store key"),
	}
}
470095
func (s *SourcesStore) Get(ctx context.Context, id int) (chronograf.Source, error) {
	err := validRole(ctx)
	if err != nil {
		return chronograf.Source{}, err
	}

	d, err := s.store.Get(ctx, id)
	if err != nil {
		return chronograf.Source{}, err
	}

	if !hasAuthorizedRole(d.Role, s.role) {
		return chronograf.Source{}, chronograf.ErrSourceNotFound
	}

	return d, nil
}
891177
func NewSourceService() *SourceService {
	return &SourceService{
		DefaultSourceFn:  func(context.Context) (*platform.Source, error) { return nil, nil },
		FindSourceByIDFn: func(context.Context, platform.ID) (*platform.Source, error) { return nil, nil },
		CreateSourceFn:   func(context.Context, *platform.Source) error { return nil },
		UpdateSourceFn:   func(context.Context, platform.ID, platform.SourceUpdate) (*platform.Source, error) { return nil, nil },
		DeleteSourceFn:   func(context.Context, platform.ID) error { return nil },
		FindSourcesFn: func(context.Context, platform.FindOptions) ([]*platform.Source, int, error) {
			return nil, 0, nil
		},
	}
}
991960
func NewSource(opts ...source.Option) source.Source {
	var (
		options    = source.NewOptions(opts...)
		name       = DefaultName
		configPath = DefaultConfigPath
		namespace  = DefaultNamespace
	)

	prefix, ok := options.Context.Value(prefixKey{}).(string)
	if ok {
		name = prefix
	}

	cfg, ok := options.Context.Value(configPathKey{}).(string)
	if ok {
		configPath = cfg
	}

	sname, ok := options.Context.Value(nameKey{}).(string)
	if ok {
		name = sname
	}

	ns, ok := options.Context.Value(namespaceKey{}).(string)
	if ok {
		namespace = ns
	}

	// TODO handle if the client fails what to do current return does not support error
	client, err := getClient(configPath)

	return &configmap{
		cerr:       err,
		client:     client,
		opts:       options,
		name:       name,
		configPath: configPath,
		namespace:  namespace,
	}
}
1155902
func NewServerClient(idStore *IDStore, options CodeStoreOptions) (Client, error) {
	codeStore, err := newCodeStore(idStore, options)
	if err != nil {
		return nil, err
	}
	return newServerClient(idStore, codeStore)
}
549624
func NewStore(path string) *Store {
	return &Store{
		path:        path,
		data:        newRaftData(),
		subscribers: make(map[string]*list.List),

		leaderCh:   make(chan bool),
		heartbeats: make(map[instanceKey]time.Time),

		closing: make(chan struct{}),

		HeartbeatTimeout:   1000 * time.Millisecond,
		ElectionTimeout:    1000 * time.Millisecond,
		LeaderLeaseTimeout: 500 * time.Millisecond,
		CommitTimeout:      50 * time.Millisecond,

		InstanceTTL:         DefaultInstanceTTL,
		ExpiryCheckInterval: DefaultExpiryCheckInterval,

		LogOutput: os.Stderr,
		Now:       time.Now,
	}
}
528082
func (s *SourcesStore) All(ctx context.Context) ([]chronograf.Source, error) {
	err := validRole(ctx)
	if err != nil {
		return nil, err
	}

	ds, err := s.store.All(ctx)
	if err != nil {
		return nil, err
	}

	// This filters sources without allocating
	// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
	sources := ds[:0]
	for _, d := range ds {
		if hasAuthorizedRole(d.Role, s.role) {
			sources = append(sources, d)
		}
	}

	return sources, nil
}
1273950
func NewScraperTargetStoreService(s influxdb.ScraperTargetStoreService,
	urm influxdb.UserResourceMappingService,
	org influxdb.OrganizationService,
) *ScraperTargetStoreService {
	return &ScraperTargetStoreService{
		UserResourceMappingService: urm,
		s:                          s,
	}
}
1172971
func NewStorageSimpleStreamsDataSource(description string, storage StorageReader, basePath string, priority int, requireSigned bool) simplestreams.DataSource {
	return &storageSimpleStreamsDataSource{description, basePath, storage, false, priority, requireSigned}
}
1239111
func (s *SQLStore) newSQLSubStore(channelID int64, limits *SubStoreLimits) *SQLSubStore {
	subStore := &SQLSubStore{
		sqlStore:  s,
		channelID: channelID,
		maxSubID:  &s.maxSubID,
		limits:    *limits,
	}
	subStore.log = s.log
	if s.opts.NoCaching {
		subStore.subLastSent = make(map[uint64]uint64)
	} else {
		subStore.cache = &sqlSubAcksPendingCache{
			subs: make(map[uint64]*sqlSubAcksPending),
		}
	}
	return subStore
}