Module: Morpheus::Cli::InfrastructureHelper

Overview

Provides common methods for infrastructure management

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 8

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#cloud_type_for_id(id) ⇒ Object



98
99
100
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 98

def cloud_type_for_id(id)
  return get_available_cloud_types().find { |z| z['id'].to_i == id.to_i}
end

#cloud_type_for_name(name) ⇒ Object



102
103
104
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 102

def cloud_type_for_name(name)
  return get_available_cloud_types().find { |z| z['name'].downcase == name.downcase || z['code'].downcase == name.downcase}
end

#cloud_type_for_name_or_id(val) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 91

def cloud_type_for_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return cloud_type_for_id(val)
  else
    return cloud_type_for_name(val)
  end
end

#clouds_interfaceObject



18
19
20
21
22
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 18

def clouds_interface
  # @api_client.clouds
  raise "#{self.class} has not defined @clouds_interface" if @clouds_interface.nil?
  @clouds_interface
end

#find_cloud_by_id(id) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 64

def find_cloud_by_id(id)
  json_results = clouds_interface.get(id.to_i)
  if json_results['zone'].empty?
    print_red_alert "Cloud not found by id #{id}"
    exit 1
  end
  cloud = json_results['zone']
  return cloud
end

#find_cloud_by_name(name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 74

def find_cloud_by_name(name)
  json_results = clouds_interface.get({name: name})
  if json_results['zones'].empty?
    print_red_alert "Cloud not found by name #{name}"
    exit 1
  end
  cloud = json_results['zones'][0]
  return cloud
end

#find_cloud_by_name_or_id(val) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 56

def find_cloud_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_cloud_by_id(val)
  else
    return find_cloud_by_name(val)
  end
end

#find_group_by_id(id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 32

def find_group_by_id(id)
  begin
    json_response = groups_interface.get(id.to_i)
    return json_response['group']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Group not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_group_by_name(name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 46

def find_group_by_name(name)
  json_results = groups_interface.get({name: name})
  if json_results['groups'].empty?
    print_red_alert "Group not found by name #{name}"
    exit 1
  end
  group = json_results['groups'][0]
  return group
end

#find_group_by_name_or_id(val) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 24

def find_group_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_group_by_id(val)
  else
    return find_group_by_name(val)
  end
end

#get_available_cloud_types(refresh = false) ⇒ Object



84
85
86
87
88
89
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 84

def get_available_cloud_types(refresh=false)
  if !@available_cloud_types || refresh
    @available_cloud_types = clouds_interface.cloud_types({max:1000})['zoneTypes']
  end
  return @available_cloud_types
end

#groups_interfaceObject



12
13
14
15
16
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 12

def groups_interface
  # @api_client.groups
  raise "#{self.class} has not defined @groups_interface" if @groups_interface.nil?
  @groups_interface
end