Moving a look from one looker server to another (or the same!)
For this example, I am hoping to take a look via a look ID and copy that to another server. This is really helpful in promoting content from a development to a production server.
I use python, and while you can use the Looker SDK, I prefer to use the python requests library. For this case I’ve put everything together in one file, but there are other best practices you can follow for managing API keys and separating these into several files.
# -*- coding: UTF-8 -*-
import requests
from pprint import pprint as pp
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class LookerApi(object):
def __init__(self, token, secret, host):
self.token = token
self.secret = secret
self.host = host
self.session = requests.Session()
self.session.verify = False
self.auth()
def auth(self):
url = '{}{}'.format(self.host,'login')
params = {'client_id':self.token,
'client_secret':self.secret
}
r = self.session.post(url,params=params)
access_token = r.json().get('access_token')
# print access_token
self.session.headers.update({'Authorization': 'token {}'.format(access_token)})
# GET /looks/
def get_look_info(self,look_id,fields=''):
url = '{}{}/{}'.format(self.host,'looks',look_id)
print url
params = {"fields":fields}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# GET /queries/
def get_query(self,query_id):
url = '{}{}/{}'.format(self.host,'queries',query_id)
print url
params = {}
r = self.session.get(url,params=params)
if r.status_code == requests.codes.ok:
return r.json()
# POST /queries/
def create_query(self,query_body, fields):
url = '{}{}'.format(self.host,'queries')
print url
params = json.dumps(query_body)
print " --- creating query --- "
r = self.session.post(url,headers = {'Content-type': 'application/json'},data=params, params = json.dumps({"fields": fields}))
if r.status_code == requests.codes.ok:
return r.json()
# POST /looks/
def create_look(self,look_body):
url = '{}{}'.format(self.host,'looks')
print url
params = json.dumps(look_body)
r = self.session.post(url,data=params)
if r.status_code == requests.codes.ok:
return r.json()
Then I log in with my api keys
dest_host = 'my destination host' ## i.e. https://looker.looker.com:19999/api/3.0/
dest_secret = 'my destination secret'
dest_token = 'my destination key'
dest_looker = LookerApi(host=dest_host,
token=dest_token,
secret = dest_secret)
source_host = 'my source host'
source_secret = 'my source secret'
source_token = 'my source key'
source_looker = LookerApi(host=source_host,
token=source_token,
secret = source_secret)
Now that I have the API ready to go, I follow my move look process:
source_name= 'my source'
source_look= 123
destination_space = '4'
look_body = source_looker.get_look_info(source_look)
print "---- Source Look Body ----"
pp(look_body)
print "---- Source query ----"
query_body = source_looker.get_query(look_body['query_id'])
pp(query_body)
print "---- New query ----"
new_query = dest_looker.create_query(query_body,'id')
new_query_id = str(new_query['id'])
print new_query_id+" is the new query id"
new_look = {}
new_look['space_id'] = destination_space
new_look['query_id'] = new_query_id
new_look['title'] = look_body['title'] + " copy from "+source_name
destination_look = dest_looker.create_look(new_look)
pp(destination_look)
When compiled, your output should look like this [shortened and masked]
https://host_name.looker.com:19999/api/3.0/looks/1
---- Source Look Body ----
{u'can': {u'copy': True,
u'create': True,
u'destroy': True,
u'dow
w': True,
...
u'show_details': True,
u'sudo': True},
u'id': 100},
u'user_id': 100,
u'view_count': None}
---- Source query ----
https://host_name.looker.com:19999/api/3.0/queries/5880
{u'can': {u'cost_estimate': True,
u'create': True,
u'download': True,
u'exp...
': None,
u'visible_ui_sections': None}
---- New query ----
https://destination_name.looker.com:19999/api/3.0/queries
--- creating query ---
47660 is the new query id
---- New Look ----
https://destination_name.looker.com:19999/api/3.0/looks
{u'can': {u'copy': True,
u'create': True,
u'destroy': True,
u'download': True,
u'find_a
...
w_details': True,
u'sudo': False},
u'id': 758},
u'user_id': 758,
u'view_count': None}
If you end up with a block of JSON printed out, that indicates you have successfully created the new Look.
Check out the full script called move_look.py
here: https://github.com/llooker/python_api_samples
Note: the link will use a LookerAPI.py file to hold the class, and a configuration file for keys. Check the readme for setting this up.