# Path: grafana_dashboards/client/elastic_search.py
# class ElasticSearchExporter(DashboardExporter):
#     def __init__(self, **kwargs):
#         super(ElasticSearchExporter, self).__init__()
#         self._host = os.getenv('ES_HOST', kwargs.get('host'))
#         password = os.getenv('ES_PASSWORD', kwargs.get('password'))
#         username = os.getenv('ES_USERNAME', kwargs.get('username'))
#         use_kerberos = os.getenv('ES_USE_KERBEROS', kwargs.get('use_kerberos'))
# 
#         if use_kerberos:
#             self._connection = KerberosConnection(self._host)
#         else:
#             self._connection = BasicAuthConnection(username, password, self._host)
# 
#     def process_dashboard(self, project_name, dashboard_name, dashboard_data):
#         super(ElasticSearchExporter, self).process_dashboard(project_name, dashboard_name, dashboard_data)
#         body = {'user': 'guest', 'group': 'guest', 'title': dashboard_data['title'], 'tags': dashboard_data['tags'],
#                 'dashboard': json.dumps(dashboard_data)}
#         logger.info("Uploading dashboard '%s' to %s", dashboard_name, self._host)
#         self._connection.make_request('/es/grafana-dash/dashboard/{0}'.format(dashboard_name), body)


import json
from mock import MagicMock
from grafana_dashboards.client.elastic_search import ElasticSearchExporter
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals




__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


def test_elastic_search():
    exporter = ElasticSearchExporter(host='host', username='username', password='password')
    exporter._connection = MagicMock()

    dashboard_data = {'title': 'title', 'tags': []}
    exporter.process_dashboard('project_name', 'dashboard_name', dashboard_data)

    body = {'user': 'guest', 'group': 'guest', 'title': 'title', 'tags': [], 'dashboard': json.dumps(dashboard_data)}
    # noinspection PyProtectedMember
    exporter._connection.make_request.assert_called_once_with('/es/grafana-dash/dashboard/dashboard_name',
                                                              body)


def test_elastic_search_with_kerberos():

# Path: grafana_dashboards/client/elastic_search.py
# class ElasticSearchExporter(DashboardExporter):
#     def __init__(self, **kwargs):
#         super(ElasticSearchExporter, self).__init__()
#         self._host = os.getenv('ES_HOST', kwargs.get('host'))
#         password = os.getenv('ES_PASSWORD', kwargs.get('password'))
#         username = os.getenv('ES_USERNAME', kwargs.get('username'))
#         use_kerberos = os.getenv('ES_USE_KERBEROS', kwargs.get('use_kerberos'))
# 
#         if use_kerberos:
#             self._connection = KerberosConnection(self._host)
#         else:
#             self._connection = BasicAuthConnection(username, password, self._host)
# 
#     def process_dashboard(self, project_name, dashboard_name, dashboard_data):
#         super(ElasticSearchExporter, self).process_dashboard(project_name, dashboard_name, dashboard_data)
#         body = {'user': 'guest', 'group': 'guest', 'title': dashboard_data['title'], 'tags': dashboard_data['tags'],
#                 'dashboard': json.dumps(dashboard_data)}
#         logger.info("Uploading dashboard '%s' to %s", dashboard_name, self._host)
#         self._connection.make_request('/es/grafana-dash/dashboard/{0}'.format(dashboard_name), body)


# Path: tests/grafana_dashboards/client/test_elastic_search.py
import json
from mock import MagicMock
from grafana_dashboards.client.elastic_search import ElasticSearchExporter

#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals




__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


def test_elastic_search():
    exporter = ElasticSearchExporter(host='host', username='username', password='password')
    exporter._connection = MagicMock()

    dashboard_data = {'title': 'title', 'tags': []}
    exporter.process_dashboard('project_name', 'dashboard_name', dashboard_data)

    body = {'user': 'guest', 'group': 'guest', 'title': 'title', 'tags': [], 'dashboard': json.dumps(dashboard_data)}
    # noinspection PyProtectedMember
    exporter._connection.make_request.assert_called_once_with('/es/grafana-dash/dashboard/dashboard_name',
                                                              body)


def test_elastic_search_with_kerberos():

    exporter = ElasticSearchExporter(host='host', use_kerberos='true')