Module: GoodData::LCM

Defined in:
lib/gooddata/lcm/lcm.rb

Class Method Summary collapse

Class Method Details

.ensure_users(domain, migration_spec, filter_on_segment = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gooddata/lcm/lcm.rb', line 10

def ensure_users(domain, migration_spec, filter_on_segment = [])
  messages = []
  # Ensure technical user is in all projects
  if migration_spec.key?(:technical_user)
    clients = domain.clients

    clients.peach do |c|
      segment = c.segment
      next if !filter_on_segment.empty? && !(filter_on_segment.include?(segment.id))
      p = client.project
      begin
        p.create_users(migration_spec[:technical_user].map { |u| { login: u, role: 'admin' } })
      rescue RestClient::Exception => e
        messages << { type: :technical_user_addition, status: 'ERROR', message: e.message }
      end
    end
  end
  messages
end

.get_dashboards(workspace, tag = nil) ⇒ Object

Retrieves all dashboards tagged with the +$PRODUCTION_TAG+ from the +workspace+ Params: +workspace+:: workspace with the tagged dashboards Returns enumeration of the tagged dashboards URIs



244
245
246
247
248
249
250
# File 'lib/gooddata/lcm/lcm.rb', line 244

def get_dashboards(workspace, tag = nil)
  if tag
    GoodData::Dashboard.find_by_tag(tag, project: workspace, client: workspace.client).map(&:uri)
  else
    GoodData::Dashboard.all(project: workspace, client: workspace.client).map(&:uri)
  end
end

.transfer_attribute_drillpaths(source_project, targets) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/gooddata/lcm/lcm.rb', line 252

def transfer_attribute_drillpaths(source_project, targets)
  semaphore = Mutex.new

  # Convert to array
  targets = [targets] unless targets.is_a?(Array)

  # Get attributes from source project
  attributes = source_project.attributes

  # Generate transfer table
  drill_paths = attributes.pmap do |attribute|
    drill_label_uri = attribute.content['drillDownStepAttributeDF']
    if drill_label_uri
      drill_label = source_project.labels(drill_label_uri)
      [attribute.uri, attribute.meta['identifier'], drill_label_uri, drill_label.identifier]
    else
      []
    end
  end
  drill_paths.reject!(&:empty?)

  # Transfer to target projects
  targets.peach do |target|
    drill_paths.peach do |drill_path_info|
      src_attr_uri, attr_identifier, drill_label_uri, drill_path_identifier = drill_path_info

      attr_uri = GoodData::MdObject.identifier_to_uri({ project: target, client: target.client }, attr_identifier)
      next unless attr_uri

      attribute = GoodData::MdObject[attr_uri, project: target, client: target.client]

      if attribute
        drill_path = GoodData::MdObject.identifier_to_uri({ project: target, client: target.client }, drill_path_identifier)
        next unless drill_path

        if !attribute.content['drillDownStepAttributeDF'] || attribute.content['drillDownStepAttributeDF'] != drill_path
          semaphore.synchronize do
            GoodData.logger.debug <<-DEBUG
Transfer from:
{
  attr_uri: #{src_attr_uri},
  attr_identifier: #{attr_identifier},
  drill_label_uri: #{drill_label_uri},
  drill_label_identifier: #{drill_path_identifier}
}
To:
{
  attr_uri: #{attr_uri},
  attr_identifier: #{attr_identifier},
  drill_label_uri: #{drill_path},
  drill_label_identifier: #{drill_path_identifier}
}
DEBUG
            GoodData.logger.info "Updating drill path of #{attr_identifier} -> #{drill_path} in '#{target.title}'"
          end

          attribute.content['drillDownStepAttributeDF'] = drill_path
          attribute.save
        end
      else
        semaphore.synchronize do
          GoodData.logger.warn "Unable to find #{attr_identifier} in '#{target.title}'"
        end
      end

      nil
    end

    nil
  end
end

.transfer_everything(client, domain, migration_spec, opts = {}) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gooddata/lcm/lcm.rb', line 30

def transfer_everything(client, domain, migration_spec, opts = {})
  filter_on_segment = migration_spec[:segments] || migration_spec['segments'] || []

  GoodData.logger.info('Ensuring Users - warning: works across whole domain not just provided segment(s)')
  ensure_users(domain, migration_spec, filter_on_segment)

  GoodData.logger.info('Migrating Blueprints')

  bp_opts = {
    update_preference: opts[:update_preference] || opts['update_preference'],
    maql_replacements: opts[:maql_replacements] || opts['maql_replacements']
  }

  #########################################################
  # New Architecture of Transfer Everything Functionality #
  #########################################################
  #
  # modes = {
  #   release: [
  #     self.synchronize_ldm,
  #     self.synchronize_label_types,
  #     self.synchronize_meta, # Tag specified? If yes, transfer only tagged stuff. If not transfer all meta.
  #     self.synchronize_etl, # Processes, Schedules, Additional Params
  #   ],
  #   provisioning: [
  #     # self.ensure_titles # Handled by Provisioning Brick?
  #     self.ensure_users,
  #     self.delete_clients,
  #     self.provision_clients, # LCM API
  #     self.synchronize_label_types,
  #     self.synchronize_etl # Processes, Schedules, Additional Params
  #   ],
  #   rollout: [ # Works on segments only, not using collect_clients
  #     self.ensure_users,
  #     self.synchronize_ldm,
  #     self.synchronize_label_types,
  #     self.synchronize_etl, # Processes, Schedules, Additional Params
  #     self.synchronize_clients
  #   ]
  # }
  #
  # mode_name = param['mode']
  # mode_actions = modes[mode_name] || fail("Invalid mode specified: '#{mode_name}'")
  # messages = mode_actions.map do |action|
  #   action(params)
  # end

  domain.segments.peach do |segment|
    next if !filter_on_segment.empty? && !(filter_on_segment.include?(segment.id))

    bp = segment.master_project.blueprint
    segment.clients.each do |c|
      p = c.project
      p.update_from_blueprint(bp, bp_opts)
    end

    # target_projects = segment.clients.map(&:project)

    # Transfer metadata objects
    # GoodData::LCM.transfer_meta(segment.master_project, target_projects)
  end

  GoodData.logger.info('Migrating Processes and Schedules')

  deployment_client = migration_spec.key?(:user_for_deployment) ? GoodData.connect(migration_spec[:user_for_deployment]) : client
  domain.clients.peach do |c|
    segment = c.segment
    next if !filter_on_segment.empty? && !(filter_on_segment.include?(segment.id))

    segment_master = segment.master_project
    project = c.project

    # set metadata
    # TODO: Review this and remove if not required or duplicate
    # FIXME: TMA-210
    project.('GOODOT_CUSTOM_PROJECT_ID', c.id)

    # copy processes
    deployment_client_segment_master = deployment_client.projects(segment_master.pid)
    deployment_client_project = deployment_client.projects(project.pid)
    GoodData::Project.transfer_processes(deployment_client_segment_master, deployment_client_project)

    GoodData::Project.transfer_schedules(segment_master, project)

    # Set up unique parameters
    deployment_client_project.schedules.peach do |s|
      # TODO: Review this and remove if not required or duplicate (GOODOT_CUSTOM_PROJECT_ID vs CLIENT_ID)
      s.update_params('GOODOT_CUSTOM_PROJECT_ID' => c.id)
      s.update_params('CLIENT_ID' => c.id)
      s.update_params('SEGMENT_ID' => segment.id)
      s.update_params(migration_spec[:additional_params] || {})
      s.update_hidden_params(migration_spec[:additional_hidden_params] || {})
      s.save
    end

    # Transfer label types
    begin
      GoodData::LCM.transfer_label_types(segment_master, project)
    rescue => e
      GoodData.logger.error("Unable to transfer label_types, reason: #{e.message}")
    end

    # Transfer tagged objects
    # FIXME: Make sure it is not duplicate functionality mentioned in TMA-171
    tag = migration_spec[:production_tag]
    GoodData::Project.transfer_tagged_stuff(segment_master, project, tag) if tag
  end

  do_not_synchronize_clients = migration_spec[:do_not_synchronize_clients]
  if do_not_synchronize_clients.nil? || !do_not_synchronize_clients
    GoodData.logger.info('Migrating Dashboards')
    if filter_on_segment.empty?
      domain.synchronize_clients
    else
      filter_on_segment.map do |s|
        domain.segments(s).synchronize_clients
      end
    end
  end

  # User groups must be migrated after dashboards
  GoodData.logger.info('Migrating User Groups')
  domain.clients.peach do |c|
    segment = c.segment
    segment_master = segment.master_project
    project = c.project
    GoodData::Project.transfer_user_groups(segment_master, project)
  end
end

.transfer_label_types(source_project, targets) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/gooddata/lcm/lcm.rb', line 160

def transfer_label_types(source_project, targets)
  semaphore = Mutex.new

  # Convert to array
  targets = [targets] unless targets.is_a?(Array)

  client = source_project.client

  # Get attributes from source project
  attributes = GoodData::Attribute[:all, client: client, project: source_project]

  # Get display forms
  display_forms = attributes.map do |attribute|
    attribute.content['displayForms']
  end

  # Flatten result
  display_forms.flatten!(2)

  # Select only display forms with content type
  display_forms.select! { |display_form| display_form['content']['type'] }

  # Generate transfer table
  transfer = {}
  display_forms.each { |display_form| transfer[display_form['meta']['identifier']] = display_form['content']['type'] }

  GoodData.logger.info 'Transferring label types'
  GoodData.logger.info JSON.pretty_generate(transfer)

  # Transfer to target projects
  targets.peach do |target|
    transfer.peach do |identifier, type|
      uri = GoodData::MdObject.identifier_to_uri({ project: target, client: target.client }, identifier)
      next unless uri

      obj = GoodData::MdObject[uri, { project: target, client: target.client }]

      if obj
        if obj.content['type'] != type
          semaphore.synchronize do
            GoodData.logger.info "Updating #{identifier} -> #{type} in '#{target.title}'"
          end

          obj.content['type'] = type
          obj.save
        end
      else
        semaphore.synchronize do
          GoodData.logger.warn "Unable to find #{identifier} in '#{target.title}'"
        end
      end

      nil
    end

    nil
  end
end

.transfer_meta(source_workspace, target_workspaces, tag = nil) ⇒ Object

Synchronizes the dashboards tagged with the +$PRODUCTION_TAG+ from development project to master projects Params: +source_workspace+:: workspace with the tagged dashboards that are going to be synchronized to the target workspaces +target_workspaces+:: array of target workspaces where the tagged dashboards are going be synchronized to



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/gooddata/lcm/lcm.rb', line 223

def transfer_meta(source_workspace, target_workspaces, tag = nil)
  objects = get_dashboards(source_workspace, tag)
  begin
    token = source_workspace.objects_export(objects)
    GoodData.logger.info "Export token: '#{token}'"
  rescue => e
    GoodData.logger.error "Export failed, reason: #{e.message}"
  end
  target_workspaces.each do |target_workspace|
    begin
      target_workspace.objects_import(token)
    rescue => e
      GoodData.logger.error "Import failed, reason: #{e.message}"
    end
  end
end