# Generated by API Builder - https://www.apibuilder.io # Service version: 2.0.0-dev # User agent: apibuilder app.apibuilder.io/hbc/svc-sundial/2.0.0-dev/ruby_client require 'cgi' require 'net/http' require 'net/https' require 'uri' require 'base64' require 'date' require 'rubygems' require 'json' require 'bigdecimal' # A service for scheduling and running Docker-based jobs module Com module Hbc module Svc module Sundial module V2 class Client module Constants NAMESPACE = 'com.hbc.svc.sundial.v2' unless defined?(Constants::NAMESPACE) USER_AGENT = 'apibuilder app.apibuilder.io/hbc/svc-sundial/2.0.0-dev/ruby_client' unless defined?(Constants::USER_AGENT) VERSION = '2.0.0-dev' unless defined?(Constants::VERSION) VERSION_MAJOR = 2 unless defined?(VERSION_MAJOR) end attr_reader :url def initialize(url, opts={}) @url = HttpClient::Preconditions.assert_class('url', url, String) @base_url = URI(url) @authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization) @default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash) @http_handler = opts.delete(:http_handler) || HttpClient::DefaultHttpHandler.new HttpClient::Preconditions.assert_empty_opts(opts) HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url) end def request(path=nil) HttpClient::Preconditions.assert_class_or_nil('path', path, String) request = HttpClient::Request.new(@http_handler, @base_url, path.to_s).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR) @default_headers.each do |key, value| request = request.with_header(key, value) end if @authorization request = request.with_auth(@authorization) end request end def healthchecks @healthchecks ||= ::Com::Hbc::Svc::Sundial::V2::Clients::Healthchecks.new(self) end def processes @processes ||= ::Com::Hbc::Svc::Sundial::V2::Clients::Processes.new(self) end def process_definitions @process_definitions ||= ::Com::Hbc::Svc::Sundial::V2::Clients::ProcessDefinitions.new(self) end def tasks @tasks ||= ::Com::Hbc::Svc::Sundial::V2::Clients::Tasks.new(self) end end module Clients class Healthchecks def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Com::Hbc::Svc::Sundial::V2::Client) end def get_metrics r = @client.request("/_internal_/metrics").get r end end class Processes def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Com::Hbc::Svc::Sundial::V2::Client) end # Retrieves a list of process runs based on search parameters. def get(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :process_definition_name => (x = opts.delete(:process_definition_name); x.nil? ? nil : HttpClient::Preconditions.assert_class('process_definition_name', x, String)), :start_time => (x = opts.delete(:start_time); x.nil? ? nil : HttpClient::Preconditions.assert_class('start_time', HttpClient::Helper.to_date_time_iso8601(x), DateTime)), :end_time => (x = opts.delete(:end_time); x.nil? ? nil : HttpClient::Preconditions.assert_class('end_time', HttpClient::Helper.to_date_time_iso8601(x), DateTime)), :max_records => (x = opts.delete(:max_records); x.nil? ? nil : HttpClient::Preconditions.assert_class('max_records', x, Integer)), :allowed_statuses => HttpClient::Preconditions.assert_class('allowed_statuses', opts.delete(:allowed_statuses), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::ProcessStatus) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::ProcessStatus.apply(x)).value } }.delete_if { |k, v| v.nil? } r = @client.request("/api/processes").with_query(query).get r.map { |x| ::Com::Hbc::Svc::Sundial::V2::Models::Process.new(x) } end # Retrieves a process run def get_by_process_id(process_id) HttpClient::Preconditions.assert_class('process_id', HttpClient::Helper.to_uuid(process_id), String) r = @client.request("/api/processes/#{process_id}").get ::Com::Hbc::Svc::Sundial::V2::Models::Process.new(r) end # Retries a failed process run by restarting failed tasks; tasks will be given # a single additional attempt def post_retry_by_process_id(process_id) HttpClient::Preconditions.assert_class('process_id', HttpClient::Helper.to_uuid(process_id), String) r = @client.request("/api/processes/#{process_id}/retry").post nil end # Terminates an active process def post_kill_by_process_id(process_id) HttpClient::Preconditions.assert_class('process_id', HttpClient::Helper.to_uuid(process_id), String) r = @client.request("/api/processes/#{process_id}/kill").post nil end end class ProcessDefinitions def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Com::Hbc::Svc::Sundial::V2::Client) end # Get all currently registered process definitions def get r = @client.request("/api/process_definitions").get r.map { |x| ::Com::Hbc::Svc::Sundial::V2::Models::ProcessDefinition.new(x) } end # Get a registered process definition def get_by_process_definition_name(process_definition_name) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}").get ::Com::Hbc::Svc::Sundial::V2::Models::ProcessDefinition.new(r) end # Updates or creates a process definition def put_by_process_definition_name(process_definition_name, process_definition) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) (x = process_definition; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::ProcessDefinition) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::ProcessDefinition.new(x)) r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}").with_json(process_definition.to_json).put ::Com::Hbc::Svc::Sundial::V2::Models::ProcessDefinition.new(r) end # Deletes a registered process definition def delete_by_process_definition_name(process_definition_name) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}").delete nil end # Triggers a new instance of the process def post_trigger_by_process_definition_name(process_definition_name, incoming={}) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :task_definition_name => (x = opts.delete(:task_definition_name); x.nil? ? nil : HttpClient::Preconditions.assert_class('task_definition_name', x, String)) }.delete_if { |k, v| v.nil? } r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}/trigger").with_query(query).post nil end # Pause the process schedule def post_pause_by_process_definition_name(process_definition_name) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}/pause").post nil end # Resume the process schedule def post_resume_by_process_definition_name(process_definition_name) HttpClient::Preconditions.assert_class('process_definition_name', process_definition_name, String) r = @client.request("/api/process_definitions/#{CGI.escape(process_definition_name)}/resume").post nil end end class Tasks def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Com::Hbc::Svc::Sundial::V2::Client) end # Retrieves the most recent tasks meeting the given criteria def get(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :process_definition_name => HttpClient::Preconditions.assert_class('process_definition_name', opts.delete(:process_definition_name), String), :task_definition_name => HttpClient::Preconditions.assert_class('task_definition_name', opts.delete(:task_definition_name), String), :allowed_statuses => HttpClient::Preconditions.assert_class('allowed_statuses', opts.delete(:allowed_statuses), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::TaskStatus) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::TaskStatus.apply(x)).value }, :start_time => (x = opts.delete(:start_time); x.nil? ? nil : HttpClient::Preconditions.assert_class('start_time', HttpClient::Helper.to_date_time_iso8601(x), DateTime)), :end_time => (x = opts.delete(:end_time); x.nil? ? nil : HttpClient::Preconditions.assert_class('end_time', HttpClient::Helper.to_date_time_iso8601(x), DateTime)), :limit => (x = opts.delete(:limit); x.nil? ? nil : HttpClient::Preconditions.assert_class('limit', x, Integer)) }.delete_if { |k, v| v.nil? } r = @client.request("/api/tasks").with_query(query).get r.map { |x| ::Com::Hbc::Svc::Sundial::V2::Models::Task.new(x) } end # Appends log entries for a task; intended for use within the task executable def post_log_entries_by_task_id(task_id, log_entries) HttpClient::Preconditions.assert_class('task_id', HttpClient::Helper.to_uuid(task_id), String) HttpClient::Preconditions.assert_class('log_entries', log_entries, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::LogEntry) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::LogEntry.new(x)) } r = @client.request("/api/tasks/#{task_id}/log_entries").with_json(log_entries.map { |o| o.to_hash }.to_json).post nil end # Appends metadata entries for a task; intended for use within the task # executable def post_metadata_by_task_id(task_id, metadata_entries) HttpClient::Preconditions.assert_class('task_id', HttpClient::Helper.to_uuid(task_id), String) HttpClient::Preconditions.assert_class('metadata_entries', metadata_entries, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry.new(x)) } r = @client.request("/api/tasks/#{task_id}/metadata").with_json(metadata_entries.map { |o| o.to_hash }.to_json).post nil end # Marks the task as having succeeded def post_succeed_by_task_id(task_id) HttpClient::Preconditions.assert_class('task_id', HttpClient::Helper.to_uuid(task_id), String) r = @client.request("/api/tasks/#{task_id}/succeed").post nil end # Marks the task as having failed def post_fail_by_task_id(task_id) HttpClient::Preconditions.assert_class('task_id', HttpClient::Helper.to_uuid(task_id), String) r = @client.request("/api/tasks/#{task_id}/fail").post nil end end end module Models # The EC2 market of the EC2 instances class AwsMarket module Types ON_DEMAND = 'on_demand' unless defined?(ON_DEMAND) SPOT = 'spot' unless defined?(SPOT) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'aws_market' end def subtype_to_hash raise 'Cannot serialize an instance of aws_market directly - must use one of the specific types: on_demand, spot' end def to_hash { @__discriminator__ => subtype_to_hash } end def AwsMarket.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::ON_DEMAND; OnDemand.new(data) when Types::SPOT; Spot.new(data) else AwsMarketUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class AwsMarketUndefinedType < AwsMarket attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end # Whether or not to create a new EMR cluster or re-use a pre-existing one class EmrCluster module Types NEW_EMR_CLUSTER = 'new_emr_cluster' unless defined?(NEW_EMR_CLUSTER) EXISTING_EMR_CLUSTER = 'existing_emr_cluster' unless defined?(EXISTING_EMR_CLUSTER) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'emr_cluster' end def subtype_to_hash raise 'Cannot serialize an instance of emr_cluster directly - must use one of the specific types: new_emr_cluster, existing_emr_cluster' end def to_hash { @__discriminator__ => subtype_to_hash } end def EmrCluster.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::NEW_EMR_CLUSTER; NewEmrCluster.new(data) when Types::EXISTING_EMR_CLUSTER; ExistingEmrCluster.new(data) else EmrClusterUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class EmrClusterUndefinedType < EmrCluster attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end # The EMR role to use on the EC2 instances class EmrJobFlowRole module Types DEFAULT_EMR_JOB_FLOW_ROLE = 'default_emr_job_flow_role' unless defined?(DEFAULT_EMR_JOB_FLOW_ROLE) CUSTOM_EMR_JOB_FLOW_ROLE = 'custom_emr_job_flow_role' unless defined?(CUSTOM_EMR_JOB_FLOW_ROLE) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'emr_job_flow_role' end def subtype_to_hash raise 'Cannot serialize an instance of emr_job_flow_role directly - must use one of the specific types: default_emr_job_flow_role, custom_emr_job_flow_role' end def to_hash { @__discriminator__ => subtype_to_hash } end def EmrJobFlowRole.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::DEFAULT_EMR_JOB_FLOW_ROLE; DefaultEmrJobFlowRole.new(data) when Types::CUSTOM_EMR_JOB_FLOW_ROLE; CustomEmrJobFlowRole.new(data) else EmrJobFlowRoleUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class EmrJobFlowRoleUndefinedType < EmrJobFlowRole attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end # The EMR service role to use class EmrServiceRole module Types DEFAULT_EMR_SERVICE_ROLE = 'default_emr_service_role' unless defined?(DEFAULT_EMR_SERVICE_ROLE) CUSTOM_EMR_SERVICE_ROLE = 'custom_emr_service_role' unless defined?(CUSTOM_EMR_SERVICE_ROLE) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'emr_service_role' end def subtype_to_hash raise 'Cannot serialize an instance of emr_service_role directly - must use one of the specific types: default_emr_service_role, custom_emr_service_role' end def to_hash { @__discriminator__ => subtype_to_hash } end def EmrServiceRole.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::DEFAULT_EMR_SERVICE_ROLE; DefaultEmrServiceRole.new(data) when Types::CUSTOM_EMR_SERVICE_ROLE; CustomEmrServiceRole.new(data) else EmrServiceRoleUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class EmrServiceRoleUndefinedType < EmrServiceRole attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end class Notification module Types EMAIL = 'email' unless defined?(EMAIL) PAGERDUTY = 'pagerduty' unless defined?(PAGERDUTY) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'notification' end def subtype_to_hash raise 'Cannot serialize an instance of notification directly - must use one of the specific types: email, pagerduty' end def to_hash { @__discriminator__ => subtype_to_hash } end def Notification.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::EMAIL; Email.new(data) when Types::PAGERDUTY; Pagerduty.new(data) else NotificationUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class NotificationUndefinedType < Notification attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end # A specification for when a process should be run class ProcessSchedule module Types CRON_SCHEDULE = 'cron_schedule' unless defined?(CRON_SCHEDULE) CONTINUOUS_SCHEDULE = 'continuous_schedule' unless defined?(CONTINUOUS_SCHEDULE) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'process_schedule' end def subtype_to_hash raise 'Cannot serialize an instance of process_schedule directly - must use one of the specific types: cron_schedule, continuous_schedule' end def to_hash { @__discriminator__ => subtype_to_hash } end def ProcessSchedule.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::CRON_SCHEDULE; CronSchedule.new(data) when Types::CONTINUOUS_SCHEDULE; ContinuousSchedule.new(data) else ProcessScheduleUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class ProcessScheduleUndefinedType < ProcessSchedule attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end class TaskExecutable module Types # Shell command to run on Sundial service instance (Experimental) SHELL_SCRIPT_COMMAND = 'shell_script_command' unless defined?(SHELL_SCRIPT_COMMAND) # Docker image to run on AWS Batch BATCH_IMAGE_COMMAND = 'batch_image_command' unless defined?(BATCH_IMAGE_COMMAND) # Command to submit Spark Jobs on AWS EMR EMR_COMMAND = 'emr_command' unless defined?(EMR_COMMAND) end attr_reader :__discriminator__ def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @__discriminator__ = opts[:__discriminator__] ||'task_executable' end def subtype_to_hash raise 'Cannot serialize an instance of task_executable directly - must use one of the specific types: shell_script_command, batch_image_command, emr_command' end def to_hash { @__discriminator__ => subtype_to_hash } end def TaskExecutable.from_json(hash) HttpClient::Preconditions.assert_class('hash', hash, Hash) hash.map do |union_type_name, data| case union_type_name when Types::SHELL_SCRIPT_COMMAND; ShellScriptCommand.new(data) when Types::BATCH_IMAGE_COMMAND; BatchImageCommand.new(data) when Types::EMR_COMMAND; EmrCommand.new(data) else TaskExecutableUndefinedType.new(:__discriminator__ => union_type_name) end end.first end end class TaskExecutableUndefinedType < TaskExecutable attr_reader :name def initialize(incoming={}) super(:__discriminator__ => 'undefined_type') opts = HttpClient::Helper.symbolize_keys(incoming) @name = HttpClient::Preconditions.assert_class('name', opts.delete(:__discriminator__), String) end def subtype_to_hash raise 'Unable to serialize undefined type to json' end def copy(incoming={}) raise 'Operation not supported for undefined type' end def to_hash raise 'Operation not supported for undefined type' end end class DefaultEmrJobFlowRole < EmrJobFlowRole attr_reader :value def initialize(value) super(:name => EmrJobFlowRole::Types::DEFAULT_EMR_JOB_FLOW_ROLE, :__discriminator__ => 'default_emr_job_flow_role') @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of DefaultEmrJobFlowRole for this value, creating a new instance for an unknown value def DefaultEmrJobFlowRole.apply(value) if value.instance_of?(DefaultEmrJobFlowRole) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || DefaultEmrJobFlowRole.new(value)) end end # Returns the instance of DefaultEmrJobFlowRole for this value, or nil if not found def DefaultEmrJobFlowRole.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) DefaultEmrJobFlowRole.ALL.find { |v| v.value == value } end def DefaultEmrJobFlowRole.ALL @@all ||= [DefaultEmrJobFlowRole.default_emr_job_flow_role] end def DefaultEmrJobFlowRole.default_emr_job_flow_role @@_default_emr_job_flow_role ||= DefaultEmrJobFlowRole.new('EMR_EC2_DefaultRole') end def subtype_to_hash value end end class DefaultEmrServiceRole < EmrServiceRole attr_reader :value def initialize(value) super(:name => EmrServiceRole::Types::DEFAULT_EMR_SERVICE_ROLE, :__discriminator__ => 'default_emr_service_role') @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of DefaultEmrServiceRole for this value, creating a new instance for an unknown value def DefaultEmrServiceRole.apply(value) if value.instance_of?(DefaultEmrServiceRole) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || DefaultEmrServiceRole.new(value)) end end # Returns the instance of DefaultEmrServiceRole for this value, or nil if not found def DefaultEmrServiceRole.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) DefaultEmrServiceRole.ALL.find { |v| v.value == value } end def DefaultEmrServiceRole.ALL @@all ||= [DefaultEmrServiceRole.default_emr_service_role] end def DefaultEmrServiceRole.default_emr_service_role @@_default_emr_service_role ||= DefaultEmrServiceRole.new('EMR_DefaultRole') end def subtype_to_hash value end end class EmrApplication attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of EmrApplication for this value, creating a new instance for an unknown value def EmrApplication.apply(value) if value.instance_of?(EmrApplication) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || EmrApplication.new(value)) end end # Returns the instance of EmrApplication for this value, or nil if not found def EmrApplication.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) EmrApplication.ALL.find { |v| v.value == value } end def EmrApplication.ALL @@all ||= [EmrApplication.hadoop, EmrApplication.hive, EmrApplication.mahout, EmrApplication.pig, EmrApplication.spark, EmrApplication.ganglia] end def EmrApplication.hadoop @@_hadoop ||= EmrApplication.new('Hadoop') end def EmrApplication.hive @@_hive ||= EmrApplication.new('Hive') end def EmrApplication.mahout @@_mahout ||= EmrApplication.new('Mahout') end def EmrApplication.pig @@_pig ||= EmrApplication.new('Pig') end def EmrApplication.spark @@_spark ||= EmrApplication.new('Spark') end def EmrApplication.ganglia @@_ganglia ||= EmrApplication.new('Ganglia') end def to_hash value end end class EmrReleaseLabel attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of EmrReleaseLabel for this value, creating a new instance for an unknown value def EmrReleaseLabel.apply(value) if value.instance_of?(EmrReleaseLabel) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || EmrReleaseLabel.new(value)) end end # Returns the instance of EmrReleaseLabel for this value, or nil if not found def EmrReleaseLabel.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) EmrReleaseLabel.ALL.find { |v| v.value == value } end def EmrReleaseLabel.ALL @@all ||= [EmrReleaseLabel.emr_5_20_0, EmrReleaseLabel.emr_5_19_0, EmrReleaseLabel.emr_5_18_0, EmrReleaseLabel.emr_5_17_0, EmrReleaseLabel.emr_5_16_0, EmrReleaseLabel.emr_5_15_0, EmrReleaseLabel.emr_5_14_0, EmrReleaseLabel.emr_5_13_0, EmrReleaseLabel.emr_5_12_1, EmrReleaseLabel.emr_5_12_0, EmrReleaseLabel.emr_5_11_1, EmrReleaseLabel.emr_5_11_0, EmrReleaseLabel.emr_5_10_0, EmrReleaseLabel.emr_5_9_0, EmrReleaseLabel.emr_4_9_2, EmrReleaseLabel.emr_4_9_1] end def EmrReleaseLabel.emr_5_20_0 @@_emr_5_20_0 ||= EmrReleaseLabel.new('emr-5.20.0') end def EmrReleaseLabel.emr_5_19_0 @@_emr_5_19_0 ||= EmrReleaseLabel.new('emr-5.19.0') end def EmrReleaseLabel.emr_5_18_0 @@_emr_5_18_0 ||= EmrReleaseLabel.new('emr-5.18.0') end def EmrReleaseLabel.emr_5_17_0 @@_emr_5_17_0 ||= EmrReleaseLabel.new('emr-5.17.0') end def EmrReleaseLabel.emr_5_16_0 @@_emr_5_16_0 ||= EmrReleaseLabel.new('emr-5.16.0') end def EmrReleaseLabel.emr_5_15_0 @@_emr_5_15_0 ||= EmrReleaseLabel.new('emr-5.15.0') end def EmrReleaseLabel.emr_5_14_0 @@_emr_5_14_0 ||= EmrReleaseLabel.new('emr-5.14.0') end def EmrReleaseLabel.emr_5_13_0 @@_emr_5_13_0 ||= EmrReleaseLabel.new('emr-5.13.0') end def EmrReleaseLabel.emr_5_12_1 @@_emr_5_12_1 ||= EmrReleaseLabel.new('emr-5.12.1') end def EmrReleaseLabel.emr_5_12_0 @@_emr_5_12_0 ||= EmrReleaseLabel.new('emr-5.12.0') end def EmrReleaseLabel.emr_5_11_1 @@_emr_5_11_1 ||= EmrReleaseLabel.new('emr-5.11.1') end def EmrReleaseLabel.emr_5_11_0 @@_emr_5_11_0 ||= EmrReleaseLabel.new('emr-5.11.0') end def EmrReleaseLabel.emr_5_10_0 @@_emr_5_10_0 ||= EmrReleaseLabel.new('emr-5.10.0') end def EmrReleaseLabel.emr_5_9_0 @@_emr_5_9_0 ||= EmrReleaseLabel.new('emr-5.9.0') end def EmrReleaseLabel.emr_4_9_2 @@_emr_4_9_2 ||= EmrReleaseLabel.new('emr-4.9.2') end def EmrReleaseLabel.emr_4_9_1 @@_emr_4_9_1 ||= EmrReleaseLabel.new('emr-4.9.1') end def to_hash value end end class NotificationOptions attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of NotificationOptions for this value, creating a new instance for an unknown value def NotificationOptions.apply(value) if value.instance_of?(NotificationOptions) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || NotificationOptions.new(value)) end end # Returns the instance of NotificationOptions for this value, or nil if not found def NotificationOptions.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) NotificationOptions.ALL.find { |v| v.value == value } end def NotificationOptions.ALL @@all ||= [NotificationOptions.always, NotificationOptions.on_failure, NotificationOptions.on_state_change, NotificationOptions.on_state_change_and_failures, NotificationOptions.never] end # Always notify when a process completes def NotificationOptions.always @@_always ||= NotificationOptions.new('always') end # Notify when a process fails def NotificationOptions.on_failure @@_on_failure ||= NotificationOptions.new('on_failure') end # Notify when a process goes from succeeding to failing and vica versa def NotificationOptions.on_state_change @@_on_state_change ||= NotificationOptions.new('on_state_change') end # Notify when going from failing to succeeded and on each failure def NotificationOptions.on_state_change_and_failures @@_on_state_change_and_failures ||= NotificationOptions.new('on_state_change_and_failures') end # Never notify def NotificationOptions.never @@_never ||= NotificationOptions.new('never') end def to_hash value end end class OnDemand < AwsMarket attr_reader :value def initialize(value) super(:name => AwsMarket::Types::ON_DEMAND, :__discriminator__ => 'on_demand') @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of OnDemand for this value, creating a new instance for an unknown value def OnDemand.apply(value) if value.instance_of?(OnDemand) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || OnDemand.new(value)) end end # Returns the instance of OnDemand for this value, or nil if not found def OnDemand.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) OnDemand.ALL.find { |v| v.value == value } end def OnDemand.ALL @@all ||= [OnDemand.on_demand] end def OnDemand.on_demand @@_on_demand ||= OnDemand.new('on_demand') end def subtype_to_hash value end end class ProcessOverlapAction attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of ProcessOverlapAction for this value, creating a new instance for an unknown value def ProcessOverlapAction.apply(value) if value.instance_of?(ProcessOverlapAction) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || ProcessOverlapAction.new(value)) end end # Returns the instance of ProcessOverlapAction for this value, or nil if not found def ProcessOverlapAction.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) ProcessOverlapAction.ALL.find { |v| v.value == value } end def ProcessOverlapAction.ALL @@all ||= [ProcessOverlapAction.wait, ProcessOverlapAction.terminate] end # The process should wait until the currently running instance finishes def ProcessOverlapAction.wait @@_wait ||= ProcessOverlapAction.new('wait') end # The currently running process should be killed def ProcessOverlapAction.terminate @@_terminate ||= ProcessOverlapAction.new('terminate') end def to_hash value end end class ProcessStatus attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of ProcessStatus for this value, creating a new instance for an unknown value def ProcessStatus.apply(value) if value.instance_of?(ProcessStatus) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || ProcessStatus.new(value)) end end # Returns the instance of ProcessStatus for this value, or nil if not found def ProcessStatus.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) ProcessStatus.ALL.find { |v| v.value == value } end def ProcessStatus.ALL @@all ||= [ProcessStatus.running, ProcessStatus.succeeded, ProcessStatus.failed] end # The process has tasks currently executing def ProcessStatus.running @@_running ||= ProcessStatus.new('running') end # All of the process's tasks succeeded on its last run def ProcessStatus.succeeded @@_succeeded ||= ProcessStatus.new('succeeded') end # At least one of the process's tasks failed on its last run def ProcessStatus.failed @@_failed ||= ProcessStatus.new('failed') end def to_hash value end end class TaskStatus attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of TaskStatus for this value, creating a new instance for an unknown value def TaskStatus.apply(value) if value.instance_of?(TaskStatus) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || TaskStatus.new(value)) end end # Returns the instance of TaskStatus for this value, or nil if not found def TaskStatus.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) TaskStatus.ALL.find { |v| v.value == value } end def TaskStatus.ALL @@all ||= [TaskStatus.submitted, TaskStatus.runnable, TaskStatus.starting, TaskStatus.pending, TaskStatus.running, TaskStatus.failed, TaskStatus.succeeded] end # The task has been submitted def TaskStatus.submitted @@_submitted ||= TaskStatus.new('submitted') end def TaskStatus.runnable @@_runnable ||= TaskStatus.new('runnable') end def TaskStatus.starting @@_starting ||= TaskStatus.new('starting') end # The task is waiting on compute resources def TaskStatus.pending @@_pending ||= TaskStatus.new('pending') end # The task is currently executing or awaiting backoff def TaskStatus.running @@_running ||= TaskStatus.new('running') end # The task has irrevocably failed def TaskStatus.failed @@_failed ||= TaskStatus.new('failed') end # The task has succeeded without serious errors def TaskStatus.succeeded @@_succeeded ||= TaskStatus.new('succeeded') end def to_hash value end end class BatchImageCommand < TaskExecutable attr_reader :image, :tag, :command, :memory, :vCpus, :job_role_arn, :environment_variables, :job_queue def initialize(incoming={}) super(:__discriminator__ => TaskExecutable::Types::BATCH_IMAGE_COMMAND) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:image, :command, :memory, :vCpus], 'BatchImageCommand') @image = HttpClient::Preconditions.assert_class('image', opts.delete(:image), String) @tag = HttpClient::Preconditions.assert_class('tag', (x = opts.delete(:tag); x.nil? ? "latest" : x), String) @command = HttpClient::Preconditions.assert_class('command', opts.delete(:command), Array).map { |v| HttpClient::Preconditions.assert_class('command', v, String) } @memory = HttpClient::Preconditions.assert_class('memory', opts.delete(:memory), Integer) @vCpus = HttpClient::Preconditions.assert_class('vCpus', opts.delete(:vCpus), Integer) @job_role_arn = (x = opts.delete(:job_role_arn); x.nil? ? nil : HttpClient::Preconditions.assert_class('job_role_arn', x, String)) @environment_variables = HttpClient::Preconditions.assert_class('environment_variables', (x = opts.delete(:environment_variables); x.nil? ? [] : x), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EnvironmentVariable) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EnvironmentVariable.new(x)) } @job_queue = (x = opts.delete(:job_queue); x.nil? ? nil : HttpClient::Preconditions.assert_class('job_queue', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) BatchImageCommand.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :image => image, :tag => tag, :command => command, :memory => memory, :vCpus => vCpus, :job_role_arn => job_role_arn, :environment_variables => environment_variables.map { |o| o.to_hash }, :job_queue => job_queue } end end class ContinuousSchedule < ProcessSchedule attr_reader :buffer_seconds def initialize(incoming={}) super(:__discriminator__ => ProcessSchedule::Types::CONTINUOUS_SCHEDULE) opts = HttpClient::Helper.symbolize_keys(incoming) @buffer_seconds = (x = opts.delete(:buffer_seconds); x.nil? ? nil : HttpClient::Preconditions.assert_class('buffer_seconds', x, Integer)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) ContinuousSchedule.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :buffer_seconds => buffer_seconds } end end # See http://quartz-scheduler.org/api/2.2.0/org/quartz/CronExpression.html class CronSchedule < ProcessSchedule attr_reader :day_of_week, :month, :day_of_month, :hours, :minutes def initialize(incoming={}) super(:__discriminator__ => ProcessSchedule::Types::CRON_SCHEDULE) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:day_of_week, :month, :day_of_month, :hours, :minutes], 'CronSchedule') @day_of_week = HttpClient::Preconditions.assert_class('day_of_week', opts.delete(:day_of_week), String) @month = HttpClient::Preconditions.assert_class('month', opts.delete(:month), String) @day_of_month = HttpClient::Preconditions.assert_class('day_of_month', opts.delete(:day_of_month), String) @hours = HttpClient::Preconditions.assert_class('hours', opts.delete(:hours), String) @minutes = HttpClient::Preconditions.assert_class('minutes', opts.delete(:minutes), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) CronSchedule.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :day_of_week => day_of_week, :month => month, :day_of_month => day_of_month, :hours => hours, :minutes => minutes } end end # Custom, preconfigured, EMR EC2 instance role class CustomEmrJobFlowRole < EmrJobFlowRole attr_reader :role_name def initialize(incoming={}) super(:__discriminator__ => EmrJobFlowRole::Types::CUSTOM_EMR_JOB_FLOW_ROLE) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:role_name], 'CustomEmrJobFlowRole') @role_name = HttpClient::Preconditions.assert_class('role_name', opts.delete(:role_name), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) CustomEmrJobFlowRole.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :role_name => role_name } end end # Custom, preconfigured, EMR Service role class CustomEmrServiceRole < EmrServiceRole attr_reader :role_name def initialize(incoming={}) super(:__discriminator__ => EmrServiceRole::Types::CUSTOM_EMR_SERVICE_ROLE) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:role_name], 'CustomEmrServiceRole') @role_name = HttpClient::Preconditions.assert_class('role_name', opts.delete(:role_name), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) CustomEmrServiceRole.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :role_name => role_name } end end # An email to send notifications to class Email < Notification attr_reader :name, :email, :notify_when def initialize(incoming={}) super(:__discriminator__ => Notification::Types::EMAIL) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:name, :email], 'Email') @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String) @email = HttpClient::Preconditions.assert_class('email', opts.delete(:email), String) @notify_when = (x = (x = opts.delete(:notify_when); x.nil? ? "on_state_change_and_failures" : x); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::NotificationOptions) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::NotificationOptions.apply(x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Email.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :name => name, :email => email, :notify_when => notify_when.value } end end class EmrCommand < TaskExecutable attr_reader :emr_cluster, :job_name, :region, :class_, :s3_jar_path, :spark_conf, :spark_packages, :args, :s3_log_details, :load_data, :save_results def initialize(incoming={}) super(:__discriminator__ => TaskExecutable::Types::EMR_COMMAND) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:emr_cluster, :job_name, :class, :s3_jar_path, :spark_conf, :args], 'EmrCommand') @emr_cluster = (x = opts.delete(:emr_cluster); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrCluster) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrCluster.from_json(x)) @job_name = HttpClient::Preconditions.assert_class('job_name', opts.delete(:job_name), String) @region = HttpClient::Preconditions.assert_class('region', (x = opts.delete(:region); x.nil? ? "us-east-1" : x), String) @class_ = HttpClient::Preconditions.assert_class('class', opts.delete(:class), String) @s3_jar_path = HttpClient::Preconditions.assert_class('s3_jar_path', opts.delete(:s3_jar_path), String) @spark_conf = HttpClient::Preconditions.assert_class('spark_conf', opts.delete(:spark_conf), Array).map { |v| HttpClient::Preconditions.assert_class('spark_conf', v, String) } @spark_packages = HttpClient::Preconditions.assert_class('spark_packages', (x = opts.delete(:spark_packages); x.nil? ? [] : x), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::MavenPackage) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::MavenPackage.new(x)) } @args = HttpClient::Preconditions.assert_class('args', opts.delete(:args), Array).map { |v| HttpClient::Preconditions.assert_class('args', v, String) } @s3_log_details = (x = opts.delete(:s3_log_details); x.nil? ? nil : (x = x; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::S3LogDetails) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::S3LogDetails.new(x))) @load_data = (x = opts.delete(:load_data); x.nil? ? nil : HttpClient::Preconditions.assert_class('load_data', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::S3Cp) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::S3Cp.new(x)) }) @save_results = (x = opts.delete(:save_results); x.nil? ? nil : HttpClient::Preconditions.assert_class('save_results', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::S3Cp) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::S3Cp.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) EmrCommand.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :emr_cluster => emr_cluster.to_hash, :job_name => job_name, :region => region, :class => class_, :s3_jar_path => s3_jar_path, :spark_conf => spark_conf, :spark_packages => spark_packages.map { |o| o.to_hash }, :args => args, :s3_log_details => s3_log_details.nil? ? nil : s3_log_details.to_hash, :load_data => load_data.nil? ? nil : load_data.map { |o| o.to_hash }, :save_results => save_results.nil? ? nil : save_results.map { |o| o.to_hash } } end end # See # https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-configure-apps.html class EmrConfiguration attr_reader :classification, :configurations, :properties def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @classification = (x = opts.delete(:classification); x.nil? ? nil : HttpClient::Preconditions.assert_class('classification', x, String)) @configurations = (x = opts.delete(:configurations); x.nil? ? nil : HttpClient::Preconditions.assert_class('configurations', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrConfiguration) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrConfiguration.new(x)) }) @properties = (x = opts.delete(:properties); x.nil? ? nil : HttpClient::Preconditions.assert_class('properties', x, Hash).inject({}) { |h, d| h[d[0]] = HttpClient::Preconditions.assert_class('properties', d[1], String); h }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) EmrConfiguration.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :classification => classification, :configurations => configurations.nil? ? nil : configurations.map { |o| o.to_hash }, :properties => properties.nil? ? nil : properties } end end # Additional configuration for Master/Core/Task EMR instance Groups. class EmrInstanceGroupDetails attr_reader :emr_instance_type, :instance_count, :aws_market, :ebs_volume_size def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:emr_instance_type, :instance_count, :aws_market], 'EmrInstanceGroupDetails') @emr_instance_type = HttpClient::Preconditions.assert_class('emr_instance_type', opts.delete(:emr_instance_type), String) @instance_count = HttpClient::Preconditions.assert_class('instance_count', opts.delete(:instance_count), Integer) @aws_market = (x = opts.delete(:aws_market); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::AwsMarket) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::AwsMarket.from_json(x)) @ebs_volume_size = (x = opts.delete(:ebs_volume_size); x.nil? ? nil : HttpClient::Preconditions.assert_class('ebs_volume_size', x, Integer)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) EmrInstanceGroupDetails.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :emr_instance_type => emr_instance_type, :instance_count => instance_count, :aws_market => aws_market.to_hash, :ebs_volume_size => ebs_volume_size } end end class EnvironmentVariable attr_reader :variable_name, :value def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:variable_name, :value], 'EnvironmentVariable') @variable_name = HttpClient::Preconditions.assert_class('variable_name', opts.delete(:variable_name), String) @value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) EnvironmentVariable.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :variable_name => variable_name, :value => value } end end # Existing EMR cluster configuration object class ExistingEmrCluster < EmrCluster attr_reader :cluster_id def initialize(incoming={}) super(:__discriminator__ => EmrCluster::Types::EXISTING_EMR_CLUSTER) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:cluster_id], 'ExistingEmrCluster') @cluster_id = HttpClient::Preconditions.assert_class('cluster_id', opts.delete(:cluster_id), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) ExistingEmrCluster.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :cluster_id => cluster_id } end end class Healthcheck attr_reader :status def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:status], 'Healthcheck') @status = HttpClient::Preconditions.assert_class('status', opts.delete(:status), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Healthcheck.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :status => status } end end class LogEntry attr_reader :log_entry_id, :when_, :source, :message def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:log_entry_id, :when, :source, :message], 'LogEntry') @log_entry_id = HttpClient::Preconditions.assert_class('log_entry_id', HttpClient::Helper.to_uuid(opts.delete(:log_entry_id)), String) @when_ = HttpClient::Preconditions.assert_class('when', HttpClient::Helper.to_date_time_iso8601(opts.delete(:when)), DateTime) @source = HttpClient::Preconditions.assert_class('source', opts.delete(:source), String) @message = HttpClient::Preconditions.assert_class('message', opts.delete(:message), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) LogEntry.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :log_entry_id => log_entry_id, :when => when_, :source => source, :message => message } end end class MavenPackage attr_reader :group_id, :artifact_id, :version def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:group_id, :artifact_id, :version], 'MavenPackage') @group_id = HttpClient::Preconditions.assert_class('group_id', opts.delete(:group_id), String) @artifact_id = HttpClient::Preconditions.assert_class('artifact_id', opts.delete(:artifact_id), String) @version = HttpClient::Preconditions.assert_class('version', opts.delete(:version), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) MavenPackage.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :group_id => group_id, :artifact_id => artifact_id, :version => version } end end class MetadataEntry attr_reader :metadata_entry_id, :when_, :key, :value def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:metadata_entry_id, :when, :key, :value], 'MetadataEntry') @metadata_entry_id = HttpClient::Preconditions.assert_class('metadata_entry_id', HttpClient::Helper.to_uuid(opts.delete(:metadata_entry_id)), String) @when_ = HttpClient::Preconditions.assert_class('when', HttpClient::Helper.to_date_time_iso8601(opts.delete(:when)), DateTime) @key = HttpClient::Preconditions.assert_class('key', opts.delete(:key), String) @value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) MetadataEntry.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :metadata_entry_id => metadata_entry_id, :when => when_, :key => key, :value => value } end end # New EMR cluster configuration object class NewEmrCluster < EmrCluster attr_reader :name, :release_label, :applications, :s3_log_uri, :master_instance, :core_instance, :task_instance, :ec2_subnet, :emr_service_role, :emr_job_flow_role, :visible_to_all_users, :configurations, :security_configuration def initialize(incoming={}) super(:__discriminator__ => EmrCluster::Types::NEW_EMR_CLUSTER) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:name, :applications, :s3_log_uri, :master_instance, :emr_service_role, :emr_job_flow_role], 'NewEmrCluster') @name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String) @release_label = (x = (x = opts.delete(:release_label); x.nil? ? "emr-5.10.0" : x); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrReleaseLabel) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrReleaseLabel.apply(x)) @applications = HttpClient::Preconditions.assert_class('applications', opts.delete(:applications), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrApplication) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrApplication.apply(x)) } @s3_log_uri = HttpClient::Preconditions.assert_class('s3_log_uri', opts.delete(:s3_log_uri), String) @master_instance = (x = opts.delete(:master_instance); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails.new(x)) @core_instance = (x = opts.delete(:core_instance); x.nil? ? nil : (x = x; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails.new(x))) @task_instance = (x = opts.delete(:task_instance); x.nil? ? nil : (x = x; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrInstanceGroupDetails.new(x))) @ec2_subnet = (x = opts.delete(:ec2_subnet); x.nil? ? nil : HttpClient::Preconditions.assert_class('ec2_subnet', x, String)) @emr_service_role = (x = opts.delete(:emr_service_role); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrServiceRole) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrServiceRole.from_json(x)) @emr_job_flow_role = (x = opts.delete(:emr_job_flow_role); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrJobFlowRole) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrJobFlowRole.from_json(x)) @visible_to_all_users = HttpClient::Preconditions.assert_boolean('visible_to_all_users', (x = opts.delete(:visible_to_all_users); x.nil? ? false : x)) @configurations = (x = opts.delete(:configurations); x.nil? ? nil : HttpClient::Preconditions.assert_class('configurations', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EmrConfiguration) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EmrConfiguration.new(x)) }) @security_configuration = (x = opts.delete(:security_configuration); x.nil? ? nil : HttpClient::Preconditions.assert_class('security_configuration', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) NewEmrCluster.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :name => name, :release_label => release_label.value, :applications => applications.map { |o| o.value }, :s3_log_uri => s3_log_uri, :master_instance => master_instance.to_hash, :core_instance => core_instance.nil? ? nil : core_instance.to_hash, :task_instance => task_instance.nil? ? nil : task_instance.to_hash, :ec2_subnet => ec2_subnet, :emr_service_role => emr_service_role.to_hash, :emr_job_flow_role => emr_job_flow_role.to_hash, :visible_to_all_users => visible_to_all_users, :configurations => configurations.nil? ? nil : configurations.map { |o| o.to_hash }, :security_configuration => security_configuration } end end # Pager Duty integration class Pagerduty < Notification attr_reader :service_key, :num_consecutive_failures, :api_url def initialize(incoming={}) super(:__discriminator__ => Notification::Types::PAGERDUTY) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:service_key], 'Pagerduty') @service_key = HttpClient::Preconditions.assert_class('service_key', opts.delete(:service_key), String) @num_consecutive_failures = HttpClient::Preconditions.assert_class('num_consecutive_failures', (x = opts.delete(:num_consecutive_failures); x.nil? ? 1 : x), Integer) @api_url = HttpClient::Preconditions.assert_class('api_url', (x = opts.delete(:api_url); x.nil? ? "https://events.pagerduty.com" : x), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Pagerduty.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :service_key => service_key, :num_consecutive_failures => num_consecutive_failures, :api_url => api_url } end end class Process attr_reader :process_id, :process_definition_name, :start_time, :status, :task def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:process_id, :process_definition_name, :start_time, :status, :task], 'Process') @process_id = HttpClient::Preconditions.assert_class('process_id', HttpClient::Helper.to_uuid(opts.delete(:process_id)), String) @process_definition_name = HttpClient::Preconditions.assert_class('process_definition_name', opts.delete(:process_definition_name), String) @start_time = HttpClient::Preconditions.assert_class('start_time', HttpClient::Helper.to_date_time_iso8601(opts.delete(:start_time)), DateTime) @status = (x = opts.delete(:status); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::ProcessStatus) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::ProcessStatus.apply(x)) @task = HttpClient::Preconditions.assert_class('task', opts.delete(:task), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::Task) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::Task.new(x)) } end def to_json JSON.dump(to_hash) end def copy(incoming={}) Process.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :process_id => process_id, :process_definition_name => process_definition_name, :start_time => start_time, :status => status.value, :task => task.map { |o| o.to_hash } } end end # A grouping of related tasks that are run as a single unit on the same schedule class ProcessDefinition attr_reader :process_definition_name, :paused, :process_description, :schedule, :task_definitions, :overlap_action, :notifications def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:process_definition_name, :task_definitions], 'ProcessDefinition') @process_definition_name = HttpClient::Preconditions.assert_class('process_definition_name', opts.delete(:process_definition_name), String) @paused = (x = opts.delete(:paused); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('paused', x)) @process_description = (x = opts.delete(:process_description); x.nil? ? nil : HttpClient::Preconditions.assert_class('process_description', x, String)) @schedule = (x = opts.delete(:schedule); x.nil? ? nil : (x = x; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::ProcessSchedule) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::ProcessSchedule.from_json(x))) @task_definitions = HttpClient::Preconditions.assert_class('task_definitions', opts.delete(:task_definitions), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::TaskDefinition) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::TaskDefinition.new(x)) } @overlap_action = (x = (x = opts.delete(:overlap_action); x.nil? ? "wait" : x); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::ProcessOverlapAction) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::ProcessOverlapAction.apply(x)) @notifications = (x = opts.delete(:notifications); x.nil? ? nil : HttpClient::Preconditions.assert_class('notifications', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::Notification) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::Notification.from_json(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) ProcessDefinition.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :process_definition_name => process_definition_name, :paused => paused, :process_description => process_description, :schedule => schedule.nil? ? nil : schedule.to_hash, :task_definitions => task_definitions.map { |o| o.to_hash }, :overlap_action => overlap_action.value, :notifications => notifications.nil? ? nil : notifications.map { |o| o.to_hash } } end end # Wrapper of the S3DistCp spark job for more info visit the aws # [s3-dist-cp](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/UsingEMR_s3distcp.html) # page class S3Cp attr_reader :source, :destination def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:source, :destination], 'S3Cp') @source = HttpClient::Preconditions.assert_class('source', opts.delete(:source), String) @destination = HttpClient::Preconditions.assert_class('destination', opts.delete(:destination), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) S3Cp.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :source => source, :destination => destination } end end class S3LogDetails attr_reader :log_group_name, :log_stream_name def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:log_group_name, :log_stream_name], 'S3LogDetails') @log_group_name = HttpClient::Preconditions.assert_class('log_group_name', opts.delete(:log_group_name), String) @log_stream_name = HttpClient::Preconditions.assert_class('log_stream_name', opts.delete(:log_stream_name), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) S3LogDetails.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :log_group_name => log_group_name, :log_stream_name => log_stream_name } end end class ShellScriptCommand < TaskExecutable attr_reader :script, :environment_variables def initialize(incoming={}) super(:__discriminator__ => TaskExecutable::Types::SHELL_SCRIPT_COMMAND) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:script], 'ShellScriptCommand') @script = HttpClient::Preconditions.assert_class('script', opts.delete(:script), String) @environment_variables = (x = opts.delete(:environment_variables); x.nil? ? nil : HttpClient::Preconditions.assert_class('environment_variables', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::EnvironmentVariable) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::EnvironmentVariable.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) ShellScriptCommand.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :script => script, :environment_variables => environment_variables.nil? ? nil : environment_variables.map { |o| o.to_hash } } end end # the maximum spot bid price class Spot < AwsMarket attr_reader :bid_price def initialize(incoming={}) super(:__discriminator__ => AwsMarket::Types::SPOT) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:bid_price], 'Spot') @bid_price = HttpClient::Preconditions.assert_class('bid_price', HttpClient::Helper.to_big_decimal(opts.delete(:bid_price)), BigDecimal) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Spot.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def subtype_to_hash { :bid_price => bid_price.to_f.to_s } end end class Task attr_reader :task_id, :process_id, :process_definition_name, :task_definition_name, :start_time, :end_time, :previous_attempt_count, :log_entries, :metadata_entries, :execution_state, :status def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:task_id, :process_id, :process_definition_name, :task_definition_name, :start_time, :previous_attempt_count, :log_entries, :metadata_entries, :status], 'Task') @task_id = HttpClient::Preconditions.assert_class('task_id', HttpClient::Helper.to_uuid(opts.delete(:task_id)), String) @process_id = HttpClient::Preconditions.assert_class('process_id', HttpClient::Helper.to_uuid(opts.delete(:process_id)), String) @process_definition_name = HttpClient::Preconditions.assert_class('process_definition_name', opts.delete(:process_definition_name), String) @task_definition_name = HttpClient::Preconditions.assert_class('task_definition_name', opts.delete(:task_definition_name), String) @start_time = HttpClient::Preconditions.assert_class('start_time', HttpClient::Helper.to_date_time_iso8601(opts.delete(:start_time)), DateTime) @end_time = (x = opts.delete(:end_time); x.nil? ? nil : HttpClient::Preconditions.assert_class('end_time', HttpClient::Helper.to_date_time_iso8601(x), DateTime)) @previous_attempt_count = HttpClient::Preconditions.assert_class('previous_attempt_count', opts.delete(:previous_attempt_count), Integer) @log_entries = HttpClient::Preconditions.assert_class('log_entries', opts.delete(:log_entries), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::LogEntry) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::LogEntry.new(x)) } @metadata_entries = HttpClient::Preconditions.assert_class('metadata_entries', opts.delete(:metadata_entries), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry.new(x)) } @execution_state = (x = opts.delete(:execution_state); x.nil? ? nil : HttpClient::Preconditions.assert_class('execution_state', x, Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::MetadataEntry.new(x)) }) @status = (x = opts.delete(:status); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::TaskStatus) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::TaskStatus.apply(x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Task.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :task_id => task_id, :process_id => process_id, :process_definition_name => process_definition_name, :task_definition_name => task_definition_name, :start_time => start_time, :end_time => end_time, :previous_attempt_count => previous_attempt_count, :log_entries => log_entries.map { |o| o.to_hash }, :metadata_entries => metadata_entries.map { |o| o.to_hash }, :execution_state => execution_state.nil? ? nil : execution_state.map { |o| o.to_hash }, :status => status.value } end end # An individual task that runs as part of a process class TaskDefinition attr_reader :task_definition_name, :dependencies, :executable, :max_attempts, :max_runtime_seconds, :backoff_base_seconds, :backoff_exponent, :require_explicit_success def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:task_definition_name, :dependencies, :executable, :max_attempts, :backoff_base_seconds], 'TaskDefinition') @task_definition_name = HttpClient::Preconditions.assert_class('task_definition_name', opts.delete(:task_definition_name), String) @dependencies = HttpClient::Preconditions.assert_class('dependencies', opts.delete(:dependencies), Array).map { |v| (x = v; x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::TaskDependency) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::TaskDependency.new(x)) } @executable = (x = opts.delete(:executable); x.is_a?(::Com::Hbc::Svc::Sundial::V2::Models::TaskExecutable) ? x : ::Com::Hbc::Svc::Sundial::V2::Models::TaskExecutable.from_json(x)) @max_attempts = HttpClient::Preconditions.assert_class('max_attempts', opts.delete(:max_attempts), Integer) @max_runtime_seconds = (x = opts.delete(:max_runtime_seconds); x.nil? ? nil : HttpClient::Preconditions.assert_class('max_runtime_seconds', x, Integer)) @backoff_base_seconds = HttpClient::Preconditions.assert_class('backoff_base_seconds', opts.delete(:backoff_base_seconds), Integer) @backoff_exponent = HttpClient::Preconditions.assert_class('backoff_exponent', (x = opts.delete(:backoff_exponent); x.nil? ? 1.0 : x), Numeric) @require_explicit_success = HttpClient::Preconditions.assert_boolean('require_explicit_success', (x = opts.delete(:require_explicit_success); x.nil? ? false : x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) TaskDefinition.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :task_definition_name => task_definition_name, :dependencies => dependencies.map { |o| o.to_hash }, :executable => executable.to_hash, :max_attempts => max_attempts, :max_runtime_seconds => max_runtime_seconds, :backoff_base_seconds => backoff_base_seconds, :backoff_exponent => backoff_exponent, :require_explicit_success => require_explicit_success } end end class TaskDependency attr_reader :task_definition_name, :success_required def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:task_definition_name], 'TaskDependency') @task_definition_name = HttpClient::Preconditions.assert_class('task_definition_name', opts.delete(:task_definition_name), String) @success_required = HttpClient::Preconditions.assert_boolean('success_required', (x = opts.delete(:success_required); x.nil? ? true : x)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) TaskDependency.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :task_definition_name => task_definition_name, :success_required => success_required } end end end # ===== END OF SERVICE DEFINITION ===== module HttpClient class HttpHandler # Returns a client instance to use # # @param base_uri The base URI for this API # @param path the Requested full http path (including any query strings) def instance(base_uri, path) raise "Override in subclass" end end class HttpHandlerInstance # Executes a request. The provided request object will be an # instance of Net::HTTP (e.g. Net::HTTP::Get) def execute(request) raise "Override in subclass" end end class DefaultHttpHandler < HttpHandler def instance(base_uri, path) DefaultHttpHandlerInstance.new(base_uri) end end class DefaultHttpHandlerInstance < HttpHandlerInstance attr_reader :client def initialize(base_uri) @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @client = Net::HTTP.new(@base_uri.host, @base_uri.port) if @base_uri.scheme == "https" configure_ssl end end def execute(request) response = begin @client.request(request) rescue SocketError => e raise StandardError.new("Error accessing uri[#{full_uri(request.path)}]: #{e}") end case response when Net::HTTPSuccess response.body else body = response.body rescue nil raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => full_uri(request.path).to_s) end end def full_uri(path) path.start_with?(@base_uri.to_s) ? path : File.join(@base_uri.to_s, path) end # Called to configure SSL if the base uri requires it def configure_ssl @client.use_ssl = true @client.verify_mode = OpenSSL::SSL::VERIFY_PEER @client.cert_store = OpenSSL::X509::Store.new @client.cert_store.set_default_paths end end class Request attr_reader :base_uri, :path, :full_uri def initialize(http_handler, base_uri, path) @http_handler = http_handler @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @path = Preconditions.assert_class('path', path, String) @full_uri = @base_uri.to_s + @path @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end def with_header(name, value) Preconditions.check_not_blank('name', name, "Header name is required") Preconditions.check_not_blank('value', value, "Header value is required") Preconditions.check_state(!@headers.has_key?(name), "Duplicate header named[%s]" % name) @headers[name] = value @header_keys_lower_case << name.downcase self end def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") if auth.scheme.name == AuthScheme::BASIC.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" end self end def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end # Wrapper to set Content-Type header to application/json and set # the provided json document as the body def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end def get(&block) do_request(Net::HTTP::Get, &block) end def delete(&block) do_request(Net::HTTP::Delete, &block) end def options(&block) do_request(Net::HTTP::Options, &block) end def post(&block) do_request(Net::HTTP::Post, &block) end def put(&block) do_request(Net::HTTP::Put, &block) end class PATCH < Net::HTTP::Put METHOD = "PATCH" end def patch(&block) do_request(PATCH, &block) end def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup if q = to_query(@params) uri += "?%s" % q end request = klass.send(:new, uri) # DEBUG curl = ['curl'] # DEBUG if klass != Net::HTTP::Get # DEBUG curl << "-X%s" % klass.name.split("::").last.upcase # DEBUG end if @body # DEBUG tmpfile = "/tmp/rest_client.tmp" # DEBUG File.open(tmpfile, "w") { |os| os << @body.to_s } # DEBUG curl << "-d@%s" % tmpfile request.body = @body end if @auth # DEBUG curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") user_pass = "%s:%s" % [@auth.username, @auth.password] encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join request.add_field("Authorization", "Basic %s" % encoded) end @headers.each { |key, value| # DEBUG curl << "-H \"%s: %s\"" % [key, value] request.add_field(key, value) } # DEBUG curl << "'%s%s'" % [@base_uri, path] # DEBUG puts curl.join(" ") raw_response = @http_handler.instance(@base_uri, request.path).execute(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end private def to_query(params={}) parts = (params || {}).map { |k,v| if v.is_a?(Enumerable) v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] } else "%s=%s" % [k, CGI.escape(v.to_s)] end } parts.empty? ? nil : parts.join("&") end end class ServerError < StandardError attr_reader :code, :details, :body, :uri def initialize(code, details, incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @code = HttpClient::Preconditions.assert_class('code', code, Integer) @details = HttpClient::Preconditions.assert_class('details', details, String) @body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String) @uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String) HttpClient::Preconditions.assert_empty_opts(opts) super(self.message) end def message m = "%s %s" % [@code, @details] if @body m << ": %s" % @body end m end def body_json @body ? JSON.parse(@body) : nil end end class PreconditionException < StandardError attr_reader :message def initialize(message) super(message) @message = message end end module Preconditions def Preconditions.check_argument(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_argument failed") end nil end def Preconditions.check_state(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_state failed") end nil end def Preconditions.check_not_nil(field_name, reference, error_message=nil) if reference.nil? raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name) end reference end def Preconditions.check_not_blank(field_name, reference, error_message=nil) if reference.to_s.strip == "" raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name) end reference end # Throws an error if opts is not empty. Useful when parsing # arguments to a function def Preconditions.assert_empty_opts(opts) if !opts.empty? raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}") end end # Requires that the provided hash has the specified keys. # @param fields A list of symbols def Preconditions.require_keys(hash, fields, error_prefix=nil) missing = fields.select { |f| !hash.has_key?(f) } if !missing.empty? msg = "Missing required fields: " + missing.join(", ") raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}") end end # Asserts that value is not nill and is_?(klass). Returns # value. Common use is # # amount = Preconditions.assert_class('amount', amount, BigDecimal) def Preconditions.assert_class(field_name, value, klass) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('klass', klass) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name]) Preconditions.check_state(value.is_a?(klass), "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_class_or_nil(field_name, value, klass) if !value.nil? Preconditions.assert_class(field_name, value, klass) end end def Preconditions.assert_boolean(field_name, value) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name) Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass), "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_boolean_or_nil(field_name, value) if !value.nil? Preconditions.assert_boolean(field_name, value) end end def Preconditions.assert_collection_of_class(field_name, values, klass) Preconditions.assert_class(field_name, values, Array) values.each { |v| Preconditions.assert_class(field_name, v, klass) } end def Preconditions.assert_hash_of_class(field_name, hash, klass) Preconditions.assert_class(field_name, hash, Hash) values.each { |k, v| Preconditions.assert_class(field_name, v, klass) } end end class AuthScheme attr_reader :name def initialize(name) @name = HttpClient::Preconditions.check_not_blank('name', name) end BASIC = AuthScheme.new("basic") unless defined?(BASIC) end class Authorization attr_reader :scheme, :username, :password def initialize(scheme, username, opts={}) @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme) @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) HttpClient::Preconditions.assert_empty_opts(opts) end def Authorization.basic(username, password=nil) Authorization.new(AuthScheme::BASIC, username, :password => password) end end module Helper def Helper.symbolize_keys(hash) Preconditions.assert_class('hash', hash, Hash) new_hash = {} hash.each { |k, v| new_hash[k.to_sym] = v } new_hash end def Helper.to_big_decimal(value) value ? BigDecimal(value.to_s) : nil end def Helper.to_object(value) value ? (value.is_a?(Hash) ? value : JSON.parse(value)) : nil end def Helper.to_uuid(value) Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/), "Invalid guid[%s]" % value) value end def Helper.to_date_iso8601(value) if value.is_a?(Date) value elsif value Date.parse(value.to_s) else nil end end def Helper.to_date_time_iso8601(value) if value.is_a?(DateTime) value elsif value DateTime.parse(value.to_s) else nil end end def Helper.date_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%d') end def Helper.date_time_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z') end TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS) FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS) def Helper.to_boolean(field_name, value) string = value.to_s.strip.downcase if TRUE_STRINGS.include?(string) true elsif FALSE_STRINGS.include?(string) false elsif string != "" raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}") else nil end end end end end end end end end