Class: Morpheus::Cli::ProvisioningSettingsCommand

Inherits:
Object
  • Object
show all
Includes:
AccountsHelper, CliCommand, WhoamiHelper
Defined in:
lib/morpheus/cli/commands/provisioning_settings_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from WhoamiHelper

#current_account, #current_user, #current_user_permissions, included, #is_master_account, #load_whoami

Methods included from AccountsHelper

#account_column_definitions, #account_users_interface, #accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #find_all_user_ids, #find_role_by_id, #find_role_by_name, #find_role_by_name_or_id, #find_user_by_id, #find_user_by_username, #find_user_by_username_or_id, #find_user_group_by_id, #find_user_group_by_name, #find_user_group_by_name_or_id, #format_access_string, #format_role_type, #format_user_role_names, #format_user_status, #get_access_color, #get_access_string, included, #list_account_column_definitions, #list_user_column_definitions, #list_user_group_column_definitions, #role_column_definitions, #roles_interface, #subtenant_role_column_definitions, #user_column_definitions, #user_group_column_definitions, #user_groups_interface

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#connect(opts) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/morpheus/cli/commands/provisioning_settings_command.rb', line 14

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @provisioning_settings_interface = @api_client.provisioning_settings
  @storage_providers_interface = @api_client.storage_providers
  @key_pairs_interface = @api_client.key_pairs
  @blueprints_interface = @api_client.blueprints
end

#get(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/commands/provisioning_settings_command.rb', line 26

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get provisioning settings."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  
  begin
    @provisioning_settings_interface.setopts(options)

    if options[:dry_run]
      print_dry_run @provisioning_settings_interface.dry.get()
      return
    end
    json_response = @provisioning_settings_interface.get()
    if options[:json]
      puts as_json(json_response, options, "provisioningSettings")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "provisioningSettings")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['provisioningSettings']], options)
      return 0
    end

    settings = json_response['provisioningSettings']

    print_h1 "Provisioning Settings"
    print cyan

    description_cols = {
      "Allow Cloud Selection" => lambda {|it| format_boolean(it['allowZoneSelection'])},
      "Allow Host Selection" => lambda {|it| format_boolean(it['allowServerSelection'])},
      "Require Environment Selection" => lambda {|it| format_boolean(it['requireEnvironments'])},
      "Show Pricing" => lambda {|it| format_boolean(it['showPricing'])},
      "Hide Datastore Stats On Selection" => lambda {|it| format_boolean(it['hideDatastoreStats'])},
      "Cross-Tenant Naming Policies" => lambda {|it| format_boolean(it['crossTenantNamingPolicies'])},
      "Reuse Naming Sequence Numbers" => lambda {|it| format_boolean(it['reuseSequence'])},
      "Show Console Keyboard Layout Settings" => lambda {|it| format_boolean(it['showConsoleKeyboardSettings'])},
      "Deployment Archive Store" => lambda {|it| it['deployStorageProvider'] ? it['deployStorageProvider']['name'] : nil},
      # Cloud-Init Settings
      "Cloud-Init Username" => lambda {|it| it['cloudInitUsername']},
      "Cloud-Init Password" => lambda {|it| it['cloudInitPassword']},
      "Cloud-Init Key Pair" => lambda {|it| it['cloudInitKeyPair'] ? it['cloudInitKeyPair']['name'] : nil},
      # Windows Settings
      "Windows Adminstrator Password" => lambda {|it| it['windowsPassword']},
      # PXE Boot Settings
      "Default Root Password" => lambda {|it| it['pxeRootPassword']},
      # App Blueprint Settings
      "Default Blueprint Type" => lambda {|it| it['defaultTemplateType'] ? it['defaultTemplateType']['name'].capitalize : 'Morpheus'}
    }
    print_description_list(description_cols, settings)
    print reset, "\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



22
23
24
# File 'lib/morpheus/cli/commands/provisioning_settings_command.rb', line 22

def handle(args)
  handle_subcommand(args)
end

#update(args) ⇒ Object



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/morpheus/cli/commands/provisioning_settings_command.rb', line 94

def update(args)
  options = {}
  params = {}

  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage()
    opts.on("--allow-cloud [on|off]", ['on','off'], "Allow cloud selection. Default is on") do |val|
      params['allowZoneSelection'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--allow-host [on|off]", ['on','off'], "Allow host selection. Default is on") do |val|
      params['allowServerSelection'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--require-env [on|off]", ['on','off'], "Require environment selection. Default is on") do |val|
      params['requireEnvironments'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--show-pricing [on|off]", ['on','off'], "Show pricing. Default is on") do |val|
      params['showPricing'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--ds-hide-stats [on|off]", ['on','off'], "Hide datastore stats on selection. Default is on") do |val|
      params['hideDatastoreStats'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--x-tenant-naming [on|off]", ['on','off'], "Cross-tenant naming policies. Default is on") do |val|
      params['crossTenantNamingPolicies'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--reuse-name-seq [on|off]", ['on','off'], "Reuse naming sequence numbers. Default is on") do |val|
      params['reuseSequence'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--deploy-bucket BUCKET", String, "Deployment archive storage provider ID or name") do |val|
      if val == 'null'
        params['deployStorageProvider'] = nil
      else
        options[:deployBucket] = val
      end
    end
    opts.on("--cloud-username STRING", String, "Cloud-init username") do |val|
      params['cloudInitUsername'] = val == 'null' ? nil : val
    end
    opts.on("--cloud-pwd STRING", String, "Cloud-init password") do |val|
      params['cloudInitPassword'] = val == 'null' ? nil : val
    end
    opts.on("--cloud-keypair KEYPAIR", String, "Cloud-init key pair ID or name") do |val|
      if val == 'null'
        params['cloudInitKeyPair'] = nil
      else
        options[:cloudKeyPair] = val
      end
    end
    opts.on("--windows-pwd STRING", String, "Windows administrator password") do |val|
      params['windowsPassword'] = val == 'null' ? nil : val
    end
    opts.on("--pxe-pwd STRING", String, "PXE Boot default root password") do |val|
      params['pxeRootPassword'] = val == 'null' ? nil : val
    end
    opts.on("--blueprint-type TYPE", String, "Default blueprint type ID, name or code") do |val|
      if val == 'null'
        params['defaultTemplateType'] = nil
      else
        options[:blueprintType] = val
      end
    end
    build_common_options(opts, options, [:json, :payload, :dry_run, :quiet, :remote])
  end

  optparse.parse!(args)
  connect(options)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = parse_payload(options)

    if !payload
      if options[:deployBucket]
        bucket = find_storage_provider(options[:deployBucket])

        if !bucket
          print_red_alert "Storage provider #{options[:deployBucket]} not found"
          exit 1
        end
        params['deployStorageProvider'] = {'id' => bucket['id']}
      end

      if options[:cloudKeyPair]
        key_pair = find_key_pair(options[:cloudKeyPair])

        if !key_pair
          print_red_alert "Key pair #{options[:cloudKeyPair]} not found"
          exit 1
        end
        params['cloudInitKeyPair'] = {'id' => key_pair['id']}
      end

      if options[:blueprintType]
        template_type = find_template_type(options[:blueprintType])

        if !template_type
          print_red_alert "Blueprint type #{options[:blueprintType]} not found"
        end
        params['defaultTemplateType'] = {'id' => template_type['id']}
      end
      payload = {'provisioningSettings' => params}
    end

    @provisioning_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @provisioning_settings_interface.dry.update(payload)
      return
    end
    json_response = @provisioning_settings_interface.update(payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Updated provisioning settings"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error updating provisioning settings: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end