Class: Chef::Knife::ClcGroupCreate

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

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#executeObject



71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/clc_group_create.rb', line 71

def execute
  group = connection.create_group(prepare_group_params)
  parent_link = group['links'].detect { |link| link['rel'] == 'parentGroup' }
  group['parent'] = parent_link['id']

  context[:group] = group

  render
end

#fieldsObject



81
82
83
# File 'lib/chef/knife/clc_group_create.rb', line 81

def fields
  %w(name id locationId description type status parent)
end

#headersObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/knife/clc_group_create.rb', line 85

def headers
  {
    'name' => 'Name',
    'id' => 'ID',
    'locationId' => 'Location',
    'description' => 'Description',
    'type' => 'Type',
    'status' => 'Status',
    'parent' => 'Parent'
  }
end

#parse_and_validate_parametersObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/knife/clc_group_create.rb', line 34

def parse_and_validate_parameters
  unless config[:clc_name]
    errors << 'Name is required'
  end

  unless config[:clc_parent]
    errors << 'Parent Group ID is required'
  end

  custom_fields = config[:clc_custom_fields]
  if custom_fields && custom_fields.any?
    parse_custom_fields(custom_fields)
  end
end

#parse_custom_fields(custom_fields) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/clc_group_create.rb', line 49

def parse_custom_fields(custom_fields)
  custom_fields.map! do |param|
    key, value = param.split('=', 2)

    unless key && value
      errors << "Custom field definition #{param} is malformed"
      next
    end

    { 'id' => key, 'value' => value }
  end
end

#prepare_group_paramsObject



62
63
64
65
66
67
68
69
# File 'lib/chef/knife/clc_group_create.rb', line 62

def prepare_group_params
  {
    'name' => config[:clc_name],
    'description' => config[:clc_description],
    'parentGroupId' => config[:clc_parent],
    'customFields' => config[:clc_custom_fields]
  }.delete_if { |_, value| value.nil? || value.empty? }
end

#renderObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/knife/clc_group_create.rb', line 97

def render
  group = context[:group]

  fields.each do |field|
    header = headers.fetch(field, field.capitalize)
    value = group.fetch(field, '-')

    if value
      ui.info ui.color(header, :bold) + ': ' + value.to_s
    end
  end
end