Class: GoodData::LCM2::CollectClients

Inherits:
BaseAction show all
Defined in:
lib/gooddata/lcm/actions/collect_clients.rb

Constant Summary collapse

DESCRIPTION =
'Collect Clients'
PARAMS =
define_params(self) do
  description 'Client Used for Connecting to GD'
  param :gdc_gd_client, instance_of(Type::GdClientType), required: true
end
RESULT_HEADER =
[
  :client,
  :segment_id,
  :title
]

Constants included from Dsl::Dsl

Dsl::Dsl::DEFAULT_OPTS, Dsl::Dsl::TYPES

Class Method Summary collapse

Methods inherited from BaseAction

check_params

Methods included from Dsl::Dsl

#define_params, #define_type, #process

Class Method Details

.call(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gooddata/lcm/actions/collect_clients.rb', line 26

def call(params)
  # Check if all required parameters were passed
  BaseAction.check_params(PARAMS, params)

  segment_names = params.segments.map(&:segment_id)

  clients = collect_clients(params, segment_names)

  results = clients.map do |client|
    {
      client: client[:id],
      segment_id: client[:segment],
      title: client[:project_title]
    }
  end

  # Return results
  {
    results: results,
    params: {
      clients: clients
    }
  }
end

.collect_clients(params, segment_names = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gooddata/lcm/actions/collect_clients.rb', line 51

def collect_clients(params, segment_names = nil)
  client_id_column = params.client_id_column || 'client_id'
  segment_id_column = params.segment_id_column || 'segment_id'
  project_id_column = params.project_id_column || 'project_id'
  project_title_column = params.project_title_column || 'project_title'
  project_token_column = params.project_token_column || 'project_token'

  clients = []
  data_source = GoodData::Helpers::DataSource.new(params.input_source)
  input_data = File.open(data_source.realize(params), 'r:UTF-8')
  CSV.foreach(input_data, :headers => true, :return_headers => false, encoding: 'utf-8') do |row|
    segment_name = row[segment_id_column]
    if segment_names.nil? || segment_names.include?(segment_name)
      clients << {
        id: row[client_id_column],
        segment: segment_name,
        project: row[project_id_column],
        settings: [
          {
            name: 'lcm.token',
            value: row[project_token_column]
          },
          {
            name: 'lcm.title',
            value: row[project_title_column]
          }
        ]
      }.compact
    end
  end

  if clients.empty?
    fail "No segments or clients qualify for provisioning. \
    Please check the input source data, platform segments, and the SEGMENTS_FILTER parameter. \
    The intersection of these three elements is empty set."
  end
  clients
end