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/fixtures.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, Fixtures, Illustrate, Jobs, OkJson, Projects, Task, Validate

Constant Summary collapse

VERSION =

client version see semver.org/

"0.7.1"
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.



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

def initialize(options={})
  user = options.delete(:user)
  api_key = options.delete(:api_key) || ENV['MORTAR_API_KEY']
  options = {
    :headers  => {},
    :host     => 'api.mortardata.com',
    :scheme   => 'https'
  }.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.



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

def connection
  @connection
end

Instance Method Details

#delete_config_var(project_name, config_var) ⇒ Object

DELETE /vX/config/:project_name



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

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

#delete_project(project_name) ⇒ Object

DELETE /vX/projects/:project



61
62
63
64
65
66
67
# File 'lib/mortar/api/projects.rb', line 61

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

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

#get_config_vars(project_name) ⇒ Object

GET /vX/config/:project_name



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

def get_config_vars(project_name)
  request(
    :expects  => 200,
    :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
# File 'lib/mortar/api/describe.rb', line 41

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

#get_fixture(fixture_id) ⇒ Object

GET /vX/fixtures/:fixture



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

def get_fixture(fixture_id)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => versioned_path("/fixtures/#{fixture_id}")
  )
end

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

GET /vX/illustrates/:illustrate



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

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

#get_job(job_id) ⇒ Object

GET /vX/jobs/:job_id



131
132
133
134
135
136
137
# File 'lib/mortar/api/jobs.rb', line 131

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

#get_jobs(skip, limit) ⇒ Object

GET /vX/jobs



121
122
123
124
125
126
127
128
# File 'lib/mortar/api/jobs.rb', line 121

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

#get_project(project_id) ⇒ Object

GET /vX/projects/:project



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

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

#get_projectsObject

GET /vX/projects



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

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

#get_task(task_id) ⇒ Object

GET /vX/tasks/:task



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

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

#get_userObject

GET /v2/user



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

def get_user
  request(
    :expects  => 200,
    :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
# File 'lib/mortar/api/validate.rb', line 41

def get_validate(validate_id, options = {})
  exclude_result = options[:exclude_result] || false
  request(
    :expects  => 200,
    :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



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

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

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

#post_fixture_limit(project_name, fixture_name, input_url, num_rows) ⇒ Object

POST /vX/fixtures



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

def post_fixture_limit(project_name, fixture_name, input_url, num_rows)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/fixtures"),
    :body     => json_encode({"fixture_name" => fixture_name,
                              "project_name" => project_name,
                              "s3_url"       => input_url,
                              "num_rows"     => num_rows})
  )
end

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

POST /vX/illustrates



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

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

  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

POST /vX/jobs



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

def post_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
  }
  
  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

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

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

POST /vX/jobs



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

def post_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]
  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
  }
  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

  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/jobs"),
    :body     => json_encode(body))
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.connection[:headers]['Authorization'] = basic_auth_authorization_header(username, password)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => versioned_path("/login")
  )
end

#post_project(project_name) ⇒ Object

POST /vX/projects



51
52
53
54
55
56
57
58
# File 'lib/mortar/api/projects.rb', line 51

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

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

POST /vX/validates



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

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

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

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

#request(params, &block) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mortar/api.rb', line 79

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



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

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

#stop_job(job_id) ⇒ Object

DELETE /v2/jobs/:job_id



140
141
142
143
144
145
146
# File 'lib/mortar/api/jobs.rb', line 140

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

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

PUT /v2/user/:user



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

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



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

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