Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3042 python client updating data stream #3168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions streampipes-client-python/streampipes/endpoint/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ def post(self, resource: Resource) -> None:
headers={"Content-type": "application/json"},
)

def put(self, resource: Resource) -> None:
"""Allows to put a resource to the StreamPipes API.

Parameters
----------
resource: Resource
The resource to be updated.

Returns
-------
None
"""

self._make_request(
request_method=self._parent_client.request_session.put,
url=f"{self.build_url()}",
data=json.dumps(resource.to_dict(use_source_names=True)),
headers={"Content-type": "application/json"},
)


class MessagingEndpoint(Endpoint):
"""Abstract implementation of a StreamPipes messaging endpoint.
Expand Down
23 changes: 23 additions & 0 deletions streampipes-client-python/tests/client/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ def test_endpoint_post(self, server_version: MagicMock, http_session: MagicMock)
headers={"Content-type": "application/json"},
)

@patch("streampipes.client.client.Session", autospec=True)
@patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
def test_endpoint_put(self, server_version: MagicMock, http_session: MagicMock):
http_session_mock = MagicMock()
http_session.return_value = http_session_mock

server_version.return_value = {"backendVersion": "0.x.y"}

client = StreamPipesClient(
client_config=StreamPipesClientConfig(
credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
host_address="localhost",
)
)

client.dataStreamApi.put(DataStream(**self.data_stream_get))

http_session_mock.put.assert_called_with(
url="https://localhost:80/streampipes-backend/api/v2/streams",
data=json.dumps(self.data_stream_get),
headers={"Content-type": "application/json"},
)

@patch("streampipes.client.client.Session", autospec=True)
@patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
def test_endpoint_data_stream_happy_path(self, server_version: MagicMock, http_session: MagicMock):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public void updateAdapter(AdapterDescription ad)
}
}

public void updateDataStream(SpDataStream dataStream) throws AdapterException {
var correspondingAdapter = adapterMasterManagement.getAdapter(dataStream.getCorrespondingAdapterId());
dataStreamResourceManager.update(dataStream);

correspondingAdapter.setDataStream(dataStream);
updateAdapter(correspondingAdapter);
}

public List<PipelineUpdateInfo> checkPipelineMigrations(AdapterDescription adapterDescription) {
var affectedPipelines = PipelineManager
.getPipelinesContainingElements(adapterDescription.getCorrespondingDataStreamElementId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@

package org.apache.streampipes.rest.impl.pe;

import org.apache.streampipes.commons.exceptions.connect.AdapterException;
import org.apache.streampipes.commons.prometheus.adapter.AdapterMetricsManager;
import org.apache.streampipes.connect.management.management.AdapterMasterManagement;
import org.apache.streampipes.connect.management.management.AdapterUpdateManagement;
import org.apache.streampipes.model.SpDataStream;
import org.apache.streampipes.model.message.Message;
import org.apache.streampipes.model.message.NotificationType;
import org.apache.streampipes.model.monitoring.SpLogMessage;
import org.apache.streampipes.resource.management.DataStreamResourceManager;
import org.apache.streampipes.rest.core.base.impl.AbstractAuthGuardedRestResource;
import org.apache.streampipes.resource.management.SpResourceManager;
import org.apache.streampipes.rest.impl.connect.AbstractAdapterResource;
import org.apache.streampipes.rest.security.AuthConstants;
import org.apache.streampipes.storage.management.StorageDispatcher;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -34,6 +40,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -42,7 +49,17 @@

@RestController
@RequestMapping("/api/v2/streams")
public class DataStreamResource extends AbstractAuthGuardedRestResource {
public class DataStreamResource extends AbstractAdapterResource<AdapterMasterManagement> {

public DataStreamResource() {
super(() -> new AdapterMasterManagement(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me what the reason is why you added this code? I do not completely understand it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed access to AdapterMasterManagement instance. I used the same pattern as in AdapterResource to bring it in

StorageDispatcher.INSTANCE.getNoSqlStore()
.getAdapterInstanceStorage(),
new SpResourceManager().manageAdapters(),
new SpResourceManager().manageDataStreams(),
AdapterMetricsManager.INSTANCE.getAdapterMetrics()
));
}

@GetMapping(path = "/available", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize(AuthConstants.HAS_READ_PIPELINE_ELEMENT_PRIVILEGE)
Expand Down Expand Up @@ -89,6 +106,24 @@ public ResponseEntity<?> addDataStream(@RequestBody SpDataStream dataStream) {
}
}

@PutMapping(
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
@PreAuthorize(AuthConstants.HAS_WRITE_PIPELINE_ELEMENT_PRIVILEGE)
public ResponseEntity<?> updateDataStream(@RequestBody SpDataStream dataStream) {
try {
getDataStreamResourceManager().update(dataStream);
var updateManager = new AdapterUpdateManagement(managementService);

updateManager.updateDataStream(dataStream);

return ok();
} catch (AdapterException e) {
return badRequest(e.getMessage());
}
}

private DataStreamResourceManager getDataStreamResourceManager() {
return getSpResourceManager().manageDataStreams();
}
Expand Down
Loading