Class: GoodData::DataSource

Inherits:
Rest::Resource show all
Defined in:
lib/gooddata/models/data_source.rb

Defined Under Namespace

Classes: AdsConnectionInfo, BigQueryConnectionInfo, ConnectionInfo, GenericConnectionInfo, RedshiftConnectionInfo, S3ConnectionInfo, SnowflakeConnectionInfo

Constant Summary collapse

DATA_SOURCES_URL =
'/gdc/dataload/dataSources'
SNOWFLAKE =
'snowflake'
REDSHIFT =
'redshift'
BIGQUERY =
'bigQuery'
GENERIC =
'generic'
S3 =
's3'
ADS =
'ads'
ERROR_MESSAGE_NO_SCHEMA =
'Data source schema has to be provided'

Instance Attribute Summary collapse

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ObjId

#obj_id

Methods inherited from Rest::Object

client, default_client

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

#initialize(json) ⇒ DataSource

Returns a new instance of DataSource.



133
134
135
136
137
138
# File 'lib/gooddata/models/data_source.rb', line 133

def initialize(json)
  super
  @json = json
  validate
  @connection_info = build_connection_info
end

Instance Attribute Details

#connection_infoObject

Returns the value of attribute connection_info.



12
13
14
# File 'lib/gooddata/models/data_source.rb', line 12

def connection_info
  @connection_info
end

Class Method Details

.[](id = :all, options = { client: GoodData.client }) ⇒ Object

Get all data sources or get a specify data source from data source identify Expected parameter value:

  • :all return all data sources
  • :data_source_id return a data source with the specify data source identify


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gooddata/models/data_source.rb', line 28

def [](id = :all, options = { client: GoodData.client })
  c = GoodData.get_client(options)

  if id == :all
    data = c.get(DATA_SOURCES_URL)
    data['dataSources']['items'].map do |ds_data|
      c.create(DataSource, ds_data)
    end
  else
    c.create(DataSource, c.get(DATA_SOURCES_URL + '/' + id))
  end
end

.all(options = { client: GoodData.client }) ⇒ Object

Get all data sources



64
65
66
# File 'lib/gooddata/models/data_source.rb', line 64

def all(options = { client: GoodData.client })
  DataSource[:all, options]
end

.create(opts) ⇒ Object

Create data source from json Expected keys:

  • :name (mandatory)
  • :alias (optional)
  • :prefix (optional)
  • :connectionInfo (mandatory)
  • :client (mandatory)


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
# File 'lib/gooddata/models/data_source.rb', line 75

def create(opts)
  ds_name = opts[:name]
  ds_alias = opts[:alias]
  ds_prefix = opts[:prefix]
  ds_connection_info = opts[:connectionInfo]

  GoodData.logger.info "Creating data source '#{ds_name}'"
  fail ArgumentError, 'Data source name has to be provided' if ds_name.nil? || ds_name.blank?
  fail ArgumentError, 'Data source connection info has to be provided' if ds_connection_info.nil?

  json = {
    'dataSource' => {
      'name' => ds_name,
      'connectionInfo' => ds_connection_info
    }
  }
  json['dataSource']['alias'] = ds_alias if ds_alias
  json['dataSource']['prefix'] = ds_prefix if ds_prefix

  # Create data source
  c = GoodData.get_client(opts)
  res = c.post(DATA_SOURCES_URL, json)

  # create the public facing object
  c.create(DataSource, res)
end

.from_alias(data_source_alias, options = { client: GoodData.client }) ⇒ DataSource

Get a specify data source from data source alias

Parameters:

  • data_source_alias (String)

    Data source alias

Returns:

  • (DataSource)

    Data source corresponding in backend or throw exception if the data source alias doesn't exist



53
54
55
56
57
58
59
60
61
# File 'lib/gooddata/models/data_source.rb', line 53

def from_alias(data_source_alias, options = { client: GoodData.client })
  data_sources = all(options)
  result = data_sources.find do |data_source|
    data_source.alias == data_source_alias
  end
  fail "Data source alias '#{data_source_alias}' has not found" unless result

  result
end

.from_id(id, options = { client: GoodData.client }) ⇒ DataSource

Get a specify data source from data source identify

Parameters:

  • id (String)

    Data source identify

Returns:

  • (DataSource)

    Data source corresponding in backend or throw exception if the data source identify doesn't exist



45
46
47
# File 'lib/gooddata/models/data_source.rb', line 45

def from_id(id, options = { client: GoodData.client })
  DataSource[id, options]
end

Instance Method Details

#aliasObject



152
153
154
# File 'lib/gooddata/models/data_source.rb', line 152

def alias
  @json['dataSource']['alias']
end

#alias=(new_alias) ⇒ Object



156
157
158
# File 'lib/gooddata/models/data_source.rb', line 156

def alias=(new_alias)
  @json['dataSource']['alias'] = new_alias
end

#deleteObject



129
130
131
# File 'lib/gooddata/models/data_source.rb', line 129

def delete
  saved? ? client.delete(uri) : nil
end

#idObject



172
173
174
# File 'lib/gooddata/models/data_source.rb', line 172

def id
  uri.split('/')[-1]
end

#is(type) ⇒ Object



176
177
178
# File 'lib/gooddata/models/data_source.rb', line 176

def is(type)
  @json['dataSource']['connectionInfo'][type]
end

#nameObject



144
145
146
# File 'lib/gooddata/models/data_source.rb', line 144

def name
  @json['dataSource']['name']
end

#name=(new_name) ⇒ Object



148
149
150
# File 'lib/gooddata/models/data_source.rb', line 148

def name=(new_name)
  @json['dataSource']['name'] = new_name
end

#prefixObject



160
161
162
# File 'lib/gooddata/models/data_source.rb', line 160

def prefix
  @json['dataSource']['prefix']
end

#prefix=(new_prefix) ⇒ Object



164
165
166
# File 'lib/gooddata/models/data_source.rb', line 164

def prefix=(new_prefix)
  @json['dataSource']['prefix'] = new_prefix
end

#saveObject

Save data source to backend. The saving will validate existing data source name and connection info. So need set values for them.

Input info:

  • :name (mandatory)
  • :alias (optional)
  • :prefix (optional)
  • :connectionInfo (mandatory)

Return: create data source in backend and return data source object corresponding with data source in backend.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gooddata/models/data_source.rb', line 113

def save
  validate
  validate_connection_info
  if saved?
    update_obj_json = client.put(uri, to_update_payload)
    @json = update_obj_json
  else
    res = client.post(DATA_SOURCES_URL, to_update_payload)
    fail 'Unable to create new Data Source' if res.nil?

    @json = res
  end
  @connection_info = build_connection_info
  self
end

#saved?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/gooddata/models/data_source.rb', line 140

def saved?
  !uri.blank?
end

#typeObject



180
181
182
# File 'lib/gooddata/models/data_source.rb', line 180

def type
  @json['dataSource']['connectionInfo'].first[0].upcase
end

#uriObject



168
169
170
# File 'lib/gooddata/models/data_source.rb', line 168

def uri
  @json['dataSource']['links']['self'] if @json && @json['dataSource'] && @json['dataSource']['links']
end