Actions.export()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
module Rapis
2
  class Actions
3
    def initialize(client)
4
      @client = client
5
      @converter = Rapis::Converter.new
6
    end
7
8
    def create(options)
9
      ret = @client.create(options['name'], options['description'])
10
      Rapis.logger.info("API id: #{ret.id}")
11
      Rapis.logger.info("API name: #{ret.name}")
12
      Rapis.logger.info("API description: #{ret.description}")
13
      ret.warnings.each { |w| Rapis.logger.warn("WARNING: #{w}") } unless ret.warnings.nil?
14
    end
15
16
    def convert(options)
17
      output = @converter.to_h(File.read(options['file']))
18
      case options['format']
19
      when 'json'
20
        output = JSON.pretty_generate(output)
21
        Rapis::Utils.print_json(output)
22
      when 'yaml'
23
        output = YAML.dump(output)
24
        Rapis::Utils.print_yaml(output)
25
      else
26
        raise OperationError, "\"#{options['format']}\" format is not supported."
27
      end
28
      File.write(options['output'], output) unless options['output'].empty?
29
    end
30
31
    def list(options)
32
      apis = []
33
      @client.get_apis.each do |a|
34
        if options['verbose']
35
          api = Rapis::Utils.struct_to_hash(a)
36
          api['stages'] = []
37
        else
38
          a_key = "#{a.id} (#{a.name})"
39
          api = { a_key => [] }
40
        end
41
42
        @client.get_stages(a.id).each do |s|
43
          if options['verbose']
44
            api['stages'] << Rapis::Utils.struct_to_hash(s)
45
          else
46
            api[a_key] << s.stage_name
47
          end
48
        end
49
        Rapis::Utils.print_yaml(YAML.dump(api))
50
        apis << api
51
      end
52
      apis
53
    end
54
55
    def export(options)
56
      dsl = @converter.to_dsl(
57
        @client.export_api(options['rest_api'], options['stage'])
58
      )
59
      Rapis::Utils.print_ruby(dsl)
60
      File.write(options['file'], dsl) if options['write']
61
    end
62
63
    def diff(options)
64
      puts Rapis::Utils.diff(
65
        @client.export_api(options['rest_api'], options['stage']),
66
        @converter.to_h(File.read(options['file']))
67
      )
68
    end
69
70
    def apply(options)
71
      ret = @client.put_api(
72
        options['rest_api'],
73
        @converter.to_h(File.read(options['file']))
74
      )
75
      Rapis.logger.info("Applied the REST API configuration to \"#{ret.id}\" (#{ret.name})")
76
      ret.warnings.each { |w| Rapis.logger.warn("WARNING: #{w}") } unless ret.warnings.nil?
77
    end
78
79
    def deploy(options)
80
      args = {}
81
      args[:rest_api_id] = options['rest_api']
82
      args[:stage_name] = options['stage']
83
      args[:description] = options['description'] unless options['description'].empty?
84
      args[:stage_description] = options['stage_description'] unless options['description'].empty?
85
      args[:cache_cluster_enabled] = (options['cache'].to_f > 0.0)
86
      args[:cache_cluster_size] = options['cache'] if args[:cache_cluster_enabled]
87
      args[:variables] = options['variables'] unless options['variables'].empty?
88
89
      ret = @client.deploy(args)
90
      summary = YAML.dump(Rapis::Utils.struct_to_hash(ret.api_summary))
91
      Rapis.logger.info("Deployment id: #{ret.id}")
92
      Rapis.logger.info("Deployment description: #{ret.description}")
93
      Rapis.logger.info("API summary :\n#{summary}")
94
    end
95
  end
96
end
97