# Buggy code
def _init_client():
    """Setup client and init datastore."""
    global client, path_prefix
    if client is not None:
        return

    etcd_kwargs = {
        "host": __opts__.get("etcd.host", "127.0.0.1"),
        "port": __opts__.get("etcd.port", 2379),
        "protocol": __opts__.get("etcd.protocol", "http"),
        "allow_reconnect": __opts__.get("etcd.allow_reconnect", True),
        "allow_redirect": __opts__.get("etcd.allow_redirect", False),
        "srv_domain": __opts__.get("etcd.srv_domain", None),
        "read_timeout": __opts__.get("etcd.read_timeout", 60),
        "username": __opts__.get("etcd.username", None),
        "password": __opts__.get("etcd.password", None),
        "cert": __opts__.get("etcd.cert", None),
        "ca_cert": __opts__.get("etcd.ca_cert", None),
    }
    path_prefix = __opts__.get("etcd.path_prefix", _DEFAULT_PATH_PREFIX)
    if path_prefix != "":
        path_prefix = "/{0}".format(path_prefix.strip("/"))
    log.info("etcd: Setting up client with params: %r", etcd_kwargs)
    client = etcd.Client(**etcd_kwargs)
    try:
        client.get(path_prefix)
    except etcd.EtcdKeyNotFound:
        log.info("etcd: Creating dir %r", path_prefix)
        client.write(path_prefix, None, dir=True)


# Traceback (SaltCacheError)
salt-master[3903]: [ERROR   ] Error in function _mine:
salt-master[3903]: Traceback (most recent call last):
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1776, in run_func
salt-master[3903]:     ret = getattr(self, func)(load)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1345, in _mine
salt-master[3903]:     return self.masterapi._mine(load, skip_verify=True)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/daemons/masterapi.py", line 607, in _mine
salt-master[3903]:     data = self.cache.fetch(cbank, ckey)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/cache/__init__.py", line 180, in fetch
salt-master[3903]:     return self.modules[fun](bank, key, **self._kwargs)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/cache/etcd_cache.py", line 150, in fetch
salt-master[3903]:     etcd_key, exc
salt-master[3903]: SaltCacheError: There was an error reading the key, /salt_cache/minions/ca.aws-cc1.dev.xxx.yyy.com/mine: Key not found : /salt_cache/minions/ca.aws-cc1.dev.xxx.yyy.com/mine
salt-master[3903]: [ERROR   ] Error in function _mine_get:
salt-master[3903]: Traceback (most recent call last):
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1776, in run_func
salt-master[3903]:     ret = getattr(self, func)(load)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1331, in _mine_get
salt-master[3903]:     return self.masterapi._mine_get(load, skip_verify=True)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/daemons/masterapi.py", line 589, in _mine_get
salt-master[3903]:     fdata = self.cache.fetch('minions/{0}'.format(minion), 'mine')
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/cache/__init__.py", line 180, in fetch
salt-master[3903]:     return self.modules[fun](bank, key, **self._kwargs)
salt-master[3903]:   File "/usr/lib/python2.7/dist-packages/salt/cache/etcd_cache.py", line 150, in fetch
salt-master[3903]:     etcd_key, exc
salt-master[3903]: SaltCacheError: There was an error reading the key, /salt_cache/minions/ca.aws-cc1.dev.xxx.yyy.com/mine: Key not found : /salt_cache/minions/ca.aws-cc1.dev.xxx.yyy.com/mine

# Fixed code
def _init_client():
    """Setup client and init datastore."""
    global client, path_prefix
    if client is not None:
        return

    etcd_kwargs = {
        "host": __opts__.get("etcd.host", "127.0.0.1"),
        "port": __opts__.get("etcd.port", 2379),
        "protocol": __opts__.get("etcd.protocol", "http"),
        "allow_reconnect": __opts__.get("etcd.allow_reconnect", True),
        "allow_redirect": __opts__.get("etcd.allow_redirect", False),
        "srv_domain": __opts__.get("etcd.srv_domain", None),
        "read_timeout": __opts__.get("etcd.read_timeout", 60),
        "username": __opts__.get("etcd.username", None),
        "password": __opts__.get("etcd.password", None),
        "cert": __opts__.get("etcd.cert", None),
        "ca_cert": __opts__.get("etcd.ca_cert", None),
    }
    path_prefix = __opts__.get("etcd.path_prefix", _DEFAULT_PATH_PREFIX)
    if path_prefix != "":
        path_prefix = "/{0}".format(path_prefix.strip("/"))
    log.info("etcd: Setting up client with params: %r", etcd_kwargs)
    client = etcd.Client(**etcd_kwargs)
    try:
        client.read(path_prefix)
    except etcd.EtcdKeyNotFound:
        log.info("etcd: Creating dir %r", path_prefix)
        client.write(path_prefix, None, dir=True)