module CustomResource::ElasticTranscoder::MixIns::Actions

Public Instance Methods

action(verb,section,proc) click to toggle source
# File lib/customresource/elastictranscoder/mixins/actions.rb, line 9
def action(verb,section,proc)
  prepare
  # data = {}
  restyp = section.gsub(/s$/,'').downcase
  method = "#{verb}_#{restyp}"

  resource = @event['ResourceProperties']
  @logger.debug "#{method} #{resource.ai}"

  # Convert the 'String' keys to :string symbols
  params = properties_to_params(resource.dup)
  # Weed out the invalid properties
  invalid_keys = validate_params(section,params)
  if invalid_keys.size > 0
    @logger.warn "Invalid keys in #{params[:name] || params.ai}:\n#{invalid_keys.ai}"
  end
  params = delete_invalid_keys(params,invalid_keys)
  # Restore these exceptions to the rule from the resource 'String' set e.g. :codec_options => { 'String': Value, }
  params = restore_exception_params(section,params,resource)
  @logger.debug params.ai

  data = proc.call(params, restyp, method, resource)
  @logger.debug data
  respond('SUCCESS', data)
  0
end
create(section) click to toggle source
# File lib/customresource/elastictranscoder/mixins/actions.rb, line 36
def create(section)
  action('create', section, lambda do |params, restyp, method, resource|
    begin
      data = nil
      # Let's see if we have presets/pipelines which match this :name
      set = []
      @elastictranscoder.send("list_#{section.downcase}", { ascending: 'true' }).each do |resp|
        resp.send("#{section.downcase}").map{ |item| set << item.to_h if item.name.match(/^#{params[:name]}$/) }
      end
      if set.size == 0
        @logger.info "#{method} #{params[:name]}"
        @elastictranscoder.send(method, params).each do |resp|
          @logger.info resp.send(restyp).ai
          data = get_item_data(resp.send(restyp).to_h, section, params)
        end
      else
        @logger.info "Use existing #{restyp} #{params[:name]}"
        data = get_item_data(set[0], section, params)
      end
      data
    rescue Exception => e
      abort! "#{restyp}/#{params[:name]}: #{e.message}"
    end
  end
  )
end
delete(section) click to toggle source
# File lib/customresource/elastictranscoder/mixins/actions.rb, line 104
def delete(section)
  action('delete', section, lambda do |params, restyp, method, resource|
    begin
      data = nil
      # Let's see if we have presets/pipelines which match this :name
      set = []
      @elastictranscoder.send("list_#{section.downcase}", { ascending: 'true' }).each do |resp|
        resp.send("#{section.downcase}").map{ |item| set << item.to_h if item.name.match(/^#{params[:name]}$/) }
      end
      unless set.size == 0
        # abort! "#{restyp}/#{params[:name]}: Resource does not exist"
        # else
        @logger.info "Delete existing #{restyp} #{set[0][:id]}::#{set[0][:name]}"
        method = "delete_#{restyp}"
        @logger.info "#{method} #{set[0][:id]}"
        @elastictranscoder.send(method, { id: set[0][:id] }) #.each do |resp|
        #   @logger.debug resp.send(restyp).ai
        data = get_item_data({ id: set[0][:id] }.merge(resource), section, params)
        # end
      end
      data
    rescue Exception => e
      abort! "#{restyp}/#{params[:name]}: #{e.message}"
    end
  end
  )
end
update(section) click to toggle source
# File lib/customresource/elastictranscoder/mixins/actions.rb, line 63
def update(section)
  action('update', section, lambda do |params, restyp, method, resource|
    begin
      data = nil
      # Let's see if we have presets/pipelines which match this :name
      set = []
      @elastictranscoder.send("list_#{section.downcase}", { ascending: 'true' }).each do |resp|
        resp.send("#{section.downcase}").map{ |item| set << item.to_h if item.name.match(/^#{params[:name]}$/) }
      end
      if set.size == 0
        method = "create_#{restyp}"
        @logger.info "#{method} #{params[:name]}"
        @elastictranscoder.send(method, params).each do |resp|
          @logger.debug resp.send(restyp).ai
          data = get_item_data(resp.send(restyp).to_h, section, params)
        end
      else
        # abort! "#{restyp}/#{params[:name]}: Resource does not exist"
        # else
        @logger.info "Delete existing #{restyp} #{set[0][:id]}::#{set[0][:name]}"
        method = "delete_#{restyp}"
        @logger.info "#{method} #{set[0][:id]}"
        @elastictranscoder.send(method, { id: set[0][:id] }) #.each do |resp|
        #   @logger.debug resp.send(restyp).ai
        # end
        @logger.info "Recreate #{restyp} #{params[:name]}"
        method = "create_#{restyp}"
        @logger.info "#{method} #{params[:name]}"
        @elastictranscoder.send(method, params).each do |resp|
          @logger.debug resp.send(restyp).ai
          data = get_item_data(resp.send(restyp).to_h, section, params)
        end
      end
      data
    rescue Exception => e
      abort! "#{restyp}/#{params[:name]}: #{e.message}"
    end
  end
  )
end