class GoodData::Segment

Constants

ProvisioningResult
SEGMENT_TEMPLATE
SYNCHRONIZE_URI

Attributes

data_product[W]
domain[RW]

Public Class Methods

[](id, opts = {}) click to toggle source

Returns list of all segments or a particular segment

@param id [String|Symbol] Uri of the segment required or :all for all segments. @return [Array<GoodData::Segment>] List of segments for a particular domain

# File lib/gooddata/models/segment.rb, line 44
def [](id, opts = {})
  domain = opts[:domain]
  fail ArgumentError, 'No :domain specified' if domain.nil?

  client = domain.client
  fail ArgumentError, 'No client specified' if client.nil?

  data_product = opts[:data_product]

  if id == :all
    GoodData::Segment.all(opts)
  else
    result = client.get(base_uri(domain, data_product) + "/segments/#{CGI.escape(id)}")
    client.create(GoodData::Segment, result.merge('domain' => domain))
  end
end
all(opts = {}) click to toggle source

Returns list of all segments for domain

@param opts [Hash] Options. Should contain :domain for which you want to get the segments. @return [Array<GoodData::Segment>] List of segments for a particular domain

# File lib/gooddata/models/segment.rb, line 65
def all(opts = {})
  domain = opts[:domain]
  fail 'Domain has to be passed in options' unless domain
  client = domain.client

  data_product = opts[:data_product]
  results = client.get(base_uri(domain, data_product) + '/segments')
  GoodData::Helpers.get_path(results, %w(segments items)).map { |i| client.create(GoodData::Segment, i, domain: domain) }
end
base_uri(domain, data_product) click to toggle source
# File lib/gooddata/models/segment.rb, line 75
def base_uri(domain, data_product)
  if data_product
    GoodData::DataProduct::ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: data_product.data_product_id }
  else
    domain.segments_uri
  end
end
create(data = {}, options = {}) click to toggle source

Creates new segment from parameters passed

@param data [Hash] Data for segment namely :segment_id and :master_project is accepted. Master_project can be given as either a PID or a Project instance @param options [Hash] Trigger of schedule. Can be cron string or reference to another schedule. @return [GoodData::Segment] New Segment instance

# File lib/gooddata/models/segment.rb, line 88
def create(data = {}, options = {})
  segment_id = data[:segment_id]
  fail 'segment_id has to be provided' if segment_id.blank?
  client = options[:client]
  segment = client.create(GoodData::Segment, GoodData::Helpers.stringify_keys(SEGMENT_TEMPLATE), options)
  segment.tap do |s|
    s.segment_id = segment_id
    s.master_project = data[:master_project]
  end
end
new(data) click to toggle source
Calls superclass method
# File lib/gooddata/models/segment.rb, line 100
def initialize(data)
  super
  @domain = data.delete('domain')
  @data_product = nil
  @json = data
end

Public Instance Methods

clients(tenant_id = :all) click to toggle source

Returns all the clients associated with the segment. Since this is potentially paging operation it returns an Enumerable.

@return [Enumerable] Clients associated with the segment

# File lib/gooddata/models/segment.rb, line 177
def clients(tenant_id = :all)
  GoodData::Client[tenant_id, domain: domain, segment: self]
end
create_client(data) click to toggle source
# File lib/gooddata/models/segment.rb, line 169
def create_client(data)
  client = GoodData::Client.create(data, client: self.client, segment: self)
  client.save
end
data_product() click to toggle source
# File lib/gooddata/models/segment.rb, line 107
def data_product
  if @data_product
    @data_product
  else
    json = client.get(data['links']['dataProduct'])
    @data_product = client.create(GoodData::DataProduct, json)
  end
end
delete(options = {}) click to toggle source

Deletes a segment instance on the API.

@return [GoodData::Segment] Segment instance

# File lib/gooddata/models/segment.rb, line 239
def delete(options = {})
  force = options[:force] == true ? true : false
  clients.peach(&:delete) if force
  client.delete(uri) if uri
  self
rescue RestClient::BadRequest => e
  payload = GoodData::Helpers.parse_http_exception(e)
  e = SegmentNotEmpty if GoodData::Helpers.get_path(payload) == 'gdc.c4.conflict.domain.segment.contains_clients'
  raise e
end
master()
Alias for: master_project
master=(a_project)
Alias for: master_project=
master_id()
Alias for: master_project_id
master_project() click to toggle source

Master project getter for the Segment. It returns the instance not just the URI

@return [GoodData::Project] Project associated with the segment

# File lib/gooddata/models/segment.rb, line 163
def master_project
  client.projects(master_project_uri)
end
Also aliased as: master
master_project=(a_project) click to toggle source

Master project id getter for the Segment.

@return [String] Segment id

# File lib/gooddata/models/segment.rb, line 135
def master_project=(a_project)
  data['masterProject'] = a_project.respond_to?(:uri) ? a_project.uri : a_project
  self
end
Also aliased as: master=
master_project_id() click to toggle source

Master project id getter for the Segment.

@return [String] Project uri

# File lib/gooddata/models/segment.rb, line 145
def master_project_id
  GoodData::Helpers.last_uri_part(master_project_uri)
end
Also aliased as: master_id
master_project_uri() click to toggle source

Master project uri getter for the Segment.

@return [String] Project uri

# File lib/gooddata/models/segment.rb, line 154
def master_project_uri
  data['masterProject']
end
Also aliased as: master_uri
master_uri()
Alias for: master_project_uri
provision_client_projects(segments = []) click to toggle source
# File lib/gooddata/models/segment.rb, line 250
def provision_client_projects(segments = [])
  body = {
    provisionClientProjects: {
      segments: segments.empty? ? [segment_id] : segments
    }
  }
  res = client.post(GoodData::DataProduct::ONE_DATA_PRODUCT_PATH % { domain_name: domain.name, id: data_product.data_product_id } + '/provisionClientProjects', body)
  res = client.poll_on_code(res['asyncTask']['links']['poll'])
  failed_count = GoodData::Helpers.get_path(res, %w(clientProjectProvisioningResult failed count), 0)
  created_count = GoodData::Helpers.get_path(res, %w(clientProjectProvisioningResult created count), 0)
  return [].to_enum if (failed_count + created_count).zero?

  Enumerator.new do |y|
    uri = GoodData::Helpers.get_path(res, %w(clientProjectProvisioningResult links details))
    loop do
      result = client.get(uri)
      (GoodData::Helpers.get_path(result, %w(clientProjectProvisioningResultDetails items)) || []).each do |item|
        y << ProvisioningResult.new(item['id'], item['status'], item['project'], item['error'])
      end
      uri = GoodData::Helpers.get_path(res, %w(clientProjectProvisioningResultDetails paging next))
      break if uri.nil?
    end
  end
end
save() click to toggle source

Creates or updates a segment instance on the API.

@return [GoodData::Segment] Segment instance

# File lib/gooddata/models/segment.rb, line 184
def save
  if uri
    client.put(uri, json)
  else
    res = client.post(self.class.base_uri(domain, @data_product ? data_product : nil) + '/segments', json)
    @json = res
  end
  self
end
segment_id() click to toggle source

Segment id getter for the Segment. Called segment_id since id is a reserved word in ruby world

@return [String] Segment id

# File lib/gooddata/models/segment.rb, line 119
def segment_id
  data['id']
end
segment_id=(an_id) click to toggle source

Segment id setter for the Segment. Called segment_id since id is a reserved word in ruby world

@param an_id [String] Id of the segment. @return [String] Segment id

# File lib/gooddata/models/segment.rb, line 127
def segment_id=(an_id)
  data['id'] = an_id
  self
end
synchronize_clients() click to toggle source

Runs async process that walks through segments and provisions projects if necessary.

@return [Array] Returns array of results

# File lib/gooddata/models/segment.rb, line 197
def synchronize_clients
  sync_uri = SYNCHRONIZE_URI % [domain.obj_id, data_product.data_product_id, id]
  res = client.post sync_uri, nil

  # wait until the instance is created
  res = client.poll_on_response(res['asyncTask']['links']['poll'], :sleep_interval => 1) do |r|
    r['synchronizationResult'].nil?
  end

  client.create(ClientSynchronizationResult, res)
end
synchronize_processes(projects = []) click to toggle source
# File lib/gooddata/models/segment.rb, line 209
def synchronize_processes(projects = [])
  projects = [projects] unless projects.is_a?(Array)
  projects = projects.map do |p|
    p = p.pid if p.respond_to?('pid')
    fail('wrong type of argument. Should be either project ID or path') unless GoodData::Project.project_id_or_path?(p)
    p.split('/').last
  end

  unless projects.empty?
    body = {
      syncConfig: {
        filters: {
          projectIdFilter: projects
        }
      }
    }
  end

  uri = '/gdc/internal/lcm/domains/%{domain}/dataproducts/%{dataproduct}/segments/%{segment}/syncProcesses'
  uri = uri % { domain: domain.name, dataproduct: data_product.data_product_id, segment: segment_id }
  res = client.post(uri, body)

  client.poll_on_response(GoodData::Helpers.get_path(res, %w(asyncTask link poll)), sleep_interval: 1) do |r|
    r['syncedResult'].nil?
  end
end