Module: CustomResource::ElasticTranscoder::MixIns::Actions

Includes:
Base::MixIns::Actions
Defined in:
lib/customresource/elastictranscoder/mixins/actions.rb

Instance Method Summary collapse

Instance Method Details

#action(verb, section, proc) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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