Class: GoodData::Segment
- Inherits:
-
Rest::Resource
- Object
- Rest::Object
- Rest::Resource
- GoodData::Segment
- Includes:
- Mixin::Links, Mixin::UriGetter
- Defined in:
- lib/gooddata/models/segment.rb
Defined Under Namespace
Classes: ProvisioningResult
Constant Summary collapse
- SYNCHRONIZE_URI =
'/gdc/domains/%s/dataproducts/%s/segments/%s/synchronizeClients'
- SEGMENT_TEMPLATE =
{ :segment => { :id => nil, :masterProject => nil } }
Instance Attribute Summary collapse
- #data_product ⇒ Object
-
#domain ⇒ Object
Returns the value of attribute domain.
Attributes inherited from Rest::Object
Class Method Summary collapse
-
.[](id, opts = {}) ⇒ Array<GoodData::Segment>
Returns list of all segments or a particular segment.
-
.all(opts = {}) ⇒ Array<GoodData::Segment>
Returns list of all segments for domain.
- .base_uri(domain, data_product) ⇒ Object
-
.create(data = {}, options = {}) ⇒ GoodData::Segment
Creates new segment from parameters passed.
Instance Method Summary collapse
-
#clients(tenant_id = :all) ⇒ Enumerable
Returns all the clients associated with the segment.
- #create_client(data) ⇒ Object
-
#delete(options = {}) ⇒ GoodData::Segment
Deletes a segment instance on the API.
-
#initialize(data) ⇒ Segment
constructor
A new instance of Segment.
-
#master_project ⇒ GoodData::Project
(also: #master)
Master project getter for the Segment.
-
#master_project=(a_project) ⇒ String
(also: #master=)
Master project id getter for the Segment.
-
#master_project_id ⇒ String
(also: #master_id)
Master project id getter for the Segment.
-
#master_project_uri ⇒ String
(also: #master_uri)
Master project uri getter for the Segment.
- #provision_client_projects(segments = []) ⇒ Object
-
#save ⇒ GoodData::Segment
Creates or updates a segment instance on the API.
-
#segment_id ⇒ String
Segment id getter for the Segment.
-
#segment_id=(an_id) ⇒ String
Segment id setter for the Segment.
-
#synchronize_clients ⇒ Array
Runs async process that walks through segments and provisions projects if necessary.
- #synchronize_processes(projects = []) ⇒ Object
Methods included from Mixin::UriGetter
Methods included from Mixin::Links
Methods included from Mixin::ObjId
Methods inherited from Rest::Object
client, default_client, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::DataGetter
Methods included from Mixin::RootKeyGetter
Methods included from Mixin::ContentGetter
Constructor Details
#initialize(data) ⇒ Segment
Returns a new instance of Segment.
100 101 102 103 104 105 |
# File 'lib/gooddata/models/segment.rb', line 100 def initialize(data) super @domain = data.delete('domain') @data_product = nil @json = data end |
Instance Attribute Details
#data_product ⇒ Object
107 108 109 110 111 112 113 114 |
# 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 |
#domain ⇒ Object
Returns the value of attribute domain.
23 24 25 |
# File 'lib/gooddata/models/segment.rb', line 23 def domain @domain end |
Class Method Details
.[](id, opts = {}) ⇒ Array<GoodData::Segment>
Returns list of all segments or a particular segment
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 = {}) ⇒ Array<GoodData::Segment>
Returns list of all segments for domain
65 66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
75 76 77 78 79 80 81 |
# 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 = {}) ⇒ GoodData::Segment
Creates new segment from parameters passed
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/gooddata/models/segment.rb', line 88 def create(data = {}, = {}) segment_id = data[:segment_id] fail 'segment_id has to be provided' if segment_id.blank? client = [:client] segment = client.create(GoodData::Segment, GoodData::Helpers.stringify_keys(SEGMENT_TEMPLATE), ) segment.tap do |s| s.segment_id = segment_id s.master_project = data[:master_project] end end |
Instance Method Details
#clients(tenant_id = :all) ⇒ Enumerable
Returns all the clients associated with the segment. Since this is potentially paging operation it returns an Enumerable.
177 178 179 |
# 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) ⇒ Object
169 170 171 172 |
# 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 |
#delete(options = {}) ⇒ GoodData::Segment
Deletes a segment instance on the API.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/gooddata/models/segment.rb', line 239 def delete( = {}) force = [: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_project ⇒ GoodData::Project Also known as: master
Master project getter for the Segment. It returns the instance not just the URI
163 164 165 |
# File 'lib/gooddata/models/segment.rb', line 163 def master_project client.projects(master_project_uri) end |
#master_project=(a_project) ⇒ String Also known as: master=
Master project id getter for the Segment.
135 136 137 138 |
# 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 |
#master_project_id ⇒ String Also known as: master_id
Master project id getter for the Segment.
145 146 147 |
# File 'lib/gooddata/models/segment.rb', line 145 def master_project_id GoodData::Helpers.last_uri_part(master_project_uri) end |
#master_project_uri ⇒ String Also known as: master_uri
Master project uri getter for the Segment.
154 155 156 |
# File 'lib/gooddata/models/segment.rb', line 154 def master_project_uri data['masterProject'] end |
#provision_client_projects(segments = []) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# 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 ⇒ GoodData::Segment
Creates or updates a segment instance on the API.
184 185 186 187 188 189 190 191 192 |
# 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 ⇒ String
Segment id getter for the Segment. Called segment_id since id is a reserved word in ruby world
119 120 121 |
# File 'lib/gooddata/models/segment.rb', line 119 def segment_id data['id'] end |
#segment_id=(an_id) ⇒ String
Segment id setter for the Segment. Called segment_id since id is a reserved word in ruby world
127 128 129 130 |
# File 'lib/gooddata/models/segment.rb', line 127 def segment_id=(an_id) data['id'] = an_id self end |
#synchronize_clients ⇒ Array
Runs async process that walks through segments and provisions projects if necessary.
197 198 199 200 201 202 203 204 205 206 207 |
# 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 = []) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# 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 |