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

Add Minio component #23567

Merged
merged 35 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6c71260
Add minio implementation
tkislan Apr 21, 2019
297cf96
Static check changes
tkislan Apr 21, 2019
df260ba
Added docstrings
tkislan Apr 21, 2019
a80dbe9
Update docstrings
tkislan Apr 21, 2019
3afaa17
Update docstrings
tkislan Apr 21, 2019
dec7b77
Fix linter errors
tkislan Apr 21, 2019
3756348
Finally fix all docstring errors
tkislan Apr 21, 2019
20fe6e8
Create services.yaml
tkislan Apr 21, 2019
504a7d0
Update CODEOWNERS
tkislan Apr 21, 2019
d972f44
Final changes
tkislan Apr 21, 2019
d2f5f7a
Remove double underscores
tkislan Apr 30, 2019
38a5f45
Merge remote-tracking branch 'origin/dev' into minio
tkislan Apr 30, 2019
ee301cd
Minor changes
tkislan Apr 30, 2019
1d933c7
Update config.yml
tkislan Apr 30, 2019
6cd6c7b
Review changes
tkislan May 2, 2019
aa306a8
Added tests
tkislan May 2, 2019
f3b8269
Fix lint errors
tkislan May 2, 2019
b8a99d2
Move tests from unittest to pytest
tkislan May 3, 2019
f4ff96d
Add minio as test requirement
tkislan May 3, 2019
3e3a4fb
Update test_minio_helper.py
tkislan May 3, 2019
a75e65e
Merge remote-tracking branch 'upstream/dev' into dev
tkislan May 3, 2019
95ce434
Merge remote-tracking branch 'origin/dev' into minio
tkislan May 3, 2019
a828bf8
Better event thread handling, added hass test
tkislan May 3, 2019
fd0d1cb
Update tests
tkislan May 8, 2019
b322581
Fixed lint errors
tkislan May 8, 2019
bc138af
Update test_minio.py
tkislan May 8, 2019
284445e
Review changes
tkislan May 17, 2019
ea2a1a6
More review changes
tkislan May 17, 2019
ab48913
Merge remote-tracking branch 'upstream/dev' into dev
tkislan Aug 18, 2019
5997271
Merge branch 'dev' into minio
tkislan Aug 18, 2019
fe8e2c6
Removed tests
tkislan Aug 19, 2019
48a6735
Applied code style changes
tkislan Aug 19, 2019
98da914
Reformat test code
tkislan Aug 19, 2019
e24f31c
Merge remote-tracking branch 'upstream/dev' into dev
tkislan Aug 19, 2019
4545a58
Merge branch 'dev' into minio
tkislan Aug 19, 2019
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
33 changes: 18 additions & 15 deletions homeassistant/components/minio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import voluptuous as vol

from homeassistant.const import EVENT_HOMEASSISTANT_START, \
tkislan marked this conversation as resolved.
Show resolved Hide resolved
EVENT_HOMEASSISTANT_STOP
EVENT_HOMEASSISTANT_STOP, CONF_FILE_PATH
import homeassistant.helpers.config_validation as cv

from .minio_helper import create_minio_client, MinioEventThread
Expand All @@ -27,6 +27,9 @@
CONF_LISTEN_SUFFIX = 'suffix'
CONF_LISTEN_EVENTS = 'events'

CONF_BUCKET = 'bucket'
CONF_KEY = 'key'

DEFAULT_LISTEN_PREFIX = ''
DEFAULT_LISTEN_SUFFIX = '.*'
DEFAULT_LISTEN_EVENTS = 's3:ObjectCreated:*'
Expand Down Expand Up @@ -60,12 +63,12 @@
}, extra=vol.ALLOW_EXTRA)

BUCKET_KEY_SCHEMA = vol.Schema({
vol.Required('bucket'): cv.template,
vol.Required('key'): cv.template,
vol.Required(CONF_BUCKET): cv.template,
tkislan marked this conversation as resolved.
Show resolved Hide resolved
vol.Required(CONF_KEY): cv.template,
})

BUCKET_KEY_FILE_SCHEMA = BUCKET_KEY_SCHEMA.extend({
vol.Required('file_path'): cv.template,
vol.Required(CONF_FILE_PATH): cv.template,
})


Expand Down Expand Up @@ -135,9 +138,9 @@ def _render_service_value(service, key):

def put_file(service):
"""Upload file service."""
bucket = _render_service_value(service, 'bucket')
key = _render_service_value(service, 'key')
file_path = _render_service_value(service, 'file_path')
bucket = _render_service_value(service, CONF_BUCKET)
key = _render_service_value(service, CONF_KEY)
file_path = _render_service_value(service, CONF_FILE_PATH)

if not hass.config.is_allowed_path(file_path):
_LOGGER.error('Invalid file_path %s', file_path)
Expand All @@ -147,9 +150,9 @@ def put_file(service):

def get_file(service):
"""Download file service."""
bucket = _render_service_value(service, 'bucket')
key = _render_service_value(service, 'key')
file_path = _render_service_value(service, 'file_path')
bucket = _render_service_value(service, CONF_BUCKET)
key = _render_service_value(service, CONF_KEY)
file_path = _render_service_value(service, CONF_FILE_PATH)

if not hass.config.is_allowed_path(file_path):
_LOGGER.error('Invalid file_path %s', file_path)
Expand All @@ -159,8 +162,8 @@ def get_file(service):

def remove_file(service):
"""Delete file service."""
bucket = _render_service_value(service, 'bucket')
key = _render_service_value(service, 'key')
bucket = _render_service_value(service, CONF_BUCKET)
key = _render_service_value(service, CONF_KEY)

minio_client.remove_object(bucket, key)

Expand Down Expand Up @@ -199,13 +202,13 @@ def run(self):
if event is None:
break

_, file_name = os.path.split(event['key'])
_, file_name = os.path.split(event[CONF_KEY])

_LOGGER.debug(
'Sending event %s, %s, %s',
event['event_name'],
event['bucket'],
event['key']
event[CONF_BUCKET],
event[CONF_KEY]
)
self._hass.bus.fire(DOMAIN, {
'file_name': file_name,
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/minio/minio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def get_minio_notification_response(
'suffix': suffix,
'events': events,
}
# noinspection PyProtectedMember
# pylint: disable=W0212
# pylint: disable=protected-access
return minio_client._url_open(
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
'GET',
bucket_name=bucket_name,
Expand Down Expand Up @@ -165,6 +164,8 @@ def run(self):
except json.JSONDecodeError:
response.close()
except AttributeError:
tkislan marked this conversation as resolved.
Show resolved Hide resolved
# When response is closed, iterator will fail to access
# the underlying socket descriptor.
break

# Wait before attempting to connect again.
Expand All @@ -180,7 +181,7 @@ def _iterate_event_stream(self, event_stream_it, minio_client):
)
# Fail gracefully. If for whatever reason this stops working,
# it shouldn't prevent it from firing events.
# pylint: disable=W0703
# pylint: disable=broad-except
except Exception as error:
_LOGGER.error(
'Failed to generate presigned url: %s',
Expand Down
19 changes: 10 additions & 9 deletions tests/components/minio/test_minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ def minio_client_fixture():
yield minio_client_mock


@pytest.fixture(name="minio_event_response")
def minio_event_response_fixture():
@pytest.fixture(name="minio_client_event")
def minio_client_event_fixture():
"""Patch helper function for minio notification stream."""
with patch(
'homeassistant.components.minio'
'.minio_helper.get_minio_notification_response'
) as get_notification_response_mock:
'homeassistant.components.minio.minio_helper.Minio'
) as minio_mock:
minio_client_mock = minio_mock.return_value

response_mock = MagicMock()
stream_mock = MagicMock()

Expand All @@ -43,9 +44,9 @@ def minio_event_response_fixture():
]

response_mock.stream.return_value = stream_mock
get_notification_response_mock.return_value = response_mock
minio_client_mock._url_open.return_value = response_mock

yield get_notification_response_mock
yield minio_client_mock


async def test_minio_services(hass, caplog, minio_client):
Expand Down Expand Up @@ -117,9 +118,9 @@ async def test_minio_services(hass, caplog, minio_client):
minio_client.reset_mock()


async def test_minio_listen(hass, caplog, minio_client, minio_event_response):
async def test_minio_listen(hass, caplog, minio_client_event):
"""Test minio listen on notifications."""
minio_client.presigned_get_object.return_value = 'http://url'
minio_client_event.presigned_get_object.return_value = 'http://url'

events = []

Expand Down