Exemplo n.º 1
0
 def _sdk(api_version):
     filename = os.getenv("LOOKERSDK_INI", "../looker.ini")
     if api_version == 4.0:
         sdk = looker_sdk.init40(filename)
     elif api_version == 3.1:
         sdk = looker_sdk.init31(filename)
     return sdk
Exemplo n.º 2
0
def init_looker_sdk(url: str,
                    client_id: str,
                    client_secret: str,
                    version: str = '4.0') -> looker_sdk.methods40.Looker40SDK:
    os.environ['LOOKERSDK_BASE_URL'] = url
    os.environ['LOOKERSDK_CLIENT_ID'] = client_id
    os.environ['LOOKERSDK_CLIENT_SECRET'] = client_secret
    os.environ['LOOKERSDK_VERIFY_SSL'] = 'true'
    os.environ['LOOKERSDK_TIMEOUT'] = '120'
    os.environ['LOOKERSDK_API_VERSION'] = '4.0'
    return looker_sdk.init40()
Exemplo n.º 3
0
def clone(ctx, use, github_repo_name):
    looker_project_name = github_repo_name
    x = lookml.ProjectSSH(
        path='.',
        git_url='[email protected]:llooker/SFDC_campaign_attribution.git',
        looker_project_name=looker_project_name)
    shutil.rmtree(f'./{x._looker_project_name}/.git')
    os.environ['LOOKERSDK_BASE_URL'] = ctx.conf[use]['base_url']
    os.environ['LOOKERSDK_CLIENT_ID'] = ctx.conf[use]['client_id']
    os.environ['LOOKERSDK_CLIENT_SECRET'] = ctx.conf[use]['client_secret']
    sdk = init40()
    sdk.update_session(body={'workspace_id': 'dev'})
    # connect to github
    _github = github.Github(ctx.conf[use]['access_token'])
    # create repo on github
    user = _github.get_user()
    repo = user.create_repo(github_repo_name)
    # repo = user.get_repo('example_789')
    p = sdk.create_project({
        "id": looker_project_name,
        "name": looker_project_name,
        "uses_git": True,
        # "git_remote_url": repo.ssh_url,
        # "git_username": "******",
        # "git_password": "******",
        # "git_username_user_attribute": "string",
        # "git_password_user_attribute": "string",
        # "git_service_name": "string",
        # "git_application_server_http_port": 0,
        # "git_application_server_http_scheme": "string",
        # "deploy_secret": "string",
        # "unset_deploy_secret": true,
        # "pull_request_mode": "string",
        # "validation_required": true,
        # "git_release_mgmt_enabled": true,
        # "allow_warnings": true,
        # "is_example": true,
        # "dependency_status": "string"
    })
    deploy_key = sdk.create_git_deploy_key(p.id)
    sdk.update_project(looker_project_name, {"git_remote_url": repo.ssh_url})
    repo.create_key(title='looker_deploy_key_pylookml', key=deploy_key)
    shutil.move(looker_project_name, looker_project_name + '_')
    y = lookml.ProjectSSH(path='.',
                          git_url=repo.ssh_url,
                          looker_project_name=looker_project_name)
    shutil.copytree(looker_project_name + '_/',
                    looker_project_name + '/',
                    dirs_exist_ok=True)
    y._git.add()
    y._git.push_origin_head()
    # y.commit()
    shutil.rmtree(looker_project_name + '_')
Exemplo n.º 4
0
def register_user(
    *, hackathon: str, first_name: str, last_name: str, email: str
) -> str:
    sdk = looker_sdk.init40()

    user = find_or_create_user(
        sdk=sdk, first_name=first_name, last_name=last_name, email=email
    )
    assert user.id
    if not user.credentials_email:
        create_email_credentials(sdk=sdk, user_id=user.id, email=email)
    if user.credentials_api3:
        client_id = user.credentials_api3[0].client_id
    else:
        client_id = create_api3_credentials(sdk=sdk, user_id=user.id).client_id
    set_user_group(sdk=sdk, user_id=user.id, hackathon=hackathon)
    set_user_attributes(sdk=sdk, user_id=user.id, hackathon=hackathon)
    assert client_id
    return client_id
Exemplo n.º 5
0
def enable_users_by_hackathons(hackathons: Sequence[str]) -> Dict[str, str]:
    global LOOKER_GROUP_PREFIX
    sdk = looker_sdk.init40()
    groups = {g.name: g.id for g in sdk.all_groups(fields="id,name")}
    ret = {}
    for hackathon in hackathons:
        try:
            group_id = groups[f"{LOOKER_GROUP_PREFIX}{hackathon}"]
        except KeyError:
            raise RegisterError(f"No group found for hackathon: '{hackathon}'")
        for user in sdk.search_users(group_id=group_id):
            assert user.id
            assert user.email
            sdk.update_user(user_id=user.id, body=models.WriteUser(is_disabled=False))
            password_reset_url = sdk.create_user_credentials_email_password_reset(
                user_id=user.id, expires=False
            ).password_reset_url
            assert password_reset_url
            setup = re.sub("password/reset", "account/setup", password_reset_url)
            ret[user.email] = setup
    return ret
Exemplo n.º 6
0
def push(ctx, use, looker_project_name, github_repo_name):
    # print(ctx.conf[use]['base_url'])
    # connect to Looker
    os.environ['LOOKERSDK_BASE_URL'] = ctx.conf[use]['base_url']
    os.environ['LOOKERSDK_CLIENT_ID'] = ctx.conf[use]['client_id']
    os.environ['LOOKERSDK_CLIENT_SECRET'] = ctx.conf[use]['client_secret']
    sdk = init40()
    sdk.update_session(body={'workspace_id': 'dev'})
    # connect to github
    _github = github.Github(ctx.conf[use]['access_token'])
    # create repo on github
    user = _github.get_user()
    repo = user.create_repo(github_repo_name)
    # repo = user.get_repo('example_789')
    p = sdk.create_project({
        "id": looker_project_name,
        "name": looker_project_name,
        "uses_git": True,
        # "git_remote_url": repo.ssh_url,
        # "git_username": "******",
        # "git_password": "******",
        # "git_username_user_attribute": "string",
        # "git_password_user_attribute": "string",
        # "git_service_name": "string",
        # "git_application_server_http_port": 0,
        # "git_application_server_http_scheme": "string",
        # "deploy_secret": "string",
        # "unset_deploy_secret": true,
        # "pull_request_mode": "string",
        # "validation_required": true,
        # "git_release_mgmt_enabled": true,
        # "allow_warnings": true,
        # "is_example": true,
        # "dependency_status": "string"
    })
    deploy_key = sdk.create_git_deploy_key(p.id)
    sdk.update_project(looker_project_name, {"git_remote_url": repo.ssh_url})
    repo.create_key(title='looker_deploy_key_pylookml', key=deploy_key)
Exemplo n.º 7
0
from google.cloud import storage
import sys
import builtins as exceptions
import looker_sdk
import os
import time
from typing import Dict, Optional

sdk = looker_sdk.init40()
models = looker_sdk.models40


def swap_dataset(dataset: str = "hubspot_marketing",
                 connection: str = "brick-layer"):
    sdk.update_connection(connection, models.WriteDBConnection(
        database=dataset))  #Update connection reference


def download_dashboard(dataset: str, id: int):
    child = os.fork()  #Fork a process to run in the background
    if child == 0:
        run_render_task(dataset, id)  #run render task as child
    else:
        parent, status = os.waitpid(child, os.WNOHANG)  #let parent exit


def run_render_task(
    dataset: str,
    dashboard: str,
    style: str = "tiled",
    width: int = 612 * 2,
import looker_sdk
import csv

####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
from looker_sdk import methods, models40
sdk = looker_sdk.init40("../looker.ini")

#### GO TO ADMIN --> GROUPS AND FIND THE GROUP ID YOU WANT TO ADD THE PEOPLE TO. ADD IT BELOW
### Alternative would be to use the search groups endpoint
### Depending on the cleanliness of your source of emails, you may want to add more error handling
### EG check for structure, add users without Looker accounts to an output file, or even pass them into another endpoint where you create an account.


def add_csv_of_users_to_group(group_name: str, file_path: str):
    group = sdk.search_groups(name=group_name)
    group = group[0]
    if group:
        data = []
        i = 0
        with open(file_path) as f:
            ## MAKE SURE YOU DOUBLE CHECK THE DELIMITER IN YOUR CSV
            reader = csv.reader(f, delimiter=' ')
            for row in reader:
                data.append(str(row[i]))

        ## loops through list and searches user
        ## grabs user id and passes that through add user to group
        try:
            for email in data:
                for user in sdk.search_users(email=email):
                    #print(user.email)
Exemplo n.º 9
0
Example:
    Pause/Resume all schedules of a Look or a Dashboard: 
    	Usecase: Admins want to temporarily bulk-pausing all schedules when there are changes in ETL or LookML projects
    	that would lead to errors in sending schedules. Currently, there is no option in Looker UI to bulk-disable/enable schedules
    Copy all schedule settings of a Look (or a Dashboard) to another Look (or a Dashboard)
    	Usecase: Admins want to copy all schedule settings (destination plans, recipients, result options, etc.) of a Look or 
    	a Dashboard to another Look or Dashboard. Currently, there is no option in Looker UI to "copy" all schedules. 

Authors: Lan

Last modified: July 18, 2021
"""

import looker_sdk
from looker_sdk import models40
sdk = looker_sdk.init40(config_file='../looker.ini', section='Looker')


def get_schedules(id, content, user_id=None, all_users=True):
  
  """ Get all schedule plans of a Looker content owned by user_id
  
  Args: 
    id: id of the Looker content containing schedules    
    content(str): 'look', 'dashboard', or 'lookml_dashboard'
    user_id(int, optional): If user_id is None then return schedules owned by the user calling the API 
    all_users(bool, optional): If all_user is True then return schedules owned by all users 
  """

  if content == 'look':
    schedules = sdk.scheduled_plans_for_look(id, user_id=user_id, all_users=all_users)
Exemplo n.º 10
0
def me():
    sdk = looker_sdk.init40()
    return sdk.me()
 def setUp(self):
     self.sdk = looker_sdk.init40("tests/api.ini")
Exemplo n.º 12
0
import pandas as pd
import looker_sdk
from looker_sdk import models40

sdk = looker_sdk.init40("/your/ini/file/path/Looker.ini")

query = """select Name, Email From Fake_Users"""
 
create = sdk.create_sql_query(body=models40.SqlQueryCreate(connection_name="your_connection", sql=query)).slug
result = sdk.run_sql_query(slug=create, result_format="json")

df = pd.read_json(result)
df.head()

for i, row in df.iterrows():
    plan = models40.WriteScheduledPlan(name="Test Schedule: " + str(f"{row[0]}"), 
                                        run_as_recipient=False, 
                                        filters_string='?Employee+Name='+str(f"{row[0]}"), 
                                        dashboard_id=1, 
                                        crontab="0 5 * * *", 
                                        scheduled_plan_destination = [models40.ScheduledPlanDestination(format="csv_zip", 
                                                                                                        address=str(f"{row[1]}"), 
                                                                                                        type = "email")])
    schedule = sdk.create_scheduled_plan(body=plan)
     
Exemplo n.º 13
0
#
# Retrieve Looker host and secret from environment or .env
#

HOST = get_env("LOOKER_EMBED_HOST")
SECRET = get_env("LOOKER_EMBED_SECRET")

DEMO_HOST = get_env("LOOKER_DEMO_HOST", 'localhost')
DEMO_PORT = int(get_env("LOOKER_DEMO_PORT", '8080'))

#
# Initialize Looker API SDK
#

looker_api_sdk = looker_sdk.init40(config_file="./demo/looker.ini")

#
# Function to create a signed URL using the Looker API SDK
#


def api_create_signed_url(embed_url, user, HOST, looker_api_sdk):
    target_url = 'https://' + HOST + urllib.parse.unquote_plus(embed_url)
    # the current front end sends a query with embed included, which we remove here
    target_url = target_url.replace("embed/", "")

    print('Creating signed URL for target URL: ' + target_url)

    target_sso_url = looker_sdk.models.EmbedSsoParams(
        target_url=target_url,
Exemplo n.º 14
0
    def test_remote_project_creation(self):

        # connect to Looker
        os.environ['LOOKERSDK_BASE_URL'] = config['Looker']['base_url']
        os.environ['LOOKERSDK_CLIENT_ID'] = config['Looker']['client_id']
        os.environ['LOOKERSDK_CLIENT_SECRET'] = config['Looker'][
            'client_secret']
        sdk = init40()
        sdk.update_session(body={'workspace_id': 'dev'})
        # connect to github
        _github = github.Github(githubConf['github']['access_token'])
        # create repo on github
        user = _github.get_user()
        # repo = user.create_repo('example_789')
        repo = user.get_repo('example_789')
        p = sdk.create_project({
            "id": "pylookml_testing_4",
            "name": "pylookml_testing_4",
            "uses_git": True,
            # "git_remote_url": repo.ssh_url,
            # "git_username": "******",
            # "git_password": "******",
            # "git_username_user_attribute": "string",
            # "git_password_user_attribute": "string",
            # "git_service_name": "string",
            # "git_application_server_http_port": 0,
            # "git_application_server_http_scheme": "string",
            # "deploy_secret": "string",
            # "unset_deploy_secret": true,
            # "pull_request_mode": "string",
            # "validation_required": true,
            # "git_release_mgmt_enabled": true,
            # "allow_warnings": true,
            # "is_example": true,
            # "dependency_status": "string"
        })
        x = sdk.create_git_deploy_key(p.id)
        sdk.update_project("pylookml_testing_4",
                           {"git_remote_url": repo.ssh_url})
        print(x)
        repo.create_key(title='foo', key=x)
        print(repo)

        # Looker sdk.create_project()
        # sdk.create_project({
        #     "id": "foo_123",
        #     "name": "foo_123",
        #     "uses_git": True,
        #     "git_remote_url": "string",
        #     "git_username": "******",
        #     "git_password": "******",
        #     "git_username_user_attribute": "string",
        #     "git_password_user_attribute": "string",
        #     "git_service_name": "string",
        #     "git_application_server_http_port": 0,
        #     "git_application_server_http_scheme": "string",
        #     "deploy_secret": "string",
        #     # "unset_deploy_secret": true,
        #     # "pull_request_mode": "string",
        #     # "validation_required": true,
        #     # "git_release_mgmt_enabled": true,
        #     # "allow_warnings": true,
        #     # "is_example": true,
        #     # "dependency_status": "string"
        # })
        # Looker update_project? -> somehow get the deploy key
        # pygithub create_key(title, key, read_only=False) https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_key
        # does the looker project then need to complete setup?
        # optional: retrieve the dev branch for a user
        # add a readme stating that the project was created programatically
        # optional -> start with a template
        # deploy to production
        pass