Package Products :: Package Zuul :: Package routers :: Module properties
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.properties

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2012, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  #  
  8  ############################################################################## 
  9   
 10   
 11  from Products.ZenUtils.Ext import DirectResponse 
 12  from Products.ZenUtils.jsonutils import unjson 
 13  from Products.ZenModel.ZenossSecurity import ZEN_ZPROPERTIES_EDIT 
 14  from Products import Zuul 
 15  from Products.Zuul.decorators import contextRequire, serviceConnectionError 
 16  from Products.ZenMessaging.audit import audit 
 17  from Products.ZenUtils.Ext import DirectRouter 
18 19 -class PropertiesRouter(DirectRouter):
20
21 - def _getFacade(self):
22 return Zuul.getFacade('properties', self.context)
23
24 - def _filterData(self, params, data):
25 """ 26 @param params: params passed to the caller and used here for filtering 27 @param data: data to be filtered and returned 28 """ 29 # filter 30 if params: 31 if isinstance(params, basestring): 32 filters = unjson(params) 33 else: 34 filters = params 35 def hasFilter(row, key, value): 36 if row.get(key) is not None: 37 return value.lower() in str(row.get(key)).lower()
38 39 for key, value in filters.iteritems(): 40 # assume AND for sorting 41 data = [row for row in data if hasFilter(row, key, value)] 42 43 return data 44
45 - def _sortData(self, sort, data, dir):
46 """ 47 @param data: data to be sorted and returned 48 """ 49 reverse = False 50 if dir != 'ASC': 51 reverse = True 52 return sorted(data, key=lambda row: row[sort], reverse=reverse) 53 54 @serviceConnectionError
55 - def getZenProperties(self, uid, start=0, params="{}", limit=None, sort=None, 56 page=None, dir='ASC'):
57 """ 58 Returns the definition and values of all 59 the zen properties for this context 60 @type uid: string 61 @param uid: unique identifier of an object 62 """ 63 facade = self._getFacade() 64 data = facade.getZenProperties(uid, exclusionList=('zCollectorPlugins', 'zCredentialsZProperties')) 65 66 data = self._filterData(params, data) 67 if sort: 68 data = self._sortData(sort, data, dir) 69 70 return DirectResponse(data=Zuul.marshal(data), totalCount=len(data))
71 72 @serviceConnectionError
73 - def getZenProperty(self, uid, zProperty):
74 """ 75 Returns information about a zproperty for a 76 given context, including its value 77 @rtype: Dictionary 78 @return: B{Properties}: 79 - path: (string) where the property is defined 80 - type: (string) type of zproperty it is 81 - options: (Array) available options for the zproperty 82 - value (Array) value of the zproperty 83 - valueAsString (string) 84 """ 85 facade = self._getFacade() 86 data = facade.getZenProperty(uid, zProperty) 87 return DirectResponse.succeed(data=Zuul.marshal(data))
88 89 @serviceConnectionError
90 - def getCustomProperties(self, uid, start=0, params="{}", limit=None, sort=None, 91 page=None, dir='ASC'):
92 """ 93 Returns the definition and values of all 94 the zen properties for this context 95 @type uid: string 96 @param uid: unique identifier of an object 97 """ 98 facade = self._getFacade() 99 data = facade.getCustomProperties(uid) 100 101 data = self._filterData(params, data) 102 if sort: 103 data = self._sortData(sort, data, dir) 104 105 return DirectResponse(data=Zuul.marshal(data), totalCount=len(data))
106
107 - def addCustomProperty(self, id, value, label, uid, type):
108 """ 109 adds a new property to the / of the tree 110 """ 111 facade = self._getFacade() 112 facade.addCustomProperty(id, value, label, uid, type) 113 return DirectResponse.succeed(msg="Property %s added successfully." % (id))
114 115 @serviceConnectionError 116 @contextRequire(ZEN_ZPROPERTIES_EDIT, 'uid')
117 - def setZenProperty(self, uid, zProperty, value=None):
118 """ 119 Sets the zProperty value. 120 @type uid: string 121 @param uid: unique identifier of an object 122 @type zProperty: string or dictionary 123 @param zProperty: either a string that represents which zproperty we are changing or 124 key value pair dictionary that is the list of zproperties we wish to change. 125 @type value: anything 126 @param value: if we are modifying a single zproperty then it is the value, it is not used 127 if a dictionary is passed in for zProperty 128 129 """ 130 facade = self._getFacade() 131 properties = dict() 132 # allow for zProperty to be a map of zproperties that need to 133 # be saved in case there is more than one 134 if not isinstance(zProperty, dict): 135 properties[zProperty] = value 136 else: 137 properties = zProperty 138 for key, value in properties.iteritems(): 139 # get old value for auditing 140 oldProperty = facade.getZenProperty(uid, key) 141 oldValue = oldProperty['value'] if 'value' in oldProperty else '' 142 143 # change it 144 facade.setZenProperty(uid, key, value) 145 146 data = facade.getZenProperty(uid, key) 147 value = str(value) if not value else value # show 'False', '0', etc. 148 oldValue = str(oldValue) if not oldValue else oldValue # must match 149 obj = facade._getObject(uid) 150 151 maskFields = 'value' if obj.zenPropIsPassword(zProperty) else None 152 audit('UI.zProperty.Edit', zProperty, maskFields_=maskFields, 153 data_={obj.meta_type: uid, 'value': value}, 154 oldData_={'value': oldValue}) 155 156 return DirectResponse(data=Zuul.marshal(data))
157 158 @serviceConnectionError 159 @contextRequire(ZEN_ZPROPERTIES_EDIT, 'uid')
160 - def deleteZenProperty(self, uid, zProperty):
161 """ 162 Removes the local instance of the each property in properties. Note 163 that the property will only be deleted if a hasProperty is true 164 * also used on custom properties or cProperties 165 @type uid: String 166 @param uid: unique identifier of an object 167 @type properties: String 168 @param properties: zenproperty identifier 169 """ 170 facade = self._getFacade() 171 data = facade.deleteZenProperty(uid, zProperty) 172 obj = facade._getObject(uid) 173 audit('UI.zProperty.Delete', zProperty, data_={obj.meta_type:uid}) 174 return DirectResponse(data=Zuul.marshal(data))
175