func OverwriteSettings()

in cloudid/cloudid.go [149:213]


func OverwriteSettings(cfg *config.C) error {

	logger := logp.NewLogger("cloudid")
	cloudID, _ := cfg.String("cloud.id", -1)
	cloudAuth, _ := cfg.String("cloud.auth", -1)

	if cloudID == "" && cloudAuth == "" {
		// nothing to hack
		return nil
	}

	logger.Debugf("cloud.id: %s, cloud.auth: %s", cloudID, cloudAuth)
	if cloudID == "" {
		return errors.New("cloud.auth specified but cloud.id is empty. Please specify both")
	}

	// cloudID overwrites
	cid, err := NewCloudID(cloudID, cloudAuth)
	if err != nil {
		return fmt.Errorf("error decoding cloud.id: %w", err)
	}

	logger.Infof("Setting Elasticsearch and Kibana URLs based on the cloud id: output.elasticsearch.hosts=%s and setup.kibana.host=%s", cid.esURL, cid.kibURL)

	esURLConfig, err := config.NewConfigFrom([]string{cid.ElasticsearchURL()})
	if err != nil {
		return err
	}

	// Before enabling the ES output, check that no other output is enabled
	tmp := struct {
		Output config.Namespace `config:"output"`
	}{}
	if err := cfg.Unpack(&tmp); err != nil {
		return err
	}
	if out := tmp.Output; out.IsSet() && out.Name() != "elasticsearch" {
		return fmt.Errorf("the cloud.id setting enables the Elasticsearch output, but you already have the %s output enabled in the config", out.Name())
	}

	err = cfg.SetChild("output.elasticsearch.hosts", -1, esURLConfig)
	if err != nil {
		return err
	}

	err = cfg.SetString("setup.kibana.host", -1, cid.KibanaURL())
	if err != nil {
		return err
	}

	if cloudAuth != "" {
		// cloudAuth overwrites
		err = cfg.SetString("output.elasticsearch.username", -1, cid.Username())
		if err != nil {
			return err
		}

		err = cfg.SetString("output.elasticsearch.password", -1, cid.Password())
		if err != nil {
			return err
		}
	}

	return nil
}