Skip to content

Commit 17eaed3

Browse files
[BUGFIX] Python 2 leftovers clean-up
- Replace basestring with str - Drop .encode('utf-8) calls - Fix broken urllib import - Add explicit dependency on requests
1 parent acf250a commit 17eaed3

4 files changed

Lines changed: 38 additions & 49 deletions

File tree

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ azure-mgmt-resource==26.0.0
1111
Jinja2==3.1.6
1212
hashmerge==0.2
1313
py-consul==1.7.1
14+
requests==2.34.2
1415
hvac==2.4.0
1516
passgen==1.1.1
1617
inflection==0.5.1

‎src/ops/ansible/filter_plugins/commonfilters.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#OF ANY KIND, either express or implied. See the License for the specific language
99
#governing permissions and limitations under the License.
1010

11-
from __future__ import absolute_import
1211
import os
1312
from ops.cli import display
1413

‎src/ops/inventory/SKMS.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@
1717
"""
1818

1919
# This version of the class requires the following:
20-
# - Python 2.6
20+
# - Python 3.12
2121
# - Python support for Requests
2222
# - Python support for JSON
23-
# @version v1.10, 2016-06-30
2423
import socket
2524
import os
2625
import getpass
2726
import requests
2827
import inspect
29-
import urllib
28+
from urllib.parse import quote
3029
import json
3130
from os.path import expanduser
3231

3332

3433
class WebApiClient(object):
3534
"""Class to allow easy access to the SKMS Web API"""
3635
# Version Constants
37-
CLIENT_TYPE = "python3.7.requests"
36+
CLIENT_TYPE = "python3.12.requests"
3837
CLIENT_VERSION = "1.10"
3938

4039
# Properties
@@ -150,7 +149,7 @@ def enable_skms_session_optimization(self, skms_session_storage_file):
150149
if (
151150
isinstance(session_info, dict) and
152151
'skms_csrf_token' in session_info and
153-
isinstance(session_info['skms_csrf_token'], basestring) and
152+
isinstance(session_info['skms_csrf_token'], str) and
154153
session_info['skms_csrf_token'].strip() != ""
155154
):
156155
self.set_skms_csrf_token(session_info['skms_csrf_token'])
@@ -166,7 +165,7 @@ def get_skms_session_id(self):
166165
def set_skms_session_id(self, skms_session_id):
167166
"""Set the SKMS session id"""
168167
if (
169-
isinstance(skms_session_id, basestring) and
168+
isinstance(skms_session_id, str) and
170169
skms_session_id.strip() != ""
171170
):
172171
self.skms_session_id = skms_session_id
@@ -267,7 +266,7 @@ def send_request(self, object_name, method_name, method_param_dict=None):
267266
verify=self.verify_ssl_chain,
268267
timeout=self.request_timeout,
269268
cert=self.trusted_cert_file_path,
270-
cookies={cookie_name: urllib.quote(self.skms_session_id)}
269+
cookies={cookie_name: quote(self.skms_session_id)}
271270
)
272271
else:
273272
# Creating new session
@@ -434,7 +433,7 @@ def get_message_list_by_type(self, message_type=''):
434433
):
435434
for message in response_dict['messages']:
436435
if (
437-
isinstance(message_type, basestring) and
436+
isinstance(message_type, str) and
438437
(message_type.strip() ==
439438
"" or message['type'].lower() == message_type.lower())
440439
):

‎src/ops/inventory/plugin/skms.py‎

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,76 +120,66 @@ def skms(args):
120120

121121
# step through device services to add them as groups
122122
for device_service in info['device_service']:
123-
device_service = device_service.replace(
124-
device_service_strip, '').encode('utf-8')
123+
device_service = device_service.replace(device_service_strip, '')
125124

126125
# add device service to top-level dictionary as a group
127126
if device_service not in dictionary_of_hosts:
128127
dictionary_of_hosts[device_service] = {"hosts": []}
129128
# add host to array of that device service group
130129
if info['name'] not in dictionary_of_hosts[device_service]['hosts']:
131-
dictionary_of_hosts[device_service]['hosts'].append(
132-
info['name'].encode('utf-8'))
130+
dictionary_of_hosts[device_service]['hosts'].append(info['name'])
133131

134132
# add environment to top-level dictionary as a group
135133
if info['environment'] not in dictionary_of_hosts:
136-
dictionary_of_hosts[info['environment'].encode(
137-
'utf-8')] = {"hosts": []}
134+
dictionary_of_hosts[info['environment']] = {"hosts": []}
138135
# add host to array of that environment group
139136
if info['name'] not in dictionary_of_hosts[info['environment']]['hosts']:
140-
dictionary_of_hosts[info['environment']]['hosts'].append(
141-
info['name'].encode('utf-8'))
137+
dictionary_of_hosts[info['environment']]['hosts'].append(info['name'])
142138

143139
# add primary_ip_address to top-level dictionary as a group
144140
if info['primary_ip_address'] not in dictionary_of_hosts:
145-
dictionary_of_hosts[info['primary_ip_address'].encode(
146-
'utf-8')] = {"hosts": []}
141+
dictionary_of_hosts[info['primary_ip_address']] = {"hosts": []}
147142
if info['name'] not in dictionary_of_hosts[info['primary_ip_address']]['hosts']:
148-
dictionary_of_hosts[info['primary_ip_address']
149-
]['hosts'].append(info['name'].encode('utf-8'))
143+
dictionary_of_hosts[info['primary_ip_address']]['hosts'].append(info['name'])
150144

151145
# add owner to top-level dictionary as a group
152146
if info['owner'] not in dictionary_of_hosts:
153-
dictionary_of_hosts[info['owner'].encode('utf-8')] = {"hosts": []}
147+
dictionary_of_hosts[info['owner']] = {"hosts": []}
154148
if info['name'] not in dictionary_of_hosts[info['owner']]['hosts']:
155-
dictionary_of_hosts[info['owner']]['hosts'].append(
156-
info['name'].encode('utf-8'))
149+
dictionary_of_hosts[info['owner']]['hosts'].append(info['name'])
157150

158151
# add site to top-level dictionary as a group
159152
if info['site'] not in dictionary_of_hosts:
160-
dictionary_of_hosts[info['site'].encode('utf-8')] = {"hosts": []}
153+
dictionary_of_hosts[info['site']] = {"hosts": []}
161154
if info['name'] not in dictionary_of_hosts[info['site']]['hosts']:
162-
dictionary_of_hosts[info['site']]['hosts'].append(
163-
info['name'].encode('utf-8'))
155+
dictionary_of_hosts[info['site']]['hosts'].append(info['name'])
164156

165157
# add cluster to top-level dictionary as a group
166158
if info['cluster'] not in dictionary_of_hosts:
167-
dictionary_of_hosts[info['cluster'].encode(
168-
'utf-8')] = {"hosts": []}
159+
dictionary_of_hosts[info['cluster']] = {"hosts": []}
169160
if info['name'] not in dictionary_of_hosts[info['cluster']]['hosts']:
170-
dictionary_of_hosts[info['cluster']]['hosts'].append(
171-
info['name'].encode('utf-8'))
161+
dictionary_of_hosts[info['cluster']]['hosts'].append(info['name'])
172162

173163
# tie some extra information to hostname in the meta variables
174-
dictionary_of_hosts['_meta']['hostvars'][info['name'].encode('utf-8')] = {
175-
'ec2_id': info['device_id'].encode('utf-8'),
176-
'ansible_ssh_host': info['primary_ip_address'].encode('utf-8'),
177-
'ansible_host': info['primary_ip_address'].encode('utf-8'),
178-
'computer_name': info['computer_name'].encode('utf-8'),
179-
'location': info['location_name'].encode('utf-8'),
180-
'name': info['name'].split('.')[0].encode('utf-8'),
181-
'operating_system': info['operating_system'].encode('utf-8'),
182-
'private_ip': info['primary_ip_address'].encode('utf-8'),
164+
dictionary_of_hosts['_meta']['hostvars'][info['name']] = {
165+
'ec2_id': info['device_id'],
166+
'ansible_ssh_host': info['primary_ip_address'],
167+
'ansible_host': info['primary_ip_address'],
168+
'computer_name': info['computer_name'],
169+
'location': info['location_name'],
170+
'name': info['name'].split('.')[0],
171+
'operating_system': info['operating_system'],
172+
'private_ip': info['primary_ip_address'],
183173
'tags': {
184-
'Adobe:Environment': info['environment'].encode('utf-8'),
185-
'Adobe:Owner': info['owner'].encode('utf-8'),
186-
'CMDB_device_service': device_service.encode('utf-8'),
187-
'CMDB_environment': info['environment'].encode('utf-8'),
188-
'CMDB_hostname': info['name'].split('.')[0].encode('utf-8'),
189-
'cluster': info['cluster'].encode('utf-8'),
190-
'environment': info['environment'].encode('utf-8'),
191-
'role': device_service.encode('utf-8'),
192-
'site': info['site'].encode('utf-8'),
174+
'Adobe:Environment': info['environment'],
175+
'Adobe:Owner': info['owner'],
176+
'CMDB_device_service': device_service,
177+
'CMDB_environment': info['environment'],
178+
'CMDB_hostname': info['name'].split('.')[0],
179+
'cluster': info['cluster'],
180+
'environment': info['environment'],
181+
'role': device_service,
182+
'site': info['site'],
193183
}
194184
}
195185

0 commit comments

Comments
 (0)