def _update_element(name, element_type, data, server=None): ''' Update an element, including it's properties ''' # Urlencode the name (names may have slashes) name = quote(name, safe='') # Update properties first if 'properties' in data: properties = [] for key, value in data['properties'].items(): properties.append({'name': key, 'value': value}) _api_post('{0}/{1}/property'.format(element_type, name), properties, server) del data['properties'] # If the element only contained properties if not data: return unquote(name) # Get the current data then merge updated data into it update_data = _get_element(name, element_type, server, with_properties=False) if update_data: update_data.update(data) else: __context__['retcode'] = salt.defaults.exitcodes.SALT_BUILD_FAIL raise CommandExecutionError('Cannot update {0}'.format(name)) # Finally, update the element _api_post('{0}/{1}'.format(element_type, name), _clean_data(update_data), server) return unquote(name)
def _delete_element(name, element_type, data, server=None): """ Delete an element """ _api_delete("{0}/{1}".format(element_type, quote(name, safe="")), data, server) return name
def _update_element(name, element_type, data, server=None): """ Update an element, including its properties """ # Urlencode the name (names may have slashes) name = quote(name, safe="") # Update properties first if "properties" in data: properties = [] for key, value in data["properties"].items(): properties.append({"name": key, "value": value}) _api_post("{0}/{1}/property".format(element_type, name), properties, server) del data["properties"] # If the element only contained properties if not data: return unquote(name) # Get the current data then merge updated data into it update_data = _get_element(name, element_type, server, with_properties=False) if update_data: update_data.update(data) else: __context__["retcode"] = salt.defaults.exitcodes.SALT_BUILD_FAIL raise CommandExecutionError("Cannot update {0}".format(name)) # Finally, update the element _api_post("{0}/{1}".format(element_type, name), _clean_data(update_data), server) return unquote(name)
def _get_element(name, element_type, server=None, with_properties=True): ''' Get an element with or without properties ''' element = {} name = quote(name, safe='') data = _api_get('{0}/{1}'.format(element_type, name), server) # Format data, get properties if asked, and return the whole thing if any(data['extraProperties']['entity']): for key, value in data['extraProperties']['entity'].items(): element[key] = value if with_properties: element['properties'] = _get_element_properties(name, element_type) return element return None
def _get_element(name, element_type, server=None, with_properties=True): """ Get an element with or without properties """ element = {} name = quote(name, safe="") data = _api_get("{0}/{1}".format(element_type, name), server) # Format data, get properties if asked, and return the whole thing if any(data["extraProperties"]["entity"]): for key, value in data["extraProperties"]["entity"].items(): element[key] = value if with_properties: element["properties"] = _get_element_properties(name, element_type) return element return None
def _replace(re_obj): return quote(re_obj.group(0), safe=u'')
def _delete_element(name, element_type, data, server=None): ''' Delete an element ''' _api_delete('{0}/{1}'.format(element_type, quote(name, safe='')), data, server) return name