Skip to content

Commit

Permalink
Support Python 3 (#3035)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmuesch authored Jan 25, 2019
1 parent d92197d commit 270f90d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ jobs:
- stage: test
env: CHECK=openldap PYTHON3=true
- stage: test
env: CHECK=openstack
env: CHECK=openstack PYTHON3=true
- stage: test
env: CHECK=openstack_controller PYTHON3=true
- stage: test
Expand Down
17 changes: 10 additions & 7 deletions openstack/datadog_checks/openstack/openstack.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
from __future__ import division

from datetime import datetime, timedelta
from urlparse import urljoin
import re
import time
import random
import copy

import requests
import simplejson as json
from six import iteritems
from six.moves.urllib.parse import urljoin

from datadog_checks.checks import AgentCheck
from datadog_checks.config import is_affirmative
Expand Down Expand Up @@ -612,7 +615,7 @@ def _instance_key(self, instance):

def delete_current_scope(self):
scope_to_delete = self._parent_scope if self._parent_scope else self._current_scope
for i_key, scope in self.instance_map.items():
for i_key, scope in list(self.instance_map.items()):
if scope is scope_to_delete:
self.log.debug("Deleting current scope: %s", i_key)
del self.instance_map[i_key]
Expand All @@ -635,7 +638,7 @@ def do_backoff(self, instance):
jitter = min(MAX_BACKOFF_SECS, BASE_BACKOFF_SECS * 2 ** self.backoff[i_key]['retries'])

# let's add some jitter (half jitter)
backoff_interval = jitter / 2
backoff_interval = jitter // 2
backoff_interval += random.randint(0, backoff_interval)

tags = instance.get('tags', [])
Expand Down Expand Up @@ -760,7 +763,7 @@ def _parse_uptime_string(self, uptime):
load_averages = uptime[uptime.find('load average:'):].split(':')[1].split(',')
uptime_sec = uptime.split(',')[0]

return {'loads': map(float, load_averages), 'uptime_sec': uptime_sec}
return {'loads': list(map(float, load_averages)), 'uptime_sec': uptime_sec}

def get_all_hypervisor_ids(self, filter_by_host=None):
nova_version = self.init_config.get("nova_api_version", DEFAULT_NOVA_API_VERSION)
Expand Down Expand Up @@ -861,7 +864,7 @@ def get_stats_for_single_hypervisor(self, hyp_id, instance, host_tags=None, cust
else:
self.service_check(self.HYPERVISOR_SC, AgentCheck.OK, tags=service_check_tags)

for label, val in hyp.iteritems():
for label, val in iteritems(hyp):
if label in NOVA_HYPERVISOR_METRICS:
metric_label = "openstack.nova.{0}".format(label)
self.gauge(metric_label, val, tags=tags)
Expand Down Expand Up @@ -1205,7 +1208,7 @@ def check(self, instance):
# The scopes we iterate over should all be OpenStackProjectScope
# instances
projects = []
for _, scope in scope_map.iteritems():
for _, scope in iteritems(scope_map):
# Store the scope on the object so we don't have to keep passing it around
self._current_scope = scope

Expand Down Expand Up @@ -1417,7 +1420,7 @@ def get_external_host_tags(self):
"""
self.log.debug("Collecting external_host_tags now")
external_host_tags = []
for k, v in self.external_host_tags.iteritems():
for k, v in iteritems(self.external_host_tags):
external_host_tags.append((k, {SOURCE_TYPE: v}))

self.log.debug("Sending external_host_tags: %s", external_host_tags)
Expand Down
11 changes: 4 additions & 7 deletions openstack/tests/test_openstack.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

# stdlib
import copy
import re
import time

# 3p
import mock
import pytest
from six import iteritems

# project
import common
from . import common

from datadog_checks.checks import AgentCheck
from datadog_checks.openstack.openstack import (
OpenStackCheck,
OpenStackProjectScope,
Expand All @@ -24,7 +22,6 @@
IncompleteIdentity
)

from datadog_checks.checks import AgentCheck

instance = common.MOCK_CONFIG["instances"][0]
instance['tags'] = ['optional:tag1']
Expand Down Expand Up @@ -146,7 +143,7 @@ def test_unscoped_from_config():

assert scope.auth_token == 'fake_token'
assert len(scope.project_scope_map) == 1
for _, scope in scope.project_scope_map.iteritems():
for _, scope in iteritems(scope.project_scope_map):
assert isinstance(scope, OpenStackProjectScope)
assert scope.auth_token == 'fake_token'
assert scope.tenant_id == '263fd9'
Expand Down
4 changes: 1 addition & 3 deletions openstack/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
minversion = 2.0
basepython = py27
envlist =
openstack
{py27,py36}-openstack
flake8

[testenv]
usedevelop = true
platform = linux|darwin|win32

[testenv:openstack]
deps =
-e../datadog_checks_base[deps]
-rrequirements-dev.txt
Expand Down

0 comments on commit 270f90d

Please sign in to comment.