Commit bdc618c2 authored by Shinya Maeda's avatar Shinya Maeda

ok

parent cf8140a7
module GoogleApi module GoogleApi
class AuthorizationsController < ApplicationController class AuthorizationsController < ApplicationController
# callback_google_api_authorizations GET|POST /google_api/authorizations/callback(.:format) google_api/authorizations#callback # /google_api/authorizations/callback(.:format)
##
# TODO:
# - Is it ok to use both "http://localhost:3000/google_api/authorizations/callback"(For login) and "http://localhost:3000/google_api/authorizations/callback"(For API token)
def callback def callback
session[access_token_key] = api_client.get_token(params[:code]) # TODO: Error handling
session[GoogleApi::CloudPlatform::Client.token_in_session] =
GoogleApi::Authentication.new(nil, callback_google_api_authorizations_url)
.get_token(params[:code])
if params[:state] if params[:state]
redirect_to params[:state] redirect_to params[:state]
...@@ -13,15 +13,5 @@ module GoogleApi ...@@ -13,15 +13,5 @@ module GoogleApi
redirect_to root_url redirect_to root_url
end end
end end
def api_client
@api_client ||=
GoogleApi::Authentication.new(nil, callback_google_api_authorizations_url)
end
def access_token_key
# :"#{api_client.scope}_access_token"
:"hoge_access_token" # TODO:
end
end end
end end
...@@ -22,7 +22,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -22,7 +22,7 @@ class Projects::ClustersController < Projects::ApplicationController
# - user.authenticate_for_gcp! # - user.authenticate_for_gcp!
# - Create this module which can be used from view # - Create this module which can be used from view
def new def new
unless session[access_token_key] unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url @authorize_url = api_client.authorize_url
end end
end end
...@@ -33,6 +33,48 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -33,6 +33,48 @@ class Projects::ClustersController < Projects::ApplicationController
# - If create manually, save in db (Prob, Project > Setting) # - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service # - Dry up with Service
def create def create
if params['creation_type'] == 'on_gke'
results = api_client.projects_zones_clusters_create(
params['gcp_project_id'],
params['cluster_zone'],
params['cluster_name'],
params['cluster_size']
)
# TODO: How to create
project.kubernetes_service.save(
end_point: results['end_point'],
ca_cert: results['ca_cert'],
token: nil,
username: results['username'],
password: results['password'],
project_namespace: params['project_namespace']
)
project.clusters.create(
creation_type: params['creation_type'],
gcp_project_id: params['gcp_project_id'],
cluster_zone: params['cluster_zone'],
cluster_name: params['cluster_name'],
kubernetes_service: project.kubernetes_service
)
elsif params['creation_type'] == 'manual'
# TODO: Transaction
project.kubernetes_service.save(
end_point: params['end_point'],
ca_cert: params['ca_cert'],
token: params['token'],
username: params['username'],
password: params['password'],
project_namespace: params['project_namespace']
)
project.clusters.create(
creation_type: params['creation_type'],
kubernetes_service: project.kubernetes_service
)
end
redirect_to action: 'index' redirect_to action: 'index'
end end
...@@ -42,7 +84,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -42,7 +84,7 @@ class Projects::ClustersController < Projects::ApplicationController
# GKE params are on-off swtich # GKE params are on-off swtich
# Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace. # Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace.
def edit def edit
unless session[access_token_key] unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url @authorize_url = api_client.authorize_url
end end
end end
...@@ -82,21 +124,16 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -82,21 +124,16 @@ class Projects::ClustersController < Projects::ApplicationController
@cluster ||= project.clusters.first @cluster ||= project.clusters.first
end end
def cluster_params # def cluster_params
params.require(:cluster).permit(:aaa) # params.require(:cluster).permit(:aaa)
end # end
def api_client def api_client
@api_client ||= @api_client ||=
GoogleApi::CloudPlatform::Client.new( GoogleApi::CloudPlatform::Client.new(
session[access_token_key], session[GoogleApi::CloudPlatform::Client.token_in_session],
callback_google_api_authorizations_url, callback_google_api_authorizations_url,
state: namespace_project_clusters_url.to_s state: namespace_project_clusters_url.to_s
) )
end end
def access_token_key
# :"#{api_client.scope}_access_token"
:"hoge_access_token" # TODO:
end
end end
...@@ -12,6 +12,6 @@ Create a new cluster ...@@ -12,6 +12,6 @@ Create a new cluster
%br %br
Avaiable zones Avaiable zones
%br %br
= link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, param1: 'value1', param2: 'value2'), method: :post = link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'on_gke', cluster_name: 'new-cluster-shinya', gcp_project_id: 'gitlab-internal', cluster_zone: 'gitlab-internal', cluster_size: 'gitlab-internal', project_namespace: 'aaa'), method: :post
= link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, param1: 'value1', param2: 'value2'), method: :post %br
= link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'manual', end_point: 'xxx.xxx.xxx.xxx', ca_cert: 'xxx...xxx', token: 'xxx', project_namespace: 'aaa'), method: :post
class CreateCiClusters < ActiveRecord::Migration class CreateCiClusters < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
def change def up
create_table :ci_clusters do |t| create_table :ci_clusters do |t|
t.integer :project_id t.integer :project_id
t.integer :owner_id t.integer :owner_id
t.datetime_with_timezone :created_at, null: false t.integer :service_id
t.datetime_with_timezone :updated_at, null: false
# General
t.boolean :enabled, default: true t.boolean :enabled, default: true
t.integer :creation_type # manual or on_gke
# k8s integration specific
t.string :project_namespace
# Cluster details
t.string :end_point t.string :end_point
t.text :ca_cert # Base64? t.text :ca_cert
t.string :token t.string :token
t.string :username t.string :username
t.string :password t.string :password
t.string :project_namespace
t.integer :creation_type # manual or on_gke # GKE
t.string :gcp_project_id
t.string :cluster_zone
t.string :cluster_name
t.datetime_with_timezone :created_at, null: false
t.datetime_with_timezone :updated_at, null: false
end end
# create_table :ci_gke_clusters do |t|
# t.integer :ci_cluster_id
# t.string :gcp_project_id
# t.string :cluster_zone
# t.string :cluster_name
# end
# add_foreign_key :ci_gke_clusters, :ci_clusters
# TODO: fk, index, encypt # TODO: fk, index, encypt
add_foreign_key :ci_clusters, :projects add_foreign_key :ci_clusters, :projects
add_foreign_key :ci_clusters, :users, column: :owner_id add_foreign_key :ci_clusters, :users, column: :owner_id
add_foreign_key :ci_clusters, :services
end end
def down def down
......
...@@ -270,16 +270,20 @@ ActiveRecord::Schema.define(version: 20170924094327) do ...@@ -270,16 +270,20 @@ ActiveRecord::Schema.define(version: 20170924094327) do
create_table "ci_clusters", force: :cascade do |t| create_table "ci_clusters", force: :cascade do |t|
t.integer "project_id" t.integer "project_id"
t.integer "owner_id" t.integer "owner_id"
t.datetime "created_at", null: false t.integer "service_id"
t.datetime "updated_at", null: false
t.boolean "enabled", default: true t.boolean "enabled", default: true
t.integer "creation_type"
t.string "project_namespace"
t.string "end_point" t.string "end_point"
t.text "ca_cert" t.text "ca_cert"
t.string "token" t.string "token"
t.string "username" t.string "username"
t.string "password" t.string "password"
t.string "project_namespace" t.string "gcp_project_id"
t.integer "creation_type" t.string "cluster_zone"
t.string "cluster_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
create_table "ci_group_variables", force: :cascade do |t| create_table "ci_group_variables", force: :cascade do |t|
...@@ -1701,6 +1705,7 @@ ActiveRecord::Schema.define(version: 20170924094327) do ...@@ -1701,6 +1705,7 @@ ActiveRecord::Schema.define(version: 20170924094327) do
add_foreign_key "ci_builds", "ci_stages", column: "stage_id", name: "fk_3a9eaa254d", on_delete: :cascade add_foreign_key "ci_builds", "ci_stages", column: "stage_id", name: "fk_3a9eaa254d", on_delete: :cascade
add_foreign_key "ci_builds", "projects", name: "fk_befce0568a", on_delete: :cascade add_foreign_key "ci_builds", "projects", name: "fk_befce0568a", on_delete: :cascade
add_foreign_key "ci_clusters", "projects" add_foreign_key "ci_clusters", "projects"
add_foreign_key "ci_clusters", "services"
add_foreign_key "ci_clusters", "users", column: "owner_id" add_foreign_key "ci_clusters", "users", column: "owner_id"
add_foreign_key "ci_group_variables", "namespaces", column: "group_id", name: "fk_33ae4d58d8", on_delete: :cascade add_foreign_key "ci_group_variables", "namespaces", column: "group_id", name: "fk_33ae4d58d8", on_delete: :cascade
add_foreign_key "ci_pipeline_schedule_variables", "ci_pipeline_schedules", column: "pipeline_schedule_id", name: "fk_41c35fda51", on_delete: :cascade add_foreign_key "ci_pipeline_schedule_variables", "ci_pipeline_schedules", column: "pipeline_schedule_id", name: "fk_41c35fda51", on_delete: :cascade
......
...@@ -2,6 +2,13 @@ module GoogleApi ...@@ -2,6 +2,13 @@ module GoogleApi
module CloudPlatform module CloudPlatform
class Client < GoogleApi::Authentication class Client < GoogleApi::Authentication
# Google::Apis::ContainerV1::ContainerService.new # Google::Apis::ContainerV1::ContainerService.new
class << self
def token_in_session
:cloud_platform_access_token
end
end
def scope def scope
'https://www.googleapis.com/auth/cloud-platform' 'https://www.googleapis.com/auth/cloud-platform'
end end
...@@ -16,8 +23,16 @@ module GoogleApi ...@@ -16,8 +23,16 @@ module GoogleApi
response response
end end
def projects_zones_clusters_create def projects_zones_clusters_create(gcp_project_id, cluster_zone, cluster_name, cluster_size)
# TODO # TODO: Google::Apis::ContainerV1::ContainerService.new
# TODO: Debug
{
'end_point' => '111.111.111.111',
'ca_cert' => 'XXXXXXXXXXXXXXXXXX',
'username' => 'AAA',
'password' => 'BBB'
}
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment