async def update(api, oldName, newName, newJsonDatasource, oldOrganizationNames, newOrganizationNames, lock, logger):
    responses = []
    # Delete the datasource from organizations it doesn't belong
    pruneOrgs = [
        item for item in oldOrganizationNames if item not in newOrganizationNames]
    pruneOrgIds = []
    try:
        pruneOrgIds = [organization.find_by_name(api, orgName).get('id')
                       for orgName in pruneOrgs]
    except GrafanaException as err:
        logger.error(
            f'Unable to get organization by name in update datasource: {err}')
    if pruneOrgIds:
        await lock.acquire()
        try:
            for orgId in pruneOrgIds:
                api.organizations.switch_organization(orgId)
                try:
                    api.datasource.delete_datasource_by_name(
                        oldName)
                except GrafanaException as err:
                    logger.error(
                        f'Unable to prune datasource with name {oldName} in organization {orgId}: {err}')
            api.organizations.switch_organization(MAIN_ORG_ID)
        finally:
            lock.release()
    # Recreate datasource for orgs that retain the datasource
    orgIds = []
    try:
        orgIds = [organization.find_by_name(api, orgName).get('id')
                  for orgName in newOrganizationNames]
    except GrafanaException as err:
        logger.error(
            f'Something went wrong when trying to lookup an organization by name: {err}')
    await lock.acquire()
    try:
        for orgId in orgIds:
            api.organizations.switch_organization(orgId)
            try:
                api.datasource.delete_datasource_by_name(oldName)
            except GrafanaException as err:
                logger.error(
                    f'Unable to delete datasource with name {newName} in organization {orgId}: {err}')
            try:
                newJsonDatasource['name'] = newName
                if newJsonDatasource.get('id'):
                    del newJsonDatasource['id']
                response = api.datasource.create_datasource(newJsonDatasource)
                responses.append(response)
            except GrafanaException as err:
                logger.error(
                    f'Unable to create datasource with name {newName} in organization {orgId}: {err}')
        api.organizations.switch_organization(MAIN_ORG_ID)
    finally:
        lock.release()
    return responses
Пример #2
0
def update(api, userId, name, email, oldOrganizationNames, newOrganizationNames):
    response = api.users.update_user(userId, {
        'email': email,
        'name': name,
        'login': name
    })
    revokeOrgs = [organization.find_by_name(api, orgName).get('id')
                  for orgName in oldOrganizationNames]
    orgIds = [organization.find_by_name(api, orgName).get('id')
              for orgName in newOrganizationNames]
    _revoke_organizations(api, userId, revokeOrgs)
    _add_organizations(api, name, orgIds)
    return response
Пример #3
0
async def delete(api, name, organizationNames, lock, logger):
    responses = []
    orgIds = []
    try:
        orgIds = [
            organization.find_by_name(api, orgName).get('id')
            for orgName in organizationNames
        ]
    except GrafanaException as err:
        logger.error(
            f'Something went wrong when trying to lookup an organization by name: {err}'
        )
    await lock.acquire()
    try:
        for orgId in orgIds:
            try:
                api.organizations.switch_organization(orgId)
                responses.append(api.dashboard.delete_dashboard(name))
            except GrafanaException as err:
                logger.error(
                    f'Unable to delete the dashboard with name {name} in organization {orgId}: {err}'
                )
        api.organizations.switch_organization(MAIN_ORG_ID)
    finally:
        lock.release()
    return responses
async def create(api, name, jsonDatasource, organizationNames, lock, logger):
    responses = []
    orgIds = []
    try:
        orgIds = [
            organization.find_by_name(api, orgName).get('id')
            for orgName in organizationNames
        ]
    except GrafanaException as err:
        logger.error(
            f'Something went wrong when trying to lookup an organization by name: {err}'
        )
    await lock.acquire()
    try:
        for orgId in orgIds:
            try:
                api.organizations.switch_organization(orgId)
                jsonDatasource['name'] = name
                if jsonDatasource.get('id'):
                    jsonDatasource.pop('id', None)
                response = api.datasource.create_datasource(jsonDatasource)
                responses.append(response)
            except GrafanaException as err:
                logger.error(
                    f'Unable to create datasource with name {name} in organization {orgId}: {err}'
                )
        api.organizations.switch_organization(MAIN_ORG_ID)
    finally:
        lock.release()
    return responses
Пример #5
0
def create(api, name, email, organizationNames):
    response = api.admin.create_user({
        'name': name,
        'email': email,
        'login': name,
        'password': '******'
    })
    userId = response.get('id')
    revokeOrgs = [MAIN_ORG_ID]
    orgIds = [organization.find_by_name(api, orgName).get('id')
              for orgName in organizationNames]
    _revoke_organizations(api, userId, revokeOrgs)
    _add_organizations(api, name, orgIds)
    return response
Пример #6
0
async def create(api, name, title, jsonDashboard, organizationNames, lock,
                 logger):
    responses = []
    orgIds = []
    try:
        orgIds = [
            organization.find_by_name(api, orgName).get('id')
            for orgName in organizationNames
        ]
    except GrafanaException as err:
        logger.error(
            f'Something went wrong when trying to lookup an organization by name: {err}'
        )
    await lock.acquire()
    try:
        for orgId in orgIds:
            try:
                api.organizations.switch_organization(orgId)
                jsonDashboard['uid'] = name
                jsonDashboard['title'] = title if title else name
                del jsonDashboard['id']
                dashboard_object = {
                    'dashboard': jsonDashboard,
                    'folderId': 0,
                    'overwrite': False
                }
                response = api.dashboard.update_dashboard(dashboard_object)
                responses.append(response)
            except GrafanaException as err:
                logger.error(
                    f'Unable to create dashboard with name {name} in organization {orgId}: {err}'
                )
        api.organizations.switch_organization(MAIN_ORG_ID)
    finally:
        lock.release()
    return responses
Пример #7
0
async def update(api, oldName, newName, newTitle, oldOrganizationNames,
                 newOrganizationNames, jsonDashboard, lock, logger):
    responses = []
    # Delete dashboards from organizations it doesn't belong
    pruneOrgs = [
        item for item in oldOrganizationNames
        if item not in newOrganizationNames
    ]
    pruneOrgIds = []
    try:
        pruneOrgIds = [
            organization.find_by_name(api, orgName).get('id')
            for orgName in pruneOrgs
        ]
    except GrafanaException as err:
        logger.error(
            f'Unable to get organization by name in update dashboard: {err}')
    if pruneOrgIds:
        await lock.acquire()
        try:
            for orgId in pruneOrgIds:
                api.organizations.switch_organization(orgId)
                try:
                    api.dashboard.delete_dashboard(oldName)
                except GrafanaException as err:
                    logger.error(
                        f'Unable to prune dashboard with uid {oldName} in organization {orgId}: err'
                    )
        finally:
            lock.release()
    # Update dashboards
    orgIds = []
    try:
        orgIds = [
            organization.find_by_name(api, orgName).get('id')
            for orgName in newOrganizationNames
        ]
    except GrafanaException as err:
        logger.error(
            f'Something went wrong when trying to lookup an organization by name: {err}'
        )
    await lock.acquire()
    try:
        for orgId in orgIds:
            try:
                api.organizations.switch_organization(orgId)
            except GrafanaException as err:
                logger.error(
                    f'Unable to switch to organization with id={orgId}: {err}')
            try:
                api.dashboard.delete_dashboard(oldName)
            except GrafanaException as err:
                if err.status_code == 404:
                    logger.debug(
                        f'Dashboard not found for update. Proceeding to create it in org {orgId}.'
                    )
                else:
                    logger.error(
                        f'Something went wrong when trying to delete dashboard {oldName} in org {orgId}: {err}'
                    )
            jsonDashboard['id'] = None
            # Setting the uid to the resource name, to be able to find it later
            jsonDashboard['uid'] = newName
            jsonDashboard['title'] = newTitle if newTitle else newName
            dashboard_object = {
                'dashboard': jsonDashboard,
                'folderId': 0,
                'overwrite': False
            }
            try:
                responses.append(
                    api.dashboard.update_dashboard(dashboard_object))
            except GrafanaException as err:
                logger.error(
                    f'Something went wrong when trying to create dashboard {newName} in org {orgId}: {err}'
                )
        api.organizations.switch_organization(MAIN_ORG_ID)
    finally:
        lock.release()
    return responses