# Buggy code
def _parse_image_meta(image=None, detail=False):
    ret = None

    if image and "Error" in image:
        ret = image
    elif image:
        name = image["manifest"]["name"]
        version = image["manifest"]["version"]
        os = image["manifest"]["os"]
        description = image["manifest"]["description"]
        published = image["manifest"]["published_at"]
        source = image["source"]
        if image["manifest"]["name"] == "docker-layer":
            # NOTE: skip docker-layer unless it has a docker:repo and docker:tag
            name = None
            docker_repo = None
            docker_tag = None
            for tag in image["manifest"]["tags"]:
                if tag.startswith("docker:tag:") and image["manifest"]["tags"][tag]:
                    docker_tag = tag.split(":")[-1]
                elif tag == "docker:repo":
                    docker_repo = image["manifest"]["tags"][tag]

            if docker_repo and docker_tag:
                name = "{}:{}".format(docker_repo, docker_tag)
                description = (
                    "Docker image imported from {repo}:{tag} on {date}.".format(
                        repo=docker_repo,
                        tag=docker_tag,
                        date=published,
                    )
                )

        if name and detail:
            ret = {
                "name": name,
                "version": version,
                "os": os,
                "description": description,
                "published": published,
                "source": source,
            }
        elif name:
            ret = "{name}@{version} [{published}]".format(
                name=name,
                version=version,
                published=published,
            )

    return ret


# Traceback (KeyError)
sudo salt global_deneb state.apply smartos_docker_container.plex3
global_deneb:
----------
ID: https://docker.io
Function: smartos.source_present
Result: True
Comment: image source https://docker.io is present
Started: 16:08:29.276973
Duration: 493.941 ms
Changes:
----------
ID: plex3.agdon.net
Function: smartos.vm_present
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/opt/tools/lib/python2.7/site-packages/salt/state.py", line 1951, in call
ret = self.states[cdata['full']](*cdata['args'], **cdata['kwargs'])
File "/opt/tools/lib/python2.7/site-packages/salt/loader.py", line 2033, in wrapper
return f(*args, **kwargs)
File "/opt/tools/lib/python2.7/site-packages/salt/states/smartos.py", line 801, in vm_present
if vmconfig['image_uuid'] not in __salt__['imgadm.list']():
File "/opt/tools/lib/python2.7/site-packages/salt/modules/smartos_imgadm.py", line 257, in list_installed
data = _parse_image_meta(image, verbose)
File "/opt/tools/lib/python2.7/site-packages/salt/modules/smartos_imgadm.py", line 60, in _parse_image_meta
name = image['manifest']['name']
KeyError: u'name'
Started: 16:08:29.772658
Duration: 1384.224 ms
Changes:

Summary for global_deneb
------------
Succeeded: 1
Failed:    1
------------
Total states run:     2
Total run time:   1.878 s

# Fixed code
def _parse_image_meta(image=None, detail=False):
    ret = None

    if image and "Error" in image:
        ret = image
    elif image and "manifest" in image:
        name = image["manifest"]["name"]
        version = image["manifest"]["version"]
        os = image["manifest"]["os"]
        description = image["manifest"]["description"]
        published = image["manifest"]["published_at"]
        source = image["source"]
        if image["manifest"]["name"] == "docker-layer":
            # NOTE: skip docker-layer unless it has a docker:repo and docker:tag
            name = None
            docker_repo = None
            docker_tag = None
            for tag in image["manifest"]["tags"]:
                if tag.startswith("docker:tag:") and image["manifest"]["tags"][tag]:
                    docker_tag = tag.split(":")[-1]
                elif tag == "docker:repo":
                    docker_repo = image["manifest"]["tags"][tag]

            if docker_repo and docker_tag:
                name = "{}:{}".format(docker_repo, docker_tag)
                description = (
                    "Docker image imported from {repo}:{tag} on {date}.".format(
                        repo=docker_repo,
                        tag=docker_tag,
                        date=published,
                    )
                )

        if name and detail:
            ret = {
                "name": name,
                "version": version,
                "os": os,
                "description": description,
                "published": published,
                "source": source,
            }
        elif name:
            ret = "{name}@{version} [{published}]".format(
                name=name,
                version=version,
                published=published,
            )

    return ret