Commit bdc618c2 authored by Shinya Maeda's avatar Shinya Maeda 🔴

ok

parent cf8140a7
Pipeline #12123280 failed with stages
in 114 minutes and 3 seconds
module GoogleApi
class AuthorizationsController < ApplicationController
# callback_google_api_authorizations GET|POST /google_api/authorizations/callback(.:format) google_api/authorizations#callback
##
# 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)
# /google_api/authorizations/callback(.:format)
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]
redirect_to params[:state]
......@@ -13,15 +13,5 @@ module GoogleApi
redirect_to root_url
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
......@@ -22,7 +22,7 @@ class Projects::ClustersController < Projects::ApplicationController
# - user.authenticate_for_gcp!
# - Create this module which can be used from view
def new
unless session[access_token_key]
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
......@@ -33,6 +33,48 @@ class Projects::ClustersController < Projects::ApplicationController
# - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service
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'
end
......@@ -42,7 +84,7 @@ class Projects::ClustersController < Projects::ApplicationController
# GKE params are on-off swtich
# Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace.
def edit
unless session[access_token_key]
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
......@@ -82,21 +124,16 @@ class Projects::ClustersController < Projects::ApplicationController
@cluster ||= project.clusters.first
end
def cluster_params
params.require(:cluster).permit(:aaa)
end
# def cluster_params
# params.require(:cluster).permit(:aaa)
# end
def api_client
@api_client ||=
GoogleApi::CloudPlatform::Client.new(
session[access_token_key],
session[GoogleApi::CloudPlatform::Client.token_in_session],
callback_google_api_authorizations_url,
state: namespace_project_clusters_url.to_s
)
end
def access_token_key
# :"#{api_client.scope}_access_token"
:"hoge_access_token" # TODO:
end
end
......@@ -12,6 +12,6 @@ Create a new cluster
%br
Avaiable zones
%br
= link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, param1: 'value1', param2: 'value2'), method: :post
= link_to "Use existing kubernets cluster", 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
%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
DOWNTIME = false
def change
def up
create_table :ci_clusters do |t|
t.integer :project_id
t.integer :owner_id
t.datetime_with_timezone :created_at, null: false
t.datetime_with_timezone :updated_at, null: false
t.integer :service_id
# General
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.text :ca_cert # Base64?
t.text :ca_cert
t.string :token
t.string :username
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
# 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
add_foreign_key :ci_clusters, :projects
add_foreign_key :ci_clusters, :users, column: :owner_id
add_foreign_key :ci_clusters, :services
end
def down
......
......@@ -270,16 +270,20 @@ ActiveRecord::Schema.define(version: 20170924094327) do
create_table "ci_clusters", force: :cascade do |t|
t.integer "project_id"
t.integer "owner_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "service_id"
t.boolean "enabled", default: true
t.integer "creation_type"
t.string "project_namespace"
t.string "end_point"
t.text "ca_cert"
t.string "token"
t.string "username"
t.string "password"
t.string "project_namespace"
t.integer "creation_type"
t.string "gcp_project_id"
t.string "cluster_zone"
t.string "cluster_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ci_group_variables", force: :cascade do |t|
......@@ -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", "projects", name: "fk_befce0568a", on_delete: :cascade
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_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
......
......@@ -2,6 +2,13 @@ module GoogleApi
module CloudPlatform
class Client < GoogleApi::Authentication
# Google::Apis::ContainerV1::ContainerService.new
class << self
def token_in_session
:cloud_platform_access_token
end
end
def scope
'https://www.googleapis.com/auth/cloud-platform'
end
......@@ -16,8 +23,16 @@ module GoogleApi
response
end
def projects_zones_clusters_create
# TODO
def projects_zones_clusters_create(gcp_project_id, cluster_zone, cluster_name, cluster_size)
# TODO: Google::Apis::ContainerV1::ContainerService.new
# TODO: Debug
{
'end_point' => '111.111.111.111',
'ca_cert' => 'XXXXXXXXXXXXXXXXXX',
'username' => 'AAA',
'password' => 'BBB'
}
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