def create(self, name, module_type, contents, description=None, all_tenants=None, datastore=None, datastore_version=None, auto_apply=None, visible=None, live_update=None): """Create a new module.""" contents = utils.encode_data(contents) body = {"module": { "name": name, "module_type": module_type, "contents": contents, }} if description is not None: body["module"]["description"] = description datastore_obj = {} if datastore: datastore_obj["type"] = datastore if datastore_version: datastore_obj["version"] = datastore_version if datastore_obj: body["module"]["datastore"] = datastore_obj if all_tenants is not None: body["module"]["all_tenants"] = int(all_tenants) if auto_apply is not None: body["module"]["auto_apply"] = int(auto_apply) if visible is not None: body["module"]["visible"] = int(visible) if live_update is not None: body["module"]["live_update"] = int(live_update) return self._create("/modules", body, "module")
def update(self, module, name=None, module_type=None, contents=None, description=None, all_tenants=None, datastore=None, datastore_version=None, auto_apply=None, visible=None, live_update=None, all_datastores=None, all_datastore_versions=None, priority_apply=None, apply_order=None, full_access=None): """Update an existing module. Passing in datastore=None or datastore_version=None has the effect of making it available for all datastores/versions. """ body = { "module": { } } if name is not None: body["module"]["name"] = name if module_type is not None: body["module"]["type"] = module_type if contents is not None: contents = utils.encode_data(contents) body["module"]["contents"] = contents if description is not None: body["module"]["description"] = description datastore_obj = {} if datastore: datastore_obj["type"] = datastore if datastore_version: datastore_obj["version"] = datastore_version if datastore_obj: body["module"]["datastore"] = datastore_obj if all_datastores: body["module"]["all_datastores"] = int(all_datastores) if all_datastore_versions: body["module"]["all_datastore_versions"] = int( all_datastore_versions) if all_tenants is not None: body["module"]["all_tenants"] = int(all_tenants) if auto_apply is not None: body["module"]["auto_apply"] = int(auto_apply) if visible is not None: body["module"]["visible"] = int(visible) if live_update is not None: body["module"]["live_update"] = int(live_update) if priority_apply is not None: body["module"]["priority_apply"] = int(priority_apply) if apply_order is not None: body["module"]["apply_order"] = apply_order if full_access is not None: body["module"]["full_access"] = int(full_access) url = "/modules/%s" % base.getid(module) resp, body = self.api.client.put(url, body=body) common.check_for_exceptions(resp, body, url) return Module(self, body['module'], loaded=True)
def create(self, name, module_type, contents, description=None, all_tenants=None, datastore=None, datastore_version=None, auto_apply=None, visible=None, live_update=None, priority_apply=None, apply_order=None, full_access=None): """Create a new module.""" contents = utils.encode_data(contents) body = { "module": { "name": name, "module_type": module_type, "contents": contents, } } if description is not None: body["module"]["description"] = description datastore_obj = {} if datastore: datastore_obj["type"] = datastore if datastore_version: datastore_obj["version"] = datastore_version if datastore_obj: body["module"]["datastore"] = datastore_obj if all_tenants is not None: body["module"]["all_tenants"] = int(all_tenants) if auto_apply is not None: body["module"]["auto_apply"] = int(auto_apply) if visible is not None: body["module"]["visible"] = int(visible) if live_update is not None: body["module"]["live_update"] = int(live_update) if priority_apply is not None: body["module"]["priority_apply"] = int(priority_apply) if apply_order is not None: body["module"]["apply_order"] = apply_order if full_access is not None: body["module"]["full_access"] = int(full_access) return self._create("/modules", body, "module")
def test_encode_decode_data(self): text_data_str = 'This is a text string' try: text_data_bytes = bytes('This is a byte stream', 'utf-8') except TypeError: text_data_bytes = bytes('This is a byte stream') random_data_str = os.urandom(12) random_data_bytes = bytearray(os.urandom(12)) special_char_str = '\x00\xFF\x00\xFF\xFF\x00' special_char_bytes = bytearray( [ord(item) for item in special_char_str]) data = [text_data_str, text_data_bytes, random_data_str, random_data_bytes, special_char_str, special_char_bytes] for datum in data: # the deserialized data is always a bytearray try: expected_deserialized = bytearray( [ord(item) for item in datum]) except TypeError: expected_deserialized = bytearray( [item for item in datum]) serialized_data = utils.encode_data(datum) self.assertIsNotNone(serialized_data, "'%s' serialized is None" % datum) deserialized_data = utils.decode_data(serialized_data) self.assertIsNotNone(deserialized_data, "'%s' deserialized is None" % datum) self.assertEqual(expected_deserialized, deserialized_data, "Serialize/Deserialize failed") # Now we write the data to a file and read it back in # to make sure the round-trip doesn't change anything. with tempfile.NamedTemporaryFile() as temp_file: with open(temp_file.name, 'wb') as fh_w: fh_w.write( bytearray([ord(item) for item in serialized_data])) with open(temp_file.name, 'rb') as fh_r: new_serialized_data = fh_r.read() new_deserialized_data = utils.decode_data( new_serialized_data) self.assertIsNotNone(new_deserialized_data, "'%s' deserialized is None" % datum) self.assertEqual(expected_deserialized, new_deserialized_data, "Serialize/Deserialize with files failed")
def test_encode_decode_data(self): text_data_str = 'This is a text string' try: text_data_bytes = bytes('This is a byte stream', 'utf-8') except TypeError: text_data_bytes = bytes('This is a byte stream') random_data_str = Crypto.Random.new().read(12) random_data_bytes = bytearray(Crypto.Random.new().read(12)) special_char_str = '\x00\xFF\x00\xFF\xFF\x00' special_char_bytes = bytearray( [ord(item) for item in special_char_str]) data = [ text_data_str, text_data_bytes, random_data_str, random_data_bytes, special_char_str, special_char_bytes ] for datum in data: # the deserialized data is always a bytearray try: expected_deserialized = bytearray( [ord(item) for item in datum]) except TypeError: expected_deserialized = bytearray([item for item in datum]) serialized_data = utils.encode_data(datum) self.assertIsNotNone(serialized_data, "'%s' serialized is None" % datum) deserialized_data = utils.decode_data(serialized_data) self.assertIsNotNone(deserialized_data, "'%s' deserialized is None" % datum) self.assertEqual(expected_deserialized, deserialized_data, "Serialize/Deserialize failed") # Now we write the data to a file and read it back in # to make sure the round-trip doesn't change anything. with tempfile.NamedTemporaryFile() as temp_file: with open(temp_file.name, 'wb') as fh_w: fh_w.write( bytearray([ord(item) for item in serialized_data])) with open(temp_file.name, 'rb') as fh_r: new_serialized_data = fh_r.read() new_deserialized_data = utils.decode_data(new_serialized_data) self.assertIsNotNone(new_deserialized_data, "'%s' deserialized is None" % datum) self.assertEqual(expected_deserialized, new_deserialized_data, "Serialize/Deserialize with files failed")