{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CreateInstance and DeleteInstance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"#\" onclick=\"history.back()\">&lt;--- Back</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The creation of a CIM instance and in turn the creation of the underlying managed resource is achieved by calling the [`CreateInstance()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.CreateInstance) method. It takes a [`pywbem.CIMInstance`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstance) object as input, which specifies the class and the initial properties for the CIM instance to be created, and returns a [`pywbem.CIMInstanceName`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstanceName) object that references the new CIM instance.\n",
    "\n",
    "The [`DeleteInstance()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.DeleteInstance) method takes a [`pywbem.CIMInstanceName`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstanceName) object and deletes the referenced CIM instance and the represented managed resource, or rejects the operation if deletion is not supported.\n",
    "\n",
    "For some CIM classes, it makes no sense to support creation or deletion of their CIM instances. For some others, that makes sense and is defined in their usage definitions in WBEM Management Profiles (see DMTF standard DSP1001). Often, management profiles that define a semantics for the creation or deletion of managed resources, leave that optional for an implementation to support. The implementation for a CIM class in the WBEM server (aka CIM provider) thus may or may not support creation or deletion of its instances and the represented managed resources.\n",
    "\n",
    "Note that the `CIMInstance` object provided as input to `CreateInstance()` does not specfify an instance path (or if it does, it will be ignored). The determination of an instance path for the new CIM instance is completely left to the CIM provider in the WBEM server. For CIM classes with natural keys (key properties other than \"InstanceID\"), some CIM providers do honor initial values for some or all of the key properties provided in the input instance."
   ]
  },
  {
   "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",
    "    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": [
    "This example has a somewhat more elaborated failure message that includes the type of exception that happened.\n",
    "\n",
    "This example also shows how specific CIM errors can be detected: If creation of the CIM instance and the corresponding managed resource is not supported, this example code accepts that and does not error out. All other errors, including other CIM errors, cause an error exit.\n",
    "\n",
    "PyWBEM maps CIM operation failures to the Python exception [`pywbem.CIMError`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMError), and raises that in this case. The CIM status code is available as a numeric value in the `status_code` attribute of the exception object. See [CIM status codes](https://pywbem.readthedocs.io/en/latest/client.html#cim-status-codes) for a definition of the CIM status code values."
   ]
  },
  {
   "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
}
