Class: Environment

Inherits:
CloudstackCli::Base show all
Defined in:
lib/cloudstack-cli/commands/environment.rb

Constant Summary

Constants included from CloudstackCli::Helper

CloudstackCli::Helper::ASYNC_STATES

Instance Attribute Summary

Attributes inherited from CloudstackCli::Base

#config

Instance Method Summary collapse

Methods inherited from CloudstackCli::Base

exit_on_failure?

Methods included from CloudstackCli::Helper

#ask_number, #bootstrap_server, #bootstrap_server_interactive, #create_port_rules, #create_server, #print_job_status, #print_options, #update_job_status, #watch_jobs

Instance Method Details

#add(env = ) ⇒ Object



21
22
23
24
25
26
27
28
29
30
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
# File 'lib/cloudstack-cli/commands/environment.rb', line 21

def add(env = options[:environment])
  config = {}
  unless options[:url]
    say "Add a new environment...", :green
    say "Environment name: #{env}" if env
    say "What's the URL of your Cloudstack API?", :yellow
    say "Example: https://my-cloudstack-service/client/api/", :green
    config[:url] = ask("URL:", :magenta)
  end
  
  unless options[:api_key]
    config[:api_key] = ask("API Key:", :magenta)
  end
  
  unless options[:secret_key]
    config[:secret_key] = ask("Secret Key:", :magenta)
  end
  
  if env
    config = {env => config}
    config[:default] = env if options[:default]
  end
  
  if File.exists? options[:config_file]
    old_config = parse_config_file
    if !env || old_config.has_key?(env)
      say "This environment already exists!", :red
      exit unless yes?("Do you want to override your settings? [y/N]", :yellow)
    end
    config = old_config.merge(config)
  else
    newfile = true
    config[:default] = env if env
  end

  write_config_file(config)
  if newfile
    say "OK. Created configuration file at #{options[:config_file]}.", :green
  else
    say "Added environment #{env}", :green
  end
end

#default(env = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cloudstack-cli/commands/environment.rb', line 90

def default(env = nil)
  config = parse_config_file
  
  unless env
    default_env = config[:default] || '-'
    say "The current default environment is \"#{default_env}\""
    exit 0
  end

  if env == '-' && config.key?(:url)
    config.delete :default
  else
    unless config.has_key?(env)
      say "Environment #{env} does not exist.", :red
      exit 1
    end
    config[:default] = env
  end

  write_config_file(config)
  say "Default environment set to #{env}."
end

#delete(env) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cloudstack-cli/commands/environment.rb', line 65

def delete(env)
  config = parse_config_file
  if env == '-'
    config.delete(:url)
    config.delete(:api_key)
    config.delete(:secret_key)
    # check if the config file is empty, delete it if true
    if config.keys.select { |key| !key.is_a? Symbol}.size == 0
      exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow)
      File.delete(options[:config_file])
      say "OK.", :green    
      exit
    end
  elsif config.delete(env)  
  else
    say "Environment #{env} does not exist.", :red
    exit 1
  end
  exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow)
  config.delete :default if config[:default] == env
  write_config_file(config)
  say "OK.", :green
end

#listObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cloudstack-cli/commands/environment.rb', line 4

def list
  config = parse_config_file
  table = [%w(Name URL Default)]
  table << ['-', config[:url], !config[:default]] if config.key?(:url)
  config.each_key do |key|
    unless key.class == Symbol
      table << [key, config[key][:url], key == config[:default]]
    end
  end
  print_table table
end