def get_label(state_machine_name, label_name): """ Get a label within a given state machine. Returns: - 200 Ok: if the label is exists. - 404 Not Found: if the state machine or label does not exist. - 410 Gone: if the label once existed but has since been deleted. Successful return codes return the full metadata for the label. """ app = server.config.app label = LabelRef(label_name, state_machine_name) try: metadata = state_machine.get_label_metadata(app, label) except UnknownLabel as e: abort( 410 if e.deleted else 404, f"Label {label.name} in state machine '{label.state_machine}' " f"does not exist.", ) except UnknownStateMachine: abort(404, f"State machine '{label.state_machine}' does not exist.") state = state_machine.get_label_state(app, label) return jsonify(metadata=metadata, state=state.name)
def test_label_get_state(app, mock_test_feed): label = LabelRef('foo', 'test_machine') with mock_test_feed(), app.new_session(): state_machine.create_label( app, label, {'foo': 'bar'}, ) with app.new_session(): assert state_machine.get_label_state(app, label).name == 'start'
def update_label(state_machine_name, label_name): """ Update a label in a state machine. Triggering progression if necessary according to the state machine configuration. Updates are _merged_ with existing metadata. Returns: - 200 Ok: if the label is successfully updated. - 400 Bad Request: if the request body is not a valid metadata. - 404 Not Found: if the state machine or label does not exist. - 410 Gone: if the label once existed but has since been deleted. Successful return codes return the full new metadata for a label. """ app = server.config.app label = LabelRef(label_name, state_machine_name) try: patch_metadata = request.get_json()['metadata'] except KeyError: abort(400, "No new metadata") try: new_metadata = state_machine.update_metadata_for_label( app, label, patch_metadata, ) state = state_machine.get_label_state(app, label) return jsonify(metadata=new_metadata, state=state.name) except UnknownStateMachine: msg = f"State machine '{state_machine_name}' does not exist" abort(404, msg) except UnknownLabel as e: abort( 410 if e.deleted else 404, f"Label {label_name} does not exist in state machine " f"'{state_machine_name}'.", )
def test_label_get_state_raises_for_unknown_state_machine(app): label = LabelRef('foo', 'unknown_machine') with pytest.raises(UnknownStateMachine), app.new_session(): assert state_machine.get_label_state(app, label)