# Buggy code
def register_ssh_key(self, public_key):
    ssh_key = self.get_ssh_key()
    args = self._get_common_args()
    name = self.module.params.get("name")

    res = None
    if not ssh_key:
        self.result["changed"] = True
        args["publickey"] = public_key
        if not self.module.check_mode:
            args["name"] = name
            res = self.query_api("registerSSHKeyPair", **args)
    else:
        fingerprint = self._get_ssh_fingerprint(public_key)
        if ssh_key["fingerprint"] != fingerprint:
            self.result["changed"] = True
            if not self.module.check_mode:
                # delete the ssh key with matching name but wrong fingerprint
                args["name"] = name
                self.query_api("deleteSSHKeyPair", **args)

        elif ssh_key["name"].lower() != name.lower():
            self.result["changed"] = True
            if not self.module.check_mode:
                # delete the ssh key with matching fingerprint but wrong name
                args["name"] = ssh_key["name"]
                self.query_api("deleteSSHKeyPair", **args)
                # First match for key retrievment will be the fingerprint.
                # We need to make another lookup if there is a key with identical name.
                self.ssh_key = None
                ssh_key = self.get_ssh_key()
                if ssh_key["fingerprint"] != fingerprint:
                    args["name"] = name
                    self.query_api("deleteSSHKeyPair", **args)

        if not self.module.check_mode and self.result["changed"]:
            args["publickey"] = public_key
            args["name"] = name
            res = self.query_api("registerSSHKeyPair", **args)

    if res and "keypair" in res:
        ssh_key = res["keypair"]

    return ssh_key


# Traceback (TypeError)
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: 'NoneType' object has no attribute '__getitem__'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/75/8jzhscr56j13rv6tj6805ch40000gn/T/ansible_P78L9S/ansible_module_cs_sshkeypair.py\", line 276, in <module>\n    main()\n  File \"/var/folders/75/8jzhscr56j13rv6tj6805ch40000gn/T/ansible_P78L9S/ansible_module_cs_sshkeypair.py\", line 267, in main\n    ssh_key = acs_sshkey.register_ssh_key(public_key)\n  File \"/var/folders/75/8jzhscr56j13rv6tj6805ch40000gn/T/ansible_P78L9S/ansible_module_cs_sshkeypair.py\", line 172, in register_ssh_key\n    if ssh_key['fingerprint'] != fingerprint:\nTypeError: 'NoneType' object has no attribute '__getitem__'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

# Fixed code
def register_ssh_key(self, public_key):
    ssh_key = self.get_ssh_key()
    args = self._get_common_args()
    name = self.module.params.get("name")

    res = None
    if not ssh_key:
        self.result["changed"] = True
        args["publickey"] = public_key
        if not self.module.check_mode:
            args["name"] = name
            res = self.query_api("registerSSHKeyPair", **args)
    else:
        fingerprint = self._get_ssh_fingerprint(public_key)
        if ssh_key["fingerprint"] != fingerprint:
            self.result["changed"] = True
            if not self.module.check_mode:
                # delete the ssh key with matching name but wrong fingerprint
                args["name"] = name
                self.query_api("deleteSSHKeyPair", **args)

        elif ssh_key["name"].lower() != name.lower():
            self.result["changed"] = True
            if not self.module.check_mode:
                # delete the ssh key with matching fingerprint but wrong name
                args["name"] = ssh_key["name"]
                self.query_api("deleteSSHKeyPair", **args)
                # First match for key retrievment will be the fingerprint.
                # We need to make another lookup if there is a key with identical name.
                self.ssh_key = None
                ssh_key = self.get_ssh_key()
                if ssh_key and ssh_key["fingerprint"] != fingerprint:
                    args["name"] = name
                    self.query_api("deleteSSHKeyPair", **args)

        if not self.module.check_mode and self.result["changed"]:
            args["publickey"] = public_key
            args["name"] = name
            res = self.query_api("registerSSHKeyPair", **args)

    if res and "keypair" in res:
        ssh_key = res["keypair"]

    return ssh_key