summaryrefslogtreecommitdiff
path: root/lib/google_api/cloud_platform/client.rb
blob: f0ca2c52aa78c6a44e11088773bb3ccd340cc7b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'google/apis/container_v1'

module GoogleApi
  module CloudPlatform
    class Client < GoogleApi::Authentication
      class << self
        def token_in_session
          :cloud_platform_access_token
        end
      end

      def scope
        'https://www.googleapis.com/auth/cloud-platform'
      end

      def projects_zones_clusters_get(project_id, zone, cluster_id)
        service = Google::Apis::ContainerV1::ContainerService.new
        service.authorization = access_token

        cluster = service.get_zone_cluster(project_id, zone, cluster_id)
        puts "#{self.class.name} - #{__callee__}: cluster: #{cluster.inspect}"
        cluster
      end

      # Responce exmaple 
      # TODO: machine_type : Defailt 3.75 GB
      def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size:, machine_type:)
        service = Google::Apis::ContainerV1::ContainerService.new
        service.authorization = access_token

        request_body = Google::Apis::ContainerV1::CreateClusterRequest.new(
            {
              "cluster": {
                "name": cluster_name,
                "initial_node_count": cluster_size
              }
            }
          )

        begin
          operation = service.create_cluster(project_id, zone, request_body)
        rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
          Rails.logger.error("#{self.class.name}: Could not create cluster #{cluster_name}: #{e}")
        end
        puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}"
        operation
      end

      def projects_zones_operations(project_id, zone, operation_id)
        service = Google::Apis::ContainerV1::ContainerService.new
        service.authorization = access_token

        operation = service.get_zone_operation(project_id, zone, operation_id)
        operation
      end

      def wait_operation_done(self_link)
        running = true

        ret = self_link.match(/projects\/(.*)\/zones\/(.*)\/operations\/(.*)/)
        project_id = ret[1]
        zone = ret[2]
        operation_id = ret[3]

        while running
          operation = projects_zones_operations(project_id, zone, operation_id)
          if operation.status != 'RUNNING'
            running = false
          end
        end
      end
    end
  end
end