Пример #1
0
def sync(state, client, list_path, resource_path, payload, check_mode):
    datastore = utils.get(client, resource_path)

    # When we are deleting stores, we do not care if there is more than one
    # datastore present. We just make sure the currently manipulated store is
    # gone. This makes our module useful in "let us clean up the mess"
    # scenarios.
    if state == "absent" and datastore is None:
        return False, None

    if state == "absent":
        if not check_mode:
            utils.delete(client, resource_path)
        return True, None

    # If the store exists, update it and ignore the fact that there might be
    # more than one present.
    if datastore:
        if _do_differ(datastore, payload):
            if check_mode:
                return True, payload
            utils.put(client, resource_path, payload)
            return True, utils.get(client, resource_path)
        return False, datastore

    # When adding a new datastore, we first make sure there is no other
    # datastore present because we do not want to be the ones who brought
    # backends into an inconsistent state.
    if utils.get(client, list_path):
        raise errors.Error("Some other external datastore is already active.")

    if check_mode:
        return True, payload
    utils.put(client, resource_path, payload)
    return True, utils.get(client, resource_path)
Пример #2
0
    def test_abort_on_invalid_status(self, mocker, status):
        client = mocker.Mock()
        client.delete.return_value = http.Response(status, "")

        with pytest.raises(errors.SyncError, match=str(status)):
            utils.delete(client, "/delete")
        client.delete.assert_called_once_with("/delete")
Пример #3
0
def update_state(client, path, old_disabled, new_disabled, check_mode):
    changed = old_disabled != new_disabled

    if not check_mode and changed:
        if new_disabled:  # `state: disabled` input parameter
            utils.delete(client, path)
        else:  # `state: enabled` input parameter
            utils.put(client, path + '/reinstate', None)

    return changed
Пример #4
0
def sync(remote_object, state, client, path, payload, check_mode):
    if state == 'disabled' and remote_object is not None:
        if not check_mode:
            utils.delete(client, path)
        return True, utils.get(client, path)

    if utils.do_differ(remote_object, payload):
        if check_mode:
            return True, payload
        utils.put(client, path, payload)
        return True, utils.get(client, path)

    return False, remote_object
Пример #5
0
def update_groups(client, path, old_groups, new_groups, check_mode):
    to_delete = set(old_groups).difference(new_groups)
    to_add = set(new_groups).difference(old_groups)

    if not check_mode:
        # Next few lines are far from atomic, which means that we can leave a
        # user in any of the intermediate states, but this is the best we can
        # do given the API limitations.
        for g in to_add:
            utils.put(client, path + '/groups/' + g, None)
        for g in to_delete:
            utils.delete(client, path + '/groups/' + g)

    return len(to_delete) + len(to_add) > 0
Пример #6
0
    def test_valid_delete(self, mocker):
        client = mocker.Mock()
        client.delete.return_value = http.Response(204, "{}")

        object = utils.delete(client, "/delete")

        assert object is None
        client.delete.assert_called_once_with("/delete")