Class: Chef::Knife::CloudstackNetworkCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
CloudstackBase
Defined in:
lib/chef/knife/cloudstack_network_create.rb

Instance Method Summary collapse

Methods included from CloudstackBase

#connection, included, #locate_config_value, #msg_pair, #validate!

Instance Method Details

#runObject



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
# File 'lib/chef/knife/cloudstack_network_create.rb', line 75

def run
  $stdout.sync = true

  validate!

  netoptions = {}
  mandatoryoptions = {}

    if (locate_config_value(:name).nil? || locate_config_value(:networkoffering).nil? || locate_config_value(:zone).nil?)
      puts "Name (-n), Service Offering ID (-o), and Zone ID (-z) are required."
    else
      mandatoryoptions['name'] = locate_config_value(:name)
      mandatoryoptions['networkofferingid'] = locate_config_value(:networkoffering)
      mandatoryoptions['zoneid'] = locate_config_value(:zone)

      if locate_config_value(:startip) != nil
        netoptions['startip'] = locate_config_value(:startip)
      end

      if locate_config_value(:endip) != nil
        netoptions['endip'] = locate_config_value(:endip)
      end

      if locate_config_value(:netmask) != nil
        netoptions['netmask'] = locate_config_value(:netmask)
      end

      if locate_config_value(:gateway) != nil
        netoptions['gateway'] = locate_config_value(:gateway)
      end

      if locate_config_value(:vlan) != nil
        netoptions['vlan'] = locate_config_value(:vlan)
      end

      if locate_config_value(:displaytext) != nil
        mandatoryoptions['displaytext'] = locate_config_value(:displaytext)
      else
        mandatoryoptions['displaytext'] = locate_config_value(:name)
      end

      Chef::Log.debug("Options: #{netoptions}")

      response = connection.create_network(mandatoryoptions['displaytext'], mandatoryoptions['name'], mandatoryoptions['networkofferingid'], mandatoryoptions['zoneid'], netoptions)

      Chef::Log.debug("API Response: #{response}")

      network_list = [
        ui.color('ID', :bold),
        ui.color('Name', :bold),
        ui.color('Display Text', :bold),
        ui.color('Zone ID', :bold),
        ui.color('VLAN', :bold),
        ui.color('State', :bold)
      ]

      newnetwork = response['createnetworkresponse']['network']

      network_list << newnetwork['id'].to_s
      network_list << newnetwork['name'].to_s
      network_list << newnetwork['displaytext'].to_s
      network_list << newnetwork['zoneid'].to_s
      network_list << newnetwork['vlan'].to_s
      network_list << newnetwork['state'].to_s

      puts ui.list(network_list, :columns_across, 6)

    end

end