// importDatacenters fetches, validates, and applies datacenter configs.
505031
func importDatacenters(c context.Context, configSet config.Set, platformIds map[string]int64) error {
	cfg := &configPB.Datacenters{}
	metadata := &config.Meta{}
	if err := cfgclient.Get(c, cfgclient.AsService, configSet, datacentersFilename, textproto.Message(cfg), metadata); err != nil {
		return errors.Annotate(err, "failed to load %s", datacentersFilename).Err()
	}
	logging.Infof(c, "Found %s revision %q", datacentersFilename, metadata.Revision)

	ctx := &validation.Context{Context: c}
	ctx.SetFile(datacentersFilename)
	validateDatacentersCfg(ctx, cfg)
	if err := ctx.Finalize(); err != nil {
		return errors.Annotate(err, "invalid config").Err()
	}

	// cfgs will be a map of datacenter config filename to Datacenter.
	cfgs := make(map[string]*configPB.Datacenter, len(cfg.Datacenter))
	// datacenters will be a slice of Datacenters.
	datacenters := make([]*configPB.Datacenter, 0, len(cfg.Datacenter))
	for _, datacenterFile := range cfg.Datacenter {
		datacenter := &configPB.Datacenter{}
		if err := cfgclient.Get(c, cfgclient.AsService, configSet, datacenterFile, textproto.Message(datacenter), nil); err != nil {
			return errors.Annotate(err, "failed to load datacenter config %q", datacenterFile).Err()
		}
		cfgs[datacenterFile] = datacenter
		datacenters = append(datacenters, datacenter)
		logging.Infof(c, "Found configured datacenter %q", datacenter.Name)
	}

	validateDatacenters(ctx, cfgs)
	if err := ctx.Finalize(); err != nil {
		return errors.Annotate(err, "invalid config").Err()
	}

	datacenterIds, err := model.EnsureDatacenters(c, datacenters)
	if err != nil {
		return errors.Annotate(err, "failed to ensure datacenters").Err()
	}
	rackIds, err := model.EnsureRacks(c, datacenters, datacenterIds)
	if err != nil {
		return errors.Annotate(err, "failed to ensure racks").Err()
	}
	err = model.EnsureSwitches(c, datacenters, rackIds)
	if err != nil {
		return errors.Annotate(err, "failed to ensure switches").Err()
	}
	kvmIds, err := model.EnsureKVMs(c, datacenters, platformIds, rackIds)
	if err != nil {
		return errors.Annotate(err, "failed to ensure KVMs").Err()
	}
	// KVMs must reference the rack they exist in, while racks may optionally reference
	// a KVM that serves them. Because of this bidirectional foreign key constraint in
	// the database, we must first ensure that everything about racks except their KVM
	// matches the config, then ensure that KVMs match the config, then ensure the KVM
	// referred to by each rack matches the config.
	err = model.EnsureRackKVMs(c, datacenters, kvmIds)
	if err != nil {
		return errors.Annotate(err, "failed to ensure rack KVMs").Err()
	}
	return nil
}
792017
func (cmd *ImportCommand) importValues(ctx context.Context, useColumnKeys bool, vals []pilosa.FieldValue) error {
	logger := log.New(cmd.Stderr, "", log.LstdFlags)

	// If keys are used, all values are sent to the primary translate store (i.e. coordinator).
	if useColumnKeys {
		logger.Printf("importing keyed values: n=%d", len(vals))
		if err := cmd.client.ImportValueK(ctx, cmd.Index, cmd.Field, vals); err != nil {
			return errors.Wrap(err, "importing keys")
		}
		return nil
	}

	// Group vals by shard.
	logger.Printf("grouping %d vals", len(vals))
	valsByShard := http.FieldValues(vals).GroupByShard()

	// Parse path into FieldValues.
	for shard, vals := range valsByShard {
		if cmd.Sort {
			sort.Sort(http.FieldValues(vals))
		}

		logger.Printf("importing shard: %d, n=%d", shard, len(vals))
		if err := cmd.client.ImportValue(ctx, cmd.Index, cmd.Field, shard, vals, pilosa.OptImportOptionsClear(cmd.Clear)); err != nil {
			return errors.Wrap(err, "importing values")
		}
	}

	return nil
}
723085
func (h *Host) serveImporter(w http.ResponseWriter, r *http.Request, imp *importer) {
	if r.Method == "POST" {
		h.serveImporterPost(w, r, imp)
		return
	}

	var setup string
	node, _ := imp.Node()
	if setuper, ok := imp.impl.(ImporterSetupHTMLer); ok && node != nil {
		setup = setuper.AccountSetupHTML(h)
	}

	h.execTemplate(w, r, importerPage{
		Title: "Importer - " + imp.Name(),
		Body: importerBody{
			Host:      h,
			Importer:  imp,
			SetupHelp: template.HTML(setup),
		},
	})
}
593908
func (t *DatacentersTable) ids(c context.Context) map[string]int64 {
	dcs := make(map[string]int64, len(t.current))
	for _, dc := range t.current {
		dcs[dc.Name] = dc.Id
	}
	return dcs
}
773136
func (api *RemoteRelationsAPI) ImportRemoteEntities(args params.RemoteEntityTokenArgs) (params.ErrorResults, error) {
	results := params.ErrorResults{
		Results: make([]params.ErrorResult, len(args.Args)),
	}
	for i, arg := range args.Args {
		err := api.importRemoteEntity(arg)
		results.Results[i].Error = common.ServerError(err)
	}
	return results, nil
}
1067415
func (r *ImportCAConfigsRPC) ImportCAConfigs(c context.Context, _ *empty.Empty) (*admin.ImportedConfigs, error) {
	content, meta, err := fetchConfigFile(c, configFile)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "can't read config file - %s", err)
	}
	logging.Infof(c, "Importing tokenserver.cfg at rev %s", meta.Revision)

	// Read list of CAs.
	msg := admin.TokenServerConfig{}
	if err = proto.UnmarshalText(content, &msg); err != nil {
		return nil, status.Errorf(codes.Internal, "can't parse config file - %s", err)
	}

	seenIDs, err := LoadCAUniqueIDToCNMap(c)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "can't load unique_id map - %s", err)
	}
	if seenIDs == nil {
		seenIDs = map[int64]string{}
	}
	seenIDsDirty := false

	// There should be no duplicates.
	seenCAs := stringset.New(len(msg.GetCertificateAuthority()))
	for _, ca := range msg.GetCertificateAuthority() {
		if seenCAs.Has(ca.Cn) {
			return nil, status.Errorf(codes.Internal, "duplicate entries in the config")
		}
		seenCAs.Add(ca.Cn)
		// Check unique ID is not being reused.
		if existing, seen := seenIDs[ca.UniqueId]; seen {
			if existing != ca.Cn {
				return nil, status.Errorf(
					codes.Internal, "duplicate unique_id %d in the config: %q and %q",
					ca.UniqueId, ca.Cn, existing)
			}
		} else {
			seenIDs[ca.UniqueId] = ca.Cn
			seenIDsDirty = true
		}
	}

	// Update the mapping CA unique_id -> CA CN. Unique integer ids are used in
	// various tokens in place of a full CN name to save space. This mapping is
	// additive (all new CAs should have different IDs).
	if seenIDsDirty {
		if err := StoreCAUniqueIDToCNMap(c, seenIDs); err != nil {
			return nil, status.Errorf(codes.Internal, "can't store unique_id map - %s", err)
		}
	}

	// Add new CA datastore entries or update existing ones.
	wg := sync.WaitGroup{}
	me := errors.NewLazyMultiError(len(msg.GetCertificateAuthority()))
	for i, ca := range msg.GetCertificateAuthority() {
		wg.Add(1)
		go func(i int, ca *admin.CertificateAuthorityConfig) {
			defer wg.Done()
			content, meta, err := fetchConfigFile(c, ca.CertPath)
			if err != nil {
				logging.Errorf(c, "Failed to fetch %q: %s", ca.CertPath, err)
				me.Assign(i, err)
			} else if err := importCA(c, ca, content, meta.Revision); err != nil {
				logging.Errorf(c, "Failed to import %q: %s", ca.Cn, err)
				me.Assign(i, err)
			}
		}(i, ca)
	}
	wg.Wait()
	if err = me.Get(); err != nil {
		return nil, status.Errorf(codes.Internal, "can't import CA - %s", err)
	}

	// Find CAs that were removed from the config.
	var toRemove []string
	q := ds.NewQuery("CA").Eq("Removed", false).KeysOnly(true)
	err = ds.Run(c, q, func(k *ds.Key) {
		if !seenCAs.Has(k.StringID()) {
			toRemove = append(toRemove, k.StringID())
		}
	})
	if err != nil {
		return nil, status.Errorf(codes.Internal, "datastore error - %s", err)
	}

	// Mark them as inactive in the datastore.
	wg = sync.WaitGroup{}
	me = errors.NewLazyMultiError(len(toRemove))
	for i, name := range toRemove {
		wg.Add(1)
		go func(i int, name string) {
			defer wg.Done()
			if err := removeCA(c, name, meta.Revision); err != nil {
				logging.Errorf(c, "Failed to remove %q: %s", name, err)
				me.Assign(i, err)
			}
		}(i, name)
	}
	wg.Wait()
	if err = me.Get(); err != nil {
		return nil, status.Errorf(codes.Internal, "datastore error - %s", err)
	}

	return &admin.ImportedConfigs{Revision: meta.Revision}, nil
}
1019395
func (sd structDef) Imports() []string {
	ip := map[string]struct{}{}

	if sd.needFmt() {
		ip[`"fmt"`] = struct{}{}
	}
	if sd.OneLineDef == "" {
		ip[`"gopkg.in/validator.v2"`] = struct{}{}
	}

	// libraries
	for _, fd := range sd.Fields {
		if fd.fieldType == "json.RawMessage" {
			ip[`"encoding/json"`] = struct{}{}
		} else if lib := libImportPath(globRootImportPath, fd.fieldType, globLibRootURLs); lib != "" {
			ip[lib] = struct{}{}
		}
	}
	return commons.MapToSortedStrings(ip)
}
315932
func (f *fragment) bulkImport(rowIDs, columnIDs []uint64, options *ImportOptions) error {
	// Verify that there are an equal number of row ids and column ids.
	if len(rowIDs) != len(columnIDs) {
		return fmt.Errorf("mismatch of row/column len: %d != %d", len(rowIDs), len(columnIDs))
	}

	if f.mutexVector != nil && !options.Clear {
		return f.bulkImportMutex(rowIDs, columnIDs)
	}
	return f.bulkImportStandard(rowIDs, columnIDs, options)
}
982620
func (r Account) GetDatacentersWithSubnetAllocations() (resp []datatypes.Location, err error) {
	err = r.Session.DoRequest("SoftLayer_Account", "getDatacentersWithSubnetAllocations", nil, &r.Options, &resp)
	return
}
559861
func BindImporter(opts SassOptions, resolver ImportResolver) int {

	idx := globalImports.Set(resolver)
	ptr := unsafe.Pointer(uintptr(idx))

	imper := C.sass_make_importer(
		C.Sass_Importer_Fn(C.SassImporterHandler),
		C.double(0),
		ptr,
	)
	impers := C.sass_make_importer_list(1)
	C.sass_importer_set_list_entry(impers, 0, imper)

	C.sass_option_set_c_importers(
		(*C.struct_Sass_Options)(unsafe.Pointer(opts)),
		impers,
	)
	return idx
}
353592
func listDatacenters(c context.Context, names stringset.Set) ([]*crimson.Datacenter, error) {
	db := database.Get(c)
	rows, err := db.QueryContext(c, `
		SELECT name, description, state
		FROM datacenters
	`)
	if err != nil {
		return nil, errors.Annotate(err, "failed to fetch datacenters").Err()
	}
	defer rows.Close()

	var datacenters []*crimson.Datacenter
	for rows.Next() {
		dc := &crimson.Datacenter{}
		if err = rows.Scan(&dc.Name, &dc.Description, &dc.State); err != nil {
			return nil, errors.Annotate(err, "failed to fetch datacenter").Err()
		}
		if matches(dc.Name, names) {
			datacenters = append(datacenters, dc)
		}
	}
	return datacenters, nil
}
903413
func (r *Router) GetDatacenters() []string {
	r.RLock()
	defer r.RUnlock()

	dcs := make([]string, 0, len(r.managers))
	for dc := range r.managers {
		dcs = append(dcs, dc)
	}

	sort.Strings(dcs)
	return dcs
}
705536
func (c *Catalog) ListDatacenters(args *struct{}, reply *[]string) error {
	dcs, err := c.srv.router.GetDatacentersByDistance()
	if err != nil {
		return err
	}

	if len(dcs) == 0 { // no WAN federation, so return the local data center name
		dcs = []string{c.srv.config.Datacenter}
	}

	*reply = dcs
	return nil
}
851144
func importPaths(args []string) []string {
	args = importPathsNoDotExpansion(args)
	var out []string
	for _, a := range args {
		if strings.Contains(a, "...") {
			if build.IsLocalImport(a) {
				out = append(out, allPackagesInFS(a)...)
			} else {
				out = append(out, allPackages(a)...)
			}
			continue
		}
		out = append(out, a)
	}
	return out
}
1163862
func (t *SwitchesTable) computeChanges(c context.Context, datacenters []*config.Datacenter) error {
	cfgs := make(map[string]*Switch, len(datacenters))
	for _, dc := range datacenters {
		for _, rack := range dc.Rack {
			for _, cfg := range rack.Switch {
				id, ok := t.racks[rack.Name]
				if !ok {
					return errors.Reason("failed to determine rack ID for switch %q: rack %q does not exist", cfg.Name, rack.Name).Err()
				}
				cfgs[cfg.Name] = &Switch{
					Switch: config.Switch{
						Name:        cfg.Name,
						Description: cfg.Description,
						Ports:       cfg.Ports,
						State:       cfg.State,
					},
					RackId: id,
				}
			}
		}
	}

	for _, s := range t.current {
		if cfg, ok := cfgs[s.Name]; ok {
			// Switch found in the config.
			if t.needsUpdate(s, cfg) {
				// Switch doesn't match the config.
				cfg.Id = s.Id
				t.updates = append(t.updates, cfg)
			}
			// Record that the switch config has been seen.
			delete(cfgs, cfg.Name)
		} else {
			// Switch not found in the config.
			t.removals = append(t.removals, s)
		}
	}

	// Switches remaining in the map are present in the config but not the database.
	// Iterate deterministically over the slices to determine which switches need to be added.
	for _, dc := range datacenters {
		for _, rack := range dc.Rack {
			for _, cfg := range rack.Switch {
				if s, ok := cfgs[cfg.Name]; ok {
					t.additions = append(t.additions, s)
				}
			}
		}
	}
	return nil
}
185273
func (o *VCenterDataCenter) VCenterClusters(info *bambou.FetchingInfo) (VCenterClustersList, *bambou.Error) {

	var list VCenterClustersList
	err := bambou.CurrentSession().FetchChildren(o, VCenterClusterIdentity, &list, info)
	return list, err
}
981440
func (rc *RulesCache) ImportConfigs(c context.Context) (rev string, err error) {
	return rc.policy.ImportConfigs(c)
}