def _load_config_one(self, filename): path = os.path.join(self._path, filename) if os.path.exists(path): array = ArrayContext() with open(path) as json_file: json_object = json.load(json_file) array.update_config_json(json_object) # TODO: We use file name as id if it is not present in the JSON. if not array.id: array.id = urllib.quote(os.path.splitext(filename)[0]) self._logger.info("Loaded config = {}".format(json_object)) return array raise ValueError("Array config {} not found".format(filename))
def add_array(json_body=None): """ Add an array to the system. The array is specified in the body. :return: The array object added, which contains the array_id which could be used later to delete this array. """ error_data = validate_array_input(json_body) if error_data: return make_rest_response(error_data, 400) try: apitoken, array_id, array_name, purity_version = get_array_info( json_body[HOST], json_body[USERNAME], json_body[PASSWORD]) except Exception as e: return make_rest_response( make_error( ErrorCodes.ArrayError.value, "Error encountered when connecting to the array: {}".format( e)), 400) del json_body[PASSWORD] json_body.update({ ArrayContext.API_TOKEN: apitoken, ArrayContext.NAME: array_name, ArrayContext.ID: array_id, ArrayContext.PURITY_VERSION: purity_version }) store = Store(array_config_path(), current_app.logger) existing_arrays = store.load_arrays() if array_id in existing_arrays: return make_rest_response( make_error( ErrorCodes.ArrayAlreadyExists.value, "Array of the same id already exists with the name '{}'.". format(existing_arrays[array_id].name)), 409) array = ArrayContext() array.update_config_json(json_body) store.save_array_config(array) # Return the array object created. return array.get_json()
def add_array(json_body=None): """ Add an array to the system. The array is specified in the body. :return: The array object added, which contains the array_id which could be used later to delete this array. """ error_data = validate_array_input(json_body) if error_data: return make_rest_response(error_data, 400) try: apitoken, array_id, array_name, purity_version = get_array_info(json_body[HOST], json_body[USERNAME], json_body[PASSWORD]) except Exception as e: return make_rest_response( make_error(ErrorCodes.ArrayError.value, "Error encountered when connecting to the array: {}".format(e)), 400) del json_body[PASSWORD] json_body.update({ ArrayContext.API_TOKEN: apitoken, ArrayContext.NAME: array_name, ArrayContext.ID: array_id, ArrayContext.PURITY_VERSION: purity_version }) store = Store(array_config_path(), current_app.logger) existing_arrays = store.load_arrays() if array_id in existing_arrays: return make_rest_response( make_error( ErrorCodes.ArrayAlreadyExists.value, "Array of the same id already exists with the name '{}'.".format( existing_arrays[array_id].name)), 409) array = ArrayContext() array.update_config_json(json_body) store.save_array_config(array) # Return the array object created. return array.get_json()