Class: Chef::Knife::ClcIpCreate

Inherits:
Chef::Knife show all
Includes:
ClcBase
Defined in:
lib/chef/knife/clc_ip_create.rb

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#executeObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef/knife/clc_ip_create.rb', line 114

def execute
  ui.info 'Requesting public IP...'
  links = connection.create_ip_address(config[:clc_server], prepare_ip_params)

  if config[:clc_wait]
    connection.wait_for(links['operation']['id']) { putc '.' }
    ui.info "\n"
    ui.info 'Public IP has been assigned'
    ui.info "You can look at new network configuration with `knife clc server show #{config[:clc_server]} --ports"
  else
    ui.info 'IP assignment request has been sent'
    ui.info "You can check assignment operation status with 'knife clc operation show #{links['operation']['id']}'"
  end
end

#parse_and_validate_parametersObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/knife/clc_ip_create.rb', line 45

def parse_and_validate_parameters
  unless config[:clc_server]
    errors << 'Server ID is required'
  end

  permissions = config[:clc_allowed_protocols]
  if permissions && permissions.any?
    parse_protocol_permissions(permissions)
  else
    errors << 'At least one protocol permission is required'
  end

  sources = config[:clc_sources]
  if sources && sources.any?
    parse_sources(sources)
  end
end

#parse_protocol_permissions(permissions) ⇒ Object



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
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/clc_ip_create.rb', line 63

def parse_protocol_permissions(permissions)
  permissions.map! do |param|
    protocol, port_range = param.split(':', 2)

    case protocol.downcase
    when 'ssh', 'sftp' then { 'protocol' => 'tcp', 'port' => 22 }
    when 'rdp' then { 'protocol' => 'tcp', 'port' => 3389 }
    when 'icmp' then { 'protocol' => 'icmp' }
    when 'http' then [{ 'protocol' => 'tcp', 'port' => 80 }, { 'protocol' => 'tcp', 'port' => 8080 }]
    when 'https' then { 'protocol' => 'tcp', 'port' => 443 }
    when 'ftp' then { 'protocol' => 'tcp', 'port' => 21 }
    when 'ftps' then { 'protocol' => 'tcp', 'port' => 990 }
    when 'winrm' then [{ 'protocol' => 'tcp', 'port' => 5985 }, { 'protocol' => 'tcp', 'port' => 5986 }]
    when 'udp', 'tcp'
      unless port_range
        errors << "No ports specified for #{param}"
      else
        ports = port_range.split('-').map do |port_string|
          Integer(port_string) rescue nil
        end

        if ports.any?(&:nil?) || ports.size > 2 || ports.size < 1
          errors << "Malformed port range for #{param}"
        end

        {
          'protocol' => protocol.downcase,
          'port' => ports[0],
          'portTo' => ports[1]
        }.keep_if { |_, value| value }
      end
    else
      errors << "Unsupported protocol for #{param}"
    end
  end

  permissions.flatten!
end

#parse_sources(sources) ⇒ Object



102
103
104
# File 'lib/chef/knife/clc_ip_create.rb', line 102

def parse_sources(sources)
  sources.map! { |cidr| { 'cidr' => cidr } }
end

#prepare_ip_paramsObject



106
107
108
109
110
111
112
# File 'lib/chef/knife/clc_ip_create.rb', line 106

def prepare_ip_params
  {
    'ports' => config[:clc_allowed_protocols],
    'sourceRestrictions' => config[:clc_sources],
    'internalIPAddress' => config[:clc_internal_ip]
  }.delete_if { |_, value| value.nil? || value.empty? }
end