Class: Vcli::CLI::Create

Inherits:
Thor
  • Object
show all
Defined in:
lib/vcli/cli/create.rb

Instance Method Summary collapse

Instance Method Details

#virtualappliance(name, vdc) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vcli/cli/create.rb', line 19

def virtualappliance(name,vdc)
  begin
    abq = AbiquoAPI.new(:abiquo_api_url => Vcli::target, :abiquo_username => Vcli::user, :abiquo_password => Vcli::password)
    link=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/virtualappliances' ,
                             :type => 'application/vnd.abiquo.virtualappliance+json')
    newvirtualappliance={name: name}
    response=abq.post(link,newvirtualappliance)
    puts "ID for Virtual Appliance - #{response.id}"
  rescue AbiquoAPIClient::Forbidden
    puts "Forbidden HTTP 403 Received"
  rescue AbiquoAPIClient::InvalidCredentials
    puts "Invalid Credentials - HTTP 401 Received" 
  rescue AbiquoAPIClient::BadRequest
    puts "Bad Request - HTTP 400 or 406 Received"
  rescue AbiquoAPIClient::NotFound
    puts "Not Found - HTTP 400 Received"
  rescue AbiquoAPIClient::UnsupportedMediaType
    puts "Unsupported Media Type Specified - HTTP 415 Received"
  end
end

#virtualmachine(name, template, vdc, va) ⇒ Object



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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/vcli/cli/create.rb', line 75

def virtualmachine(name, template, vdc, va)
  begin
    abq = AbiquoAPI.new(:abiquo_api_url => Vcli::target, :abiquo_username => Vcli::user, :abiquo_password => Vcli::password)
    # Link to post
    link=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/virtualappliances/' + va + '/virtualmachines' ,
                             :type => 'application/vnd.abiquo.virtualmachine+json')
    # Hash to populate for new virtual machine
    newvirtualmachine=Hash.new
    # Hash to populate for link
    link2hash=Hash.new
    linkhash=Hash.new
    # Link to get link for Template
    link2=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/action/templates/',
                              :type => 'application/vnd.abiquo.virtualmachinetemplates+json',
                              :client => abq)
    templates=link2.get
    templates.each {|l|
      if l.id.to_s == template.to_s
        link2hash[:title]=l.name
        link2hash[:rel]= "virtualmachinetemplate"
        link2hash[:type]="application/vnd.abiquo.virtualmachinetemplate+json"
        link2hash[:href]=l.url
      end
    }
    newvirtualmachine[:label]=linkhash[:title]
    newvirtualmachine[:label]=name
    arrayforlinks=Array.new
    arrayforlinks[0]=link2hash
    newvirtualmachine[:links]=arrayforlinks
    cpu=options[:cpu].to_s
    ram=options[:ram].to_s
    description=options[:description].to_s
    password=options[:password].to_s
    ip=options[:ip].to_s
    vlan=options[:vlan].to_s
    newvirtualmachine[:cpu]= cpu if cpu.length >0
    newvirtualmachine[:ram]= ram if ram.length >0
    newvirtualmachine[:description]= description if description.length >0
    newvirtualmachine[:password]=password if password.length >0
    newvirtualmachine[:vdrpEnabled]=true
    # Create VirtualMachine
    response=abq.post(link,newvirtualmachine)
    puts "ID for Virtual Machine - #{response.id}"
    system("vcli deploy --vdc=#{vdc} --va=#{va} --vm=#{response.id}")
    sleep 20
    puts
    system("vcli poweroff --vdc=#{vdc} --va=#{va} --vm=#{response.id}")
    puts
    
    # Link to get put modified machine         
    link4=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/virtualappliances/' + va + '/virtualmachines/' + response.id.to_s ,
                              :type => 'application/vnd.abiquo.virtualmachine+json',
                              :client => abq)
    # Private IPS in VLAN         
    link5=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/privatenetworks/' + vlan + '/ips',
                              :type => 'application/vnd.abiquo.privateips+json',
                              :client => abq)
    listofips=abq.list(link5)
    listofips.each { |m|
      if m.ip == ip
        if !m.has_link?(:virtualmachine)
          url=m.url
          break
        else
          puts "IP Address already allocated"
          exit 1
        end
      end
    }
    reqoptions=Hash.new
    reqoptions[:accept]  = 'application/vnd.abiquo.acceptedrequest+json'
    virtualmachine2=abq.get(link4)
    puts virtualmachine2.to_yaml
    response=abq.put(link4,virtualmachine2,reqoptions)
  rescue AbiquoAPIClient::Forbidden
    puts "Forbidden HTTP 403 Received"
  rescue AbiquoAPIClient::InvalidCredentials
    puts "Invalid Credentials - HTTP 401 Received" 
  rescue AbiquoAPIClient::BadRequest
    puts "Bad Request - HTTP 400 or 406 Received"
  rescue AbiquoAPIClient::NotFound
    puts "Not Found - HTTP 400 Received"
  rescue AbiquoAPIClient::UnsupportedMediaType
    puts "Unsupported Media Type Specified - HTTP 415 Received"
  rescue AbiquoAPIClient::Error => e
    puts "Error - "+ e.message
  end
end

#vlan(name, address, mask, gateway, vdc) ⇒ Object



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
# File 'lib/vcli/cli/create.rb', line 41

def vlan(name, address, mask, gateway, vdc)
  begin
    abq = AbiquoAPI.new(:abiquo_api_url => Vcli::target, :abiquo_username => Vcli::user, :abiquo_password => Vcli::password)
    link=AbiquoAPI::Link.new(:href => 'api/cloud/virtualdatacenters/' + vdc + '/privatenetworks' ,
                             :type => 'application/vnd.abiquo.vlan+json')
    #  TODO Check the limits before creating new VLAN
    # If exceeds limit then throw an error
    # If within limits the create VLAN
    
    newvlan={name: name, address: address, gateway: gateway, mask: mask}
    response=abq.post(link,newvlan)
    puts "ID for VLAN - #{response.id}"
  rescue AbiquoAPIClient::Forbidden
    puts "Forbidden HTTP 403 Received"
  rescue AbiquoAPIClient::InvalidCredentials
    puts "Invalid Credentials - HTTP 401 Received" 
  rescue AbiquoAPIClient::BadRequest
    puts "Bad Request - HTTP 400 or 406 Received"
  rescue AbiquoAPIClient::NotFound
    puts "Not Found - HTTP 400 Received"
  rescue AbiquoAPIClient::UnsupportedMediaType
    puts "Unsupported Media Type Specified - HTTP 415 Received"
  rescue AbiquoAPIClient::Error => e
    puts "Error - "+ e.message
  end
end