Using the API to Configure Your Project's Git Connection

low_priority
done
reply
(vincent) #1

With Looker 4.18+, we introduce a new API endpoint to configure your git project settings. In this article, we’ll be using the Ruby SDK so if you haven’t already, go ahead and run gem install looker-sdk. We’ll also assume a little programming knowledge. If you would like some more examples, check this out.

Initialization

Lets start off by establishing our path and including the required libraries:

#!/usr/bin/env ruby
require 'looker-sdk'

Sweet, ezpz.

Now, lets go ahead and instantiate a new client object, sdk, for us to use to call API endpoints:

sdk = LookerSDK::Client.new(
  :client_id => "YOUR_ID",
  :client_secret => "YOUR_SECRET",
  :api_endpoint => "https://YOUR_LOOKER_SITE.com:19999/api/3.0",
  :connection_options => {:ssl => {:verify => false}}
)

Since I’m using my local instance with a self-signed HTTPS certificate, I set :connection_options => {:ssl => {:verify => false}}.

To get your client_id and client_secret, navigate to your Admin > Users page, find you, the click edit. You’ll see an option for API 3 keys. If you haven’t already, go ahead and generate your id and secret.

Now we are ready to configure that project’s git settings. I’ll be using GitHub in my example but this can work with any git service that we support. To see more on those supported services, check this out.

##Configuring the Project’s Git Settings

First thing we have to do is switch our API session into dev mode. To do that, we will use the update_session(body) endpoint and pass the parameter "dev", like so:

sdk.update_session(:workspace_id => "dev")

Almost there.

Now, what we will want to do is run create_git_deploy_key() which creates a public/private key pair for authenticating ssh git requests from Looker to a remote git repository for a particular Looker project. This endpoint takes one parameter which is the id of the project, aka the name of the project. Then we will want to hit git_deploy_key() to output the key we will need to add in the git repo Deploy Keys.

sdk.create_git_deploy_key("project_name")
puts sdk.git_deploy_key("project_name")

One thing to keep in mind about this call is that you can only run it once successfully. What I mean by that, is if a key already exists in Looker for that project, and error will be thrown, stoping the rest of the script from running. So, like the great programmers we are, lets wrap this call in a begin/rescue block

begin
	#lets try to create a new key
	sdk.create_git_deploy_key("project_name")
rescue
	#catching the possibility of a key existing so lets just print the key after error message
	puts "Key already exists, see below"
end

#display new key to add in github
puts sdk.git_deploy_key("project_name")

What is happening here is we try(begin) to create a new key and if one exists, we catch(rescue) the error and displaying a message instead to the console. Then we display that already existing key. If there isn’t a key we create(begin) one and output that to the console.

Once we have that key, some manual intervention is required. Grab that key and add it to the git repo Deploy Keys and give Looker write permission:

One more call and we are good to go. And that call is the update_project(). We are going to pass a few more parameters:

sdk.update_project("project_id", {:git_remote_url => "repo_url", :git_service_name => "github", :validation_required => "true", :is_example => "false", :pull_request_mode => "off"})

The first parameter is the name of the project and the second parameter a hash of key value pairs.
The repo url can be find on the website and should look something like:

I also made turned on validation required since I want to make sure I have no errors or warnings before I commit and deploy but disabled pull requests. It’s all up to you and your org.

##Conclusion

Since the above assumes one project, what if I wanted to add multiple at a time that gets loaded from a json file. Sure, no problem. Just parse that json file into a ruby hash and then wrap everything in a .each do loop. Here is everything put together.

#!/usr/bin/env ruby
require 'looker-sdk'
require 'json'

#grab the json file of project_id => git_url key values
file_to_read = ARGV.first

#read the file
file = File.read(file_to_read)

#parse the file to a ruby hash
git_projects = JSON.parse(file)

sdk = LookerSDK::Client.new(
  :client_id => "YOUR_ID",
  :client_secret => "YOUR_SECRET",
  :api_endpoint => "https://YOUR_LOOKER_SITE.com:19999/api/3.0",
  :connection_options => {:ssl => {:verify => false}}
)

#puts sdk.alive

#checking which mode I'm in
puts sdk.session[:workspace_id]

#switch to dev mode
sdk.update_session(:workspace_id => "dev")

#confirm that worked
puts sdk.session[:workspace_id]


#loop through hash, printing key value pairs
git_projects.each do |project_id, repo_url|
	#generate a new key, will error if one exists so lets catch if it does
	begin
		#lets try to create a new key
		sdk.create_git_deploy_key("#{project_id}")
	rescue
		#catching the possibility of a key existing so lets just print the key after error message
		puts "Key already exists, see below"
	end

	#display new key to add in github
	puts sdk.git_deploy_key("#{project_id}")

	# have to grab the key and enter it into github before calling update_project
	puts "Hit enter to continue after adding the deploy key to GitHub"
	gets

	#update project settings
	sdk.update_project("#{project_id}", {:git_remote_url => "#{repo_url}", :git_service_name => "github", :validation_required => "true", :is_example => "false", :pull_request_mode => "off"})
end
1 Like

(Noha Taha) #2

Does the sdk.update_project api reset the Github connection the same way it would if I was to do it from the UI? We’re concerned about the reset causing branches or non-commited/merged work to be lost so were considering the use of API to reset the deploy key

0 Likes

(Izzy) #3

Yep, I believe updating the project with a new git_remote_url is pretty much equivalent to resetting the connection from the UI.

0 Likes