{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ModifyInstance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"#\" onclick=\"history.back()\">&lt;--- Back</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Existing CIM instances can be modified by having the values of properties changed. This is achieved using the [`ModifyInstance()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.ModifyInstance) method. It takes a [`pywbem.CIMInstance`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstance) object as input, which references the CIM instance to be modified with its `path` attribute, and specifies new values for the properties.\n",
    "\n",
    "The `PropertyList` input parameter of the method specifies the names of the properties that are to be modified. If this parameter is not provided, all properties are modified. Those properties that are to be modified but have no new values specified in the input instance get their default values.\n",
    "\n",
    "The values of key properties cannot be modified.\n",
    "\n",
    "Again, the CIM provider on the WBEM server may or may not support the modification of CIM instances."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import sys\n",
    "import pywbem\n",
    "\n",
    "username = 'user'\n",
    "password = 'password'\n",
    "classname = 'CIM_ComputerSystem'\n",
    "namespace = 'root/interop'\n",
    "server = 'http://localhost'\n",
    "\n",
    "conn = pywbem.WBEMConnection(server, (username, password),\n",
    "                             default_namespace=namespace,\n",
    "                             no_verification=True)\n",
    "\n",
    "filter_inst = pywbem.CIMInstance(\n",
    "    'CIM_IndicationFilter',\n",
    "    {'Name': 'pywbem_test',\n",
    "     'Query': 'SELECT * FROM CIM_Indication',\n",
    "     'QueryLanguage': 'WQL'})\n",
    "    \n",
    "print('Creating instance of class: %s' % filter_inst.classname)\n",
    "try:\n",
    "    filter_path = conn.CreateInstance(filter_inst, namespace)\n",
    "except pywbem.Error as exc:\n",
    "    if isinstance(exc, pywbem.CIMError) and \\\n",
    "       exc.status_code == pywbem.CIM_ERR_NOT_SUPPORTED:\n",
    "        print('WBEM server does not support creation of dynamic filters.')\n",
    "        filter_path = None\n",
    "    else:\n",
    "        print('CreateInstance failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "        sys.exit(1)\n",
    "\n",
    "if filter_path is not None:\n",
    "    print('Created instance: %s' % filter_path)\n",
    "\n",
    "    filter_inst['Query'] = 'SELECT * FROM CIM_ProcessIndication'\n",
    "\n",
    "    filter_inst.path = filter_path\n",
    "\n",
    "    print('Modifying the instance')\n",
    "    try:\n",
    "        conn.ModifyInstance(filter_inst, PropertyList=['Query'])\n",
    "    except pywbem.Error as exc:\n",
    "        if isinstance(exc, pywbem.CIMError) and exc[0] == pywbem.CIM_ERR_NOT_SUPPORTED:\n",
    "            print('Modifying CIM_IndicationFilter is not supported')\n",
    "        else:\n",
    "            print('ModifyInstance failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "            sys.exit(1)\n",
    "\n",
    "    print('Deleting the instance again, to clean up')\n",
    "    try:\n",
    "        conn.DeleteInstance(filter_path)\n",
    "    except pywbem.Error as exc:\n",
    "        print('DeleteInstance failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "        sys.exit(1)\n",
    "    print('Deleted the instance')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `ModifyInstance()` method takes the instance path from the `path` attribute of its instance argument. Because the instance path of the new instance is only known after `CreateInstance()` returns successfully, we need to set the `path` attribute of the instance object accordingly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"#\" onclick=\"history.back()\">&lt;--- Back</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
