Ejemplo n.º 1
0
def update_event_enabled(
    datasource: sources.DataSource,
    cfg: dict,
    event_source_type: EventType,
    scope: EventScope = None,
) -> bool:
    """Determine if a particular EventType is enabled.

    For the `event_source_type` passed in, check whether this EventType
    is enabled in the `updates` section of the userdata. If `updates`
    is not enabled in userdata, check if defined as one of the
    `default_events` on the datasource. `scope` may be used to
    narrow the check to a particular `EventScope`.

    Note that on first boot, userdata may NOT be available yet. In this
    case, we only have the data source's `default_update_events`,
    so an event that should be enabled in userdata may be denied.
    """
    default_events = (
        datasource.default_update_events
    )  # type: Dict[EventScope, Set[EventType]]
    user_events = userdata_to_events(
        cfg.get("updates", {})
    )  # type: Dict[EventScope, Set[EventType]]
    # A value in the first will override a value in the second
    allowed = util.mergemanydict(
        [
            copy.deepcopy(user_events),
            copy.deepcopy(default_events),
        ]
    )
    LOG.debug("Allowed events: %s", allowed)

    if not scope:
        scopes = allowed.keys()
    else:
        scopes = [scope]
    scope_values = [s.value for s in scopes]

    for evt_scope in scopes:
        if event_source_type in allowed.get(evt_scope, []):
            LOG.debug(
                "Event Allowed: scope=%s EventType=%s",
                evt_scope.value,
                event_source_type,
            )
            return True

    LOG.debug(
        "Event Denied: scopes=%s EventType=%s", scope_values, event_source_type
    )
    return False
Ejemplo n.º 2
0
 def test_userdata_to_events(self):
     userdata = {"network": {"when": ["boot"]}}
     expected = {EventScope.NETWORK: {EventType.BOOT}}
     assert expected == userdata_to_events(userdata)
Ejemplo n.º 3
0
 def test_userdata_to_events(self):
     userdata = {'network': {'when': ['boot']}}
     expected = {EventScope.NETWORK: {EventType.BOOT}}
     assert expected == userdata_to_events(userdata)
Ejemplo n.º 4
0
 def test_invalid_event(self, caplog):
     userdata = {"network": {"when": ["bootasdfasdf"]}}
     userdata_to_events(userdata)
     assert ("'bootasdfasdf' is not a valid EventType! Update data "
             "will be ignored for 'network' scope" in caplog.text)
Ejemplo n.º 5
0
 def test_invalid_event(self, caplog):
     userdata = {'network': {'when': ['bootasdfasdf']}}
     userdata_to_events(userdata)
     assert ("'bootasdfasdf' is not a valid EventType! Update data "
             "will be ignored for 'network' scope") in caplog.text