Replace json module usage with a helper to ensure unicode content is handled properly

This adds wrappers for json.dump{,s} which disable `ensure_ascii` by
default.
This commit is contained in:
Erik Johnson 2017-12-22 13:54:53 -06:00
parent 66e090e75d
commit 7b13a7df8b
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F
222 changed files with 1676 additions and 1546 deletions

View file

@ -72,7 +72,7 @@ Other optional functions can be included to add support for
.. code-block:: python .. code-block:: python
import redis import redis
import json import salt.utils.json
def returner(ret): def returner(ret):
''' '''
@ -84,7 +84,7 @@ Other optional functions can be included to add support for
port=6379, port=6379,
db='0') db='0')
serv.sadd("%(id)s:jobs" % ret, ret['jid']) serv.sadd("%(id)s:jobs" % ret, ret['jid'])
serv.set("%(jid)s:%(id)s" % ret, json.dumps(ret['return'])) serv.set("%(jid)s:%(id)s" % ret, salt.utils.json.dumps(ret['return']))
serv.sadd('jobs', ret['jid']) serv.sadd('jobs', ret['jid'])
serv.sadd(ret['jid'], ret['id']) serv.sadd(ret['jid'], ret['id'])
@ -168,6 +168,8 @@ must implement the following functions:
.. code-block:: python .. code-block:: python
import salt.utils.json
def save_load(jid, load): def save_load(jid, load):
''' '''
Save the load to the specified jid id Save the load to the specified jid id
@ -176,7 +178,7 @@ must implement the following functions:
jid, load jid, load
) VALUES ( ) VALUES (
'{0}', '{1}' '{0}', '{1}'
);'''.format(jid, json.dumps(load)) );'''.format(jid, salt.utils.json.dumps(load))
# cassandra_cql.cql_query may raise a CommandExecutionError # cassandra_cql.cql_query may raise a CommandExecutionError
try: try:
@ -185,8 +187,9 @@ must implement the following functions:
log.critical('Could not save load in jids table.') log.critical('Could not save load in jids table.')
raise raise
except Exception as e: except Exception as e:
log.critical('''Unexpected error while inserting into log.critical(
jids: {0}'''.format(str(e))) 'Unexpected error while inserting into jids: {0}'.format(e)
)
raise raise
@ -316,6 +319,8 @@ contains the jid and therefore is guaranteed to be unique.
.. code-block:: python .. code-block:: python
import salt.utils.json
def event_return(events): def event_return(events):
''' '''
Return event to mysql server Return event to mysql server
@ -329,7 +334,7 @@ contains the jid and therefore is guaranteed to be unique.
data = event.get('data', '') data = event.get('data', '')
sql = '''INSERT INTO `salt_events` (`tag`, `data`, `master_id` ) sql = '''INSERT INTO `salt_events` (`tag`, `data`, `master_id` )
VALUES (%s, %s, %s)''' VALUES (%s, %s, %s)'''
cur.execute(sql, (tag, json.dumps(data), __opts__['id'])) cur.execute(sql, (tag, salt.utils.json.dumps(data), __opts__['id']))
Testing the Returner Testing the Returner

View file

@ -753,7 +753,7 @@ This proxymodule enables "package" installation.
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs # Import python libs
import json import salt.utils.json
import logging import logging
# Import Salt's libs # Import Salt's libs
@ -821,7 +821,7 @@ This proxymodule enables "package" installation.
jsonret.append(ln_) jsonret.append(ln_)
if '}' in ln_: if '}' in ln_:
in_json = False in_json = False
return json.loads('\n'.join(jsonret)) return salt.utils.json.loads('\n'.join(jsonret))
def package_list(): def package_list():

View file

@ -3,11 +3,10 @@
Create ssh executor system Create ssh executor system
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import base64 import base64
import copy import copy
import getpass import getpass
import json
import logging import logging
import multiprocessing import multiprocessing
import subprocess import subprocess
@ -1017,7 +1016,7 @@ class Single(object):
if '_error' in opts_pkg: if '_error' in opts_pkg:
#Refresh failed #Refresh failed
retcode = opts_pkg['retcode'] retcode = opts_pkg['retcode']
ret = json.dumps({'local': opts_pkg}) ret = salt.utils.json.dumps({'local': opts_pkg})
return ret, retcode return ret, retcode
opts_pkg['file_roots'] = self.opts['file_roots'] opts_pkg['file_roots'] = self.opts['file_roots']
@ -1139,9 +1138,9 @@ class Single(object):
# Mimic the json data-structure that "salt-call --local" will # Mimic the json data-structure that "salt-call --local" will
# emit (as seen in ssh_py_shim.py) # emit (as seen in ssh_py_shim.py)
if isinstance(result, dict) and 'local' in result: if isinstance(result, dict) and 'local' in result:
ret = json.dumps({'local': result['local']}) ret = salt.utils.json.dumps({'local': result['local']})
else: else:
ret = json.dumps({'local': {'return': result}}) ret = salt.utils.json.dumps({'local': {'return': result}})
return ret, retcode return ret, retcode
def _cmd_str(self): def _cmd_str(self):

View file

@ -2,19 +2,19 @@
''' '''
Manage transport commands via ssh Manage transport commands via ssh
''' '''
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
# Import python libs # Import python libs
import re import re
import os import os
import sys import sys
import json
import time import time
import logging import logging
import subprocess import subprocess
# Import salt libs # Import salt libs
import salt.defaults.exitcodes import salt.defaults.exitcodes
import salt.utils.json
import salt.utils.nb_popen import salt.utils.nb_popen
import salt.utils.vt import salt.utils.vt
@ -420,7 +420,7 @@ class Shell(object):
'flag:\n{0}').format(stdout) 'flag:\n{0}').format(stdout)
return ret_stdout, '', 254 return ret_stdout, '', 254
elif buff and buff.endswith('_||ext_mods||_'): elif buff and buff.endswith('_||ext_mods||_'):
mods_raw = json.dumps(self.mods, separators=(',', ':')) + '|_E|0|' mods_raw = salt.utils.json.dumps(self.mods, separators=(',', ':')) + '|_E|0|'
term.sendline(mods_raw) term.sendline(mods_raw)
if stdout: if stdout:
old_stdout = stdout old_stdout = stdout

View file

@ -7,8 +7,7 @@ This is not intended to be instantiated as a module, rather it is a
helper script used by salt.client.ssh.Single. It is here, in a helper script used by salt.client.ssh.Single. It is here, in a
separate file, for convenience of development. separate file, for convenience of development.
''' '''
from __future__ import absolute_import, print_function
from __future__ import absolute_import, print_function, unicode_literals
import hashlib import hashlib
import tarfile import tarfile

View file

@ -2,13 +2,12 @@
''' '''
Create ssh executor system Create ssh executor system
''' '''
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
# Import python libs # Import python libs
import logging import logging
import os import os
import tarfile import tarfile
import tempfile import tempfile
import json
import shutil import shutil
from contextlib import closing from contextlib import closing
@ -16,6 +15,7 @@ from contextlib import closing
import salt.client.ssh.shell import salt.client.ssh.shell
import salt.client.ssh import salt.client.ssh
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.stringutils import salt.utils.stringutils
import salt.utils.thin import salt.utils.thin
@ -181,13 +181,13 @@ def prep_trans_tar(opts, file_client, chunks, file_refs, pillar=None, id_=None,
[salt.utils.url.create('_utils')], [salt.utils.url.create('_utils')],
] ]
with salt.utils.files.fopen(lowfn, 'w+') as fp_: with salt.utils.files.fopen(lowfn, 'w+') as fp_:
fp_.write(salt.utils.stringutils.to_str(json.dumps(chunks))) salt.utils.json.dump(chunks, fp_)
if pillar: if pillar:
with salt.utils.files.fopen(pillarfn, 'w+') as fp_: with salt.utils.files.fopen(pillarfn, 'w+') as fp_:
fp_.write(salt.utils.stringutils.to_str(json.dumps(pillar))) salt.utils.json.dump(pillar, fp_)
if roster_grains: if roster_grains:
with salt.utils.files.fopen(roster_grainsfn, 'w+') as fp_: with salt.utils.files.fopen(roster_grainsfn, 'w+') as fp_:
fp_.write(salt.utils.stringutils.to_str(json.dumps(roster_grains))) salt.utils.json.dump(roster_grains, fp_)
if id_ is None: if id_ is None:
id_ = '' id_ = ''

View file

@ -7,13 +7,13 @@ as ZeroMQ salt, but via ssh.
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import json
import copy import copy
# Import salt libs # Import salt libs
import salt.loader import salt.loader
import salt.utils.data import salt.utils.data
import salt.utils.json
import salt.client.ssh import salt.client.ssh
# Import 3rd-party libs # Import 3rd-party libs
@ -103,8 +103,12 @@ class FunctionWrapper(object):
The remote execution function The remote execution function
''' '''
argv = [cmd] argv = [cmd]
argv.extend([json.dumps(arg) for arg in args]) argv.extend([salt.utils.json.dumps(arg) for arg in args])
argv.extend(['{0}={1}'.format(key, json.dumps(val)) for key, val in six.iteritems(kwargs)]) argv.extend(
['{0}={1}'.format(salt.utils.stringutils.to_str(key),
salt.utils.json.dumps(val))
for key, val in six.iteritems(kwargs)]
)
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
self.opts, self.opts,
argv, argv,
@ -121,7 +125,7 @@ class FunctionWrapper(object):
'stderr': stderr, 'stderr': stderr,
'retcode': retcode} 'retcode': retcode}
try: try:
ret = json.loads(stdout, object_hook=salt.utils.data.decode_dict) ret = salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
if len(ret) < 2 and 'local' in ret: if len(ret) < 2 and 'local' in ret:
ret = ret['local'] ret = ret['local']
ret = ret.get('return', {}) ret = ret.get('return', {})

View file

@ -4,7 +4,7 @@ Return config information
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import re import re
import os import os

View file

@ -3,7 +3,7 @@
Wrap the cp module allowing for managed ssh file transfers Wrap the cp module allowing for managed ssh file transfers
''' '''
# Import Python libs # Import Python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import logging import logging
import os import os

View file

@ -4,15 +4,15 @@ Return/control aspects of the grains data
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import collections import collections
import copy import copy
import math import math
import json
# Import salt libs # Import salt libs
import salt.utils.data import salt.utils.data
import salt.utils.dictupdate import salt.utils.dictupdate
import salt.utils.json
from salt.defaults import DEFAULT_TARGET_DELIM from salt.defaults import DEFAULT_TARGET_DELIM
from salt.exceptions import SaltException from salt.exceptions import SaltException
@ -74,7 +74,7 @@ def get(key, default='', delimiter=DEFAULT_TARGET_DELIM, ordered=True):
if ordered is True: if ordered is True:
grains = __grains__ grains = __grains__
else: else:
grains = json.loads(json.dumps(__grains__)) grains = salt.utils.json.loads(salt.utils.json.dumps(__grains__))
return salt.utils.data.traverse_dict_and_list( return salt.utils.data.traverse_dict_and_list(
__grains__, __grains__,
key, key,

View file

@ -7,7 +7,7 @@ Wrapper function for mine operations for salt-ssh
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import copy import copy
# Import salt libs # Import salt libs

View file

@ -2,7 +2,7 @@
''' '''
Extract the pillar data for this minion Extract the pillar data for this minion
''' '''
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
# Import python libs # Import python libs
import collections import collections

View file

@ -10,7 +10,7 @@ salt-ssh calls and return the data from them.
No access control is needed because calls cannot originate from the minions. No access control is needed because calls cannot originate from the minions.
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function
import copy import copy
import logging import logging

View file

@ -2,12 +2,10 @@
''' '''
Create ssh executor system Create ssh executor system
''' '''
from __future__ import absolute_import, print_function
from __future__ import absolute_import, print_function, unicode_literals
# Import python libs # Import python libs
import os import os
import time import time
import json
import logging import logging
# Import salt libs # Import salt libs
@ -19,6 +17,7 @@ import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.hashutils import salt.utils.hashutils
import salt.utils.jid import salt.utils.jid
import salt.utils.json
import salt.utils.platform import salt.utils.platform
import salt.utils.state import salt.utils.state
import salt.utils.thin import salt.utils.thin
@ -174,7 +173,7 @@ def sls(mods, saltenv='base', test=None, exclude=None, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))
@ -318,7 +317,7 @@ def low(data, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))
@ -407,7 +406,7 @@ def high(data, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))
@ -650,7 +649,7 @@ def highstate(test=None, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))
@ -731,7 +730,7 @@ def top(topfn, test=None, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))
@ -1094,7 +1093,7 @@ def single(fun, name, test=None, **kwargs):
# Read in the JSON data and return the data structure # Read in the JSON data and return the data structure
try: try:
return json.loads(stdout, object_hook=salt.utils.data.decode_dict) return salt.utils.json.loads(stdout, object_hook=salt.utils.data.decode_dict)
except Exception as e: except Exception as e:
log.error("JSON Render failed for: %s\n%s", stdout, stderr) log.error("JSON Render failed for: %s\n%s", stdout, stderr)
log.error(six.text_type(e)) log.error(six.text_type(e))

View file

@ -28,7 +28,6 @@ Set up the cloud configuration at ``/etc/salt/cloud.providers`` or
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import time import time
import json
import pprint import pprint
import logging import logging
import hmac import hmac
@ -43,6 +42,7 @@ from salt.ext.six.moves.urllib.parse import quote as _quote # pylint: disable=i
# Import salt cloud libs # Import salt cloud libs
import salt.utils.cloud import salt.utils.cloud
import salt.utils.data import salt.utils.data
import salt.utils.json
import salt.config as config import salt.config as config
from salt.exceptions import ( from salt.exceptions import (
SaltCloudNotFound, SaltCloudNotFound,
@ -824,7 +824,7 @@ def query(params=None):
content = request.text content = request.text
result = json.loads(content, object_hook=salt.utils.data.encode_dict) result = salt.utils.json.loads(content, object_hook=salt.utils.data.encode_dict)
if 'Code' in result: if 'Code' in result:
raise SaltCloudSystemExit( raise SaltCloudSystemExit(
pprint.pformat(result.get('Message', {})) pprint.pformat(result.get('Message', {}))

View file

@ -63,14 +63,14 @@ cloud configuration at
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import importlib
import logging import logging
import time import time
import json
# Import salt libs # Import salt libs
import importlib
from salt.exceptions import SaltCloudSystemExit
# Import salt cloud libs
import salt.config as config import salt.config as config
import salt.utils.json
from salt.exceptions import SaltCloudSystemExit
# Get logging started # Get logging started
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -157,8 +157,8 @@ def list_nodes_full(call=None, for_output=True):
creds = get_creds() creds = get_creds()
clc.v1.SetCredentials(creds["token"], creds["token_pass"]) clc.v1.SetCredentials(creds["token"], creds["token_pass"])
servers_raw = clc.v1.Server.GetServers(location=None) servers_raw = clc.v1.Server.GetServers(location=None)
servers_raw = json.dumps(servers_raw) servers_raw = salt.utils.json.dumps(servers_raw)
servers = json.loads(servers_raw) servers = salt.utils.json.loads(servers_raw)
return servers return servers
@ -181,8 +181,8 @@ def get_monthly_estimate(call=None, for_output=True):
) )
try: try:
billing_raw = clc.v1.Billing.GetAccountSummary(alias=creds["accountalias"]) billing_raw = clc.v1.Billing.GetAccountSummary(alias=creds["accountalias"])
billing_raw = json.dumps(billing_raw) billing_raw = salt.utils.json.dumps(billing_raw)
billing = json.loads(billing_raw) billing = salt.utils.json.loads(billing_raw)
billing = round(billing["MonthlyEstimate"], 2) billing = round(billing["MonthlyEstimate"], 2)
return {"Monthly Estimate": billing} return {"Monthly Estimate": billing}
except RuntimeError: except RuntimeError:
@ -201,8 +201,8 @@ def get_month_to_date(call=None, for_output=True):
) )
try: try:
billing_raw = clc.v1.Billing.GetAccountSummary(alias=creds["accountalias"]) billing_raw = clc.v1.Billing.GetAccountSummary(alias=creds["accountalias"])
billing_raw = json.dumps(billing_raw) billing_raw = salt.utils.json.dumps(billing_raw)
billing = json.loads(billing_raw) billing = salt.utils.json.loads(billing_raw)
billing = round(billing["MonthToDateTotal"], 2) billing = round(billing["MonthToDateTotal"], 2)
return {"Month To Date": billing} return {"Month To Date": billing}
except RuntimeError: except RuntimeError:
@ -243,8 +243,8 @@ def get_group_estimate(call=None, for_output=True, **kwargs):
) )
try: try:
billing_raw = clc.v1.Billing.GetGroupEstimate(group=group, alias=creds["accountalias"], location=location) billing_raw = clc.v1.Billing.GetGroupEstimate(group=group, alias=creds["accountalias"], location=location)
billing_raw = json.dumps(billing_raw) billing_raw = salt.utils.json.dumps(billing_raw)
billing = json.loads(billing_raw) billing = salt.utils.json.loads(billing_raw)
estimate = round(billing["MonthlyEstimate"], 2) estimate = round(billing["MonthlyEstimate"], 2)
month_to_date = round(billing["MonthToDate"], 2) month_to_date = round(billing["MonthToDate"], 2)
return {"Monthly Estimate": estimate, "Month to Date": month_to_date} return {"Monthly Estimate": estimate, "Month to Date": month_to_date}

View file

@ -27,16 +27,16 @@ under the "SSH Keys" section.
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import os
import time
import json
import pprint
import logging
import decimal import decimal
import logging
import os
import pprint
import time
# Import Salt Libs # Import Salt Libs
import salt.utils.cloud import salt.utils.cloud
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.config as config import salt.config as config
from salt.exceptions import ( from salt.exceptions import (
SaltCloudConfigError, SaltCloudConfigError,
@ -562,7 +562,7 @@ def query(method='droplets', droplet_id=None, command=None, args=None, http_meth
'personal_access_token', get_configured_provider(), __opts__, search_global=False 'personal_access_token', get_configured_provider(), __opts__, search_global=False
) )
data = json.dumps(args) data = salt.utils.json.dumps(args)
requester = getattr(requests, http_method) requester = getattr(requests, http_method)
request = requester(path, data=data, headers={'Authorization': 'Bearer ' + personal_access_token, 'Content-Type': 'application/json'}) request = requester(path, data=data, headers={'Authorization': 'Bearer ' + personal_access_token, 'Content-Type': 'application/json'})
@ -584,7 +584,7 @@ def query(method='droplets', droplet_id=None, command=None, args=None, http_meth
content = request.text content = request.text
result = json.loads(content) result = salt.utils.json.loads(content)
if result.get('status', '').lower() == 'error': if result.get('status', '').lower() == 'error':
raise SaltCloudSystemExit( raise SaltCloudSystemExit(
pprint.pformat(result.get('error_message', {})) pprint.pformat(result.get('error_message', {}))

View file

@ -84,7 +84,6 @@ import binascii
import datetime import datetime
import base64 import base64
import msgpack import msgpack
import json
import re import re
import decimal import decimal
@ -92,6 +91,7 @@ import decimal
import salt.utils.cloud import salt.utils.cloud
import salt.utils.files import salt.utils.files
import salt.utils.hashutils import salt.utils.hashutils
import salt.utils.json
from salt._compat import ElementTree as ET from salt._compat import ElementTree as ET
import salt.utils.http as http import salt.utils.http as http
import salt.utils.aws as aws import salt.utils.aws as aws
@ -4845,7 +4845,7 @@ def _parse_pricing(url, name):
# Turn the data into something that's easier/faster to process # Turn the data into something that's easier/faster to process
regions = {} regions = {}
price_json = json.loads(price_js) price_json = salt.utils.json.loads(price_js)
for region in price_json['config']['regions']: for region in price_json['config']['regions']:
sizes = {} sizes = {}
for itype in region['instanceTypes']: for itype in region['instanceTypes']:

View file

@ -53,7 +53,6 @@ included:
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import json
import logging import logging
import base64 import base64
import pprint import pprint
@ -70,6 +69,7 @@ from salt.ext.six.moves import http_client # pylint: disable=import-error,no-na
import salt.utils.cloud import salt.utils.cloud
import salt.utils.files import salt.utils.files
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.config as config import salt.config as config
from salt.cloud.libcloudfuncs import node_state from salt.cloud.libcloudfuncs import node_state
from salt.exceptions import ( from salt.exceptions import (
@ -357,7 +357,7 @@ def create_node(**kwargs):
if firewall_enabled is not None: if firewall_enabled is not None:
create_data['firewall_enabled'] = firewall_enabled create_data['firewall_enabled'] = firewall_enabled
data = json.dumps(create_data) data = salt.utils.json.dumps(create_data)
ret = query(command='/my/machines', data=data, method='POST', ret = query(command='/my/machines', data=data, method='POST',
location=location) location=location)
@ -503,7 +503,7 @@ def take_action(name=None, call=None, command=None, data=None, method='GET',
) )
if data: if data:
data = json.dumps(data) data = salt.utils.json.dumps(data)
ret = [] ret = []
try: try:
@ -956,7 +956,7 @@ def import_key(kwargs=None, call=None):
kwargs['key'] = fp_.read() kwargs['key'] = fp_.read()
send_data = {'name': kwargs['keyname'], 'key': kwargs['key']} send_data = {'name': kwargs['keyname'], 'key': kwargs['key']}
kwargs['data'] = json.dumps(send_data) kwargs['data'] = salt.utils.json.dumps(send_data)
rcode, data = query( rcode, data = query(
command='my/keys', command='my/keys',
@ -1095,7 +1095,7 @@ def query(action=None,
# post form data # post form data
if not data: if not data:
data = json.dumps({}) data = salt.utils.json.dumps({})
return_content = None return_content = None
result = salt.utils.http.query( result = salt.utils.http.query(

View file

@ -10,7 +10,6 @@ Please read :ref:`core config documentation <config_lxc>`.
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import os import os
import logging import logging
import copy import copy
@ -19,6 +18,7 @@ from pprint import pformat
# Import salt cloud libs # Import salt cloud libs
import salt.utils.cloud import salt.utils.cloud
import salt.utils.json
import salt.config as config import salt.config as config
from salt.exceptions import SaltCloudSystemExit from salt.exceptions import SaltCloudSystemExit
@ -137,15 +137,15 @@ def _salt(fun, *args, **kw):
cache = True cache = True
laps = laps // __CACHED_FUNS[fun] laps = laps // __CACHED_FUNS[fun]
try: try:
sargs = json.dumps(args) sargs = salt.utils.json.dumps(args)
except TypeError: except TypeError:
sargs = '' sargs = ''
try: try:
skw = json.dumps(kw) skw = salt.utils.json.dumps(kw)
except TypeError: except TypeError:
skw = '' skw = ''
try: try:
skwargs = json.dumps(kwargs) skwargs = salt.utils.json.dumps(kwargs)
except TypeError: except TypeError:
skwargs = '' skwargs = ''
cache_key = (laps, target, fun, sargs, skw, skwargs) cache_key = (laps, target, fun, sargs, skw, skwargs)

View file

@ -194,13 +194,13 @@ from __future__ import absolute_import
# Import Python Libs # Import Python Libs
import copy import copy
import json
import logging import logging
import os import os
import pprint import pprint
import socket import socket
# Import Salt Libs # Import Salt Libs
import salt.utils.json
import salt.config as config import salt.config as config
import salt.ext.six as six import salt.ext.six as six
from salt.exceptions import ( from salt.exceptions import (
@ -817,7 +817,7 @@ def call(conn=None, call=None, kwargs=None):
func = kwargs.pop('func') func = kwargs.pop('func')
for key, value in kwargs.items(): for key, value in kwargs.items():
try: try:
kwargs[key] = json.loads(value) kwargs[key] = salt.utils.json.loads(value)
except ValueError: except ValueError:
continue continue
try: try:

View file

@ -32,11 +32,10 @@ import time
import pprint import pprint
import logging import logging
import re import re
import json
# Import salt libs # Import salt libs
from salt.ext import six import salt.utils.cloud
import salt.utils import salt.utils.json
# Import salt cloud libs # Import salt cloud libs
import salt.config as config import salt.config as config
@ -45,9 +44,10 @@ from salt.exceptions import (
SaltCloudExecutionFailure, SaltCloudExecutionFailure,
SaltCloudExecutionTimeout SaltCloudExecutionTimeout
) )
from salt.ext.six.moves import range
# Import Third Party Libs # Import 3rd-party Libs
from salt.ext import six
from salt.ext.six.moves import range
try: try:
import requests import requests
HAS_REQUESTS = True HAS_REQUESTS = True
@ -632,7 +632,7 @@ def _import_api():
re_filter = re.compile('(?<=pveapi =)(.*)(?=^;)', re.DOTALL | re.MULTILINE) re_filter = re.compile('(?<=pveapi =)(.*)(?=^;)', re.DOTALL | re.MULTILINE)
api_json = re_filter.findall(returned_data.text)[0] api_json = re_filter.findall(returned_data.text)[0]
api = json.loads(api_json) api = salt.utils.json.loads(api_json)
def _get_properties(path="", method="GET", forced_params=None): def _get_properties(path="", method="GET", forced_params=None):

View file

@ -29,7 +29,6 @@ Set up the cloud configuration at ``/etc/salt/cloud.providers`` or
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import time import time
import json
import pprint import pprint
import logging import logging
import hmac import hmac
@ -41,6 +40,7 @@ from salt.ext.six.moves.urllib.parse import quote as _quote # pylint: disable=i
from salt.ext.six.moves import range from salt.ext.six.moves import range
import salt.utils.cloud import salt.utils.cloud
import salt.utils.data import salt.utils.data
import salt.utils.json
import salt.config as config import salt.config as config
from salt.exceptions import ( from salt.exceptions import (
SaltCloudNotFound, SaltCloudNotFound,
@ -156,8 +156,7 @@ def query(params=None):
if isinstance(value[i - 1], dict): if isinstance(value[i - 1], dict):
for sk, sv in value[i - 1].items(): for sk, sv in value[i - 1].items():
if isinstance(sv, dict) or isinstance(sv, list): if isinstance(sv, dict) or isinstance(sv, list):
# sv = json_dump(sv) sv = salt.utils.json.dumps(sv, separators=(',', ':'))
sv = json.dumps(sv, separators=(',', ':'))
real_parameters['{0}.{1}.{2}'.format(key, i, sk)] = sv real_parameters['{0}.{1}.{2}'.format(key, i, sk)] = sv
else: else:
real_parameters['{0}.{1}'.format(key, i)] = value[i - 1] real_parameters['{0}.{1}'.format(key, i)] = value[i - 1]
@ -188,7 +187,7 @@ def query(params=None):
log.debug(request.url) log.debug(request.url)
content = request.text content = request.text
result = json.loads(content, object_hook=salt.utils.data.encode_dict) result = salt.utils.json.loads(content, object_hook=salt.utils.data.encode_dict)
# print('response:') # print('response:')
# pprint.pprint(result) # pprint.pprint(result)

View file

@ -25,7 +25,6 @@ the cloud configuration at ``/etc/salt/cloud.providers`` or
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import pprint import pprint
import time import time
@ -33,6 +32,7 @@ import time
# Import Salt Libs # Import Salt Libs
from salt.ext.six.moves import range from salt.ext.six.moves import range
import salt.utils.cloud import salt.utils.cloud
import salt.utils.json
import salt.config as config import salt.config as config
from salt.exceptions import ( from salt.exceptions import (
SaltCloudNotFound, SaltCloudNotFound,
@ -347,7 +347,7 @@ def query(method='servers', server_id=None, command=None, args=None,
'token', get_configured_provider(), __opts__, search_global=False 'token', get_configured_provider(), __opts__, search_global=False
) )
data = json.dumps(args) data = salt.utils.json.dumps(args)
requester = getattr(requests, http_method) requester = getattr(requests, http_method)
request = requester( request = requester(

View file

@ -14,7 +14,6 @@ import logging
import traceback import traceback
import multiprocessing import multiprocessing
import subprocess import subprocess
import json
# Import salt libs # Import salt libs
from salt.ext import six from salt.ext import six
@ -22,6 +21,7 @@ import salt.daemons.masterapi
import salt.utils.args import salt.utils.args
import salt.utils.data import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.kinds as kinds import salt.utils.kinds as kinds
import salt.utils.process import salt.utils.process
import salt.utils.stringutils import salt.utils.stringutils
@ -62,7 +62,7 @@ def jobber_check(self):
rms.append(jid) rms.append(jid)
data = self.shells.value[jid] data = self.shells.value[jid]
stdout, stderr = data['proc'].communicate() stdout, stderr = data['proc'].communicate()
ret = json.loads(salt.utils.stringutils.to_str(stdout), object_hook=salt.utils.data.encode_dict)['local'] ret = salt.utils.json.loads(stdout, object_hook=salt.utils.data.encode_dict)['local']
route = {'src': (self.stack.value.local.name, 'manor', 'jid_ret'), route = {'src': (self.stack.value.local.name, 'manor', 'jid_ret'),
'dst': (data['msg']['route']['src'][0], None, 'remote_cmd')} 'dst': (data['msg']['route']['src'][0], None, 'remote_cmd')}
ret['cmd'] = '_return' ret['cmd'] = '_return'

View file

@ -7,10 +7,10 @@ Send events from Docker events
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import traceback import traceback
import salt.utils.json
import salt.utils.event import salt.utils.event
# pylint: disable=import-error # pylint: disable=import-error
@ -83,7 +83,7 @@ def start(docker_url='unix://var/run/docker.sock',
try: try:
events = client.events() events = client.events()
for event in events: for event in events:
data = json.loads(event.decode(__salt_system_encoding__, errors='replace')) data = salt.utils.json.loads(event.decode(__salt_system_encoding__, errors='replace'))
# https://github.com/docker/cli/blob/master/cli/command/system/events.go#L109 # https://github.com/docker/cli/blob/master/cli/command/system/events.go#L109
# https://github.com/docker/engine-api/blob/master/types/events/events.go # https://github.com/docker/engine-api/blob/master/types/events/events.go
# Each output includes the event type, actor id, name and action. # Each output includes the event type, actor id, name and action.

View file

@ -38,7 +38,6 @@ keys make the engine interactive.
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import time import time
import json
import os import os
@ -52,6 +51,7 @@ import salt.utils.args
import salt.utils.event import salt.utils.event
import salt.utils.files import salt.utils.files
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.runner import salt.runner
import salt.client import salt.client
import salt.loader import salt.loader
@ -93,10 +93,11 @@ def _publish_file(token, room, filepath, message='', outputter=None, api_url=Non
url = "{0}/v2/room/{1}/share/file".format(api_url, room) url = "{0}/v2/room/{1}/share/file".format(api_url, room)
headers = {'Content-type': 'multipart/related; boundary=boundary123456'} headers = {'Content-type': 'multipart/related; boundary=boundary123456'}
headers['Authorization'] = "Bearer " + token headers['Authorization'] = "Bearer " + token
msg = json.dumps({'message': message}) msg = salt.utils.json.dumps({'message': message})
# future lint: disable=blacklisted-function
with salt.utils.files.fopen(filepath, 'rb') as rfh: with salt.utils.files.fopen(filepath, 'rb') as rfh:
payload = """\ payload = str("""\
--boundary123456 --boundary123456
Content-Type: application/json; charset=UTF-8 Content-Type: application/json; charset=UTF-8
Content-Disposition: attachment; name="metadata" Content-Disposition: attachment; name="metadata"
@ -109,7 +110,10 @@ Content-Disposition: attachment; name="file"; filename="{1}"
{2} {2}
--boundary123456--\ --boundary123456--\
""".format(msg, os.path.basename(filepath), rfh.read()) """).format(msg,
os.path.basename(salt.utils.stringutils.to_str(filepath)),
salt.utils.stringutils.to_str(rfh.read()))
# future lint: enable=blacklisted-function
salt.utils.http.query(url, method='POST', header_dict=headers, data=payload) salt.utils.http.query(url, method='POST', header_dict=headers, data=payload)
@ -414,7 +418,7 @@ def start(token,
else: else:
tmp_path_fn = salt.utils.files.mkstemp() tmp_path_fn = salt.utils.files.mkstemp()
with salt.utils.files.fopen(tmp_path_fn, 'w+') as fp_: with salt.utils.files.fopen(tmp_path_fn, 'w+') as fp_:
fp_.write(json.dumps(ret, sort_keys=True, indent=4)) salt.utils.json.dump(ret, fp_, sort_keys=True, indent=4)
_publish_file(token, room, tmp_path_fn, message=message_string, api_url=api_url) _publish_file(token, room, tmp_path_fn, message=message_string, api_url=api_url)
salt.utils.files.safe_rm(tmp_path_fn) salt.utils.files.safe_rm(tmp_path_fn)
time.sleep(wait_time or _DEFAULT_SLEEP) time.sleep(wait_time or _DEFAULT_SLEEP)

View file

@ -32,12 +32,12 @@ them onto a logstash endpoint via HTTP requests.
from __future__ import absolute_import from __future__ import absolute_import
# Import python lib # Import python lib
import json
import fnmatch import fnmatch
# Import salt libs # Import salt libs
import salt.utils.http import salt.utils.http
import salt.utils.event import salt.utils.event
import salt.utils.json
# ---------------------------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------------------------
# module properties # module properties
@ -62,7 +62,7 @@ def _logstash(url, data):
url, url,
'POST', 'POST',
header_dict=_HEADERS, header_dict=_HEADERS,
data=json.dumps(data), data=salt.utils.json.dumps(data),
decode=True, decode=True,
status=True, status=True,
opts=__opts__ opts=__opts__

View file

@ -42,11 +42,10 @@ To test this engine
salt '*' test.ping cmd.run uptime salt '*' test.ping cmd.run uptime
''' '''
from __future__ import absolute_import, print_function, unicode_literals
from __future__ import absolute_import
# Import Salt libs # Import Salt libs
import salt.utils.event import salt.utils.event
from salt.ext import six import salt.utils.json
# Import third party libs # Import third party libs
try: try:
@ -67,33 +66,14 @@ except ImportError: # for systems without TLS support.
import socket import socket
import random import random
import time import time
import codecs
import uuid import uuid
import logging import logging
import json
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def __virtual__(): def __virtual__():
if not HAS_CERTIFI: return True if HAS_CERTIFI and HAS_SSL else False
return False
if not HAS_SSL:
return False
return True
def _to_unicode(ch):
return codecs.unicode_escape_decode(ch)[0]
def _is_unicode(ch):
return isinstance(ch, six.text_type)
def _create_unicode(ch):
return six.text_type(ch, 'utf-8')
class PlainTextSocketAppender(object): class PlainTextSocketAppender(object):
@ -111,8 +91,8 @@ class PlainTextSocketAppender(object):
# Error message displayed when an incorrect Token has been detected # Error message displayed when an incorrect Token has been detected
self.INVALID_TOKEN = ("\n\nIt appears the LOGENTRIES_TOKEN " self.INVALID_TOKEN = ("\n\nIt appears the LOGENTRIES_TOKEN "
"parameter you entered is incorrect!\n\n") "parameter you entered is incorrect!\n\n")
# Unicode Line separator character \u2028 # Encoded unicode line separator
self.LINE_SEP = _to_unicode(r'\u2028') self.LINE_SEP = salt.utils.stringutils.to_str('\u2028')
self.verbose = verbose self.verbose = verbose
self._conn = None self._conn = None
@ -149,17 +129,12 @@ class PlainTextSocketAppender(object):
self._conn.close() self._conn.close()
def put(self, data): def put(self, data):
# Replace newlines with Unicode line separator # Replace newlines with Unicode line separator for multi-line events
# for multi-line events multiline = data.replace('\n', self.LINE_SEP) + str('\n') # future lint: disable=blacklisted-function
if not _is_unicode(data):
multiline = _create_unicode(data).replace('\n', self.LINE_SEP)
else:
multiline = data.replace('\n', self.LINE_SEP)
multiline += "\n"
# Send data, reconnect if needed # Send data, reconnect if needed
while True: while True:
try: try:
self._conn.send(multiline.encode('utf-8')) self._conn.send(multiline)
except socket.error: except socket.error:
self.reopen_connection() self.reopen_connection()
continue continue
@ -196,14 +171,6 @@ else:
SocketAppender = TLSSocketAppender SocketAppender = TLSSocketAppender
def _get_appender(endpoint='data.logentries.com', port=10000):
return SocketAppender(verbose=False, LE_API=endpoint, LE_PORT=port)
def _emit(token, msg):
return '{0} {1}'.format(token, msg)
def start(endpoint='data.logentries.com', def start(endpoint='data.logentries.com',
port=10000, port=10000,
token=None, token=None,
@ -230,13 +197,19 @@ def start(endpoint='data.logentries.com',
except ValueError: except ValueError:
log.warning('Not a valid logentries token') log.warning('Not a valid logentries token')
appender = _get_appender(endpoint, port) appender = SocketAppender(verbose=False, LE_API=endpoint, LE_PORT=port)
appender.reopen_connection() appender.reopen_connection()
while True: while True:
event = event_bus.get_event() event = event_bus.get_event()
if event: if event:
msg = '{0} {1}'.format(tag, json.dumps(event)) # future lint: disable=blacklisted-function
appender.put(_emit(token, msg)) msg = str(' ').join((
salt.utils.stringutils.to_str(token),
salt.utils.stringutils.to_str(tag),
salt.utils.json.dumps(event)
))
# future lint: enable=blacklisted-function
appender.put(msg)
appender.close_connection() appender.close_connection()

View file

@ -94,7 +94,6 @@ In addition, other groups are being loaded from pillars.
from __future__ import absolute_import from __future__ import absolute_import
import ast import ast
import datetime import datetime
import json
import itertools import itertools
import logging import logging
import time import time
@ -118,6 +117,7 @@ import salt.runner
import salt.utils.args import salt.utils.args
import salt.utils.event import salt.utils.event
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.utils.slack import salt.utils.slack
from salt.utils.yamldumper import OrderedDumper from salt.utils.yamldumper import OrderedDumper
@ -372,7 +372,7 @@ class SlackClient(object):
log.warn('Got a message that I could not log. The reason is: {}'.format(uee)) log.warn('Got a message that I could not log. The reason is: {}'.format(uee))
# Convert UTF to string # Convert UTF to string
_text = json.dumps(_text) _text = salt.utils.json.dumps(_text)
_text = yaml.safe_load(_text) _text = yaml.safe_load(_text)
if not _text: if not _text:
@ -574,7 +574,7 @@ class SlackClient(object):
indent=0) indent=0)
try: try:
#return yaml.dump(data, **params).replace("\n\n", "\n") #return yaml.dump(data, **params).replace("\n\n", "\n")
return json.dumps(data, sort_keys=True, indent=1) return salt.utils.json.dumps(data, sort_keys=True, indent=1)
# pylint: disable=broad-except # pylint: disable=broad-except
except Exception as exc: except Exception as exc:
import pprint import pprint
@ -626,7 +626,7 @@ class SlackClient(object):
# emulate lookup_jid's return, which is just minion:return # emulate lookup_jid's return, which is just minion:return
# pylint is tripping # pylint is tripping
# pylint: disable=missing-whitespace-after-comma # pylint: disable=missing-whitespace-after-comma
job_data = json.dumps({key:val['return'] for key, val in jid_result.items()}) job_data = salt.utils.json.dumps({key:val['return'] for key, val in jid_result.items()})
results[jid] = yaml.load(job_data) results[jid] = yaml.load(job_data)
return results return results
@ -692,7 +692,7 @@ class SlackClient(object):
content=return_text) content=return_text)
# Handle unicode return # Handle unicode return
log.debug('Got back {} via the slack client'.format(r)) log.debug('Got back {} via the slack client'.format(r))
resp = yaml.safe_load(json.dumps(r)) resp = yaml.safe_load(salt.utils.json.dumps(r))
if 'ok' in resp and resp['ok'] is False: if 'ok' in resp and resp['ok'] is False:
this_job['channel'].send_message('Error: {0}'.format(resp['error'])) this_job['channel'].send_message('Error: {0}'.format(resp['error']))
del outstanding[jid] del outstanding[jid]

View file

@ -77,9 +77,9 @@ Additionally you can define cross account sqs:
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import time import time
import json
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.event import salt.utils.event
# Import third party libs # Import third party libs
@ -140,7 +140,7 @@ def _process_queue(q, q_name, fire_master, tag='salt/engine/sqs', owner_acct_id=
msgs = q.get_messages(wait_time_seconds=20) msgs = q.get_messages(wait_time_seconds=20)
for msg in msgs: for msg in msgs:
if message_format == "json": if message_format == "json":
fire_master(tag=tag, data={'message': json.loads(msg.get_body())}) fire_master(tag=tag, data={'message': salt.utils.json.loads(msg.get_body())})
else: else:
fire_master(tag=tag, data={'message': msg.get_body()}) fire_master(tag=tag, data={'message': msg.get_body()})
msg.delete() msg.delete()

View file

@ -5,11 +5,11 @@ A simple test engine, not intended for real use but as an example
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.event import salt.utils.event
import salt.utils.json
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -34,6 +34,6 @@ def start():
while True: while True:
event = event_bus.get_event() event = event_bus.get_event()
jevent = json.dumps(event) jevent = salt.utils.json.dumps(event)
if event: if event:
log.debug(jevent) log.debug(jevent)

View file

@ -4,13 +4,13 @@ Sudo executor module
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
try: try:
from shlex import quote as _cmd_quote # pylint: disable=E0611 from shlex import quote as _cmd_quote # pylint: disable=E0611
except ImportError: except ImportError:
from pipes import quote as _cmd_quote from pipes import quote as _cmd_quote
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.syspaths import salt.syspaths
@ -70,7 +70,7 @@ def execute(opts, data, func, args, kwargs):
cmd_ret = __salt__['cmd.run_all'](cmd, use_vt=True, python_shell=False) cmd_ret = __salt__['cmd.run_all'](cmd, use_vt=True, python_shell=False)
if cmd_ret['retcode'] == 0: if cmd_ret['retcode'] == 0:
cmd_meta = json.loads(cmd_ret['stdout'])['local'] cmd_meta = salt.utils.json.loads(cmd_ret['stdout'])['local']
ret = cmd_meta['return'] ret = cmd_meta['return']
__context__['retcode'] = cmd_meta.get('retcode', 0) __context__['retcode'] = cmd_meta.get('retcode', 0)
else: else:

View file

@ -47,10 +47,8 @@ permissions.
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import base64 import base64
import json
import logging import logging
import os import os
import os.path
import shutil import shutil
# Import salt libs # Import salt libs
@ -58,6 +56,7 @@ import salt.fileserver
import salt.utils.files import salt.utils.files
import salt.utils.gzip_util import salt.utils.gzip_util
import salt.utils.hashutils import salt.utils.hashutils
import salt.utils.json
import salt.utils.path import salt.utils.path
from salt.utils.versions import LooseVersion from salt.utils.versions import LooseVersion
@ -260,7 +259,7 @@ def update():
with salt.utils.files.fopen(lk_fn, 'w+') as fp_: with salt.utils.files.fopen(lk_fn, 'w+') as fp_:
fp_.write('') fp_.write('')
with salt.utils.files.fopen(container_list, 'w') as fp_: with salt.utils.files.fopen(container_list, 'w') as fp_:
fp_.write(json.dumps(blob_names)) salt.utils.json.dump(blob_names, fp_)
try: try:
os.unlink(lk_fn) os.unlink(lk_fn)
except Exception: except Exception:
@ -314,7 +313,7 @@ def file_list(load):
if not os.path.exists(container_list): if not os.path.exists(container_list):
continue continue
with salt.utils.files.fopen(container_list, 'r') as fp_: with salt.utils.files.fopen(container_list, 'r') as fp_:
ret.update(set(json.load(fp_))) ret.update(set(salt.utils.json.load(fp_)))
except Exception as exc: except Exception as exc:
log.error('azurefs: an error ocurred retrieving file lists. ' log.error('azurefs: an error ocurred retrieving file lists. '
'It should be resolved next time the fileserver ' 'It should be resolved next time the fileserver '

View file

@ -14,11 +14,11 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import os import os
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.dictupdate import salt.utils.dictupdate
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
@ -119,7 +119,7 @@ def _sdc_mdata(mdata_list=None, mdata_get=None):
mdata_grain = mdata_grain.replace('-', '_') mdata_grain = mdata_grain.replace('-', '_')
mdata_grain = mdata_grain.replace(':', '_') mdata_grain = mdata_grain.replace(':', '_')
if mdata_grain in sdc_json_keys: if mdata_grain in sdc_json_keys:
grains['mdata']['sdc'][mdata_grain] = json.loads(mdata_value) grains['mdata']['sdc'][mdata_grain] = salt.utils.json.loads(mdata_value)
else: else:
grains['mdata']['sdc'][mdata_grain] = mdata_value grains['mdata']['sdc'][mdata_grain] = mdata_value

View file

@ -17,12 +17,12 @@ metadata server set `metadata_server_grains: True`.
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs # Import python libs
import json
import os import os
import socket import socket
# Import salt libs # Import salt libs
import salt.utils.http as http import salt.utils.http as http
import salt.utils.json
# metadata server information # metadata server information
@ -69,7 +69,7 @@ def _search(prefix="latest/"):
# (gtmanfred) This try except block is slightly faster than # (gtmanfred) This try except block is slightly faster than
# checking if the string starts with a curly brace # checking if the string starts with a curly brace
try: try:
ret[line] = json.loads(retdata) ret[line] = salt.utils.json.loads(retdata)
except ValueError: except ValueError:
ret[line] = retdata ret[line] = retdata
return ret return ret

View file

@ -15,11 +15,11 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import os import os
import re import re
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.dictupdate import salt.utils.dictupdate
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
from salt.ext.six.moves import zip from salt.ext.six.moves import zip
@ -84,7 +84,7 @@ def _smartos_computenode_data():
grains['computenode_vms_type'][vms[vm]['type']] += 1 grains['computenode_vms_type'][vms[vm]['type']] += 1
# sysinfo derived grains # sysinfo derived grains
sysinfo = json.loads(__salt__['cmd.run']('sysinfo')) sysinfo = salt.utils.json.loads(__salt__['cmd.run']('sysinfo'))
grains['computenode_sdc_version'] = sysinfo['SDC Version'] grains['computenode_sdc_version'] = sysinfo['SDC Version']
grains['computenode_vm_capable'] = sysinfo['VM Capable'] grains['computenode_vm_capable'] = sysinfo['VM Capable']
if sysinfo['VM Capable']: if sysinfo['VM Capable']:

View file

@ -8,7 +8,6 @@ used to manage salt keys directly without interfacing with the CLI.
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
import os import os
import copy import copy
import json
import stat import stat
import shutil import shutil
import fnmatch import fnmatch
@ -27,6 +26,7 @@ import salt.utils.crypt
import salt.utils.data import salt.utils.data
import salt.utils.event import salt.utils.event
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.kinds import salt.utils.kinds
import salt.utils.master import salt.utils.master
import salt.utils.sdb import salt.utils.sdb
@ -1032,10 +1032,8 @@ class RaetKey(Key):
continue continue
path = os.path.join(road_cache, road) path = os.path.join(road_cache, road)
with salt.utils.files.fopen(path, 'rb') as fp_: with salt.utils.files.fopen(path, 'rb') as fp_:
# Do not use to_unicode to decode this. It needs to stay as
# bytes to be deserialized.
if ext == '.json': if ext == '.json':
data = json.load(fp_) data = salt.utils.json.load(fp_)
elif ext == '.msgpack': elif ext == '.msgpack':
data = msgpack.load(fp_) data = msgpack.load(fp_)
role = salt.utils.stringutils.to_unicode(data['role']) role = salt.utils.stringutils.to_unicode(data['role'])

View file

@ -158,7 +158,6 @@
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import json
import logging import logging
import logging.handlers import logging.handlers
import datetime import datetime
@ -166,6 +165,7 @@ import datetime
# Import salt libs # Import salt libs
from salt.log.setup import LOG_LEVELS from salt.log.setup import LOG_LEVELS
from salt.log.mixins import NewStyleClassMixIn from salt.log.mixins import NewStyleClassMixIn
import salt.utils.json
import salt.utils.network import salt.utils.network
# Import Third party libs # Import Third party libs
@ -325,7 +325,7 @@ class LogstashFormatter(logging.Formatter, NewStyleClassMixIn):
continue continue
message_dict['@fields'][key] = repr(value) message_dict['@fields'][key] = repr(value)
return json.dumps(message_dict) return salt.utils.json.dumps(message_dict)
def format_v1(self, record): def format_v1(self, record):
message_dict = { message_dict = {
@ -369,7 +369,7 @@ class LogstashFormatter(logging.Formatter, NewStyleClassMixIn):
continue continue
message_dict[key] = repr(value) message_dict[key] = repr(value)
return json.dumps(message_dict) return salt.utils.json.dumps(message_dict)
class DatagramLogstashHandler(logging.handlers.DatagramHandler): class DatagramLogstashHandler(logging.handlers.DatagramHandler):

View file

@ -338,14 +338,15 @@ class SaltLoggingClass(six.with_metaclass(LoggingMixInMeta, LOGGING_LOGGER_CLASS
extra = None extra = None
# Let's try to make every logging message unicode # Let's try to make every logging message unicode
if isinstance(msg, six.string_types) \
and not isinstance(msg, six.text_type):
salt_system_encoding = __salt_system_encoding__ salt_system_encoding = __salt_system_encoding__
if salt_system_encoding == 'ascii': if salt_system_encoding == 'ascii':
# Encoding detection most likely failed, let's use the utf-8 # Encoding detection most likely failed, let's use the utf-8
# value which we defaulted before __salt_system_encoding__ was # value which we defaulted before __salt_system_encoding__ was
# implemented # implemented
salt_system_encoding = 'utf-8' salt_system_encoding = 'utf-8'
if isinstance(msg, six.string_types) \
and not isinstance(msg, six.text_type):
try: try:
_msg = msg.decode(salt_system_encoding, 'replace') _msg = msg.decode(salt_system_encoding, 'replace')
except UnicodeDecodeError: except UnicodeDecodeError:
@ -353,11 +354,23 @@ class SaltLoggingClass(six.with_metaclass(LoggingMixInMeta, LOGGING_LOGGER_CLASS
else: else:
_msg = msg _msg = msg
_args = []
for item in args:
if isinstance(item, six.string_types) \
and not isinstance(item, six.text_type):
try:
_args.append(item.decode(salt_system_encoding, 'replace'))
except UnicodeDecodeError:
_args.append(item.decode(salt_system_encoding, 'ignore'))
else:
_args.append(item)
_args = tuple(_args)
if six.PY3: if six.PY3:
logrecord = _LOG_RECORD_FACTORY(name, level, fn, lno, _msg, args, logrecord = _LOG_RECORD_FACTORY(name, level, fn, lno, _msg, _args,
exc_info, func, sinfo) exc_info, func, sinfo)
else: else:
logrecord = _LOG_RECORD_FACTORY(name, level, fn, lno, _msg, args, logrecord = _LOG_RECORD_FACTORY(name, level, fn, lno, _msg, _args,
exc_info, func) exc_info, func)
if extra is not None: if extra is not None:

View file

@ -36,8 +36,8 @@ import importlib
import yaml import yaml
import fnmatch import fnmatch
import subprocess import subprocess
import json
import salt.utils.json
from salt.exceptions import LoaderError, CommandExecutionError from salt.exceptions import LoaderError, CommandExecutionError
from salt.utils import timed_subprocess from salt.utils import timed_subprocess
@ -150,7 +150,8 @@ class AnsibleModuleCaller(object):
''))) '')))
if args: if args:
kwargs['_raw_params'] = ' '.join(args) kwargs['_raw_params'] = ' '.join(args)
js_args = '{{"ANSIBLE_MODULE_ARGS": {args}}}'.format(args=json.dumps(kwargs)) js_args = str('{{"ANSIBLE_MODULE_ARGS": {args}}}') # future lint: disable=blacklisted-function
js_args = js_args.format(args=salt.utils.json.dumps(kwargs))
proc_out = timed_subprocess.TimedProc(["echo", "{0}".format(js_args)], proc_out = timed_subprocess.TimedProc(["echo", "{0}".format(js_args)],
stdout=subprocess.PIPE, timeout=self.timeout) stdout=subprocess.PIPE, timeout=self.timeout)
@ -160,7 +161,7 @@ class AnsibleModuleCaller(object):
proc_exc.run() proc_exc.run()
try: try:
out = json.loads(proc_exc.stdout) out = salt.utils.json.loads(proc_exc.stdout)
except ValueError as ex: except ValueError as ex:
out = {'Error': (proc_exc.stderr and (proc_exc.stderr + '.') or str(ex))} out = {'Error': (proc_exc.stderr and (proc_exc.stderr + '.') or str(ex))}
if proc_exc.stdout: if proc_exc.stdout:

View file

@ -6,13 +6,13 @@ Aptly Debian repository manager.
''' '''
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import os import os
import re import re
# Import salt libs # Import salt libs
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.stringutils as stringutils import salt.utils.stringutils as stringutils
@ -123,7 +123,7 @@ def get_config(config_path=_DEFAULT_CONFIG_PATH):
cmd_ret = _cmd_run(cmd) cmd_ret = _cmd_run(cmd)
return json.loads(cmd_ret) return salt.utils.json.loads(cmd_ret)
def list_repos(config_path=_DEFAULT_CONFIG_PATH, with_packages=False): def list_repos(config_path=_DEFAULT_CONFIG_PATH, with_packages=False):

View file

@ -23,7 +23,6 @@ import os
import re import re
import logging import logging
import time import time
import json
# Import third party libs # Import third party libs
import yaml import yaml
@ -42,6 +41,7 @@ import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.functools import salt.utils.functools
import salt.utils.itertools import salt.utils.itertools
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.pkg import salt.utils.pkg
import salt.utils.pkg.deb import salt.utils.pkg.deb
@ -137,7 +137,7 @@ def _get_ppa_info_from_launchpad(owner_name, ppa_name):
owner_name, ppa_name) owner_name, ppa_name)
request = _Request(lp_url, headers={'Accept': 'application/json'}) request = _Request(lp_url, headers={'Accept': 'application/json'})
lp_page = _urlopen(request) lp_page = _urlopen(request)
return json.load(lp_page) return salt.utils.json.load(lp_page)
def _reconstruct_ppa_name(owner_name, ppa_name): def _reconstruct_ppa_name(owner_name, ppa_name):

View file

@ -6,9 +6,9 @@ Support for the Amazon Simple Queue Service.
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
from salt.ext import six from salt.ext import six
@ -65,7 +65,7 @@ def _run_aws(cmd, region, opts, user, **kwargs):
rtn = __salt__['cmd.run'](cmd, runas=user, python_shell=False) rtn = __salt__['cmd.run'](cmd, runas=user, python_shell=False)
return json.loads(rtn) if rtn else '' return salt.utils.json.loads(rtn) if rtn else ''
def receive_message(queue, region, num=1, opts=None, user=None): def receive_message(queue, region, num=1, opts=None, user=None):

View file

@ -7,7 +7,7 @@ An execution module which can manipulate an f5 bigip via iControl REST
# Import python libs # Import python libs
from __future__ import absolute_import from __future__ import absolute_import
import json import salt.utils.json
import logging as logger import logging as logger
# Import third party libs # Import third party libs
@ -72,7 +72,7 @@ def _load_response(response):
''' '''
try: try:
data = json.loads(response.text) data = salt.utils.json.loads(response.text)
except ValueError: except ValueError:
data = response.text data = response.text
@ -181,7 +181,7 @@ def _set_value(value):
value = value.replace('}j', '}') value = value.replace('}j', '}')
try: try:
return json.loads(value) return salt.utils.json.loads(value)
except Exception: except Exception:
raise salt.exceptions.CommandExecutionError raise salt.exceptions.CommandExecutionError
@ -252,7 +252,10 @@ def start_transaction(hostname, username, password, label):
#post to REST to get trans id #post to REST to get trans id
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/transaction', data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/transaction',
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -342,7 +345,10 @@ def commit_transaction(hostname, username, password, label):
#patch to REST to get trans id #patch to REST to get trans id
try: try:
response = bigip_session.patch(BIG_IP_URL_BASE.format(host=hostname)+'/transaction/{trans_id}'.format(trans_id=trans_id), data=json.dumps(payload)) response = bigip_session.patch(
BIG_IP_URL_BASE.format(host=hostname) + '/transaction/{trans_id}'.format(trans_id=trans_id),
data=salt.utils.json.dumps(payload)
)
return _load_response(response) return _load_response(response)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -460,7 +466,9 @@ def create_node(hostname, username, password, name, address, trans_label=None):
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/node', data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/node',
data=salt.utils.json.dumps(payload))
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -537,7 +545,10 @@ def modify_node(hostname, username, password, name,
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/node/{name}'.format(name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/node/{name}'.format(name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -758,7 +769,10 @@ def create_pool(hostname, username, password, name, members=None,
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/pool', data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool',
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -902,7 +916,10 @@ def modify_pool(hostname, username, password, name,
#post to REST #post to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/pool/{name}'.format(name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool/{name}'.format(name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -999,7 +1016,10 @@ def replace_pool_members(hostname, username, password, name, members):
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/pool/{name}'.format(name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool/{name}'.format(name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1052,7 +1072,10 @@ def add_pool_member(hostname, username, password, name, member):
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/pool/{name}/members'.format(name=name), data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool/{name}/members'.format(name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1138,7 +1161,10 @@ def modify_pool_member(hostname, username, password, name, member,
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/pool/{name}/members/{member}'.format(name=name, member=member), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool/{name}/members/{member}'.format(name=name, member=member),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1493,7 +1519,10 @@ def create_virtual(hostname, username, password, name, destination,
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/virtual', data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/virtual',
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1762,7 +1791,10 @@ def modify_virtual(hostname, username, password, name,
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/virtual/{name}'.format(name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/virtual/{name}'.format(name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1879,7 +1911,10 @@ def create_monitor(hostname, username, password, monitor_type, name, **kwargs):
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/monitor/{type}'.format(type=monitor_type), data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/monitor/{type}'.format(type=monitor_type),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -1926,7 +1961,10 @@ def modify_monitor(hostname, username, password, monitor_type, name, **kwargs):
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/monitor/{type}/{name}'.format(type=monitor_type, name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/monitor/{type}/{name}'.format(type=monitor_type, name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -2077,7 +2115,10 @@ def create_profile(hostname, username, password, profile_type, name, **kwargs):
#post to REST #post to REST
try: try:
response = bigip_session.post(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/profile/{type}'.format(type=profile_type), data=json.dumps(payload)) response = bigip_session.post(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/profile/{type}'.format(type=profile_type),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)
@ -2162,7 +2203,10 @@ def modify_profile(hostname, username, password, profile_type, name, **kwargs):
#put to REST #put to REST
try: try:
response = bigip_session.put(BIG_IP_URL_BASE.format(host=hostname)+'/ltm/profile/{type}/{name}'.format(type=profile_type, name=name), data=json.dumps(payload)) response = bigip_session.put(
BIG_IP_URL_BASE.format(host=hostname) + '/ltm/profile/{type}/{name}'.format(type=profile_type, name=name),
data=salt.utils.json.dumps(payload)
)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
return _load_connection_error(hostname, e) return _load_connection_error(hostname, e)

View file

@ -78,13 +78,13 @@ Connection module for Amazon APIGateway
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
import datetime import datetime
# Import Salt libs # Import Salt libs
from salt.ext import six from salt.ext import six
import salt.utils.boto3 import salt.utils.boto3
import salt.utils.compat import salt.utils.compat
import salt.utils.json
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -1161,7 +1161,7 @@ def update_api_model_schema(restApiId, modelName, schema, region=None, key=None,
''' '''
try: try:
schema_json = json.dumps(schema) if isinstance(schema, dict) else schema schema_json = salt.utils.json.dumps(schema) if isinstance(schema, dict) else schema
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
response = _api_model_patch_replace(conn, restApiId, modelName, '/schema', schema_json) response = _api_model_patch_replace(conn, restApiId, modelName, '/schema', schema_json)
return {'updated': True, 'model': _convert_datetime_str(response)} return {'updated': True, 'model': _convert_datetime_str(response)}
@ -1202,7 +1202,7 @@ def create_api_model(restApiId, modelName, modelDescription, schema, contentType
''' '''
try: try:
schema_json = json.dumps(schema) if isinstance(schema, dict) else schema schema_json = salt.utils.json.dumps(schema) if isinstance(schema, dict) else schema
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
model = conn.create_model(restApiId=restApiId, name=modelName, description=modelDescription, model = conn.create_model(restApiId=restApiId, name=modelName, description=modelDescription,
schema=schema_json, contentType=contentType) schema=schema_json, contentType=contentType)

View file

@ -50,7 +50,6 @@ from __future__ import absolute_import
import datetime import datetime
import time import time
import logging import logging
import json
import sys import sys
import time import time
import email.mime.multipart import email.mime.multipart
@ -79,6 +78,7 @@ except ImportError:
# Import Salt libs # Import Salt libs
import salt.utils.boto3 import salt.utils.boto3
import salt.utils.compat import salt.utils.compat
import salt.utils.json
import salt.utils.odict as odict import salt.utils.odict as odict
@ -235,13 +235,13 @@ def create(name, launch_config_name, availability_zones, min_size, max_size,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(availability_zones, six.string_types): if isinstance(availability_zones, six.string_types):
availability_zones = json.loads(availability_zones) availability_zones = salt.utils.json.loads(availability_zones)
if isinstance(load_balancers, six.string_types): if isinstance(load_balancers, six.string_types):
load_balancers = json.loads(load_balancers) load_balancers = salt.utils.json.loads(load_balancers)
if isinstance(vpc_zone_identifier, six.string_types): if isinstance(vpc_zone_identifier, six.string_types):
vpc_zone_identifier = json.loads(vpc_zone_identifier) vpc_zone_identifier = salt.utils.json.loads(vpc_zone_identifier)
if isinstance(tags, six.string_types): if isinstance(tags, six.string_types):
tags = json.loads(tags) tags = salt.utils.json.loads(tags)
# Make a list of tag objects from the dict. # Make a list of tag objects from the dict.
_tags = [] _tags = []
if tags: if tags:
@ -261,11 +261,11 @@ def create(name, launch_config_name, availability_zones, min_size, max_size,
propagate_at_launch=propagate_at_launch) propagate_at_launch=propagate_at_launch)
_tags.append(_tag) _tags.append(_tag)
if isinstance(termination_policies, six.string_types): if isinstance(termination_policies, six.string_types):
termination_policies = json.loads(termination_policies) termination_policies = salt.utils.json.loads(termination_policies)
if isinstance(suspended_processes, six.string_types): if isinstance(suspended_processes, six.string_types):
suspended_processes = json.loads(suspended_processes) suspended_processes = salt.utils.json.loads(suspended_processes)
if isinstance(scheduled_actions, six.string_types): if isinstance(scheduled_actions, six.string_types):
scheduled_actions = json.loads(scheduled_actions) scheduled_actions = salt.utils.json.loads(scheduled_actions)
retries = 30 retries = 30
while True: while True:
try: try:
@ -325,19 +325,19 @@ def update(name, launch_config_name, availability_zones, min_size, max_size,
if not conn: if not conn:
return False, "failed to connect to AWS" return False, "failed to connect to AWS"
if isinstance(availability_zones, six.string_types): if isinstance(availability_zones, six.string_types):
availability_zones = json.loads(availability_zones) availability_zones = salt.utils.json.loads(availability_zones)
if isinstance(load_balancers, six.string_types): if isinstance(load_balancers, six.string_types):
load_balancers = json.loads(load_balancers) load_balancers = salt.utils.json.loads(load_balancers)
if isinstance(vpc_zone_identifier, six.string_types): if isinstance(vpc_zone_identifier, six.string_types):
vpc_zone_identifier = json.loads(vpc_zone_identifier) vpc_zone_identifier = salt.utils.json.loads(vpc_zone_identifier)
if isinstance(tags, six.string_types): if isinstance(tags, six.string_types):
tags = json.loads(tags) tags = salt.utils.json.loads(tags)
if isinstance(termination_policies, six.string_types): if isinstance(termination_policies, six.string_types):
termination_policies = json.loads(termination_policies) termination_policies = salt.utils.json.loads(termination_policies)
if isinstance(suspended_processes, six.string_types): if isinstance(suspended_processes, six.string_types):
suspended_processes = json.loads(suspended_processes) suspended_processes = salt.utils.json.loads(suspended_processes)
if isinstance(scheduled_actions, six.string_types): if isinstance(scheduled_actions, six.string_types):
scheduled_actions = json.loads(scheduled_actions) scheduled_actions = salt.utils.json.loads(scheduled_actions)
# Massage our tagset into add / remove lists # Massage our tagset into add / remove lists
# Use a boto3 call here b/c the boto2 call doeesn't implement filters # Use a boto3 call here b/c the boto2 call doeesn't implement filters
@ -510,7 +510,7 @@ def get_cloud_init_mime(cloud_init):
salt myminion boto.get_cloud_init_mime <cloud init> salt myminion boto.get_cloud_init_mime <cloud init>
''' '''
if isinstance(cloud_init, six.string_types): if isinstance(cloud_init, six.string_types):
cloud_init = json.loads(cloud_init) cloud_init = salt.utils.json.loads(cloud_init)
_cloud_init = email.mime.multipart.MIMEMultipart() _cloud_init = email.mime.multipart.MIMEMultipart()
if 'boothooks' in cloud_init: if 'boothooks' in cloud_init:
for script_name, script in six.iteritems(cloud_init['boothooks']): for script_name, script in six.iteritems(cloud_init['boothooks']):
@ -655,9 +655,9 @@ def create_launch_configuration(name, image_id, key_name=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(security_groups, six.string_types): if isinstance(security_groups, six.string_types):
security_groups = json.loads(security_groups) security_groups = salt.utils.json.loads(security_groups)
if isinstance(block_device_mappings, six.string_types): if isinstance(block_device_mappings, six.string_types):
block_device_mappings = json.loads(block_device_mappings) block_device_mappings = salt.utils.json.loads(block_device_mappings)
_bdms = [] _bdms = []
if block_device_mappings: if block_device_mappings:
# Boto requires objects for the mappings and the devices. # Boto requires objects for the mappings and the devices.

View file

@ -48,9 +48,9 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json
import yaml import yaml
import salt.utils.json
import salt.utils.odict as odict import salt.utils.odict as odict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -66,7 +66,7 @@ try:
except ImportError: except ImportError:
HAS_BOTO = False HAS_BOTO = False
from salt.ext.six import string_types from salt.ext import six
def __virtual__(): def __virtual__():
@ -220,16 +220,16 @@ def create_or_update_alarm(
period = int(period) period = int(period)
if evaluation_periods: if evaluation_periods:
evaluation_periods = int(evaluation_periods) evaluation_periods = int(evaluation_periods)
if isinstance(dimensions, string_types): if isinstance(dimensions, six.string_types):
dimensions = json.loads(dimensions) dimensions = salt.utils.json.loads(dimensions)
if not isinstance(dimensions, dict): if not isinstance(dimensions, dict):
log.error("could not parse dimensions argument: must be json encoding of a dict: '{0}'".format(dimensions)) log.error("could not parse dimensions argument: must be json encoding of a dict: '{0}'".format(dimensions))
return False return False
if isinstance(alarm_actions, string_types): if isinstance(alarm_actions, six.string_types):
alarm_actions = alarm_actions.split(",") alarm_actions = alarm_actions.split(",")
if isinstance(insufficient_data_actions, string_types): if isinstance(insufficient_data_actions, six.string_types):
insufficient_data_actions = insufficient_data_actions.split(",") insufficient_data_actions = insufficient_data_actions.split(",")
if isinstance(ok_actions, string_types): if isinstance(ok_actions, six.string_types):
ok_actions = ok_actions.split(",") ok_actions = ok_actions.split(",")
# convert provided action names into ARN's # convert provided action names into ARN's

View file

@ -48,7 +48,7 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json import salt.utils.json
import salt.utils.compat import salt.utils.compat
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -67,7 +67,7 @@ except ImportError as e:
HAS_BOTO = False HAS_BOTO = False
# pylint: enable=import-error # pylint: enable=import-error
from salt.ext.six import string_types from salt.ext import six
def __virtual__(): def __virtual__():
@ -281,8 +281,8 @@ def put_targets(Rule, Targets,
''' '''
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(Targets, string_types): if isinstance(Targets, six.string_types):
Targets = json.loads(Targets) Targets = salt.utils.json.loads(Targets)
failures = conn.put_targets(Rule=Rule, Targets=Targets) failures = conn.put_targets(Rule=Rule, Targets=Targets)
if failures and failures.get('FailedEntryCount', 0) > 0: if failures and failures.get('FailedEntryCount', 0) > 0:
return {'failures': failures.get('FailedEntries')} return {'failures': failures.get('FailedEntries')}
@ -311,8 +311,8 @@ def remove_targets(Rule, Ids,
''' '''
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(Ids, string_types): if isinstance(Ids, six.string_types):
Ids = json.loads(Ids) Ids = salt.utils.json.loads(Ids)
failures = conn.remove_targets(Rule=Rule, Ids=Ids) failures = conn.remove_targets(Rule=Rule, Ids=Ids)
if failures and failures.get('FailedEntryCount', 0) > 0: if failures and failures.get('FailedEntryCount', 0) > 0:
return {'failures': failures.get('FailedEntries', 1)} return {'failures': failures.get('FailedEntries', 1)}

View file

@ -49,11 +49,11 @@ Connection module for Amazon EC2
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import time import time
import json
# Import Salt libs # Import Salt libs
import salt.utils.compat import salt.utils.compat
import salt.utils.data import salt.utils.data
import salt.utils.json
from salt.ext import six from salt.ext import six
from salt.exceptions import SaltInvocationError, CommandExecutionError from salt.exceptions import SaltInvocationError, CommandExecutionError
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
@ -822,7 +822,7 @@ def _to_blockdev_map(thing):
if isinstance(thing, BlockDeviceMapping): if isinstance(thing, BlockDeviceMapping):
return thing return thing
if isinstance(thing, six.string_types): if isinstance(thing, six.string_types):
thing = json.loads(thing) thing = salt.utils.json.loads(thing)
if not isinstance(thing, dict): if not isinstance(thing, dict):
log.error("Can't convert '{0}' of type {1} to a " log.error("Can't convert '{0}' of type {1} to a "
"boto.ec2.blockdevicemapping.BlockDeviceMapping".format(thing, type(thing))) "boto.ec2.blockdevicemapping.BlockDeviceMapping".format(thing, type(thing)))

View file

@ -77,12 +77,12 @@ Connection module for Amazon Elasticsearch Service
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
# Import Salt libs # Import Salt libs
from salt.ext import six from salt.ext import six
import salt.utils.boto3 import salt.utils.boto3
import salt.utils.compat import salt.utils.compat
import salt.utils.json
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
@ -256,12 +256,12 @@ def create(DomainName, ElasticsearchClusterConfig=None, EBSOptions=None,
val = locals()[k] val = locals()[k]
if isinstance(val, six.string_types): if isinstance(val, six.string_types):
try: try:
val = json.loads(val) val = salt.utils.json.loads(val)
except ValueError as e: except ValueError as e:
return {'updated': False, 'error': 'Error parsing {0}: {1}'.format(k, e.message)} return {'updated': False, 'error': 'Error parsing {0}: {1}'.format(k, e.message)}
kwargs[k] = val kwargs[k] = val
if 'AccessPolicies' in kwargs: if 'AccessPolicies' in kwargs:
kwargs['AccessPolicies'] = json.dumps(kwargs['AccessPolicies']) kwargs['AccessPolicies'] = salt.utils.json.dumps(kwargs['AccessPolicies'])
if 'ElasticsearchVersion' in kwargs: if 'ElasticsearchVersion' in kwargs:
kwargs['ElasticsearchVersion'] = str(kwargs['ElasticsearchVersion']) kwargs['ElasticsearchVersion'] = str(kwargs['ElasticsearchVersion'])
domain = conn.create_elasticsearch_domain(DomainName=DomainName, **kwargs) domain = conn.create_elasticsearch_domain(DomainName=DomainName, **kwargs)
@ -330,12 +330,12 @@ def update(DomainName, ElasticsearchClusterConfig=None, EBSOptions=None,
val = locals()[k] val = locals()[k]
if isinstance(val, six.string_types): if isinstance(val, six.string_types):
try: try:
val = json.loads(val) val = salt.utils.json.loads(val)
except ValueError as e: except ValueError as e:
return {'updated': False, 'error': 'Error parsing {0}: {1}'.format(k, e.message)} return {'updated': False, 'error': 'Error parsing {0}: {1}'.format(k, e.message)}
call_args[k] = val call_args[k] = val
if 'AccessPolicies' in call_args: if 'AccessPolicies' in call_args:
call_args['AccessPolicies'] = json.dumps(call_args['AccessPolicies']) call_args['AccessPolicies'] = salt.utils.json.dumps(call_args['AccessPolicies'])
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
domain = conn.update_elasticsearch_domain_config(DomainName=DomainName, **call_args) domain = conn.update_elasticsearch_domain_config(DomainName=DomainName, **call_args)

View file

@ -48,18 +48,17 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json
import time import time
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Import Salt libs # Import Salt libs
import salt.utils.json
import salt.utils.odict as odict import salt.utils.odict as odict
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
# Import third party libs # Import third party libs
from salt.ext import six from salt.ext import six
from salt.ext.six import string_types
try: try:
import boto import boto
import boto.ec2 # pylint: enable=unused-import import boto.ec2 # pylint: enable=unused-import
@ -254,11 +253,11 @@ def create(name, availability_zones, listeners, subnets=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if exists(name, region, key, keyid, profile): if exists(name, region, key, keyid, profile):
return True return True
if isinstance(availability_zones, string_types): if isinstance(availability_zones, six.string_types):
availability_zones = json.loads(availability_zones) availability_zones = salt.utils.json.loads(availability_zones)
if isinstance(listeners, string_types): if isinstance(listeners, six.string_types):
listeners = json.loads(listeners) listeners = salt.utils.json.loads(listeners)
_complex_listeners = [] _complex_listeners = []
for listener in listeners: for listener in listeners:
@ -321,8 +320,8 @@ def create_listeners(name, listeners, region=None, key=None, keyid=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(listeners, string_types): if isinstance(listeners, six.string_types):
listeners = json.loads(listeners) listeners = salt.utils.json.loads(listeners)
_complex_listeners = [] _complex_listeners = []
for listener in listeners: for listener in listeners:
@ -352,8 +351,8 @@ def delete_listeners(name, ports, region=None, key=None, keyid=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(ports, string_types): if isinstance(ports, six.string_types):
ports = json.loads(ports) ports = salt.utils.json.loads(ports)
try: try:
conn.delete_load_balancer_listeners(name, ports) conn.delete_load_balancer_listeners(name, ports)
msg = 'Deleted ELB listeners on {0}'.format(name) msg = 'Deleted ELB listeners on {0}'.format(name)
@ -379,8 +378,8 @@ def apply_security_groups(name, security_groups, region=None, key=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(security_groups, string_types): if isinstance(security_groups, six.string_types):
security_groups = json.loads(security_groups) security_groups = salt.utils.json.loads(security_groups)
try: try:
conn.apply_security_groups_to_lb(name, security_groups) conn.apply_security_groups_to_lb(name, security_groups)
msg = 'Applied security_groups on ELB {0}'.format(name) msg = 'Applied security_groups on ELB {0}'.format(name)
@ -407,8 +406,8 @@ def enable_availability_zones(name, availability_zones, region=None, key=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(availability_zones, string_types): if isinstance(availability_zones, six.string_types):
availability_zones = json.loads(availability_zones) availability_zones = salt.utils.json.loads(availability_zones)
try: try:
conn.enable_availability_zones(name, availability_zones) conn.enable_availability_zones(name, availability_zones)
msg = 'Enabled availability_zones on ELB {0}'.format(name) msg = 'Enabled availability_zones on ELB {0}'.format(name)
@ -434,8 +433,8 @@ def disable_availability_zones(name, availability_zones, region=None, key=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(availability_zones, string_types): if isinstance(availability_zones, six.string_types):
availability_zones = json.loads(availability_zones) availability_zones = salt.utils.json.loads(availability_zones)
try: try:
conn.disable_availability_zones(name, availability_zones) conn.disable_availability_zones(name, availability_zones)
msg = 'Disabled availability_zones on ELB {0}'.format(name) msg = 'Disabled availability_zones on ELB {0}'.format(name)
@ -461,8 +460,8 @@ def attach_subnets(name, subnets, region=None, key=None, keyid=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(subnets, string_types): if isinstance(subnets, six.string_types):
subnets = json.loads(subnets) subnets = salt.utils.json.loads(subnets)
try: try:
conn.attach_lb_to_subnets(name, subnets) conn.attach_lb_to_subnets(name, subnets)
msg = 'Attached ELB {0} on subnets.'.format(name) msg = 'Attached ELB {0} on subnets.'.format(name)
@ -488,8 +487,8 @@ def detach_subnets(name, subnets, region=None, key=None, keyid=None,
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(subnets, string_types): if isinstance(subnets, six.string_types):
subnets = json.loads(subnets) subnets = salt.utils.json.loads(subnets)
try: try:
conn.detach_lb_from_subnets(name, subnets) conn.detach_lb_from_subnets(name, subnets)
msg = 'Detached ELB {0} from subnets.'.format(name) msg = 'Detached ELB {0} from subnets.'.format(name)

View file

@ -38,15 +38,15 @@ Connection module for Amazon IAM
#pylint: disable=E0602 #pylint: disable=E0602
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import, print_function, unicode_literals
import logging import logging
import json
import yaml import yaml
import time import time
# Import salt libs # Import salt libs
from salt.ext import six from salt.ext import six
import salt.utils.compat import salt.utils.compat
import salt.utils.json
import salt.utils.odict as odict import salt.utils.odict as odict
# Import third party libs # Import third party libs
@ -123,11 +123,10 @@ def create_instance_profile(name, region=None, key=None, keyid=None,
# This call returns an instance profile if successful and an exception # This call returns an instance profile if successful and an exception
# if not. It's annoying. # if not. It's annoying.
conn.create_instance_profile(name) conn.create_instance_profile(name)
log.info('Created {0} instance profile.'.format(name)) log.info('Created %s instance profile.', name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create {0} instance profile.' log.error('Failed to create %s instance profile.', name)
log.error(msg.format(name))
return False return False
return True return True
@ -149,11 +148,10 @@ def delete_instance_profile(name, region=None, key=None, keyid=None,
return True return True
try: try:
conn.delete_instance_profile(name) conn.delete_instance_profile(name)
log.info('Deleted {0} instance profile.'.format(name)) log.info('Deleted %s instance profile.', name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete {0} instance profile.' log.error('Failed to delete %s instance profile.', name)
log.error(msg.format(name))
return False return False
return True return True
@ -192,7 +190,7 @@ def describe_role(name, region=None, key=None, keyid=None, profile=None):
if not info: if not info:
return False return False
role = info.get_role_response.get_role_result.role role = info.get_role_response.get_role_result.role
role['assume_role_policy_document'] = json.loads(_unquote( role['assume_role_policy_document'] = salt.utils.json.loads(_unquote(
role.assume_role_policy_document role.assume_role_policy_document
)) ))
# If Sid wasn't defined by the user, boto will still return a Sid in # If Sid wasn't defined by the user, boto will still return a Sid in
@ -206,8 +204,7 @@ def describe_role(name, region=None, key=None, keyid=None, profile=None):
return role return role
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get {0} information.' log.error('Failed to get %s information.', name)
log.error(msg.format(name))
return False return False
@ -231,12 +228,11 @@ def create_user(user_name, path=None, region=None, key=None, keyid=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
conn.create_user(user_name, path) conn.create_user(user_name, path)
log.info('Created user : {0}.'.format(user_name)) log.info('Created IAM user : %s.', user_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create user {0}.' log.error('Failed to create IAM user %s.', user_name)
log.error(msg.format(user_name))
return False return False
@ -258,8 +254,8 @@ def get_all_access_keys(user_name, marker=None, max_items=None,
return conn.get_all_access_keys(user_name, marker, max_items) return conn.get_all_access_keys(user_name, marker, max_items)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
log.error('Failed to get user\'s {0} access keys.'.format(user_name)) log.error('Failed to get access keys for IAM user %s.', user_name)
return str(e) return six.text_type(e)
def create_access_key(user_name, region=None, key=None, keyid=None, profile=None): def create_access_key(user_name, region=None, key=None, keyid=None, profile=None):
@ -280,7 +276,7 @@ def create_access_key(user_name, region=None, key=None, keyid=None, profile=None
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
log.error('Failed to create access key.') log.error('Failed to create access key.')
return str(e) return six.text_type(e)
def delete_access_key(access_key_id, user_name=None, region=None, key=None, def delete_access_key(access_key_id, user_name=None, region=None, key=None,
@ -301,8 +297,8 @@ def delete_access_key(access_key_id, user_name=None, region=None, key=None,
return conn.delete_access_key(access_key_id, user_name) return conn.delete_access_key(access_key_id, user_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
log.error('Failed to delete access key id {0}.'.format(access_key_id)) log.error('Failed to delete access key id %s.', access_key_id)
return str(e) return six.text_type(e)
def delete_user(user_name, region=None, key=None, keyid=None, def delete_user(user_name, region=None, key=None, keyid=None,
@ -323,12 +319,12 @@ def delete_user(user_name, region=None, key=None, keyid=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
conn.delete_user(user_name) conn.delete_user(user_name)
log.info('Deleted user : {0} .'.format(user_name)) log.info('Deleted IAM user : %s .', user_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
log.error('Failed to delete user {0}'.format(user_name)) log.error('Failed to delete IAM user %s', user_name)
return str(e) return six.text_type(e)
def get_user(user_name=None, region=None, key=None, keyid=None, profile=None): def get_user(user_name=None, region=None, key=None, keyid=None, profile=None):
@ -351,8 +347,7 @@ def get_user(user_name=None, region=None, key=None, keyid=None, profile=None):
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get user {0} info.' log.error('Failed to get IAM user %s info.', user_name)
log.error(msg.format(user_name))
return False return False
@ -377,12 +372,11 @@ def create_group(group_name, path=None, region=None, key=None, keyid=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
conn.create_group(group_name, path) conn.create_group(group_name, path)
log.info('Created group : {0}.'.format(group_name)) log.info('Created IAM group : %s.', group_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create group {0}.' log.error('Failed to create IAM group %s.', group_name)
log.error(msg.format(group_name))
return False return False
@ -406,8 +400,7 @@ def get_group(group_name, region=None, key=None, keyid=None, profile=None):
return info['get_group_response']['get_group_result']['group'] return info['get_group_response']['get_group_result']['group']
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get group {0} info.' log.error('Failed to get IAM group %s info.', group_name)
log.error(msg.format(group_name))
return False return False
@ -442,8 +435,7 @@ def get_group_members(group_name, region=None, key=None, keyid=None, profile=Non
return users return users
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get group {0} members.' log.error('Failed to get members for IAM group %s.', group_name)
log.error(msg.format(group_name))
return False return False
@ -462,8 +454,7 @@ def add_user_to_group(user_name, group_name, region=None, key=None, keyid=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username : {0} does not exist.' log.error('Username : %s does not exist.', user_name)
log.error(msg.format(user_name, group_name))
return False return False
if user_exists_in_group(user_name, group_name, region=region, key=key, if user_exists_in_group(user_name, group_name, region=region, key=key,
keyid=keyid, profile=profile): keyid=keyid, profile=profile):
@ -476,8 +467,7 @@ def add_user_to_group(user_name, group_name, region=None, key=None, keyid=None,
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to add user {0} to group {1}.' log.error('Failed to add IAM user %s to group %s.', user_name, group_name)
log.error(msg.format(user_name, group_name))
return False return False
@ -502,8 +492,7 @@ def user_exists_in_group(user_name, group_name, region=None, key=None, keyid=Non
if users: if users:
for _user in users: for _user in users:
if user_name == _user['user_name']: if user_name == _user['user_name']:
msg = 'Username : {0} is already in group {1}.' log.debug('IAM user %s is already in IAM group %s.', user_name, group_name)
log.info(msg.format(user_name, group_name))
return True return True
return False return False
@ -523,8 +512,7 @@ def remove_user_from_group(group_name, user_name, region=None, key=None, keyid=N
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username : {0} does not exist.' log.error('IAM user %s does not exist.', user_name)
log.error(msg.format(user_name, group_name))
return False return False
if not user_exists_in_group(user_name, group_name, region=region, key=key, if not user_exists_in_group(user_name, group_name, region=region, key=key,
keyid=keyid, profile=profile): keyid=keyid, profile=profile):
@ -537,8 +525,7 @@ def remove_user_from_group(group_name, user_name, region=None, key=None, keyid=N
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to remove user {0} from group {1}.' log.error('Failed to remove IAM user %s from group %s', user_name, group_name)
log.error(msg.format(user_name, group_name))
return False return False
@ -558,23 +545,21 @@ def put_group_policy(group_name, policy_name, policy_json, region=None, key=None
group = get_group(group_name, region=region, key=key, keyid=keyid, group = get_group(group_name, region=region, key=key, keyid=keyid,
profile=profile) profile=profile)
if not group: if not group:
log.error('Group {0} does not exist'.format(group_name)) log.error('Group %s does not exist', group_name)
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
if not isinstance(policy_json, six.string_types): if not isinstance(policy_json, six.string_types):
policy_json = json.dumps(policy_json) policy_json = salt.utils.json.dumps(policy_json)
created = conn.put_group_policy(group_name, policy_name, created = conn.put_group_policy(group_name, policy_name,
policy_json) policy_json)
if created: if created:
log.info('Created policy for group {0}.'.format(group_name)) log.info('Created policy for IAM group %s.', group_name)
return True return True
msg = 'Could not create policy for group {0}' log.error('Could not create policy for IAM group %s', group_name)
log.error(msg.format(group_name))
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create policy for group {0}' log.error('Failed to create policy for IAM group %s', group_name)
log.error(msg.format(group_name))
return False return False
@ -599,13 +584,11 @@ def delete_group_policy(group_name, policy_name, region=None, key=None,
return True return True
try: try:
conn.delete_group_policy(group_name, policy_name) conn.delete_group_policy(group_name, policy_name)
msg = 'Successfully deleted {0} policy for group {1}.' log.info('Successfully deleted policy %s for IAM group %s.', policy_name, group_name)
log.info(msg.format(policy_name, group_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete {0} policy for group {1}.' log.error('Failed to delete policy %s for IAM group %s.', policy_name, group_name)
log.error(msg.format(policy_name, group_name))
return False return False
@ -625,17 +608,16 @@ def get_group_policy(group_name, policy_name, region=None, key=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
info = conn.get_group_policy(group_name, policy_name) info = conn.get_group_policy(group_name, policy_name)
log.debug('info for group policy is : {0}'.format(info)) log.debug('info for group policy is : %s', info)
if not info: if not info:
return False return False
info = info.get_group_policy_response.get_group_policy_result.policy_document info = info.get_group_policy_response.get_group_policy_result.policy_document
info = _unquote(info) info = _unquote(info)
info = json.loads(info, object_pairs_hook=odict.OrderedDict) info = salt.utils.json.loads(info, object_pairs_hook=odict.OrderedDict)
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get group {0} info.' log.error('Failed to get IAM group %s info.', group_name)
log.error(msg.format(group_name))
return False return False
@ -750,13 +732,11 @@ def delete_group(group_name, region=None, key=None,
return True return True
try: try:
conn.delete_group(group_name) conn.delete_group(group_name)
msg = 'Successfully deleted group {0}.' log.info('Successfully deleted IAM group %s.', group_name)
log.info(msg.format(group_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete group {0}.' log.error('Failed to delete IAM group %s.', group_name)
log.error(msg.format(group_name))
return False return False
@ -776,21 +756,19 @@ def create_login_profile(user_name, password, region=None, key=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username {0} does not exist' log.error('IAM user %s does not exist', user_name)
log.error(msg.format(user_name))
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
info = conn.create_login_profile(user_name, password) info = conn.create_login_profile(user_name, password)
log.info('Created profile for user {0}.'.format(user_name)) log.info('Created profile for IAM user %s.', user_name)
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
if 'Conflict' in e: if 'Conflict' in e:
log.info('Profile already exists for user {0}.'.format(user_name)) log.info('Profile already exists for IAM user %s.', user_name)
return 'Conflict' return 'Conflict'
msg = 'Failed to update profile for user {0}.' log.error('Failed to update profile for IAM user %s.', user_name)
log.error(msg.format(user_name))
return False return False
@ -809,21 +787,19 @@ def delete_login_profile(user_name, region=None, key=None, keyid=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username {0} does not exist' log.error('IAM user %s does not exist', user_name)
log.error(msg.format(user_name))
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
info = conn.delete_login_profile(user_name) info = conn.delete_login_profile(user_name)
log.info('Deleted login profile for user {0}.'.format(user_name)) log.info('Deleted login profile for IAM user %s.', user_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
if 'Not Found' in e: if 'Not Found' in e:
log.info('Login profile already deleted for user {0}.'.format(user_name)) log.info('Login profile already deleted for IAM user %s.', user_name)
return True return True
msg = 'Failed to delete login profile for user {0}.' log.error('Failed to delete login profile for IAM user %s.', user_name)
log.error(msg.format(user_name))
return False return False
@ -842,8 +818,7 @@ def get_all_mfa_devices(user_name, region=None, key=None, keyid=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username {0} does not exist' log.error('IAM user %s does not exist', user_name)
log.error(msg.format(user_name))
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
@ -853,10 +828,9 @@ def get_all_mfa_devices(user_name, region=None, key=None, keyid=None,
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
if 'Not Found' in e: if 'Not Found' in e:
log.info('Could not find user {0}.'.format(user_name)) log.info('Could not find IAM user %s.', user_name)
return [] return []
msg = 'Failed to get all MFA devices for user {0}.' log.error('Failed to get all MFA devices for IAM user %s.', user_name)
log.error(msg.format(user_name))
return False return False
@ -876,21 +850,19 @@ def deactivate_mfa_device(user_name, serial, region=None, key=None, keyid=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
msg = 'Username {0} does not exist' log.error('IAM user %s does not exist', user_name)
log.error(msg.format(user_name))
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
conn.deactivate_mfa_device(user_name, serial) conn.deactivate_mfa_device(user_name, serial)
log.info('Deactivated MFA device {1} for user {0}.'.format(user_name, serial)) log.info('Deactivated MFA device %s for IAM user %s.', serial, user_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
if 'Not Found' in e: if 'Not Found' in e:
log.info('MFA device {1} not associated with user {0}.'.format(user_name, serial)) log.info('MFA device %s not associated with IAM user %s.', serial, user_name)
return True return True
msg = 'Failed to deactivate MFA device {1} for user {0}.' log.error('Failed to deactivate MFA device %s for IAM user %s.', serial, user_name)
log.error(msg.format(user_name, serial))
return False return False
@ -907,15 +879,14 @@ def delete_virtual_mfa_device(serial, region=None, key=None, keyid=None, profile
conn = __utils__['boto3.get_connection_func']('iam')() conn = __utils__['boto3.get_connection_func']('iam')()
try: try:
conn.delete_virtual_mfa_device(SerialNumber=serial) conn.delete_virtual_mfa_device(SerialNumber=serial)
log.info('Deleted virtual MFA device {0}.'.format(serial)) log.info('Deleted virtual MFA device %s.', serial)
return True return True
except botocore.exceptions.ClientError as e: except botocore.exceptions.ClientError as e:
log.debug(e) log.debug(e)
if 'NoSuchEntity' in str(e): if 'NoSuchEntity' in six.text_type(e):
log.info('Virtual MFA device {0} not found.'.format(serial)) log.info('Virtual MFA device %s not found.', serial)
return True return True
msg = 'Failed to delete virtual MFA device {0}.' log.error('Failed to delete virtual MFA device %s.', serial)
log.error(msg.format(serial))
return False return False
@ -1000,12 +971,11 @@ def create_role(name, policy_document=None, path=None, region=None, key=None,
try: try:
conn.create_role(name, assume_role_policy_document=policy_document, conn.create_role(name, assume_role_policy_document=policy_document,
path=path) path=path)
log.info('Created {0} iam role.'.format(name)) log.info('Created IAM role %s.', name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.error(e) log.error(e)
msg = 'Failed to create {0} iam role.' log.error('Failed to create IAM role %s.', name)
log.error(msg.format(name))
return False return False
@ -1025,12 +995,11 @@ def delete_role(name, region=None, key=None, keyid=None, profile=None):
return True return True
try: try:
conn.delete_role(name) conn.delete_role(name)
log.info('Deleted {0} iam role.'.format(name)) log.info('Deleted %s IAM role.', name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete {0} iam role.' log.error('Failed to delete %s IAM role.', name)
log.error(msg.format(name))
return False return False
@ -1076,10 +1045,10 @@ def associate_profile_to_role(profile_name, role_name, region=None, key=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not role_exists(role_name, region, key, keyid, profile): if not role_exists(role_name, region, key, keyid, profile):
log.error('IAM role {0} does not exist.'.format(role_name)) log.error('IAM role %s does not exist.', role_name)
return False return False
if not instance_profile_exists(profile_name, region, key, keyid, profile): if not instance_profile_exists(profile_name, region, key, keyid, profile):
log.error('Instance profile {0} does not exist.'.format(profile_name)) log.error('Instance profile %s does not exist.', profile_name)
return False return False
associated = profile_associated(role_name, profile_name, region, key, keyid, profile) associated = profile_associated(role_name, profile_name, region, key, keyid, profile)
if associated: if associated:
@ -1087,13 +1056,11 @@ def associate_profile_to_role(profile_name, role_name, region=None, key=None,
else: else:
try: try:
conn.add_role_to_instance_profile(profile_name, role_name) conn.add_role_to_instance_profile(profile_name, role_name)
msg = 'Added {0} instance profile to {1} role.' log.info('Added %s instance profile to IAM role %s.', profile_name, role_name)
log.info(msg.format(profile_name, role_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to add {0} instance profile to {1} role.' log.error('Failed to add %s instance profile to IAM role %s', profile_name, role_name)
log.error(msg.format(profile_name, role_name))
return False return False
@ -1111,10 +1078,10 @@ def disassociate_profile_from_role(profile_name, role_name, region=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not role_exists(role_name, region, key, keyid, profile): if not role_exists(role_name, region, key, keyid, profile):
log.error('IAM role {0} does not exist.'.format(role_name)) log.error('IAM role %s does not exist.', role_name)
return False return False
if not instance_profile_exists(profile_name, region, key, keyid, profile): if not instance_profile_exists(profile_name, region, key, keyid, profile):
log.error('Instance profile {0} does not exist.'.format(profile_name)) log.error('Instance profile %s does not exist.', profile_name)
return False return False
associated = profile_associated(role_name, profile_name, region, key, keyid, profile) associated = profile_associated(role_name, profile_name, region, key, keyid, profile)
if not associated: if not associated:
@ -1122,13 +1089,11 @@ def disassociate_profile_from_role(profile_name, role_name, region=None,
else: else:
try: try:
conn.remove_role_from_instance_profile(profile_name, role_name) conn.remove_role_from_instance_profile(profile_name, role_name)
msg = 'Removed {0} instance profile from {1} role.' log.info('Removed %s instance profile from IAM role %s.', profile_name, role_name)
log.info(msg.format(profile_name, role_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to remove {0} instance profile from {1} role.' log.error('Failed to remove %s instance profile from IAM role %s.', profile_name, role_name)
log.error(msg.format(profile_name, role_name))
return False return False
@ -1173,7 +1138,7 @@ def get_role_policy(role_name, policy_name, region=None, key=None,
_policy = _policy.get_role_policy_response.policy_document _policy = _policy.get_role_policy_response.policy_document
# Policy is url encoded # Policy is url encoded
_policy = _unquote(_policy) _policy = _unquote(_policy)
_policy = json.loads(_policy, object_pairs_hook=odict.OrderedDict) _policy = salt.utils.json.loads(_policy, object_pairs_hook=odict.OrderedDict)
return _policy return _policy
except boto.exception.BotoServerError: except boto.exception.BotoServerError:
return {} return {}
@ -1199,20 +1164,20 @@ def create_role_policy(role_name, policy_name, policy, region=None, key=None,
return True return True
mode = 'modify' mode = 'modify'
if isinstance(policy, six.string_types): if isinstance(policy, six.string_types):
policy = json.loads(policy, object_pairs_hook=odict.OrderedDict) policy = salt.utils.json.loads(policy, object_pairs_hook=odict.OrderedDict)
try: try:
_policy = json.dumps(policy) _policy = salt.utils.json.dumps(policy)
conn.put_role_policy(role_name, policy_name, _policy) conn.put_role_policy(role_name, policy_name, _policy)
if mode == 'create': if mode == 'create':
msg = 'Successfully added {0} policy to {1} role.' msg = 'Successfully added policy %s to IAM role %s.'
else: else:
msg = 'Successfully modified {0} policy for role {1}.' msg = 'Successfully modified policy %s for IAM role %s.'
log.info(msg.format(policy_name, role_name)) log.info(msg, policy_name, role_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.error(e) log.error(e)
msg = 'Failed to {0} {1} policy for role {2}.' log.error('Failed to %s policy %s for IAM role %s.',
log.error(msg.format(mode, policy_name, role_name)) mode, policy_name, role_name)
return False return False
@ -1234,13 +1199,13 @@ def delete_role_policy(role_name, policy_name, region=None, key=None,
return True return True
try: try:
conn.delete_role_policy(role_name, policy_name) conn.delete_role_policy(role_name, policy_name)
msg = 'Successfully deleted {0} policy for role {1}.' log.info('Successfully deleted policy %s for IAM role %s.',
log.info(msg.format(policy_name, role_name)) policy_name, role_name)
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete {0} policy for role {1}.' log.error('Failed to delete policy %s for IAM role %s.',
log.error(msg.format(policy_name, role_name)) policy_name, role_name)
return False return False
@ -1260,18 +1225,16 @@ def update_assume_role_policy(role_name, policy_document, region=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(policy_document, six.string_types): if isinstance(policy_document, six.string_types):
policy_document = json.loads(policy_document, policy_document = salt.utils.json.loads(policy_document,
object_pairs_hook=odict.OrderedDict) object_pairs_hook=odict.OrderedDict)
try: try:
_policy_document = json.dumps(policy_document) _policy_document = salt.utils.json.dumps(policy_document)
conn.update_assume_role_policy(role_name, _policy_document) conn.update_assume_role_policy(role_name, _policy_document)
msg = 'Successfully updated assume role policy for role {0}.' log.info('Successfully updated assume role policy for IAM role %s.', role_name)
log.info(msg.format(role_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.error(e) log.error(e)
msg = 'Failed to update assume role policy for role {0}.' log.error('Failed to update assume role policy for IAM role %s.', role_name)
log.error(msg.format(role_name))
return False return False
@ -1289,9 +1252,9 @@ def build_policy(region=None, key=None, keyid=None, profile=None):
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if hasattr(conn, 'build_policy'): if hasattr(conn, 'build_policy'):
policy = json.loads(conn.build_policy()) policy = salt.utils.json.loads(conn.build_policy())
elif hasattr(conn, '_build_policy'): elif hasattr(conn, '_build_policy'):
policy = json.loads(conn._build_policy()) policy = salt.utils.json.loads(conn._build_policy())
else: else:
return {} return {}
# The format we get from build_policy isn't going to be what we get back # The format we get from build_policy isn't going to be what we get back
@ -1416,7 +1379,7 @@ def get_all_user_policies(user_name, marker=None, max_items=None, region=None, k
.. code-block:: bash .. code-block:: bash
salt myminion boto_iam.get_group mygroup salt myminion boto_iam.get_all_user_policies myuser
''' '''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
@ -1427,8 +1390,7 @@ def get_all_user_policies(user_name, marker=None, max_items=None, region=None, k
return _list.policy_names return _list.policy_names
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get user {0} policy.' log.error('Failed to get policies for user %s.', user_name)
log.error(msg.format(user_name))
return False return False
@ -1447,17 +1409,16 @@ def get_user_policy(user_name, policy_name, region=None, key=None, keyid=None, p
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
info = conn.get_user_policy(user_name, policy_name) info = conn.get_user_policy(user_name, policy_name)
log.debug('Info for user policy is : {0}.'.format(info)) log.debug('Info for IAM user %s policy %s: %s.', user_name, policy_name, info)
if not info: if not info:
return False return False
info = info.get_user_policy_response.get_user_policy_result.policy_document info = info.get_user_policy_response.get_user_policy_result.policy_document
info = _unquote(info) info = _unquote(info)
info = json.loads(info, object_pairs_hook=odict.OrderedDict) info = salt.utils.json.loads(info, object_pairs_hook=odict.OrderedDict)
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get user {0} policy.' log.error('Failed to get policy %s for IAM user %s.', policy_name, user_name)
log.error(msg.format(user_name))
return False return False
@ -1475,23 +1436,21 @@ def put_user_policy(user_name, policy_name, policy_json, region=None, key=None,
''' '''
user = get_user(user_name, region, key, keyid, profile) user = get_user(user_name, region, key, keyid, profile)
if not user: if not user:
log.error('User {0} does not exist'.format(user_name)) log.error('IAM user %s does not exist', user_name)
return False return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
if not isinstance(policy_json, six.string_types): if not isinstance(policy_json, six.string_types):
policy_json = json.dumps(policy_json) policy_json = salt.utils.json.dumps(policy_json)
created = conn.put_user_policy(user_name, policy_name, created = conn.put_user_policy(user_name, policy_name,
policy_json) policy_json)
if created: if created:
log.info('Created policy for user {0}.'.format(user_name)) log.info('Created policy %s for IAM user %s.', policy_name, user_name)
return True return True
msg = 'Could not create policy for user {0}.' log.error('Could not create policy %s for IAM user %s.', policy_name, user_name)
log.error(msg.format(user_name))
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create policy for user {0}.' log.error('Failed to create policy %s for IAM user %s.', policy_name, user_name)
log.error(msg.format(user_name))
return False return False
@ -1515,13 +1474,11 @@ def delete_user_policy(user_name, policy_name, region=None, key=None, keyid=None
return True return True
try: try:
conn.delete_user_policy(user_name, policy_name) conn.delete_user_policy(user_name, policy_name)
msg = 'Successfully deleted {0} policy for user {1}.' log.info('Successfully deleted policy %s for IAM user %s.', policy_name, user_name)
log.info(msg.format(policy_name, user_name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete {0} policy for user {1}.' log.error('Failed to delete policy %s for IAM user %s.', policy_name, user_name)
log.error(msg.format(policy_name, user_name))
return False return False
@ -1556,12 +1513,11 @@ def upload_server_cert(cert_name, cert_body, private_key, cert_chain=None, path=
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
info = conn.upload_server_cert(cert_name, cert_body, private_key, cert_chain) info = conn.upload_server_cert(cert_name, cert_body, private_key, cert_chain)
log.info('Created certificate {0}.'.format(cert_name)) log.info('Created certificate %s.', cert_name)
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to failed to create certificate {0}.' log.error('Failed to failed to create certificate %s.', cert_name)
log.error(msg.format(cert_name))
return False return False
@ -1585,8 +1541,7 @@ def get_server_certificate(cert_name, region=None, key=None, keyid=None, profile
return info return info
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to get certificate {0} information.' log.error('Failed to get certificate %s information.', cert_name)
log.error(msg.format(cert_name))
return False return False
@ -1607,8 +1562,7 @@ def delete_server_cert(cert_name, region=None, key=None, keyid=None, profile=Non
return conn.delete_server_cert(cert_name) return conn.delete_server_cert(cert_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to delete certificate {0}.' log.error('Failed to delete certificate %s.', cert_name)
log.error(msg.format(cert_name))
return False return False
@ -1651,7 +1605,7 @@ def export_users(path_prefix='/', region=None, key=None, keyid=None,
policies = {} policies = {}
for policy_name in _policies: for policy_name in _policies:
_policy = conn.get_user_policy(name, policy_name) _policy = conn.get_user_policy(name, policy_name)
_policy = json.loads(_unquote( _policy = salt.utils.json.loads(_unquote(
_policy.get_user_policy_response.get_user_policy_result.policy_document _policy.get_user_policy_response.get_user_policy_result.policy_document
)) ))
policies[policy_name] = _policy policies[policy_name] = _policy
@ -1684,14 +1638,14 @@ def export_roles(path_prefix='/', region=None, key=None, keyid=None, profile=Non
policies = {} policies = {}
for policy_name in _policies: for policy_name in _policies:
_policy = conn.get_role_policy(name, policy_name) _policy = conn.get_role_policy(name, policy_name)
_policy = json.loads(_unquote( _policy = salt.utils.json.loads(_unquote(
_policy.get_role_policy_response.get_role_policy_result.policy_document _policy.get_role_policy_response.get_role_policy_result.policy_document
)) ))
policies[policy_name] = _policy policies[policy_name] = _policy
role_sls = [] role_sls = []
role_sls.append({"name": name}) role_sls.append({"name": name})
role_sls.append({"policies": policies}) role_sls.append({"policies": policies})
role_sls.append({'policy_document': json.loads(_unquote(role.assume_role_policy_document))}) role_sls.append({'policy_document': salt.utils.json.loads(_unquote(role.assume_role_policy_document))})
role_sls.append({"path": role.path}) role_sls.append({"path": role.path})
results["manage role " + name] = {"boto_iam_role.present": role_sls} results["manage role " + name] = {"boto_iam_role.present": role_sls}
return _safe_dump(results) return _safe_dump(results)
@ -1763,7 +1717,7 @@ def create_policy(policy_name, policy_document, path=None, description=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not isinstance(policy_document, six.string_types): if not isinstance(policy_document, six.string_types):
policy_document = json.dumps(policy_document) policy_document = salt.utils.json.dumps(policy_document)
params = {} params = {}
for arg in 'path', 'description': for arg in 'path', 'description':
if locals()[arg] is not None: if locals()[arg] is not None:
@ -1772,11 +1726,10 @@ def create_policy(policy_name, policy_document, path=None, description=None,
return True return True
try: try:
conn.create_policy(policy_name, policy_document, **params) conn.create_policy(policy_name, policy_document, **params)
log.info('Created {0} policy.'.format(policy_name)) log.info('Created IAM policy %s.', policy_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create {0} policy.' log.error('Failed to create IAM policy %s.', policy_name)
log.error(msg.format(policy_name))
return False return False
return True return True
@ -1799,12 +1752,11 @@ def delete_policy(policy_name,
return True return True
try: try:
conn.delete_policy(policy_arn) conn.delete_policy(policy_arn)
log.info('Deleted {0} policy.'.format(policy_name)) log.info('Deleted %s policy.', policy_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to delete {0} policy: {1}.' log.error('Failed to delete %s policy: %s.', policy_name, aws.get('message'))
log.error(msg.format(policy_name, aws.get('message')))
return False return False
return True return True
@ -1891,7 +1843,7 @@ def create_policy_version(policy_name, policy_document, set_as_default=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not isinstance(policy_document, six.string_types): if not isinstance(policy_document, six.string_types):
policy_document = json.dumps(policy_document) policy_document = salt.utils.json.dumps(policy_document)
params = {} params = {}
for arg in ('set_as_default',): for arg in ('set_as_default',):
if locals()[arg] is not None: if locals()[arg] is not None:
@ -1900,12 +1852,11 @@ def create_policy_version(policy_name, policy_document, set_as_default=None,
try: try:
ret = conn.create_policy_version(policy_arn, policy_document, **params) ret = conn.create_policy_version(policy_arn, policy_document, **params)
vid = ret.get('create_policy_version_response', {}).get('create_policy_version_result', {}).get('policy_version', {}).get('version_id') vid = ret.get('create_policy_version_response', {}).get('create_policy_version_result', {}).get('policy_version', {}).get('version_id')
log.info('Created {0} policy version {1}.'.format(policy_name, vid)) log.info('Created IAM policy %s version %s.', policy_name, vid)
return {'created': True, 'version_id': vid} return {'created': True, 'version_id': vid}
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to create {0} policy version.' log.error('Failed to create IAM policy %s version %s.', policy_name, vid)
log.error(msg.format(policy_name))
return {'created': False, 'error': __utils__['boto.get_error'](e)} return {'created': False, 'error': __utils__['boto.get_error'](e)}
@ -1927,12 +1878,12 @@ def delete_policy_version(policy_name, version_id,
return True return True
try: try:
conn.delete_policy_version(policy_arn, version_id) conn.delete_policy_version(policy_arn, version_id)
log.info('Deleted {0} policy version {1}.'.format(policy_name, version_id)) log.info('Deleted IAM policy %s version %s.', policy_name, version_id)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to delete {0} policy version {1}: {2}' log.error('Failed to delete IAM policy %s version %s: %s',
log.error(msg.format(policy_name, version_id, aws.get('message'))) policy_name, version_id, aws.get('message'))
return False return False
return True return True
@ -1956,8 +1907,7 @@ def list_policy_versions(policy_name,
return ret.get('list_policy_versions_response', {}).get('list_policy_versions_result', {}).get('versions') return ret.get('list_policy_versions_response', {}).get('list_policy_versions_result', {}).get('versions')
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to list {0} policy versions.' log.error('Failed to list versions for IAM policy %s.', policy_name)
log.error(msg.format(policy_name))
return [] return []
@ -1977,12 +1927,12 @@ def set_default_policy_version(policy_name, version_id,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.set_default_policy_version(policy_arn, version_id) conn.set_default_policy_version(policy_arn, version_id)
log.info('Set {0} policy to version {1}.'.format(policy_name, version_id)) log.info('Set %s policy to version %s.', policy_name, version_id)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to set {0} policy to version {1}: {2}' log.error('Failed to set %s policy to version %s: %s',
log.error(msg.format(policy_name, version_id, aws.get('message'))) policy_name, version_id, aws.get('message'))
return False return False
return True return True
@ -2003,11 +1953,10 @@ def attach_user_policy(policy_name, user_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.attach_user_policy(policy_arn, user_name) conn.attach_user_policy(policy_arn, user_name)
log.info('Attached {0} policy to user {1}.'.format(policy_name, user_name)) log.info('Attached policy %s to IAM user %s.', policy_name, user_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to attach {0} policy to user {1}.' log.error('Failed to attach %s policy to IAM user %s.', policy_name, user_name)
log.error(msg.format(policy_name, user_name))
return False return False
return True return True
@ -2028,11 +1977,10 @@ def detach_user_policy(policy_name, user_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.detach_user_policy(policy_arn, user_name) conn.detach_user_policy(policy_arn, user_name)
log.info('Detached {0} policy to user {1}.'.format(policy_name, user_name)) log.info('Detached %s policy from IAM user %s.', policy_name, user_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to detach {0} policy to user {1}.' log.error('Failed to detach %s policy from IAM user %s.', policy_name, user_name)
log.error(msg.format(policy_name, user_name))
return False return False
return True return True
@ -2053,11 +2001,10 @@ def attach_group_policy(policy_name, group_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.attach_group_policy(policy_arn, group_name) conn.attach_group_policy(policy_arn, group_name)
log.info('Attached {0} policy to group {1}.'.format(policy_name, group_name)) log.info('Attached policy %s to IAM group %s.', policy_name, group_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to attach {0} policy to group {1}.' log.error('Failed to attach policy %s to IAM group %s.', policy_name, group_name)
log.error(msg.format(policy_name, group_name))
return False return False
return True return True
@ -2078,11 +2025,10 @@ def detach_group_policy(policy_name, group_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.detach_group_policy(policy_arn, group_name) conn.detach_group_policy(policy_arn, group_name)
log.info('Detached {0} policy to group {1}.'.format(policy_name, group_name)) log.info('Detached policy %s from IAM group %s.', policy_name, group_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to detach {0} policy to group {1}.' log.error('Failed to detach policy %s from IAM group %s.', policy_name, group_name)
log.error(msg.format(policy_name, group_name))
return False return False
return True return True
@ -2103,11 +2049,10 @@ def attach_role_policy(policy_name, role_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.attach_role_policy(policy_arn, role_name) conn.attach_role_policy(policy_arn, role_name)
log.info('Attached {0} policy to role {1}.'.format(policy_name, role_name)) log.info('Attached policy %s to IAM role %s.', policy_name, role_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to attach {0} policy to role {1}.' log.error('Failed to attach policy %s to IAM role %s.', policy_name, role_name)
log.error(msg.format(policy_name, role_name))
return False return False
return True return True
@ -2128,11 +2073,10 @@ def detach_role_policy(policy_name, role_name,
policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile) policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)
try: try:
conn.detach_role_policy(policy_arn, role_name) conn.detach_role_policy(policy_arn, role_name)
log.info('Detached {0} policy to role {1}.'.format(policy_name, role_name)) log.info('Detached policy %s from IAM role %s.', policy_name, role_name)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to detach {0} policy to role {1}.' log.error('Failed to detach policy %s from IAM role %s.', policy_name, role_name)
log.error(msg.format(policy_name, role_name))
return False return False
return True return True
@ -2174,7 +2118,7 @@ def list_entities_for_policy(policy_name, path_prefix=None, entity_filter=None,
time.sleep(5) time.sleep(5)
retries -= 1 retries -= 1
continue continue
log.error('Failed to list {0} policy entities: {1}'.format(policy_name, e.message)) log.error('Failed to list entities for IAM policy %s: %s', policy_name, e.message)
return {} return {}
return {} return {}
@ -2206,8 +2150,7 @@ def list_attached_user_policies(user_name, path_prefix=None, entity_filter=None,
return policies return policies
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to list user {0} attached policies.' log.error('Failed to list attached policies for IAM user %s.', user_name)
log.error(msg.format(user_name))
return [] return []
@ -2238,8 +2181,7 @@ def list_attached_group_policies(group_name, path_prefix=None, entity_filter=Non
return policies return policies
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to list group {0} attached policies.' log.error('Failed to list attached policies for IAM group %s.', group_name)
log.error(msg.format(group_name))
return [] return []
@ -2270,8 +2212,7 @@ def list_attached_role_policies(role_name, path_prefix=None, entity_filter=None,
return policies return policies
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.debug(e) log.debug(e)
msg = 'Failed to list role {0} attached policies.' log.error('Failed to list attached policies for IAM role %s.', role_name)
log.error(msg.format(role_name))
return [] return []
@ -2288,14 +2229,12 @@ def create_saml_provider(name, saml_metadata_document, region=None, key=None, ke
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try: try:
conn.create_saml_provider(saml_metadata_document, name) conn.create_saml_provider(saml_metadata_document, name)
msg = 'Successfully created {0} SAML provider.' log.info('Successfully created %s SAML provider.', name)
log.info(msg.format(name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to create SAML provider {0}.' log.error('Failed to create SAML provider %s.', name)
log.error(msg.format(name))
return False return False
@ -2319,8 +2258,7 @@ def get_saml_provider_arn(name, region=None, key=None, keyid=None, profile=None)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to get ARN of SAML provider {0}.' log.error('Failed to get ARN of SAML provider %s.', name)
log.error(msg.format(name))
return False return False
@ -2338,18 +2276,15 @@ def delete_saml_provider(name, region=None, key=None, keyid=None, profile=None):
try: try:
saml_provider_arn = get_saml_provider_arn(name, region=region, key=key, keyid=keyid, profile=profile) saml_provider_arn = get_saml_provider_arn(name, region=region, key=key, keyid=keyid, profile=profile)
if not saml_provider_arn: if not saml_provider_arn:
msg = 'SAML provider {0} not found.' log.info('SAML provider %s not found.', name)
log.info(msg.format(name))
return True return True
conn.delete_saml_provider(saml_provider_arn) conn.delete_saml_provider(saml_provider_arn)
msg = 'Successfully deleted {0} SAML provider.' log.info('Successfully deleted SAML provider %s.', name)
log.info(msg.format(name))
return True return True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) aws = __utils__['boto.get_error'](e)
log.debug(aws) log.debug(aws)
msg = 'Failed to delete {0} SAML provider.' log.error('Failed to delete SAML provider %s.', name)
log.error(msg.format(name))
return False return False
@ -2371,10 +2306,8 @@ def list_saml_providers(region=None, key=None, keyid=None, profile=None):
providers.append(arn['arn'].rsplit('/', 1)[1]) providers.append(arn['arn'].rsplit('/', 1)[1])
return providers return providers
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) log.debug(__utils__['boto.get_error'](e))
log.debug(aws) log.error('Failed to get list of SAML providers.')
msg = 'Failed to get list of SAML providers.'
log.error(msg)
return False return False
@ -2393,10 +2326,8 @@ def get_saml_provider(name, region=None, key=None, keyid=None, profile=None):
provider = conn.get_saml_provider(name) provider = conn.get_saml_provider(name)
return provider['get_saml_provider_response']['get_saml_provider_result']['saml_metadata_document'] return provider['get_saml_provider_response']['get_saml_provider_result']['saml_metadata_document']
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) log.debug(__utils__['boto.get_error'](e))
log.debug(aws) log.error('Failed to get SAML provider document %s.', name)
msg = 'Failed to get SAML provider document.'
log.error(msg)
return False return False
@ -2414,15 +2345,12 @@ def update_saml_provider(name, saml_metadata_document, region=None, key=None, ke
try: try:
saml_provider_arn = get_saml_provider_arn(name, region=region, key=key, keyid=keyid, profile=profile) saml_provider_arn = get_saml_provider_arn(name, region=region, key=key, keyid=keyid, profile=profile)
if not saml_provider_arn: if not saml_provider_arn:
msg = 'SAML provider {0} not found.' log.info('SAML provider %s not found.', name)
log.info(msg.format(name))
return False return False
if conn.update_saml_provider(name, saml_metadata_document): if conn.update_saml_provider(name, saml_metadata_document):
return True return True
return False return False
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
aws = __utils__['boto.get_error'](e) log.debug(__utils__['boto.get_error'](e))
log.debug(aws) log.error('Failed to update SAML provider %s.', name)
msg = 'Failed to update of SAML provider.'
log.error(msg.format(name))
return False return False

View file

@ -52,12 +52,12 @@ The dependencies listed above can be installed via package or pip.
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
import datetime import datetime
# Import Salt libs # Import Salt libs
import salt.utils.boto3 import salt.utils.boto3
import salt.utils.compat import salt.utils.compat
import salt.utils.json
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -329,7 +329,7 @@ def create_policy(policyName, policyDocument,
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not isinstance(policyDocument, string_types): if not isinstance(policyDocument, string_types):
policyDocument = json.dumps(policyDocument) policyDocument = salt.utils.json.dumps(policyDocument)
policy = conn.create_policy(policyName=policyName, policy = conn.create_policy(policyName=policyName,
policyDocument=policyDocument) policyDocument=policyDocument)
if policy: if policy:
@ -446,7 +446,7 @@ def create_policy_version(policyName, policyDocument, setAsDefault=False,
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not isinstance(policyDocument, string_types): if not isinstance(policyDocument, string_types):
policyDocument = json.dumps(policyDocument) policyDocument = salt.utils.json.dumps(policyDocument)
policy = conn.create_policy_version(policyName=policyName, policy = conn.create_policy_version(policyName=policyName,
policyDocument=policyDocument, policyDocument=policyDocument,
setAsDefault=setAsDefault) setAsDefault=setAsDefault)

View file

@ -40,11 +40,11 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
from salt.serializers import json
# Import Salt libs # Import Salt libs
import salt.utils.compat import salt.utils.compat
import salt.utils.odict as odict import salt.utils.odict as odict
import salt.serializers.json
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -146,7 +146,7 @@ def create_key(policy=None, description=None, key_usage=None, region=None,
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
r = {} r = {}
_policy = json.serialize(policy) _policy = salt.serializers.json.serialize(policy)
try: try:
key_metadata = conn.create_key( key_metadata = conn.create_key(
_policy, _policy,
@ -432,7 +432,7 @@ def get_key_policy(key_id, policy_name, region=None, key=None, keyid=None,
r = {} r = {}
try: try:
key_policy = conn.get_key_policy(key_id, policy_name) key_policy = conn.get_key_policy(key_id, policy_name)
r['key_policy'] = json.deserialize( r['key_policy'] = salt.serializers.json.deserialize(
key_policy['Policy'], key_policy['Policy'],
object_pairs_hook=odict.OrderedDict object_pairs_hook=odict.OrderedDict
) )
@ -536,7 +536,7 @@ def put_key_policy(key_id, policy_name, policy, region=None, key=None,
r = {} r = {}
try: try:
conn.put_key_policy(key_id, policy_name, json.serialize(policy)) conn.put_key_policy(key_id, policy_name, salt.serializers.json.serialize(policy))
r['result'] = True r['result'] = True
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
r['result'] = False r['result'] = False

View file

@ -82,7 +82,6 @@ The dependencies listed above can be installed via package or pip.
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
import time import time
import random import random
@ -90,6 +89,7 @@ import random
from salt.ext import six from salt.ext import six
import salt.utils.compat import salt.utils.compat
import salt.utils.files import salt.utils.files
import salt.utils.json
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range # pylint: disable=import-error from salt.ext.six.moves import range # pylint: disable=import-error
@ -207,7 +207,7 @@ def _filedata(infile):
def _resolve_vpcconfig(conf, region=None, key=None, keyid=None, profile=None): def _resolve_vpcconfig(conf, region=None, key=None, keyid=None, profile=None):
if isinstance(conf, six.string_types): if isinstance(conf, six.string_types):
conf = json.loads(conf) conf = salt.utils.json.loads(conf)
if not conf: if not conf:
return None return None
if not isinstance(conf, dict): if not isinstance(conf, dict):
@ -583,7 +583,7 @@ def get_permissions(FunctionName, Qualifier=None,
**kwargs) **kwargs)
policy = policy.get('Policy', {}) policy = policy.get('Policy', {})
if isinstance(policy, six.string_types): if isinstance(policy, six.string_types):
policy = json.loads(policy) policy = salt.utils.json.loads(policy)
if policy is None: if policy is None:
policy = {} policy = {}
permissions = {} permissions = {}

View file

@ -54,12 +54,12 @@ The dependencies listed above can be installed via package or pip.
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
# Import Salt libs # Import Salt libs
from salt.ext import six from salt.ext import six
from salt.ext.six.moves import range # pylint: disable=import-error from salt.ext.six.moves import range # pylint: disable=import-error
import salt.utils.compat import salt.utils.compat
import salt.utils.json
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
@ -227,7 +227,7 @@ def delete_objects(Bucket, Delete, MFA=None, RequestPayer=None,
''' '''
if isinstance(Delete, six.string_types): if isinstance(Delete, six.string_types):
Delete = json.loads(Delete) Delete = salt.utils.json.loads(Delete)
if not isinstance(Delete, dict): if not isinstance(Delete, dict):
raise SaltInvocationError("Malformed Delete request.") raise SaltInvocationError("Malformed Delete request.")
if 'Objects' not in Delete: if 'Objects' not in Delete:
@ -483,7 +483,7 @@ def put_acl(Bucket,
kwargs = {} kwargs = {}
if AccessControlPolicy is not None: if AccessControlPolicy is not None:
if isinstance(AccessControlPolicy, six.string_types): if isinstance(AccessControlPolicy, six.string_types):
AccessControlPolicy = json.loads(AccessControlPolicy) AccessControlPolicy = salt.utils.json.loads(AccessControlPolicy)
kwargs['AccessControlPolicy'] = AccessControlPolicy kwargs['AccessControlPolicy'] = AccessControlPolicy
for arg in ('ACL', for arg in ('ACL',
'GrantFullControl', 'GrantFullControl',
@ -523,7 +523,7 @@ def put_cors(Bucket,
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if CORSRules is not None and isinstance(CORSRules, six.string_types): if CORSRules is not None and isinstance(CORSRules, six.string_types):
CORSRules = json.loads(CORSRules) CORSRules = salt.utils.json.loads(CORSRules)
conn.put_bucket_cors(Bucket=Bucket, CORSConfiguration={'CORSRules': CORSRules}) conn.put_bucket_cors(Bucket=Bucket, CORSConfiguration={'CORSRules': CORSRules})
return {'updated': True, 'name': Bucket} return {'updated': True, 'name': Bucket}
except ClientError as e: except ClientError as e:
@ -558,7 +558,7 @@ def put_lifecycle_configuration(Bucket,
try: try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if Rules is not None and isinstance(Rules, six.string_types): if Rules is not None and isinstance(Rules, six.string_types):
Rules = json.loads(Rules) Rules = salt.utils.json.loads(Rules)
conn.put_bucket_lifecycle_configuration(Bucket=Bucket, LifecycleConfiguration={'Rules': Rules}) conn.put_bucket_lifecycle_configuration(Bucket=Bucket, LifecycleConfiguration={'Rules': Rules})
return {'updated': True, 'name': Bucket} return {'updated': True, 'name': Bucket}
except ClientError as e: except ClientError as e:
@ -596,7 +596,7 @@ def put_logging(Bucket,
else: else:
logstatus = {} logstatus = {}
if TargetGrants is not None and isinstance(TargetGrants, six.string_types): if TargetGrants is not None and isinstance(TargetGrants, six.string_types):
TargetGrants = json.loads(TargetGrants) TargetGrants = salt.utils.json.loads(TargetGrants)
conn.put_bucket_logging(Bucket=Bucket, BucketLoggingStatus=logstatus) conn.put_bucket_logging(Bucket=Bucket, BucketLoggingStatus=logstatus)
return {'updated': True, 'name': Bucket} return {'updated': True, 'name': Bucket}
except ClientError as e: except ClientError as e:
@ -629,15 +629,15 @@ def put_notification_configuration(Bucket,
if TopicConfigurations is None: if TopicConfigurations is None:
TopicConfigurations = [] TopicConfigurations = []
elif isinstance(TopicConfigurations, six.string_types): elif isinstance(TopicConfigurations, six.string_types):
TopicConfigurations = json.loads(TopicConfigurations) TopicConfigurations = salt.utils.json.loads(TopicConfigurations)
if QueueConfigurations is None: if QueueConfigurations is None:
QueueConfigurations = [] QueueConfigurations = []
elif isinstance(QueueConfigurations, six.string_types): elif isinstance(QueueConfigurations, six.string_types):
QueueConfigurations = json.loads(QueueConfigurations) QueueConfigurations = salt.utils.json.loads(QueueConfigurations)
if LambdaFunctionConfigurations is None: if LambdaFunctionConfigurations is None:
LambdaFunctionConfigurations = [] LambdaFunctionConfigurations = []
elif isinstance(LambdaFunctionConfigurations, six.string_types): elif isinstance(LambdaFunctionConfigurations, six.string_types):
LambdaFunctionConfigurations = json.loads(LambdaFunctionConfigurations) LambdaFunctionConfigurations = salt.utils.json.loads(LambdaFunctionConfigurations)
# TODO allow the user to use simple names & substitute ARNs for those names # TODO allow the user to use simple names & substitute ARNs for those names
conn.put_bucket_notification_configuration(Bucket=Bucket, NotificationConfiguration={ conn.put_bucket_notification_configuration(Bucket=Bucket, NotificationConfiguration={
'TopicConfigurations': TopicConfigurations, 'TopicConfigurations': TopicConfigurations,
@ -670,7 +670,7 @@ def put_policy(Bucket, Policy,
if Policy is None: if Policy is None:
Policy = '{}' Policy = '{}'
elif not isinstance(Policy, six.string_types): elif not isinstance(Policy, six.string_types):
Policy = json.dumps(Policy) Policy = salt.utils.json.dumps(Policy)
conn.put_bucket_policy(Bucket=Bucket, Policy=Policy) conn.put_bucket_policy(Bucket=Bucket, Policy=Policy)
return {'updated': True, 'name': Bucket} return {'updated': True, 'name': Bucket}
except ClientError as e: except ClientError as e:
@ -714,7 +714,7 @@ def put_replication(Bucket, Role, Rules,
if Rules is None: if Rules is None:
Rules = [] Rules = []
elif isinstance(Rules, six.string_types): elif isinstance(Rules, six.string_types):
Rules = json.loads(Rules) Rules = salt.utils.json.loads(Rules)
conn.put_bucket_replication(Bucket=Bucket, ReplicationConfiguration={ conn.put_bucket_replication(Bucket=Bucket, ReplicationConfiguration={
'Role': Role, 'Role': Role,
'Rules': Rules 'Rules': Rules
@ -838,7 +838,7 @@ def put_website(Bucket, ErrorDocument=None, IndexDocument=None,
val = locals()[key] val = locals()[key]
if val is not None: if val is not None:
if isinstance(val, six.string_types): if isinstance(val, six.string_types):
WebsiteConfiguration[key] = json.loads(val) WebsiteConfiguration[key] = salt.utils.json.loads(val)
else: else:
WebsiteConfiguration[key] = val WebsiteConfiguration[key] = val
conn.put_bucket_website(Bucket=Bucket, conn.put_bucket_website(Bucket=Bucket,

View file

@ -48,7 +48,9 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json
# Import Salt libs
import salt.utils.json
# Import 3rd-party libs # Import 3rd-party libs
from salt.ext import six from salt.ext import six
@ -87,13 +89,13 @@ def _preprocess_attributes(attributes):
Pre-process incoming queue attributes before setting them Pre-process incoming queue attributes before setting them
''' '''
if isinstance(attributes, six.string_types): if isinstance(attributes, six.string_types):
attributes = json.loads(attributes) attributes = salt.utils.json.loads(attributes)
def stringified(val): def stringified(val):
# Some attributes take full json policy documents, but they take them # Some attributes take full json policy documents, but they take them
# as json strings. Convert the value back into a json string. # as json strings. Convert the value back into a json string.
if isinstance(val, dict): if isinstance(val, dict):
return json.dumps(val) return salt.utils.json.dumps(val)
return val return val
return dict( return dict(

View file

@ -11,11 +11,11 @@ available.
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs # Import python libs
import json
import logging import logging
import shlex import shlex
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
@ -129,7 +129,7 @@ def install(pkg,
raise CommandExecutionError(result['stderr']) raise CommandExecutionError(result['stderr'])
# If package is already installed, Bower will emit empty dict to STDOUT # If package is already installed, Bower will emit empty dict to STDOUT
stdout = json.loads(result['stdout']) stdout = salt.utils.json.loads(result['stdout'])
return stdout != {} return stdout != {}
@ -174,7 +174,7 @@ def uninstall(pkg, dir, runas=None, env=None):
raise CommandExecutionError(result['stderr']) raise CommandExecutionError(result['stderr'])
# If package is not installed, Bower will emit empty dict to STDOUT # If package is not installed, Bower will emit empty dict to STDOUT
stdout = json.loads(result['stdout']) stdout = salt.utils.json.loads(result['stdout'])
return stdout != {} return stdout != {}
@ -214,7 +214,7 @@ def list_(dir, runas=None, env=None):
if result['retcode'] != 0: if result['retcode'] != 0:
raise CommandExecutionError(result['stderr']) raise CommandExecutionError(result['stderr'])
return json.loads(result['stdout'])['dependencies'] return salt.utils.json.loads(result['stdout'])['dependencies']
def prune(dir, runas=None, env=None): def prune(dir, runas=None, env=None):
@ -255,4 +255,4 @@ def prune(dir, runas=None, env=None):
raise CommandExecutionError(result['stderr']) raise CommandExecutionError(result['stderr'])
# Bower returns an empty dictionary if nothing was pruned # Bower returns an empty dictionary if nothing was pruned
return json.loads(result['stdout']) return salt.utils.json.loads(result['stdout'])

View file

@ -83,19 +83,19 @@ queries based on the internal schema of said version.
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import json
import re import re
import ssl import ssl
# Import Salt Libs # Import Salt Libs
import salt.utils.json
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
from salt.ext import six from salt.ext import six
from salt.ext.six.moves import range from salt.ext.six.moves import range
SSL_VERSION = 'ssl_version' SSL_VERSION = 'ssl_version'
SSL_VERSION = 'ssl_version'
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
__virtualname__ = 'cassandra_cql' __virtualname__ = 'cassandra_cql'
@ -728,7 +728,7 @@ def create_keyspace(keyspace, replication_strategy='SimpleStrategy', replication
if replication_datacenters: if replication_datacenters:
if isinstance(replication_datacenters, six.string_types): if isinstance(replication_datacenters, six.string_types):
try: try:
replication_datacenter_map = json.loads(replication_datacenters) replication_datacenter_map = salt.utils.json.loads(replication_datacenters)
replication_map.update(**replication_datacenter_map) replication_map.update(**replication_datacenter_map)
except BaseException: # pylint: disable=W0703 except BaseException: # pylint: disable=W0703
log.error("Could not load json replication_datacenters.") log.error("Could not load json replication_datacenters.")

View file

@ -8,9 +8,9 @@ Currently this only works when run through a proxy minion.
''' '''
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.utils.platform import salt.utils.platform
from salt.exceptions import get_error_message from salt.exceptions import get_error_message
@ -105,7 +105,7 @@ def update_job(name, config):
''' '''
if 'name' not in config: if 'name' not in config:
config['name'] = name config['name'] = name
data = json.dumps(config) data = salt.utils.json.dumps(config)
try: try:
response = salt.utils.http.query( response = salt.utils.http.query(
"{0}/scheduler/iso8601".format(_base_url()), "{0}/scheduler/iso8601".format(_base_url()),

View file

@ -10,7 +10,6 @@ from __future__ import absolute_import, print_function, unicode_literals
# Import python libs # Import python libs
import functools import functools
import glob import glob
import json
import logging import logging
import os import os
import shutil import shutil
@ -27,6 +26,7 @@ import tempfile
import salt.utils.args import salt.utils.args
import salt.utils.data import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
import salt.utils.powershell import salt.utils.powershell
@ -3272,7 +3272,7 @@ def powershell(cmd,
**kwargs) **kwargs)
try: try:
return json.loads(response) return salt.utils.json.loads(response)
except Exception: except Exception:
log.error("Error converting PowerShell JSON return", exc_info=True) log.error("Error converting PowerShell JSON return", exc_info=True)
return {} return {}
@ -3578,7 +3578,7 @@ def powershell_all(cmd,
# If we fail to parse stdoutput we will raise an exception # If we fail to parse stdoutput we will raise an exception
try: try:
result = json.loads(stdoutput) result = salt.utils.json.loads(stdoutput)
except Exception: except Exception:
err_msg = "cmd.powershell_all " + \ err_msg = "cmd.powershell_all " + \
"cannot parse the Powershell output." "cannot parse the Powershell output."

View file

@ -8,6 +8,12 @@ https://www.consul.io
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import base64
import logging
# Import salt libs
import salt.utils.http
import salt.utils.json
# Import 3rd-party libs # Import 3rd-party libs
# pylint: disable=import-error,no-name-in-module,redefined-builtin # pylint: disable=import-error,no-name-in-module,redefined-builtin
@ -16,13 +22,6 @@ import salt.ext.six
import salt.ext.six.moves.http_client import salt.ext.six.moves.http_client
# pylint: enable=import-error,no-name-in-module # pylint: enable=import-error,no-name-in-module
# Import salt libs
import salt.utils.http
import base64
import json
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
@ -84,7 +83,7 @@ def _query(function,
if data is None: if data is None:
data = {} data = {}
data = json.dumps(data) data = salt.utils.json.dumps(data)
result = salt.utils.http.query( result = salt.utils.http.query(
url, url,

View file

@ -26,7 +26,6 @@ Support for RFC 2136 dynamic DNS updates.
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs # Import python libs
import logging import logging
import json
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -40,6 +39,7 @@ except ImportError as e:
dns_support = False dns_support = False
import salt.utils.files import salt.utils.files
import salt.utils.json
def __virtual__(): def __virtual__():
@ -71,7 +71,7 @@ def _get_keyring(keyfile):
keyring = None keyring = None
if keyfile: if keyfile:
with salt.utils.files.fopen(keyfile) as _f: with salt.utils.files.fopen(keyfile) as _f:
keyring = dns.tsigkeyring.from_text(json.load(_f)) keyring = dns.tsigkeyring.from_text(salt.utils.json.load(_f))
return keyring return keyring

View file

@ -6,7 +6,6 @@ Module to work with salt formula defaults files
from __future__ import absolute_import from __future__ import absolute_import
import copy import copy
import json
import logging import logging
import os import os
import yaml import yaml
@ -15,6 +14,7 @@ import salt.fileclient
import salt.utils.data import salt.utils.data
import salt.utils.dictupdate as dictupdate import salt.utils.dictupdate as dictupdate
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.url import salt.utils.url
@ -57,9 +57,9 @@ def _load(formula):
suffix = file_.rsplit('.', 1)[-1] suffix = file_.rsplit('.', 1)[-1]
if suffix == 'yaml': if suffix == 'yaml':
loader = yaml loader = yaml.safe_load
elif suffix == 'json': elif suffix == 'json':
loader = json loader = salt.utils.json.load
else: else:
log.debug("Failed to determine loader for %r", file_) log.debug("Failed to determine loader for %r", file_)
continue continue
@ -67,7 +67,7 @@ def _load(formula):
if os.path.exists(file_): if os.path.exists(file_):
log.debug("Reading defaults from %r", file_) log.debug("Reading defaults from %r", file_)
with salt.utils.files.fopen(file_) as fhr: with salt.utils.files.fopen(file_) as fhr:
defaults = loader.load(fhr) defaults = loader(fhr)
log.debug("Read defaults %r", defaults) log.debug("Read defaults %r", defaults)
return defaults or {} return defaults or {}

View file

@ -212,6 +212,7 @@ from salt.ext.six.moves import map # pylint: disable=import-error,redefined-bui
import salt.utils.docker.translate.container import salt.utils.docker.translate.container
import salt.utils.docker.translate.network import salt.utils.docker.translate.network
import salt.utils.functools import salt.utils.functools
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.pillar import salt.pillar
import salt.exceptions import salt.exceptions
@ -357,7 +358,7 @@ def _get_client(timeout=NOTSET, **kwargs):
python_shell=False) python_shell=False)
try: try:
docker_machine_json = \ docker_machine_json = \
json.loads(__utils__['stringutils.to_str'](docker_machine_json)) salt.utils.json.loads(docker_machine_json)
docker_machine_tls = \ docker_machine_tls = \
docker_machine_json['HostOptions']['AuthOptions'] docker_machine_json['HostOptions']['AuthOptions']
docker_machine_ip = docker_machine_json['Driver']['IPAddress'] docker_machine_ip = docker_machine_json['Driver']['IPAddress']
@ -3971,12 +3972,7 @@ def build(path=None,
stream_data = [] stream_data = []
for line in response: for line in response:
stream_data.extend( stream_data.extend(salt.utils.json.loads(line, cls=DockerJSONDecoder))
json.loads(
__utils__['stringutils.to_str'](line),
cls=DockerJSONDecoder
)
)
errors = [] errors = []
# Iterate through API response and collect information # Iterate through API response and collect information
for item in stream_data: for item in stream_data:
@ -4511,7 +4507,7 @@ def pull(image,
for event in response: for event in response:
log.debug('pull event: %s', event) log.debug('pull event: %s', event)
try: try:
event = json.loads(__utils__['stringutils.to_str'](event)) event = salt.utils.json.loads(event)
except Exception as exc: except Exception as exc:
raise CommandExecutionError( raise CommandExecutionError(
'Unable to interpret API event: \'{0}\''.format(event), 'Unable to interpret API event: \'{0}\''.format(event),
@ -4605,7 +4601,7 @@ def push(image,
# Iterate through API response and collect information # Iterate through API response and collect information
for event in response: for event in response:
try: try:
event = json.loads(__utils__['stringutils.to_str'](event)) event = salt.utils.json.loads(event)
except Exception as exc: except Exception as exc:
raise CommandExecutionError( raise CommandExecutionError(
'Unable to interpret API event: \'{0}\''.format(event), 'Unable to interpret API event: \'{0}\''.format(event),

View file

@ -12,9 +12,9 @@ except ImportError: # python3
from urllib.parse import quote, unquote from urllib.parse import quote, unquote
try: try:
import json
import requests import requests
import salt.defaults.exitcodes import salt.defaults.exitcodes
import salt.utils.json
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
HAS_LIBS = True HAS_LIBS = True
except ImportError: except ImportError:
@ -97,7 +97,7 @@ def _api_response(response):
raise CommandExecutionError('Bad username or password') raise CommandExecutionError('Bad username or password')
elif response.status_code == 200 or response.status_code == 500: elif response.status_code == 200 or response.status_code == 500:
try: try:
data = json.loads(response.content) data = salt.utils.json.loads(response.content)
if data['exit_code'] != 'SUCCESS': if data['exit_code'] != 'SUCCESS':
__context__['retcode'] = salt.defaults.exitcodes.SALT_BUILD_FAIL __context__['retcode'] = salt.defaults.exitcodes.SALT_BUILD_FAIL
raise CommandExecutionError(data['message']) raise CommandExecutionError(data['message'])
@ -132,7 +132,7 @@ def _api_post(path, data, server=None):
url=_get_url(server['ssl'], server['url'], server['port'], path), url=_get_url(server['ssl'], server['url'], server['port'], path),
auth=_get_auth(server['user'], server['password']), auth=_get_auth(server['user'], server['password']),
headers=_get_headers(), headers=_get_headers(),
data=json.dumps(data), data=salt.utils.json.dumps(data),
verify=False verify=False
) )
return _api_response(response) return _api_response(response)

View file

@ -17,7 +17,6 @@ import random
import logging import logging
import operator import operator
import collections import collections
import json
import math import math
import yaml import yaml
from functools import reduce # pylint: disable=redefined-builtin from functools import reduce # pylint: disable=redefined-builtin
@ -27,6 +26,7 @@ from salt.ext import six
import salt.utils.compat import salt.utils.compat
import salt.utils.data import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.platform import salt.utils.platform
import salt.utils.yamldumper import salt.utils.yamldumper
from salt.defaults import DEFAULT_TARGET_DELIM from salt.defaults import DEFAULT_TARGET_DELIM
@ -116,7 +116,7 @@ def get(key, default='', delimiter=DEFAULT_TARGET_DELIM, ordered=True):
if ordered is True: if ordered is True:
grains = __grains__ grains = __grains__
else: else:
grains = json.loads(json.dumps(__grains__)) grains = salt.utils.json.loads(salt.utils.json.dumps(__grains__))
return salt.utils.data.traverse_dict_and_list( return salt.utils.data.traverse_dict_and_list(
grains, grains,
key, key,

View file

@ -43,13 +43,13 @@ Module for handling OpenStack Heat calls
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import time import time
import json
import logging import logging
import yaml import yaml
# Import Salt libs # Import Salt libs
from salt.ext import six from salt.ext import six
import salt.utils.files import salt.utils.files
import salt.utils.json
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
# pylint: disable=import-error # pylint: disable=import-error
@ -203,7 +203,7 @@ def _parse_template(tmpl_str):
''' '''
tmpl_str = tmpl_str.strip() tmpl_str = tmpl_str.strip()
if tmpl_str.startswith('{'): if tmpl_str.startswith('{'):
tpl = json.loads(tmpl_str) tpl = salt.utils.json.loads(tmpl_str)
else: else:
try: try:
tpl = yaml.load(tmpl_str, Loader=YamlLoader) tpl = yaml.load(tmpl_str, Loader=YamlLoader)

View file

@ -30,7 +30,6 @@ Module for sending messages to hipchat.
''' '''
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
# Import 3rd-party Libs # Import 3rd-party Libs
@ -41,6 +40,7 @@ from salt.ext.six.moves import range
import salt.ext.six.moves.http_client import salt.ext.six.moves.http_client
import salt.utils.http import salt.utils.http
import salt.utils.json
# pylint: enable=import-error,no-name-in-module,redefined-builtin # pylint: enable=import-error,no-name-in-module,redefined-builtin
@ -154,7 +154,7 @@ def _query(function,
elif api_version == 'v2': elif api_version == 'v2':
headers['Authorization'] = 'Bearer {0}'.format(api_key) headers['Authorization'] = 'Bearer {0}'.format(api_key)
if data: if data:
data = json.dumps(data) data = salt.utils.json.dumps(data)
if method == 'POST': if method == 'POST':
headers['Content-Type'] = 'application/json' headers['Content-Type'] = 'application/json'

View file

@ -14,12 +14,12 @@ Requires an ``api_key`` in ``/etc/salt/minion``:
# Import python libs # Import python libs
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import json
import logging import logging
import time import time
# Import salt libs # Import salt libs
import salt.utils.http import salt.utils.http
import salt.utils.json
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -89,7 +89,7 @@ def trigger_event(event=None, **kwargs):
data['occurredat'] = time.strftime("%B %d, %Y %I:%M%p", time.localtime()) data['occurredat'] = time.strftime("%B %d, %Y %I:%M%p", time.localtime())
result = _query(event=event, result = _query(event=event,
method='POST', method='POST',
data=json.dumps(data) data=salt.utils.json.dumps(data)
) )
if 'status' in result: if 'status' in result:
if result['status'] == 200: if result['status'] == 200:

View file

@ -36,10 +36,10 @@ except ImportError:
HAS_INFLUXDB = False HAS_INFLUXDB = False
import collections import collections
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.json
from salt.state import STATE_INTERNAL_KEYWORDS as _STATE_INTERNAL_KEYWORDS from salt.state import STATE_INTERNAL_KEYWORDS as _STATE_INTERNAL_KEYWORDS
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -678,7 +678,7 @@ def _pull_query_results(resultset):
for _header, _values in resultset.items(): for _header, _values in resultset.items():
_header, _group_tags = _header _header, _group_tags = _header
if _group_tags: if _group_tags:
_results[_header][json.dumps(_group_tags)] = [_value for _value in _values] _results[_header][salt.utils.json.dumps(_group_tags)] = [_value for _value in _values]
else: else:
_results[_header] = [_value for _value in _values] _results[_header] = [_value for _value in _values]
return dict(sorted(_results.items())) return dict(sorted(_results.items()))

View file

@ -9,22 +9,19 @@ Edit ini files
(for example /etc/sysctl.conf) (for example /etc/sysctl.conf)
''' '''
from __future__ import absolute_import, print_function
# Import Python libs # Import Python libs
from __future__ import print_function from __future__ import absolute_import, print_function
from __future__ import absolute_import
import os import os
import re import re
import json
# Import Salt libs # Import Salt libs
from salt.ext import six
import salt.utils.files import salt.utils.files
import salt.utils.json
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
from salt.utils.odict import OrderedDict from salt.utils.odict import OrderedDict
# Import 3rd-party libs
from salt.ext import six
__virtualname__ = 'ini' __virtualname__ = 'ini'
@ -349,10 +346,10 @@ class _Section(OrderedDict):
def __repr__(self, _repr_running=None): def __repr__(self, _repr_running=None):
_repr_running = _repr_running or {} _repr_running = _repr_running or {}
super_repr = super(_Section, self).__repr__(_repr_running) super_repr = super(_Section, self).__repr__(_repr_running)
return os.linesep.join((super_repr, json.dumps(self, indent=4))) return os.linesep.join((super_repr, salt.utils.json.dumps(self, indent=4)))
def __str__(self): def __str__(self):
return json.dumps(self, indent=4) return salt.utils.json.dumps(self, indent=4)
def __eq__(self, item): def __eq__(self, item):
return (isinstance(item, self.__class__) and return (isinstance(item, self.__class__) and

View file

@ -17,7 +17,6 @@ from __future__ import absolute_import
# Import python libraries # Import python libraries
import logging import logging
import json
import os import os
try: try:
@ -27,6 +26,7 @@ except ImportError:
# Import Salt libs # Import Salt libs
import salt.utils.files import salt.utils.files
import salt.utils.json
from salt.ext import six from salt.ext import six
# Juniper interface libraries # Juniper interface libraries
@ -234,7 +234,7 @@ def rpc(cmd=None, dest=None, format='xml', **kwargs):
if format == 'text': if format == 'text':
write_response = reply.text write_response = reply.text
elif format == 'json': elif format == 'json':
write_response = json.dumps(reply, indent=1) write_response = salt.utils.json.dumps(reply, indent=1)
else: else:
write_response = etree.tostring(reply) write_response = etree.tostring(reply)
with salt.utils.files.fopen(dest, 'w') as fp: with salt.utils.files.fopen(dest, 'w') as fp:

View file

@ -18,7 +18,6 @@ from __future__ import absolute_import
import os import os
import re import re
import json
import logging as logger import logging as logger
import base64 import base64
from salt.ext import six from salt.ext import six
@ -28,6 +27,7 @@ from salt.ext.six.moves.urllib.parse import urlparse as _urlparse # pylint: dis
import salt.utils.files import salt.utils.files
import salt.utils.http as http import salt.utils.http as http
import salt.utils.json
__virtualname__ = 'k8s' __virtualname__ = 'k8s'
@ -74,12 +74,15 @@ def _kpost(url, data):
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
# Make request # Make request
log.trace("url is: {0}, data is: {1}".format(url, data)) log.trace("url is: {0}, data is: {1}".format(url, data))
ret = http.query(url, method='POST', header_dict=headers, data=json.dumps(data)) ret = http.query(url,
method='POST',
header_dict=headers,
data=salt.utils.json.dumps(data))
# Check requests status # Check requests status
if ret.get('error'): if ret.get('error'):
return ret return ret
else: else:
return json.loads(ret.get('body')) return salt.utils.json.loads(ret.get('body'))
def _kput(url, data): def _kput(url, data):
@ -88,12 +91,15 @@ def _kput(url, data):
# Prepare headers # Prepare headers
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
# Make request # Make request
ret = http.query(url, method='PUT', header_dict=headers, data=json.dumps(data)) ret = http.query(url,
method='PUT',
header_dict=headers,
data=salt.utils.json.dumps(data))
# Check requests status # Check requests status
if ret.get('error'): if ret.get('error'):
return ret return ret
else: else:
return json.loads(ret.get('body')) return salt.utils.json.loads(ret.get('body'))
def _kpatch(url, data): def _kpatch(url, data):
@ -103,13 +109,13 @@ def _kpatch(url, data):
headers = {"Content-Type": "application/json-patch+json"} headers = {"Content-Type": "application/json-patch+json"}
# Make request # Make request
ret = http.query(url, method='PATCH', header_dict=headers, ret = http.query(url, method='PATCH', header_dict=headers,
data=json.dumps(data)) data=salt.utils.json.dumps(data))
# Check requests status # Check requests status
if ret.get('error'): if ret.get('error'):
log.error("Got an error: {0}".format(ret.get("error"))) log.error("Got an error: {0}".format(ret.get("error")))
return ret return ret
else: else:
return json.loads(ret.get('body')) return salt.utils.json.loads(ret.get('body'))
def _kname(obj): def _kname(obj):
@ -179,7 +185,7 @@ def _get_labels(node, apiserver_url):
ret = http.query(url) ret = http.query(url)
# Check requests status # Check requests status
if 'body' in ret: if 'body' in ret:
ret = json.loads(ret.get('body')) ret = salt.utils.json.loads(ret.get('body'))
elif ret.get('status', 0) == 404: elif ret.get('status', 0) == 404:
return "Node {0} doesn't exist".format(node) return "Node {0} doesn't exist".format(node)
else: else:
@ -387,7 +393,7 @@ def _get_namespaces(apiserver_url, name=""):
# Make request # Make request
ret = http.query(url) ret = http.query(url)
if ret.get("body"): if ret.get("body"):
return json.loads(ret.get("body")) return salt.utils.json.loads(ret.get("body"))
else: else:
return None return None
@ -493,7 +499,7 @@ def _get_secrets(namespace, name, apiserver_url):
# Make request # Make request
ret = http.query(url) ret = http.query(url)
if ret.get("body"): if ret.get("body"):
return json.loads(ret.get("body")) return salt.utils.json.loads(ret.get("body"))
else: else:
return None return None

View file

@ -14,13 +14,12 @@ Kapacitor execution module.
.. versionadded:: 2016.11.0 .. versionadded:: 2016.11.0
''' '''
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.utils.path import salt.utils.path
from salt.utils.decorators import memoize from salt.utils.decorators import memoize
@ -68,7 +67,7 @@ def get_task(name):
if response['status'] == 404: if response['status'] == 404:
return None return None
data = json.loads(response['body']) data = salt.utils.json.loads(response['body'])
if version() < '0.13': if version() < '0.13':
return { return {

View file

@ -13,21 +13,20 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import copy import copy
import functools import functools
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.data import salt.utils.data
import salt.utils.functools import salt.utils.functools
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.pkg import salt.utils.pkg
import salt.utils.versions import salt.utils.versions
from salt.exceptions import CommandExecutionError, MinionError from salt.exceptions import CommandExecutionError, MinionError
from salt.ext import six
from salt.ext.six.moves import zip
# Import third party libs # Import third party libs
import json from salt.ext import six
from salt.ext.six.moves import zip
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -292,7 +291,7 @@ def _info(*pkgs):
if brew_result['retcode']: if brew_result['retcode']:
log.error('Failed to get info about packages: {0}'.format(' '.join(pkgs))) log.error('Failed to get info about packages: {0}'.format(' '.join(pkgs)))
return {} return {}
output = json.loads(brew_result['stdout']) output = salt.utils.json.loads(brew_result['stdout'])
return dict(zip(pkgs, output)) return dict(zip(pkgs, output))
@ -422,7 +421,7 @@ def list_upgrades(refresh=True, **kwargs): # pylint: disable=W0613
ret = {} ret = {}
try: try:
data = json.loads(res['stdout']) data = salt.utils.json.loads(res['stdout'])
except ValueError as err: except ValueError as err:
msg = 'unable to interpret output from "brew outdated": {0}'.format(err) msg = 'unable to interpret output from "brew outdated": {0}'.format(err)
log.error(msg) log.error(msg)

View file

@ -19,10 +19,12 @@ In the minion configuration file, the following block is required:
''' '''
from __future__ import absolute_import from __future__ import absolute_import
# import python std lib # Import Python libs
import json
import logging import logging
# Import Salt libs
import salt.utils.json
# import third party # import third party
try: try:
import requests import requests
@ -115,7 +117,7 @@ def _http_request(url,
log.debug('Querying {}'.format(url)) log.debug('Querying {}'.format(url))
req = session.post(url, req = session.post(url,
headers=headers, headers=headers,
data=json.dumps(data)) data=salt.utils.json.dumps(data))
req_body = req.json() req_body = req.json()
ret = _default_ret() ret = _default_ret()
log.debug('Status code: %d', req.status_code) log.debug('Status code: %d', req.status_code)

View file

@ -8,9 +8,9 @@ Currently this only works when run through a proxy minion.
''' '''
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import salt.utils.http import salt.utils.http
import salt.utils.json
import salt.utils.platform import salt.utils.platform
from salt.exceptions import get_error_message from salt.exceptions import get_error_message
@ -114,7 +114,7 @@ def update_app(id, config):
# mirror marathon-ui handling for uris deprecation (see # mirror marathon-ui handling for uris deprecation (see
# mesosphere/marathon-ui#594 for more details) # mesosphere/marathon-ui#594 for more details)
config.pop('fetch', None) config.pop('fetch', None)
data = json.dumps(config) data = salt.utils.json.dumps(config)
try: try:
response = salt.utils.http.query( response = salt.utils.http.query(
"{0}/v2/apps/{1}?force=true".format(_base_url(), id), "{0}/v2/apps/{1}?force=true".format(_base_url(), id),

View file

@ -16,16 +16,12 @@ Module for sending messages to Mattermost
''' '''
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import, print_function, unicode_literals
import json
import logging import logging
# Import 3rd-party libs # Import Salt libs
# pylint: disable=import-error,no-name-in-module,redefined-builtin import salt.utils.json
import salt.utils.mattermost import salt.utils.mattermost
# pylint: enable=import-error,no-name-in-module
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -136,9 +132,8 @@ def post_message(message,
if username: if username:
parameters['username'] = username parameters['username'] = username
parameters['text'] = '```' + message + '```' # pre-formatted, fixed-width text parameters['text'] = '```' + message + '```' # pre-formatted, fixed-width text
log.debug('Parameters: {0}'.format(parameters)) log.debug('Parameters: %s', parameters)
result = salt.utils.mattermost.query(api_url=api_url, data = str('payload={0}').format(salt.utils.json.dumps(parameters)) # future lint: disable=blacklisted-function
hook=hook, result = salt.utils.mattermost.query(api_url=api_url, hook=hook, data=data)
data='payload={0}'.format(json.dumps(parameters)))
return bool(result) return bool(result)

View file

@ -17,10 +17,10 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import logging import logging
import json
import re import re
# Import salt libs # Import salt libs
import salt.utils.json
from salt.utils.versions import LooseVersion as _LooseVersion from salt.utils.versions import LooseVersion as _LooseVersion
from salt.exceptions import get_error_message as _get_error_message from salt.exceptions import get_error_message as _get_error_message
@ -80,7 +80,7 @@ def _to_dict(objects):
""" """
try: try:
if isinstance(objects, six.string_types): if isinstance(objects, six.string_types):
objects = json.loads(objects) objects = salt.utils.json.loads(objects)
except ValueError as err: except ValueError as err:
log.error("Could not parse objects: %s", err) log.error("Could not parse objects: %s", err)
raise err raise err

View file

@ -10,19 +10,17 @@ Module for sending messages to MS Teams
msteams: msteams:
hook_url: https://outlook.office.com/webhook/837 hook_url: https://outlook.office.com/webhook/837
''' '''
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
# Import 3rd-party libs # Import Salt libs
# pylint: disable=import-error,no-name-in-module,redefined-builtin import salt.utils.json
import salt.ext.six.moves.http_client
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
# Import 3rd-party libs
import salt.ext.six.moves.http_client # pylint: disable=import-error,no-name-in-module,redefined-builtin
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
__virtualname__ = 'msteams' __virtualname__ = 'msteams'
@ -78,7 +76,10 @@ def post_card(message,
"themeColor": theme_color "themeColor": theme_color
} }
result = salt.utils.http.query(hook_url, method='POST', data=json.dumps(payload), status=True) result = salt.utils.http.query(hook_url,
method='POST',
data=salt.utils.json.dumps(payload),
status=True)
if result['status'] <= 201: if result['status'] <= 201:
return True return True

View file

@ -9,10 +9,10 @@ except ImportError:
from pipes import quote as _cmd_quote from pipes import quote as _cmd_quote
# Import python libs # Import python libs
import json
import logging import logging
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.user import salt.utils.user
import salt.modules.cmdmod import salt.modules.cmdmod
@ -169,7 +169,7 @@ def install(pkg=None,
# npm >1.2.21 is putting the output to stderr even though retcode is 0 # npm >1.2.21 is putting the output to stderr even though retcode is 0
npm_output = result['stdout'] or result['stderr'] npm_output = result['stdout'] or result['stderr']
try: try:
return json.loads(npm_output) return salt.utils.json.loads(npm_output)
except ValueError: except ValueError:
pass pass
@ -192,7 +192,7 @@ def _extract_json(npm_output):
while lines and (lines[0].startswith('[fsevents]') or lines[0].startswith('Pass ')): while lines and (lines[0].startswith('[fsevents]') or lines[0].startswith('Pass ')):
lines = lines[1:] lines = lines[1:]
try: try:
return json.loads(''.join(lines)) return salt.utils.json.loads(''.join(lines))
except ValueError: except ValueError:
pass pass
return None return None
@ -322,7 +322,7 @@ def list_(pkg=None, dir=None, runas=None, env=None, depth=None):
if result['retcode'] != 0 and result['stderr']: if result['retcode'] != 0 and result['stderr']:
raise CommandExecutionError(result['stderr']) raise CommandExecutionError(result['stderr'])
return json.loads(result['stdout']).get('dependencies', {}) return salt.utils.json.loads(result['stdout']).get('dependencies', {})
def cache_clean(path=None, runas=None, env=None, force=False): def cache_clean(path=None, runas=None, env=None, force=False):

View file

@ -22,12 +22,12 @@ Module for sending data to OpsGenie
''' '''
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import requests import requests
# Import Salt libs # Import Salt libs
import salt.exceptions import salt.exceptions
import salt.utils.json
API_ENDPOINT = "https://api.opsgenie.com/v1/json/saltstack?apiKey=" API_ENDPOINT = "https://api.opsgenie.com/v1/json/saltstack?apiKey="
@ -93,6 +93,8 @@ def post_data(api_key=None, name='OpsGenie Execution Module', reason=None,
log.debug('Below data will be posted:\n' + str(data)) log.debug('Below data will be posted:\n' + str(data))
log.debug('API Key:' + api_key + '\t API Endpoint:' + API_ENDPOINT) log.debug('API Key:' + api_key + '\t API Endpoint:' + API_ENDPOINT)
response = requests.post(url=API_ENDPOINT + api_key, data=json.dumps(data), response = requests.post(
url=API_ENDPOINT + api_key,
data=salt.utils.json.dumps(data),
headers={'Content-Type': 'application/json'}) headers={'Content-Type': 'application/json'})
return response.status_code, response.text return response.status_code, response.text

View file

@ -7,13 +7,13 @@ Support for OSQuery - https://osquery.io.
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs # Import python libs
import json import logging
# Import Salt libs # Import Salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -41,7 +41,7 @@ def _table_attrs(table):
res = __salt__['cmd.run_all'](cmd) res = __salt__['cmd.run_all'](cmd)
if res['retcode'] == 0: if res['retcode'] == 0:
attrs = [] attrs = []
text = json.loads(res['stdout']) text = salt.utils.json.loads(res['stdout'])
for item in text: for item in text:
attrs.append(item['name']) attrs.append(item['name'])
return attrs return attrs
@ -62,7 +62,7 @@ def _osquery(sql, format='json'):
ret['result'] = False ret['result'] = False
ret['error'] = res['stderr'] ret['error'] = res['stderr']
else: else:
ret['data'] = json.loads(res['stdout']) ret['data'] = salt.utils.json.loads(res['stdout'])
return ret return ret

View file

@ -20,10 +20,10 @@ from __future__ import absolute_import
# Import python libs # Import python libs
import yaml import yaml
import json
# Import salt libs # Import salt libs
import salt.utils.functools import salt.utils.functools
import salt.utils.json
import salt.utils.pagerduty import salt.utils.pagerduty
from salt.ext.six import string_types from salt.ext.six import string_types
@ -181,7 +181,7 @@ def create_event(service_key=None, description=None, details=None,
if isinstance(details, string_types): if isinstance(details, string_types):
details = {'details': details} details = {'details': details}
ret = json.loads(salt.utils.pagerduty.query( ret = salt.utils.json.loads(salt.utils.pagerduty.query(
method='POST', method='POST',
profile_dict=__salt__['config.option'](profile), profile_dict=__salt__['config.option'](profile),
api_key=service_key, api_key=service_key,

View file

@ -20,8 +20,8 @@ For PagerDuty API details, see https://developer.pagerduty.com/documentation/res
''' '''
from __future__ import absolute_import from __future__ import absolute_import
import json
import requests import requests
import salt.utils.json
def __virtual__(): def __virtual__():
@ -164,7 +164,7 @@ def _query(method='GET', profile=None, url=None, path='api/v1',
url, url,
headers=headers, headers=headers,
params=params, params=params,
data=json.dumps(data), data=salt.utils.json.dumps(data),
verify=verify_ssl verify=verify_ssl
) )
@ -185,7 +185,7 @@ def _query(method='GET', profile=None, url=None, path='api/v1',
url, url,
headers=headers, headers=headers,
params=params, params=params,
data=data, # must not use json.dumps() or offset/limit get lost data=data, # Already serialized above, don't do it again
verify=verify_ssl).json() verify=verify_ssl).json()
offset = next_page_results['offset'] offset = next_page_results['offset']
limit = next_page_results['limit'] limit = next_page_results['limit']

View file

@ -83,11 +83,11 @@ import shutil
import logging import logging
import sys import sys
import tempfile import tempfile
import json
# Import Salt libs # Import Salt libs
import salt.utils.data import salt.utils.data
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.locales import salt.utils.locales
import salt.utils.platform import salt.utils.platform
import salt.utils.url import salt.utils.url
@ -1151,7 +1151,7 @@ def list_upgrades(bin_env=None,
packages = {} packages = {}
try: try:
json_results = json.loads(result['stdout']) json_results = salt.utils.json.loads(result['stdout'])
for json_result in json_results: for json_result in json_results:
packages[json_result['name']] = json_result['latest_version'] packages[json_result['name']] = json_result['latest_version']
except ValueError: except ValueError:

View file

@ -4,26 +4,25 @@ Module to provide RabbitMQ compatibility to Salt.
Todo: A lot, need to add cluster support, logging, and minion configuration Todo: A lot, need to add cluster support, logging, and minion configuration
data. data.
''' '''
# Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
# Import python libs
import json
import re
import logging import logging
import os import os
import os.path
import random import random
import re
import string import string
# Import salt libs # Import Salt libs
import salt.utils.itertools import salt.utils.itertools
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
import salt.utils.user import salt.utils.user
from salt.exceptions import CommandExecutionError, SaltInvocationError
# Import 3rd-party libs
from salt.ext import six from salt.ext import six
from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range from salt.ext.six.moves import range
from salt.exceptions import CommandExecutionError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -866,7 +865,7 @@ def set_policy(vhost, name, pattern, definition, priority=None, apply_to=None, r
if runas is None and not salt.utils.platform.is_windows(): if runas is None and not salt.utils.platform.is_windows():
runas = salt.utils.user.get_user() runas = salt.utils.user.get_user()
if isinstance(definition, dict): if isinstance(definition, dict):
definition = json.dumps(definition) definition = salt.utils.json.dumps(definition)
if not isinstance(definition, six.string_types): if not isinstance(definition, six.string_types):
raise SaltInvocationError( raise SaltInvocationError(
'The \'definition\' argument must be a dictionary or JSON string' 'The \'definition\' argument must be a dictionary or JSON string'

View file

@ -15,12 +15,12 @@ Requires a ``username`` and a ``password`` in ``/etc/salt/minion``:
# Import python libs # Import python libs
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import json
import logging import logging
# Import salt libs # Import salt libs
from salt.exceptions import SaltInvocationError from salt.exceptions import SaltInvocationError
import salt.utils.http import salt.utils.http
import salt.utils.json
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -202,7 +202,7 @@ def update_item(name, id_, field=None, value=None, postdata=None):
action=name, action=name,
command=id_, command=id_,
method='POST', method='POST',
data=json.dumps(postdata), data=salt.utils.json.dumps(postdata),
) )
return result return result

View file

@ -22,16 +22,15 @@ from __future__ import absolute_import
import logging import logging
# Import 3rd-party libs # Import 3rd-party libs
import json
# pylint: disable=import-error,no-name-in-module,redefined-builtin # pylint: disable=import-error,no-name-in-module,redefined-builtin
import salt.ext.six import salt.ext.six
import salt.ext.six.moves.http_client import salt.ext.six.moves.http_client
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
# pylint: enable=import-error,no-name-in-module,redefined-builtin
# Import salt libs # Import salt libs
import salt.utils.http import salt.utils.http
import salt.utils.json
# pylint: enable=import-error,no-name-in-module,redefined-builtin
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
__virtualname__ = 'random_org' __virtualname__ = 'random_org'
@ -97,7 +96,7 @@ def _query(api_version=None, data=None):
api_url = 'https://api.random.org/' api_url = 'https://api.random.org/'
base_url = _urljoin(api_url, 'json-rpc/' + str(api_version) + '/invoke') base_url = _urljoin(api_url, 'json-rpc/' + str(api_version) + '/invoke')
data = json.dumps(data) data = salt.utils.json.dumps(data)
result = salt.utils.http.query( result = salt.utils.http.query(
base_url, base_url,

View file

@ -2,13 +2,12 @@
''' '''
Control a salt cloud system Control a salt cloud system
''' '''
from __future__ import absolute_import
# Import python libs # Import python libs
import json from __future__ import absolute_import
# Import salt libs # Import salt libs
import salt.utils.data import salt.utils.data
import salt.utils.json
HAS_CLOUD = False HAS_CLOUD = False
try: try:
@ -43,7 +42,7 @@ def create(name, profile):
cmd = 'salt-cloud --out json -p {0} {1}'.format(profile, name) cmd = 'salt-cloud --out json -p {0} {1}'.format(profile, name)
out = __salt__['cmd.run_stdout'](cmd, python_shell=False) out = __salt__['cmd.run_stdout'](cmd, python_shell=False)
try: try:
ret = json.loads(out, object_hook=salt.utils.data.encode_dict) ret = salt.utils.json.loads(out, object_hook=salt.utils.data.encode_dict)
except ValueError: except ValueError:
ret = {} ret = {}
return ret return ret

View file

@ -8,17 +8,18 @@ Wrapper around Server Density API
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
import os import os
import tempfile import tempfile
# Import Salt libs
import salt.utils.json
from salt.exceptions import CommandExecutionError
# Import 3rd-party libs # Import 3rd-party libs
from salt.ext import six from salt.ext import six
from salt.ext.six.moves import map # pylint: disable=import-error,no-name-in-module,redefined-builtin from salt.ext.six.moves import map # pylint: disable=import-error,no-name-in-module,redefined-builtin
from salt.exceptions import CommandExecutionError
try: try:
import requests import requests
ENABLED = True ENABLED = True
@ -99,7 +100,7 @@ def create(name, **params):
log.debug('Server Density API Response content: {0}'.format(api_response.content)) log.debug('Server Density API Response content: {0}'.format(api_response.content))
if api_response.status_code == 200: if api_response.status_code == 200:
try: try:
return json.loads(api_response.content) return salt.utils.json.loads(api_response.content)
except ValueError: except ValueError:
log.error('Could not parse API Response content: {0}'.format(api_response.content)) log.error('Could not parse API Response content: {0}'.format(api_response.content))
raise CommandExecutionError( raise CommandExecutionError(
@ -130,7 +131,7 @@ def delete(device_id):
log.debug('Server Density API Response content: {0}'.format(api_response.content)) log.debug('Server Density API Response content: {0}'.format(api_response.content))
if api_response.status_code == 200: if api_response.status_code == 200:
try: try:
return json.loads(api_response.content) return salt.utils.json.loads(api_response.content)
except ValueError: except ValueError:
log.error('Could not parse API Response content: {0}'.format(api_response.content)) log.error('Could not parse API Response content: {0}'.format(api_response.content))
raise CommandExecutionError( raise CommandExecutionError(
@ -172,13 +173,13 @@ def ls(**params):
api_response = requests.get( api_response = requests.get(
'https://api.serverdensity.io/inventory/{0}'.format(endpoint), 'https://api.serverdensity.io/inventory/{0}'.format(endpoint),
params={'token': get_sd_auth('api_token'), 'filter': json.dumps(params)} params={'token': get_sd_auth('api_token'), 'filter': salt.utils.json.dumps(params)}
) )
log.debug('Server Density API Response: {0}'.format(api_response)) log.debug('Server Density API Response: {0}'.format(api_response))
log.debug('Server Density API Response content: {0}'.format(api_response.content)) log.debug('Server Density API Response content: {0}'.format(api_response.content))
if api_response.status_code == 200: if api_response.status_code == 200:
try: try:
return json.loads(api_response.content) return salt.utils.json.loads(api_response.content)
except ValueError: except ValueError:
log.error( log.error(
'Could not parse Server Density API Response content: {0}' 'Could not parse Server Density API Response content: {0}'
@ -217,7 +218,7 @@ def update(device_id, **params):
log.debug('Server Density API Response content: {0}'.format(api_response.content)) log.debug('Server Density API Response content: {0}'.format(api_response.content))
if api_response.status_code == 200: if api_response.status_code == 200:
try: try:
return json.loads(api_response.content) return salt.utils.json.loads(api_response.content)
except ValueError: except ValueError:
log.error( log.error(
'Could not parse Server Density API Response content: {0}' 'Could not parse Server Density API Response content: {0}'

View file

@ -18,21 +18,21 @@ Module for sending messages to Slack
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import logging import logging
# Import Salt libs
import salt.utils.json
import salt.utils.slack
from salt.exceptions import SaltInvocationError
# Import 3rd-party libs # Import 3rd-party libs
# pylint: disable=import-error,no-name-in-module,redefined-builtin # pylint: disable=import-error,no-name-in-module,redefined-builtin
from salt.ext.six.moves.urllib.parse import urlencode as _urlencode from salt.ext.six.moves.urllib.parse import urlencode as _urlencode
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
from salt.ext.six.moves import range from salt.ext.six.moves import range
import salt.ext.six.moves.http_client import salt.ext.six.moves.http_client
import salt.utils.slack
# pylint: enable=import-error,no-name-in-module # pylint: enable=import-error,no-name-in-module
from salt.exceptions import SaltInvocationError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
__virtualname__ = 'slack' __virtualname__ = 'slack'
@ -308,7 +308,7 @@ def call_hook(message,
data = _urlencode( data = _urlencode(
{ {
'payload': json.dumps(payload, ensure_ascii=False) 'payload': salt.utils.json.dumps(payload)
} }
) )
result = salt.utils.http.query(url, method='POST', data=data, status=True) result = salt.utils.http.query(url, method='POST', data=data, status=True)

View file

@ -6,9 +6,9 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json
# Import Salt libs # Import Salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
import salt.utils.decorators as decorators import salt.utils.decorators as decorators
@ -144,7 +144,7 @@ def avail(search=None, verbose=False):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
return ret return ret
for image in json.loads(res['stdout']): for image in salt.utils.json.loads(res['stdout']):
if image['manifest']['disabled'] or not image['manifest']['public']: if image['manifest']['disabled'] or not image['manifest']['public']:
continue continue
if search and search not in image['manifest']['name']: if search and search not in image['manifest']['name']:
@ -178,7 +178,7 @@ def list_installed(verbose=False):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
return ret return ret
for image in json.loads(res['stdout']): for image in salt.utils.json.loads(res['stdout']):
result[image['manifest']['uuid']] = _parse_image_meta(image, verbose) result[image['manifest']['uuid']] = _parse_image_meta(image, verbose)
return result return result
@ -205,7 +205,7 @@ def show(uuid):
if retcode != 0: if retcode != 0:
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
return ret return ret
ret = json.loads(res['stdout']) ret = salt.utils.json.loads(res['stdout'])
return ret return ret
@ -230,7 +230,7 @@ def get(uuid):
if retcode != 0: if retcode != 0:
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
return ret return ret
ret = json.loads(res['stdout']) ret = salt.utils.json.loads(res['stdout'])
return ret return ret

View file

@ -6,7 +6,6 @@ from __future__ import absolute_import
# Import Python libs # Import Python libs
import logging import logging
import json
import os import os
try: try:
from shlex import quote as _quote_args # pylint: disable=E0611 from shlex import quote as _quote_args # pylint: disable=E0611
@ -17,8 +16,10 @@ except ImportError:
import salt.utils.args import salt.utils.args
import salt.utils.decorators as decorators import salt.utils.decorators as decorators
import salt.utils.files import salt.utils.files
import salt.utils.json
import salt.utils.path import salt.utils.path
import salt.utils.platform import salt.utils.platform
import salt.utils.stringutils
from salt.utils.odict import OrderedDict from salt.utils.odict import OrderedDict
# Import 3rd-party libs # Import 3rd-party libs
@ -96,7 +97,7 @@ def _create_update_from_file(mode='create', uuid=None, path=None):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
if 'stderr' in res: if 'stderr' in res:
if res['stderr'][0] == '{': if res['stderr'][0] == '{':
ret['Error'] = json.loads(res['stderr']) ret['Error'] = salt.utils.json.loads(res['stderr'])
else: else:
ret['Error'] = res['stderr'] ret['Error'] = res['stderr']
return ret return ret
@ -113,7 +114,7 @@ def _create_update_from_file(mode='create', uuid=None, path=None):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
if 'stderr' in res: if 'stderr' in res:
if res['stderr'][0] == '{': if res['stderr'][0] == '{':
ret['Error'] = json.loads(res['stderr']) ret['Error'] = salt.utils.json.loads(res['stderr'])
else: else:
ret['Error'] = res['stderr'] ret['Error'] = res['stderr']
return ret return ret
@ -133,7 +134,7 @@ def _create_update_from_cfg(mode='create', uuid=None, vmcfg=None):
# write json file # write json file
vmadm_json_file = __salt__['temp.file'](prefix='vmadm-') vmadm_json_file = __salt__['temp.file'](prefix='vmadm-')
with salt.utils.files.fopen(vmadm_json_file, 'w') as vmadm_json: with salt.utils.files.fopen(vmadm_json_file, 'w') as vmadm_json:
vmadm_json.write(json.dumps(vmcfg)) salt.utils.json.dump(vmcfg, vmadm_json)
# vmadm validate create|update [-f <filename>] # vmadm validate create|update [-f <filename>]
cmd = '{vmadm} validate {mode} {brand} -f {vmadm_json_file}'.format( cmd = '{vmadm} validate {mode} {brand} -f {vmadm_json_file}'.format(
@ -148,7 +149,7 @@ def _create_update_from_cfg(mode='create', uuid=None, vmcfg=None):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
if 'stderr' in res: if 'stderr' in res:
if res['stderr'][0] == '{': if res['stderr'][0] == '{':
ret['Error'] = json.loads(res['stderr']) ret['Error'] = salt.utils.json.loads(res['stderr'])
else: else:
ret['Error'] = res['stderr'] ret['Error'] = res['stderr']
return ret return ret
@ -165,7 +166,7 @@ def _create_update_from_cfg(mode='create', uuid=None, vmcfg=None):
ret['Error'] = _exit_status(retcode) ret['Error'] = _exit_status(retcode)
if 'stderr' in res: if 'stderr' in res:
if res['stderr'][0] == '{': if res['stderr'][0] == '{':
ret['Error'] = json.loads(res['stderr']) ret['Error'] = salt.utils.json.loads(res['stderr'])
else: else:
ret['Error'] = res['stderr'] ret['Error'] = res['stderr']
return ret return ret
@ -404,7 +405,7 @@ def lookup(search=None, order=None, one=False):
if one: if one:
result = res['stdout'] result = res['stdout']
else: else:
for vm in json.loads(res['stdout']): for vm in salt.utils.json.loads(res['stdout']):
result.append(vm) result.append(vm)
return result return result
@ -525,7 +526,7 @@ def get(vm, key='uuid'):
if retcode != 0: if retcode != 0:
ret['Error'] = res['stderr'] if 'stderr' in res else _exit_status(retcode) ret['Error'] = res['stderr'] if 'stderr' in res else _exit_status(retcode)
return ret return ret
return json.loads(res['stdout']) return salt.utils.json.loads(res['stdout'])
def info(vm, info_type='all', key='uuid'): def info(vm, info_type='all', key='uuid'):
@ -570,7 +571,7 @@ def info(vm, info_type='all', key='uuid'):
if retcode != 0: if retcode != 0:
ret['Error'] = res['stderr'] if 'stderr' in res else _exit_status(retcode) ret['Error'] = res['stderr'] if 'stderr' in res else _exit_status(retcode)
return ret return ret
return json.loads(res['stdout']) return salt.utils.json.loads(res['stdout'])
def create_snapshot(vm, name, key='uuid'): def create_snapshot(vm, name, key='uuid'):
@ -756,10 +757,10 @@ def reprovision(vm, image, key='uuid'):
ret['Error'] = 'Image ({0}) is not present on this host'.format(image) ret['Error'] = 'Image ({0}) is not present on this host'.format(image)
return ret return ret
# vmadm reprovision <uuid> [-f <filename>] # vmadm reprovision <uuid> [-f <filename>]
cmd = 'echo {image} | {vmadm} reprovision {uuid}'.format( cmd = str('echo {image} | {vmadm} reprovision {uuid}').format( # future lint: disable=blacklisted-function
vmadm=vmadm, vmadm=salt.utils.stringutils.to_str(vmadm),
uuid=vm, uuid=salt.utils.stringutils.to_str(vm),
image=_quote_args(json.dumps({'image_uuid': image})) image=_quote_args(salt.utils.json.dumps({'image_uuid': image}))
) )
res = __salt__['cmd.run_all'](cmd, python_shell=True) res = __salt__['cmd.run_all'](cmd, python_shell=True)
retcode = res['retcode'] retcode = res['retcode']

View file

@ -61,7 +61,6 @@ verbose : True
# Import python Libs # Import python Libs
from __future__ import absolute_import from __future__ import absolute_import
import json
import os import os
# Import 3rd-party libs # Import 3rd-party libs
@ -77,6 +76,7 @@ from salt.ext.six.moves.urllib.request import (
# pylint: enable=no-name-in-module,import-error # pylint: enable=no-name-in-module,import-error
# Import salt libs # Import salt libs
import salt.utils.json
import salt.utils.path import salt.utils.path
# ######################### PRIVATE METHODS ############################## # ######################### PRIVATE METHODS ##############################
@ -258,7 +258,7 @@ def _auth(url):
def _http_request(url, request_timeout=None): def _http_request(url, request_timeout=None):
''' '''
PRIVATE METHOD PRIVATE METHOD
Uses json.load to fetch the JSON results from the solr API. Uses salt.utils.json.load to fetch the JSON results from the solr API.
url : str url : str
a complete URL that can be passed to urllib.open a complete URL that can be passed to urllib.open
@ -274,10 +274,8 @@ def _http_request(url, request_timeout=None):
try: try:
request_timeout = __salt__['config.option']('solr.request_timeout') request_timeout = __salt__['config.option']('solr.request_timeout')
if request_timeout is None: kwargs = {} if request_timeout is None else {'timeout': request_timeout}
data = json.load(_urlopen(url)) data = salt.utils.json.load(_urlopen(url, **kwargs))
else:
data = json.load(_urlopen(url, timeout=request_timeout))
return _get_return_dict(True, data, []) return _get_return_dict(True, data, [])
except Exception as err: except Exception as err:
return _get_return_dict(False, {}, ["{0} : {1}".format(url, err)]) return _get_return_dict(False, {}, ["{0} : {1}".format(url, err)])

View file

@ -14,7 +14,6 @@ states themselves.
# Import python libs # Import python libs
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import fnmatch import fnmatch
import json
import logging import logging
import os import os
import shutil import shutil
@ -34,8 +33,10 @@ import salt.utils.files
import salt.utils.functools import salt.utils.functools
import salt.utils.hashutils import salt.utils.hashutils
import salt.utils.jid import salt.utils.jid
import salt.utils.json
import salt.utils.platform import salt.utils.platform
import salt.utils.state import salt.utils.state
import salt.utils.stringutils
import salt.utils.url import salt.utils.url
import salt.utils.versions import salt.utils.versions
from salt.exceptions import CommandExecutionError, SaltInvocationError from salt.exceptions import CommandExecutionError, SaltInvocationError
@ -1996,7 +1997,7 @@ def pkg(pkg_path,
s_pkg.close() s_pkg.close()
lowstate_json = os.path.join(root, 'lowstate.json') lowstate_json = os.path.join(root, 'lowstate.json')
with salt.utils.files.fopen(lowstate_json, 'r') as fp_: with salt.utils.files.fopen(lowstate_json, 'r') as fp_:
lowstate = json.load(fp_, object_hook=salt.utils.data.encode_dict) lowstate = salt.utils.json.load(fp_)
# Check for errors in the lowstate # Check for errors in the lowstate
for chunk in lowstate: for chunk in lowstate:
if not isinstance(chunk, dict): if not isinstance(chunk, dict):
@ -2004,14 +2005,14 @@ def pkg(pkg_path,
pillar_json = os.path.join(root, 'pillar.json') pillar_json = os.path.join(root, 'pillar.json')
if os.path.isfile(pillar_json): if os.path.isfile(pillar_json):
with salt.utils.files.fopen(pillar_json, 'r') as fp_: with salt.utils.files.fopen(pillar_json, 'r') as fp_:
pillar_override = json.load(fp_) pillar_override = salt.utils.json.load(fp_)
else: else:
pillar_override = None pillar_override = None
roster_grains_json = os.path.join(root, 'roster_grains.json') roster_grains_json = os.path.join(root, 'roster_grains.json')
if os.path.isfile(roster_grains_json): if os.path.isfile(roster_grains_json):
with salt.utils.files.fopen(roster_grains_json, 'r') as fp_: with salt.utils.files.fopen(roster_grains_json, 'r') as fp_:
roster_grains = json.load(fp_, object_hook=salt.utils.data.encode_dict) roster_grains = salt.utils.json.load(fp_, object_hook=salt.utils.data.encode_dict)
if os.path.isfile(roster_grains_json): if os.path.isfile(roster_grains_json):
popts['grains'] = roster_grains popts['grains'] = roster_grains
@ -2237,12 +2238,15 @@ def event(tagmatch='*',
if fnmatch.fnmatch(ret['tag'], tagmatch): if fnmatch.fnmatch(ret['tag'], tagmatch):
if not quiet: if not quiet:
print('{0}\t{1}'.format( salt.utils.stringutils.print_cli(
ret['tag'], str('{0}\t{1}').format( # future lint: blacklisted-function
json.dumps( salt.utils.stringutils.to_str(ret['tag']),
salt.utils.json.dumps(
ret['data'], ret['data'],
sort_keys=pretty, sort_keys=pretty,
indent=None if not pretty else 4))) indent=None if not pretty else 4)
)
)
sys.stdout.flush() sys.stdout.flush()
count -= 1 count -= 1

View file

@ -24,7 +24,7 @@ More information: https://docker-py.readthedocs.io/en/stable/
""" """
# Import python libraries # Import python libraries
from __future__ import absolute_import from __future__ import absolute_import
import json import salt.utils.json
try: try:
import docker import docker
@ -238,8 +238,8 @@ def swarm_service_info(service_name=str):
salt_return = {} salt_return = {}
client = docker.APIClient(base_url='unix://var/run/docker.sock') client = docker.APIClient(base_url='unix://var/run/docker.sock')
service = client.inspect_service(service=service_name) service = client.inspect_service(service=service_name)
getdata = json.dumps(service) getdata = salt.utils.json.dumps(service)
dump = json.loads(getdata) dump = salt.utils.json.loads(getdata)
version = dump['Version']['Index'] version = dump['Version']['Index']
name = dump['Spec']['Name'] name = dump['Spec']['Name']
network_mode = dump['Spec']['EndpointSpec']['Mode'] network_mode = dump['Spec']['EndpointSpec']['Mode']
@ -316,8 +316,8 @@ def node_ls(server=str):
salt_return = {} salt_return = {}
client = docker.APIClient(base_url='unix://var/run/docker.sock') client = docker.APIClient(base_url='unix://var/run/docker.sock')
service = client.nodes(filters=({'name': server})) service = client.nodes(filters=({'name': server}))
getdata = json.dumps(service) getdata = salt.utils.json.dumps(service)
dump = json.loads(getdata) dump = salt.utils.json.loads(getdata)
for items in dump: for items in dump:
docker_version = items['Description']['Engine']['EngineVersion'] docker_version = items['Description']['Engine']['EngineVersion']
platform = items['Description']['Platform'] platform = items['Description']['Platform']

View file

@ -23,11 +23,16 @@ https://github.com/mongolab/mongolab-telemetry-api-docs/blob/master/alerts.md
:depends: requests :depends: requests
''' '''
# Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
from salt._compat import string_types
import json
import logging import logging
# Import Salt libs
import salt.utils.json
# Import 3rd-party libs
from salt.ext import six
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Import third party libs # Import third party libs
@ -57,7 +62,7 @@ def _auth(api_key=None, profile='telemetry'):
if api_key is None and profile is None: if api_key is None and profile is None:
raise Exception("Missing api_key and profile") raise Exception("Missing api_key and profile")
if profile: if profile:
if isinstance(profile, string_types): if isinstance(profile, six.string_types):
_profile = __salt__['config.option'](profile) _profile = __salt__['config.option'](profile)
elif isinstance(profile, dict): elif isinstance(profile, dict):
_profile = profile _profile = profile
@ -184,7 +189,7 @@ def get_notification_channel_id(notify_channel, profile="telemetry"):
"name": notify_channel[:notify_channel.find('@')] + 'EscalationPolicy', "name": notify_channel[:notify_channel.find('@')] + 'EscalationPolicy',
"email": notify_channel "email": notify_channel
} }
response = requests.post(post_url, data=json.dumps(data), headers=auth) response = requests.post(post_url, data=salt.utils.json.dumps(data), headers=auth)
if response.status_code == 200: if response.status_code == 200:
log.info("Successfully created EscalationPolicy {0} with EmailNotificationChannel {1}" log.info("Successfully created EscalationPolicy {0} with EmailNotificationChannel {1}"
.format(data.get('name'), notify_channel)) .format(data.get('name'), notify_channel))
@ -224,7 +229,7 @@ def get_alarms(deployment_id, profile="telemetry"):
return 'No alarms defined for deployment: {0}'.format(deployment_id) return 'No alarms defined for deployment: {0}'.format(deployment_id)
else: else:
# Non 200 response, sent back the error response' # Non 200 response, sent back the error response'
return {'err_code': response.status_code, 'err_msg': json.loads(response.text).get('err', '')} return {'err_code': response.status_code, 'err_msg': salt.utils.json.loads(response.text).get('err', '')}
def create_alarm(deployment_id, metric_name, data, api_key=None, profile="telemetry"): def create_alarm(deployment_id, metric_name, data, api_key=None, profile="telemetry"):
@ -259,19 +264,24 @@ def create_alarm(deployment_id, metric_name, data, api_key=None, profile="teleme
} }
try: try:
response = requests.post(request_uri, data=json.dumps(post_body), headers=auth) response = requests.post(request_uri, data=salt.utils.json.dumps(post_body), headers=auth)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
# TODO: May be we should retry? # TODO: May be we should retry?
log.error(str(e)) log.error(str(e))
if response.status_code >= 200 and response.status_code < 300: if response.status_code >= 200 and response.status_code < 300:
# update cache # update cache
log.info("Created alarm on metric: {0} in deployment: {1}".format(metric_name, deployment_id)) log.info('Created alarm on metric: %s in deployment: %s', metric_name, deployment_id)
log.debug("Updating cache for metric {0} in deployment {1}: {2}".format(metric_name, deployment_id, response.json())) log.debug('Updating cache for metric %s in deployment %s: %s',
metric_name, deployment_id, response.json())
_update_cache(deployment_id, metric_name, response.json()) _update_cache(deployment_id, metric_name, response.json())
else: else:
log.error("Failed to create alarm on metric: {0} in deployment {1}: payload: {2}". log.error(
format(metric_name, deployment_id, json.dumps(post_body))) str('Failed to create alarm on metric: %s in deployment %s: payload: %s'), # future lint: disable=blacklisted-function
salt.utils.stringutils.to_str(metric_name),
salt.utils.stringutils.to_str(deployment_id),
salt.utils.json.dumps(post_body)
)
return response.status_code >= 200 and response.status_code < 300, response.json() return response.status_code >= 200 and response.status_code < 300, response.json()
@ -308,19 +318,26 @@ def update_alarm(deployment_id, metric_name, data, api_key=None, profile="teleme
} }
try: try:
response = requests.put(request_uri, data=json.dumps(post_body), headers=auth) response = requests.put(request_uri, data=salt.utils.json.dumps(post_body), headers=auth)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
log.error("Update failed {0}" .format(str(e))) log.error('Update failed: %s', e)
return False, str(e) return False, str(e)
if response.status_code >= 200 and response.status_code < 300: if response.status_code >= 200 and response.status_code < 300:
# Also update cache # Also update cache
log.debug("Updating cache for metric {0} in deployment {1}: {2}".format(metric_name, deployment_id, response.json())) log.debug('Updating cache for metric %s in deployment %s: %s',
metric_name, deployment_id, response.json())
_update_cache(deployment_id, metric_name, response.json()) _update_cache(deployment_id, metric_name, response.json())
log.info("Updated alarm on metric: {0} in deployment: {1}".format(metric_name, deployment_id)) log.info('Updated alarm on metric: %s in deployment: %s', metric_name, deployment_id)
return True, response.json() return True, response.json()
err_msg = "Failed to create alarm on metric: {0} in deployment {1}: payload: {2}".format(metric_name, deployment_id, json.dumps(post_body)) err_msg = str( # future lint: disable=blacklisted-function
'Failed to create alarm on metric: {0} in deployment: {1} '
'payload: {2}').format(
salt.utils.stringutils.to_str(metric_name),
salt.utils.stringutils.to_str(deployment_id),
salt.utils.json.dumps(post_body)
)
log.error(err_msg) log.error(err_msg)
return False, err_msg return False, err_msg

Some files were not shown because too many files have changed in this diff Show more