func (c *collector) authenticate(cfg interface{}, tenant string) error {
	if _, found := c.providers[tenant]; !found {
		// get credentials and endpoint from configuration
		items, err := config.GetConfigItems(cfg, "endpoint", "user", "password")
		if err != nil {
			return err
		}

		endpoint := items["endpoint"].(string)
		user := items["user"].(string)
		password := items["password"].(string)

		provider, err := openstackintel.Authenticate(endpoint, user, password, tenant)
		if err != nil {
			return err
		}
		// set provider and dispatch API version based on priority
		c.providers[tenant] = provider
		c.service = services.Dispatch(provider)

		// set Commoner interface
		c.common = openstackintel.Common{}
	}

	return nil
}
// init initializes InfluxdbCollector instance based on config `cfg`
func (ic *InfluxdbCollector) init(cfg interface{}) {
	settings, err := config.GetConfigItems(cfg, "host", "port", "user", "password")
	handleErr(err)

	err = ic.service.InitURLs(settings)
	handleErr(err)

	ic.initialized = true
}
func getServer(cfg interface{}) string {
	items, err := config.GetConfigItems(cfg, esHost, esPort)
	if err != nil {
		log.Fatal(err.Error())
	}

	server := items[esHost].(string)
	port := items[esPort].(int)

	return fmt.Sprintf("%s:%d", server, port)
}
func getTenants(cfg interface{}) (map[string]string, error) {
	items, err := config.GetConfigItems(cfg, "endpoint", "user", "password")
	if err != nil {
		return nil, err
	}

	endpoint := items["endpoint"].(string)
	user := items["user"].(string)
	password := items["password"].(string)

	// retrieve list of all available tenants for provided endpoint, user and password
	cmn := openstackintel.Common{}
	allTenants, err := cmn.GetTenants(endpoint, user, password)
	if err != nil {
		return nil, err
	}

	return allTenants, nil
}
func TestMesos_CollectMetrics(t *testing.T) {
	cfg := setupCfg()
	cfgItems, err := config.GetConfigItems(cfg, []string{"master", "agent"}...)
	if err != nil {
		panic(err)
	}

	// Clean slate
	teardown(cfgItems["master"].(string))
	launchTasks(cfgItems["master"].(string), cfgItems["agent"].(string))

	Convey("Collect metrics from a Mesos master and agent", t, func() {
		mc := NewMesosCollector()
		_, err := mc.GetMetricTypes(cfg)
		if err != nil {
			panic(err)
		}

		Convey("Should collect requested metrics from the master", func() {
			mts := []plugin.MetricType{
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "master", "master", "tasks_running"),
					Config_:    cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "master", "registrar", "state_store_ms", "p99"),
					Config_:    cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "master", "system", "load_5min"),
					Config_:    cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "master").
						AddDynamicElement("framework_id", "Framework ID").
						AddStaticElements("used_resources", "cpus"),
					Config_: cfg.ConfigDataNode,
				},
			}

			metrics, err := mc.CollectMetrics(mts)
			So(err, ShouldBeNil)
			So(metrics, ShouldNotBeNil)
			So(len(metrics), ShouldEqual, 5)
			So(metrics[0].Namespace().String(), ShouldEqual, "/intel/mesos/master/master/tasks_running")
			So(metrics[1].Namespace().String(), ShouldEqual, "/intel/mesos/master/registrar/state_store_ms/p99")
			So(metrics[2].Namespace().String(), ShouldEqual, "/intel/mesos/master/system/load_5min")
			So(metrics[3].Namespace().Strings()[4], ShouldEqual, "used_resources")
		})

		// NOTE: in future versions of Mesos, the term "slave" will change to "agent". This has the potential
		// to break this test if the Mesos version is bumped in CI and this test isn't updated at the same time.
		Convey("Should collect requested metrics from the agent", func() {
			mts := []plugin.MetricType{
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "agent", "slave", "tasks_running"),
					Config_:    cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "agent", "system", "load_5min"),
					Config_:    cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "agent").
						AddDynamicElement("framework_id", "Framework ID").
						AddDynamicElement("executor_id", "Executor ID").
						AddStaticElement("cpus_system_time_secs"),
					Config_: cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "agent").
						AddDynamicElement("framework_id", "Framework ID").
						AddDynamicElement("executor_id", "Executor ID").
						AddStaticElement("disk_used_bytes"),
					Config_: cfg.ConfigDataNode,
				},
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "agent").
						AddDynamicElement("framework_id", "Framework ID").
						AddDynamicElement("executor_id", "Executor ID").
						AddStaticElement("mem_total_bytes"),
					Config_: cfg.ConfigDataNode,
				},
			}

			metrics, err := mc.CollectMetrics(mts)
			So(err, ShouldBeNil)
			So(metrics, ShouldNotBeNil)
			So(len(metrics), ShouldEqual, 8)
			So(metrics[0].Namespace().String(), ShouldEqual, "/intel/mesos/agent/slave/tasks_running")
			So(metrics[1].Namespace().String(), ShouldEqual, "/intel/mesos/agent/system/load_5min")
			So(metrics[2].Namespace().Strings()[5], ShouldEqual, "cpus_system_time_secs")
			So(metrics[4].Namespace().Strings()[5], ShouldEqual, "disk_used_bytes")
			So(metrics[6].Namespace().Strings()[5], ShouldEqual, "mem_total_bytes")
		})

		Convey("Should return an error if an invalid metric was requested", func() {
			mts := []plugin.MetricType{
				plugin.MetricType{
					Namespace_: core.NewNamespace("intel", "mesos", "master", "foo", "bar", "baz"),
					Config_:    cfg.ConfigDataNode,
				},
			}

			metrics, err := mc.CollectMetrics(mts)
			So(metrics, ShouldBeNil)
			So(err, ShouldNotBeNil)
		})
	})

	teardown(cfgItems["master"].(string))
}