Class: Rapis::Actions

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/rapis/actions.rb

Instance Method Summary collapse

Methods included from Logger::Helper

debug, fatal, info, log, warn

Constructor Details

#initializeActions

Returns a new instance of Actions.



12
13
14
15
# File 'lib/rapis/actions.rb', line 12

def initialize
  @client = Rapis::Client.new
  @converter = Rapis::Converter.new
end

Instance Method Details

#apply(options) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/rapis/actions.rb', line 76

def apply(options)
  ret = @client.put_api(
    options['rest_api'],
    @converter.to_h(File.read(options['file']))
  )
  info("Applied the REST API configuration to \"#{ret.id}\" (#{ret.name})")
  ret.warnings.each {|w| warn("WARNING: #{w}") } unless ret.warnings.nil?
end

#convert(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rapis/actions.rb', line 25

def convert(options)
  output = @converter.to_h(File.read(options['file']))
  case options['format']
  when 'json'
    output = JSON.pretty_generate(output)
    Rapis::Utils.print_json(output)
  when 'yaml'
    output = YAML.dump(output)
    Rapis::Utils.print_yaml(output)
  else
    raise "\"#{options['format']}\" format is not supported."
  end
  File.write(options['output'], output) unless options['output'].empty?
end

#create(name, options) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rapis/actions.rb', line 17

def create(name, options)
  ret = @client.create(name, options['description'])
  info("API id: #{ret.id}")
  info("API name: #{ret.name}")
  info("API description: #{ret.description}")
  ret.warnings.each {|w| warn("WARNING: #{w}") } unless ret.warnings.nil?
end

#deploy(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rapis/actions.rb', line 85

def deploy(options)
  args = {}
  args[:rest_api_id] = options['rest_api']
  args[:stage_name] = options['stage']
  args[:description] = options['description'] unless options['description'].empty?
  args[:stage_description] = options['stage_description'] unless options['description'].empty?
  args[:cache_cluster_enabled] = (options['cache'].to_f > 0.0)
  args[:cache_cluster_size] = options['cache'] if args[:cache_cluster_enabled]
  args[:variables] = options['variables'] unless options['variables'].empty?

  ret = @client.deploy(args)
  summary = YAML.dump(Rapis::Utils.struct_to_hash(ret.api_summary))
  info("Deployment id: #{ret.id}")
  info("Deployment description: #{ret.description}")
  info("API summary :\n#{summary}")
end

#diff(options) ⇒ Object



69
70
71
72
73
74
# File 'lib/rapis/actions.rb', line 69

def diff(options)
  puts Rapis::Utils.diff(
    @client.export_api(options['rest_api'], options['stage']),
    @converter.to_h(File.read(options['file']))
  )
end

#export(options) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rapis/actions.rb', line 61

def export(options)
  dsl = @converter.to_dsl(
    @client.export_api(options['rest_api'], options['stage'])
  )
  Rapis::Utils.print_ruby(dsl)
  File.write(options['file'], dsl) if options['write']
end

#list(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rapis/actions.rb', line 40

def list(options)
  @client.get_apis.each do |a|
    if options['verbose']
      api = Rapis::Utils.struct_to_hash(a)
      api['stages'] = []
    else
      a_key = "#{a.id} (#{a.name})"
      api = {a_key => []}
    end

    @client.get_stages(a.id).each do |s|
      if options['verbose']
        api['stages'] << Rapis::Utils.struct_to_hash(s)
      else
        api[a_key] = s.stage_name
      end
    end
    Rapis::Utils.print_yaml(YAML.dump(api))
  end
end