Class: Mortar::API

Inherits:
Object
  • Object
show all
Includes:
BasicAuth
Defined in:
lib/mortar/api.rb,
lib/mortar/api/jobs.rb,
lib/mortar/api/user.rb,
lib/mortar/api/login.rb,
lib/mortar/api/tasks.rb,
lib/mortar/api/config.rb,
lib/mortar/api/errors.rb,
lib/mortar/api/version.rb,
lib/mortar/api/clusters.rb,
lib/mortar/api/describe.rb,
lib/mortar/api/projects.rb,
lib/mortar/api/validate.rb,
lib/mortar/api/basicauth.rb,
lib/mortar/api/illustrate.rb,
lib/mortar/api/vendor/okjson.rb

Defined Under Namespace

Modules: BasicAuth, Clusters, Describe, Errors, Illustrate, Jobs, OkJson, Projects, Task, Validate

Constant Summary collapse

VERSION =

client version see semver.org/

"0.8.11"
SERVER_API_VERSION =
"2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BasicAuth

#basic_auth_authorization_header

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mortar/api.rb', line 57

def initialize(options={})
  user = options.delete(:user)
  api_key = options.delete(:api_key) || ENV['MORTAR_API_KEY']
  options = {
    :headers  => {},
    :host     => 'api.mortardata.com',
    :retry_limit => 5,
    :scheme   => 'https',
    :ssl_version => :TLSv1
  }.merge(options)
  options[:headers] = {
    'Accept'                => 'application/json',
    'Accept-Encoding'       => 'gzip',
    'Content-Type'          => 'application/json',
    #'Accept-Language'       => 'en-US, en;q=0.8',
    'Authorization'         => basic_auth_authorization_header(user, api_key),
    'User-Agent'            => "mortar-api-ruby/#{Mortar::API::VERSION}",
    'X-Ruby-Version'        => RUBY_VERSION,
    'X-Ruby-Platform'       => RUBY_PLATFORM
  }.merge(options[:headers])
  @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



55
56
57
# File 'lib/mortar/api.rb', line 55

def connection
  @connection
end

Instance Method Details

#delete_config_var(project_name, config_var) ⇒ Object

DELETE /vX/config/:project_name



45
46
47
48
49
50
51
52
53
54
# File 'lib/mortar/api/config.rb', line 45

def delete_config_var(project_name, config_var)
  body = {'key' => config_var}
  request(
    :expects => 200,
    :idempotent => true,
    :method  => :delete,
    :path    => versioned_path("/config/#{escape(project_name)}"),
    :body    => json_encode(body)
  )
end

#delete_project(project_name) ⇒ Object

DELETE /vX/projects/:project



67
68
69
70
71
72
73
# File 'lib/mortar/api/projects.rb', line 67

def delete_project(project_name)
  request(
    :expects => 200,
    :method => :delete,
    :path => versioned_path("/projects/#{project_name}")
  )
end

#get_clustersObject

GET /vX/clusters



31
32
33
34
35
36
37
38
# File 'lib/mortar/api/clusters.rb', line 31

def get_clusters()
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/clusters")
  )
end

#get_config_vars(project_name) ⇒ Object

GET /vX/config/:project_name



35
36
37
38
39
40
41
42
# File 'lib/mortar/api/config.rb', line 35

def get_config_vars(project_name)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/config/#{escape(project_name)}")
  )
end

#get_describe(describe_id, options = {}) ⇒ Object

GET /vX/describes/:describe



41
42
43
44
45
46
47
48
49
50
# File 'lib/mortar/api/describe.rb', line 41

def get_describe(describe_id, options = {})
  exclude_result = options[:exclude_result] || false
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/describes/#{describe_id}"),
    :query    => {:exclude_result => exclude_result}
  )
end

#get_illustrate(illustrate_id, options = {}) ⇒ Object

GET /vX/illustrates/:illustrate



47
48
49
50
51
52
53
54
55
56
# File 'lib/mortar/api/illustrate.rb', line 47

def get_illustrate(illustrate_id, options = {})
  exclude_result = options[:exclude_result] || false
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/illustrates/#{illustrate_id}"),
    :query    => {:exclude_result => exclude_result}
  )
end

#get_job(job_id) ⇒ Object

GET /vX/jobs/:job_id



180
181
182
183
184
185
186
187
# File 'lib/mortar/api/jobs.rb', line 180

def get_job(job_id)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/jobs/#{job_id}")
  )
end

#get_jobs(skip, limit, job_type = Jobs::JOB_TYPE_PIG) ⇒ Object

GET /vX/jobs



169
170
171
172
173
174
175
176
177
# File 'lib/mortar/api/jobs.rb', line 169

def get_jobs(skip, limit, job_type=Jobs::JOB_TYPE_PIG)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/jobs"),
    :query    => { job_type => job_type, :skip => skip, :limit => limit }
  )
end

#get_project(project_id) ⇒ Object

GET /vX/projects/:project



43
44
45
46
47
48
49
50
# File 'lib/mortar/api/projects.rb', line 43

def get_project(project_id)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/projects/#{project_id}")
  )
end

#get_projectsObject

GET /vX/projects



33
34
35
36
37
38
39
40
# File 'lib/mortar/api/projects.rb', line 33

def get_projects()
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/projects")
  )
end

#get_task(task_id) ⇒ Object

GET /vX/tasks/:task



30
31
32
33
34
35
36
37
# File 'lib/mortar/api/tasks.rb', line 30

def get_task(task_id)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/tasks/#{task_id}")
  )
end

#get_userObject

GET /v2/user



25
26
27
28
29
30
31
32
# File 'lib/mortar/api/user.rb', line 25

def get_user
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/user")
  )
end

#get_validate(validate_id, options = {}) ⇒ Object

GET /vX/validates/:validate



41
42
43
44
45
46
47
48
49
50
# File 'lib/mortar/api/validate.rb', line 41

def get_validate(validate_id, options = {})
  exclude_result = options[:exclude_result] || false
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :get,
    :path     => versioned_path("/validates/#{validate_id}"),
    :query    => {:exclude_result => exclude_result}
  )
end

#post_describe(project_name, pigscript, pigscript_alias, git_ref, options = {}) ⇒ Object

POST /vX/describes



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
# File 'lib/mortar/api/describe.rb', line 53

def post_describe(project_name, pigscript, pigscript_alias, git_ref, options = {})
  parameters = options[:parameters] || {}
  body = {"project_name" => project_name,
          "pigscript_name" => pigscript,
          "alias" => pigscript_alias,
          "git_ref" => git_ref,
          "parameters" => parameters
          }

  #If no pig_version is set, leave it to server to figure out version.
  unless options[:pig_version].nil?
    body["pig_version"] = options[:pig_version]
  end

  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/describes"),
    :body     => json_encode(body)
  )
end

#post_illustrate(project_name, pigscript, pigscript_alias, skip_pruning, git_ref, options = {}) ⇒ Object

POST /vX/illustrates



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
# File 'lib/mortar/api/illustrate.rb', line 59

def post_illustrate(project_name, pigscript, pigscript_alias, skip_pruning, git_ref, options = {})
  parameters = options[:parameters] || {}
  body = {"project_name" => project_name,
          "pigscript_name" => pigscript,
          "alias" => pigscript_alias,
          "skip_pruning" => skip_pruning,
          "git_ref" => git_ref,
          "parameters" => parameters
          }

  #If no pig_version is set, leave it to server to figure out version.
  unless options[:pig_version].nil?
    body["pig_version"] = options[:pig_version]
  end

  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/illustrates"),
    :body     => json_encode(body)
  )
end

#post_job_existing_cluster(project_name, script_name, git_ref, cluster_id, options = {}) ⇒ Object

DEPRECATED: Please use post_pig_job_existing_cluster instead.



200
201
202
203
# File 'lib/mortar/api/jobs.rb', line 200

def post_job_existing_cluster(project_name, script_name, git_ref, cluster_id, options={})
  warn "[DEPRECATION] 'post_job_existing_cluster' is deprecated.  Please use 'post_pig_job_existing_cluster' instead."
  post_pig_job_existing_cluster(project_name, script_name, git_ref, cluster_id, options=options)
end

#post_job_new_cluster(project_name, script_name, git_ref, cluster_size, options = {}) ⇒ Object

DEPRECATED: Please use post_pig_job_new_cluster instead.



206
207
208
209
# File 'lib/mortar/api/jobs.rb', line 206

def post_job_new_cluster(project_name, script_name, git_ref, cluster_size, options={})
  warn "[DEPRECATION] 'post_job_new_cluster' is deprecated.  Please use 'post_pig_job_new_cluster' instead."
  post_pig_job_new_cluster(project_name, script_name, git_ref, cluster_size, options=options)
end

#post_login(username, password) ⇒ Object

POST /v2/login



21
22
23
24
25
26
27
28
29
# File 'lib/mortar/api/login.rb', line 21

def (username, password)
  # reset authorization to use username/password basic auth
  @connection.data[:headers]['Authorization'] = basic_auth_authorization_header(username, password)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/login")
  )
end

#post_luigi_job(project_name, luigiscript_name, git_ref, options = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mortar/api/jobs.rb', line 149

def post_luigi_job(project_name, luigiscript_name, git_ref, options={})
  parameters = options[:parameters] || {}
  body = { "project_name" => project_name,
    "git_ref" => git_ref,
    "luigiscript_name" => luigiscript_name,
    "parameters" => parameters,
    "job_type" => Jobs::JOB_TYPE_LUIGI
  }
  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/jobs"),
    :body     => json_encode(body))
end

#post_pig_job_existing_cluster(project_name, script_name, git_ref, cluster_id, options = {}) ⇒ Object

POST /vX/jobs



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
# File 'lib/mortar/api/jobs.rb', line 75

def post_pig_job_existing_cluster(project_name, script_name, git_ref, cluster_id, options={})
  parameters = options[:parameters] || {}
  notify_on_job_finish = options[:notify_on_job_finish].nil? ? true : options[:notify_on_job_finish]
  is_control_script = options[:is_control_script] || false
  
  body = {"project_name" => project_name,
    "git_ref" => git_ref,
    "cluster_id" => cluster_id,
    "parameters" => parameters,
    "notify_on_job_finish" => notify_on_job_finish,
    "job_type" => Jobs::JOB_TYPE_PIG
  }
  
  if is_control_script
    body["controlscript_name"] = script_name
  else
    body["pigscript_name"] = script_name
  end

  #If no pig_version is set, leave it to server to figure out version.
  unless options[:pig_version].nil?
    body["pig_version"] = options[:pig_version]
  end

  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/jobs"),
    :body     => json_encode(body))
end

#post_pig_job_new_cluster(project_name, script_name, git_ref, cluster_size, options = {}) ⇒ Object

POST /vX/jobs



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
# File 'lib/mortar/api/jobs.rb', line 111

def post_pig_job_new_cluster(project_name, script_name, git_ref, cluster_size, options={})
  cluster_type = options[:cluster_type].nil? ? Jobs::CLUSTER_TYPE__PERSISTENT : options[:cluster_type]
  notify_on_job_finish = options[:notify_on_job_finish].nil? ? true : options[:notify_on_job_finish]
  use_spot_instances = options[:use_spot_instances].nil? ? false : options[:use_spot_instances]
  parameters = options[:parameters] || {}
  is_control_script = options[:is_control_script] || false

  body = { "project_name" => project_name,
    "git_ref" => git_ref,
    "cluster_size" => cluster_size,
    "cluster_type" => cluster_type,
    "parameters" => parameters,
    "notify_on_job_finish" => notify_on_job_finish,
    "job_type" => Jobs::JOB_TYPE_PIG,
    "use_spot_instances" => use_spot_instances
  }
  if is_control_script
    body["controlscript_name"] = script_name
  else
    body["pigscript_name"] = script_name
  end

  #If no pig_version is set, leave it to server to figure out version.
  unless options[:pig_version].nil?
    body["pig_version"] = options[:pig_version]
  end

  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/jobs"),
    :body     => json_encode(body))
end

#post_project(project_name, is_private = true) ⇒ Object

POST /vX/projects



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mortar/api/projects.rb', line 53

def post_project(project_name, is_private = true)
  payload = {
    "project_name" => project_name,
    "is_private" => is_private
  }
  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/projects"),
    :body     => json_encode(payload)
  )
end

#post_validate(project_name, pigscript, git_ref, options = {}) ⇒ Object

POST /vX/validates



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mortar/api/validate.rb', line 53

def post_validate(project_name, pigscript, git_ref, options = {})
  parameters = options[:parameters] || {}
  body = {"project_name" => project_name,
          "pigscript_name" => pigscript,
          "git_ref" => git_ref,
          "parameters" => parameters
         }

  #If no pig_version is set, leave it to server to figure out version.
  unless options[:pig_version].nil?
    body["pig_version"] = options[:pig_version]
  end

  unless options[:project_script_path].nil?
    body["project_script_path"] = options[:project_script_path]
  end

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/validates"),
    :body     => json_encode(body)
  )
end

#put_config_vars(project_name, config_vars) ⇒ Object

PUT /vX/config/:project_name



24
25
26
27
28
29
30
31
32
# File 'lib/mortar/api/config.rb', line 24

def put_config_vars(project_name, config_vars)
  request(
    :expects  => 200,
    :idempotent => true,
    :method   => :put,
    :path     => versioned_path("/config/#{escape(project_name)}"),
    :body     => json_encode(config_vars)
  )
end

#request(params, &block) ⇒ Object



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
# File 'lib/mortar/api.rb', line 80

def request(params, &block)
  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status
      when 400 then Mortar::API::Errors::BadRequest
      when 401 then Mortar::API::Errors::Unauthorized
      when 402 then Mortar::API::Errors::VerificationRequired
      when 403 then Mortar::API::Errors::Forbidden
      when 404 then Mortar::API::Errors::NotFound
      when 408 then Mortar::API::Errors::Timeout
      when 409 then Mortar::API::Errors::Conflict
      when 422 then Mortar::API::Errors::RequestFailed
      when 423 then Mortar::API::Errors::Locked
      when /50./ then Mortar::API::Errors::RequestFailed
      else Mortar::API::Errors::ErrorWithResponse
    end

    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    raise(reerror)
  end

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    begin
      response.body = Mortar::API::OkJson.decode(response.body)
    rescue
      # leave non-JSON body as is
    end
  end

  #Check if we got a redirect and treat it as an error.
  if response.body.include? "redirect"
    redirect_error = Mortar::API::Errors::Redirect.new(response.body["error"], response.body)
    raise(redirect_error)
  end

  # reset (non-persistent) connection
  @connection.reset

  response
end

#stop_cluster(cluster_id) ⇒ Object

DELETE /v2/clusters/:cluster_id



41
42
43
44
45
46
47
48
# File 'lib/mortar/api/clusters.rb', line 41

def stop_cluster(cluster_id)
  request(
    :expects => 200,
    :idempotent => true,
    :method  => :delete,
    :path    => versioned_path("/clusters/#{cluster_id}")
  )
end

#stop_job(job_id) ⇒ Object

DELETE /v2/jobs/:job_id



190
191
192
193
194
195
196
197
# File 'lib/mortar/api/jobs.rb', line 190

def stop_job(job_id)
  request(
    :expects => 200,
    :idempotent => true,
    :method  => :delete,
    :path    => versioned_path("/jobs/#{job_id}")
  )
end

#update_user(user_id, options = {}) ⇒ Object

PUT /v2/user/:user



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mortar/api/user.rb', line 35

def update_user(user_id, options={})
  update_params = {}
  options.each do |key, value|
    update_params[key.to_s()] = value
  end
  body = json_encode(update_params)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => versioned_path("/user/#{user_id}"),
    :body     => body
  )
end

#versioned_path(resource) ⇒ Object



126
127
128
129
# File 'lib/mortar/api.rb', line 126

def versioned_path(resource)
  no_slash_resource = resource.start_with?("/") ? resource[1,resource.size] : resource
  "/v#{SERVER_API_VERSION}/#{no_slash_resource}"
end