Class: Embulk::Output::ZendeskUsers

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output/zendesk_users.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, schema, count, &control) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/embulk/output/zendesk_users.rb', line 9

def self.transaction(config, schema, count, &control)
  # configuration code:
  task = {
    "login_url" => config.param("login_url", :string, default: nil),
    "auth_method" => config.param("auth_method", :string, default: "token"),
    "username" => config.param("username", :string, default: nil),
    "token" => config.param("token", :string, default: nil),
    "method" => config.param("method", :string, default: "update"),
    "id_column" => config.param("id_column", :string, default: "id"),
    "tags_column" => config.param("tags_column", :string, default: nil),
    "user_fields_column" => config.param("user_fields_column", :string, default: nil),
    "timeout" => config.param("timeout", :integer, default: 5),
    "open_timeout" => config.param("open_timeout", :integer, default: 2)
  }

  # resumable output:
  # resume(task, schema, count, &control)

  # non-resumable output:
  task_reports = yield(task)
  next_config_diff = {}
  return next_config_diff
end

Instance Method Details

#abortObject



140
141
# File 'lib/embulk/output/zendesk_users.rb', line 140

def abort
end

#add(page) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/embulk/output/zendesk_users.rb', line 85

def add(page)
  Embulk.logger.info { "Connecting to #{@login_url}" }
  if @method == "update" then
    # Batch Update updates up to 100 users.
    page.each_slice(100).with_index do |records, index|
      Embulk.logger.info { "Uploading #{records.size} records" }
      update_users(records)
    end
  end
end

#closeObject



82
83
# File 'lib/embulk/output/zendesk_users.rb', line 82

def close
end

#commitObject



143
144
145
146
# File 'lib/embulk/output/zendesk_users.rb', line 143

def commit
  task_report = {}
  return task_report
end

#finishObject



137
138
# File 'lib/embulk/output/zendesk_users.rb', line 137

def finish
end

#initObject

def self.resume(task, schema, count, &control)

task_reports = yield(task)

next_config_diff = {}
return next_config_diff

end



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
# File 'lib/embulk/output/zendesk_users.rb', line 40

def init
  # initialization code:
  @login_url = task["login_url"]
  unless @login_url
    raise "'login_url' is required."
  end
  @auth_method = task["auth_method"]
  unless @auth_method == "token"
    raise "Only 'token' is supported in auth_method"
  end
  @username = task["username"]
  unless @username
    raise "username is required"
  end
  @token = task["token"]
  unless @username
    raise "'token' is required"
  end
  @method = task["method"]
  unless @method == "update"
    raise "Only 'update' is supporeted in method"
  end
  @id_column = task["id_column"]
  @tags_column = task["tags_column"]
  @user_fields_column = task["user_fields_column"]
  @timeout = task["timeout"]
  @open_timeout = task["open_timeout"]

  @client = ZendeskAPI::Client.new do |config|
    config.url = @login_url + "/api/v2"
    config.retry = true
    config.username = @username
    config.token = @token
    config.client_options = {
      :request => {
        :timeout => @timeout,
        :open_timeout => @open_timeout
      }
    }
  end
end

#update_users(records) ⇒ Object



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
# File 'lib/embulk/output/zendesk_users.rb', line 96

def update_users(records)
  requests = Array.new
  records.each do |record|
     data = Hash[schema.names.zip(record)]
     # Choose only target columns
     temp = {}
     temp.store("id", data["#{@id_column}"])
     temp.store("tags", data["#{@tags_column}"]) if @tags_column
     temp.store("user_fields", data["#{@user_fields_column}"]) if @user_fields_column
     Embulk.logger.debug {"Uploading data: #{temp}"}
     requests << temp
  end

  begin
    job_status = @client.users.update_many!(requests)
  rescue ZendeskAPI::Error::NetworkError => e
    Embulk.logger.warn {"#{e}"}
    Embulk.logger.warn {"Retrying..."}
    retry              
  end
  
  # https://github.com/zendesk/zendesk_api_client_rb#apps-api
  # Note: job statuses are currently not supported, so you must manually poll the job status API for app creation.
  body = {}
  until %w{failed completed}.include?(job_status['status'])
    begin
      response = @client.connection.get(job_status['url'])
    rescue ZendeskAPI::Error::NetworkError => e
      Embulk.logger.warn {"#{e}"}
      Embulk.logger.warn {"Retrying..."}
      retry              
    end
    job_status = response.body['job_status']
    sleep(1)
  end

  job_status['results'].each do |result|
    Embulk.logger.warn { "ID:#{result['id']}, Error:#{result['error']}, Details: #{result['details']}" } unless result['success']
  end
end