Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudstack_ruby_client/client_helper.rb

Constant Summary collapse

MALFORMED_CMDS =
{
  /getvmpassword/i            => 'getVMPassword'
}
MALFORMED_RESPONSES =

The following is malformed response title in ACS, should be fixed

{
  /(create|list)counter/i         => 'counterresponse',
  /createcondition/i              => 'conditionresponse',
  /createautoscalepolicy/i        => 'autoscalepolicyresponse',
  /createautoscalevmprofile/i     => 'autoscalevmprofileresponse',
  /createautoscalevmgroup/i       => 'autoscalevmgroupresponse',
  /enableautoscalevmgroup/i       => 'enableautoscalevmGroupresponse',
  /disableautoscalevmgroup/i      => 'disableautoscalevmGroupresponse',
  /assignvirtualmachine/i         => 'moveuservmresponse',
  /resetsshkeyforvirtualmachine/i => 'resetSSHKeyforvirtualmachineresponse',
  /restorevirtualmachine/i        => 'restorevmresponse',
  /activateproject/i              => 'activaterojectresponse',
  /listnetworkdevice/i            => 'listnetworkdevice',
  /listniciranvpdevicenetworks/i  => 'listniciranvpdevicenetworks',
  /cancelstoragemaintenance/i     => 'cancelprimarystoragemaintenanceresponse',
  /enablestoragemaintenance/i     => 'prepareprimarystorageformaintenanceresponse',
  /copyiso/i                      => 'copytemplateresponse',
  /deleteiso/i                    => 'deleteisosresponse',
  /listisopermissions/i           => 'listtemplatepermissionsresponse'
}

Instance Method Summary collapse

Instance Method Details

#cmd_processor(*args) ⇒ Object



31
32
33
34
35
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
62
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
103
104
# File 'lib/cloudstack_ruby_client/client_helper.rb', line 31

def cmd_processor(*args)
  args.each do |arg|
    arga = arg.to_s.split('_')
    meta_method = %Q{
      def #{arg}(args={});

        command = "#{
          arga.each_with_index.map {|x, i|
            i==0 ? x : x.capitalize
          }.join('')
        }"

        resp_title = "#{arga.join('')}response"
    } +

    #
    # The following code block is dealing with malformed api commands
    #

    %Q{
        MALFORMED_RESPONSES.each do |k, v|
          if k =~ command
            resp_title = v
          end
        end

        MALFORMED_CMDS.each do |k, v|
          if k =~ command
            command = v
          end
        end

        if /(list|create|delete)networkacl.*/i =~ command
          command.gsub!(/acl/i, 'ACL')
        end

        if /.*(ssh).*/i =~ command
          command.gsub!(/ssh/i, 'SSH')
        end

        if /(list|create|delete)lbstickinesspolic.*/i =~ command
          command.gsub!(/lb/i, 'LB')
        end

        if /.*vpc.*/i =~ command
          command.gsub!(/vpc/i, 'VPC')
        end
    } + 
    %Q{
        params = {'command' => command}
        params.merge!(args) unless args.empty?

        response = request(params)
        json = JSON.parse(response.body)
        
        if !response.is_a?(Net::HTTPOK)
          if ["431","530"].include?(response.code) and ["9999","4350"].include?(json[resp_title]['cserrorcode'])
             raise ArgumentError, json[resp_title]['errortext']
          end

          raise RuntimeError, json['errorresponse']['errortext'] if response.code == "432"

          puts 'Error ' + response.code + ':'
          puts JSON.pretty_generate(json)
          exit 1
        end

        json[resp_title]
      end
    }
    
    self.class_eval(meta_method)
  end
end