# Buggy code
def __init__(
    self,
    opts,
    pillar=None,
    jid=None,
    pillar_enc=None,
    proxy=None,
    context=None,
    mocked=False,
    loader="states",
):
    self.states_loader = loader
    if "grains" not in opts:
        opts["grains"] = salt.loader.grains(opts)
    self.opts = opts
    self.proxy = proxy
    self._pillar_override = pillar
    if pillar_enc is not None:
        try:
            pillar_enc = pillar_enc.lower()
        except AttributeError:
            pillar_enc = str(pillar_enc).lower()
        if pillar_enc not in VALID_PILLAR_ENC:
            raise SaltInvocationError(
                "Invalid pillar encryption type. Valid types are: {0}".format(
                    ", ".join(VALID_PILLAR_ENC)
                )
            )
    self._pillar_enc = pillar_enc
    self.opts["pillar"] = self._gather_pillar()
    self.state_con = context or {}
    self.load_modules(proxy=proxy)
    self.active = set()
    self.mod_init = set()
    self.pre = {}
    self.__run_num = 0
    self.jid = jid
    self.instance_id = str(id(self))
    self.inject_globals = {}
    self.mocked = mocked


# Traceback (KeyError)
Invoking the same function in state files

$ salt cluster state.show_highstate
cluster:
- Rendering SLS 'base:esxcluster' failed: Jinja error: 'esxcluster.get_details'
Traceback (most recent call last):
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/utils/templates.py", line 368, in render_jinja_tmpl
output = template.render(**decoded_context)
File ".../jinja2/2.7.3-py27/lib/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File ".../jinja2/2.7.3-py27/lib/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 29, in top-level template code
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/modules/vsphere.py", line 262, in __supports_proxies
return fn(*args, **clean_kwargs(**kwargs))
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/modules/vsphere.py", line 295, in _gets_service_instance_via_proxy
connection_details = _get_proxy_connection_details()
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/modules/vsphere.py", line 233, in _get_proxy_connection_details
details = __salt__['esxcluster.get_details']()
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/modules/esxcluster.py", line 29, in get_details
return __proxy__['esxcluster.get_details']()
File "/var/tmp/bleotu/virtual/saltstack/install/common/lib/salt/utils/context.py", line 191, in __getitem__
return self._dict()[key]
KeyError: 'esxcluster.get_details'

; line 29

---
[...]
esxcluster.cluster_configured:
- {{ cluster_config }}
{%- endif %}

{%- if esxcluster_states_enabled.get('cluster_vsan_datastore') %}
{%- if pillar.get('vsan', {}).get('datastore_name') and     <======================
**salt['vsphere.get_cluster_vsan_data']()**.get('datastore_name') %}
cluster_vsan_datastore:
esxcluster.vsan_datastore_configured:
- datastore_name : {{ pillar['vsan']['datastore_name'] }}
{%- endif %}
[...]
---

# Fixed code
def __init__(
    self,
    opts,
    pillar=None,
    jid=None,
    pillar_enc=None,
    proxy=None,
    context=None,
    mocked=False,
    loader="states",
):
    self.states_loader = loader
    if "grains" not in opts:
        opts["grains"] = salt.loader.grains(opts)
    self.opts = opts
    self.proxy = proxy
    self._pillar_override = pillar
    if pillar_enc is not None:
        try:
            pillar_enc = pillar_enc.lower()
        except AttributeError:
            pillar_enc = str(pillar_enc).lower()
        if pillar_enc not in VALID_PILLAR_ENC:
            raise SaltInvocationError(
                "Invalid pillar encryption type. Valid types are: {0}".format(
                    ", ".join(VALID_PILLAR_ENC)
                )
            )
    self._pillar_enc = pillar_enc
    self.opts["pillar"] = self._gather_pillar()
    self.state_con = context or {}
    self.load_modules()
    self.active = set()
    self.mod_init = set()
    self.pre = {}
    self.__run_num = 0
    self.jid = jid
    self.instance_id = str(id(self))
    self.inject_globals = {}
    self.mocked = mocked