knife-opc 0.3.1 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c54f906018c0289af53b226e2c727b05316d7804
4
- data.tar.gz: 80b121d10fbedf271168accbb16d4aa3e4d9bf6a
2
+ SHA256:
3
+ metadata.gz: d7af88ddccf9a69fbab41970cfeb23bc0f9fd0f0b04451673933115f887c4d27
4
+ data.tar.gz: c0ab4f3057dcee7b9a5d10d64de463ebb638f3aecb477be696b6b83a3dd2ab8b
5
5
  SHA512:
6
- metadata.gz: 2924964157951a54db7916efec203806267910954516e1081559684fb0d3d7e1f52a4cccc18b1bce664db08f2726e44ee190a9e7c53e3d2fa66f828fd6b549cb
7
- data.tar.gz: 5bf0cb7097e45c2c8665bf53cb2cce2249f8d422f850ca0bfcc73534868889a7b430b95ba864ce0b18e75d8d1958c70fe6f3b23edbe3487b8c2f256aaef6d3d4
6
+ metadata.gz: 1e4e0abf5f50ff0f8b5eef59551c1e1e2ae341a8e2d4bd31579f77e99c6be410b9257ffd0ff870c5ef4a73e7ef3f6a874c93fa68306c6b1a0dad6dfe89379649
7
+ data.tar.gz: e220bd8ea974284ac13910b7fe6006d9bfe67a42e2fc87c21031674062b2eb672330c5109fad4303ae8eb7171e1ed4e416766692868941a1f2c90f4d71eef66a
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,24 +18,24 @@
18
18
 
19
19
  module Opc
20
20
  class OpcOrgCreate < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
21
+ category "CHEF ORGANIZATION MANAGEMENT"
22
22
  banner "knife opc org create ORG_SHORT_NAME ORG_FULL_NAME (options)"
23
23
 
24
24
  option :filename,
25
- :long => '--filename FILENAME',
26
- :short => '-f FILENAME',
27
- :description => 'Write validator private key to FILENAME rather than STDOUT'
25
+ long: "--filename FILENAME",
26
+ short: "-f FILENAME",
27
+ description: "Write validator private key to FILENAME rather than STDOUT"
28
28
 
29
29
  option :association_user,
30
- :long => '--association_user USERNAME',
31
- :short => '-a USERNAME',
32
- :description => 'Invite USERNAME to the new organization after creation'
30
+ long: "--association_user USERNAME",
31
+ short: "-a USERNAME",
32
+ description: "Invite USERNAME to the new organization after creation"
33
33
 
34
34
  attr_accessor :org_name, :org_full_name
35
35
 
36
36
  deps do
37
- require 'chef/org'
38
- require 'chef/org/group_operations'
37
+ require_relative "../org"
38
+ require_relative "../org/group_operations"
39
39
  end
40
40
 
41
41
  def run
@@ -47,8 +47,8 @@ module Opc
47
47
  exit 1
48
48
  end
49
49
 
50
- org = Chef::Org.from_hash({ 'name' => org_name,
51
- 'full_name' => org_full_name}).create
50
+ org = Chef::Org.from_hash({ "name" => org_name,
51
+ "full_name" => org_full_name }).create
52
52
  if config[:filename]
53
53
  File.open(config[:filename], "w") do |f|
54
54
  f.print(org.private_key)
@@ -59,8 +59,8 @@ module Opc
59
59
 
60
60
  if config[:association_user]
61
61
  org.associate_user(config[:association_user])
62
- org.add_user_to_group('admins', config[:association_user])
63
- org.add_user_to_group('billing-admins', config[:association_user])
62
+ org.add_user_to_group("admins", config[:association_user])
63
+ org.add_user_to_group("billing-admins", config[:association_user])
64
64
  end
65
65
  end
66
66
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,17 +15,19 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
+ require_relative "../mixin/root_rest"
18
19
 
19
20
  module Opc
20
21
  class OpcOrgDelete < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
22
+ category "CHEF ORGANIZATION MANAGEMENT"
22
23
  banner "knife opc org delete ORG_NAME"
23
24
 
25
+ include Chef::Mixin::RootRestv0
26
+
24
27
  def run
25
28
  org_name = @name_args[0]
26
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
27
29
  ui.confirm "Do you want to delete the organization #{org_name}"
28
- ui.output @chef_rest.delete_rest("organizations/#{org_name}")
30
+ ui.output root_rest.delete("organizations/#{org_name}")
29
31
  end
30
32
  end
31
33
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +15,11 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
+ require_relative "../mixin/root_rest"
18
19
 
19
20
  module Opc
20
21
  class OpcOrgEdit < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
22
+ category "CHEF ORGANIZATION MANAGEMENT"
22
23
  banner "knife opc org edit ORG"
23
24
 
24
25
  def run
@@ -30,8 +31,9 @@ module Opc
30
31
  exit 1
31
32
  end
32
33
 
33
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
34
- original_org = @chef_rest.get_rest("organizations/#{org_name}")
34
+ include Chef::Mixin::RootRestv0
35
+
36
+ original_org = root_rest.get("organizations/#{org_name}")
35
37
  edited_org = edit_data(original_org)
36
38
 
37
39
  if original_org == edited_org
@@ -39,9 +41,8 @@ module Opc
39
41
  exit
40
42
  end
41
43
 
42
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
43
44
  ui.msg edited_org
44
- result = @chef_rest.put_rest("organizations/#{org_name}", edited_org)
45
+ root_rest.put("organizations/#{org_name}", edited_org)
45
46
  ui.msg("Saved #{org_name}.")
46
47
  end
47
48
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,27 +15,29 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
+ require_relative "../mixin/root_rest"
18
19
 
19
20
  module Opc
20
21
  class OpcOrgList < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
22
+ category "CHEF ORGANIZATION MANAGEMENT"
22
23
  banner "knife opc org list"
23
24
 
24
25
  option :with_uri,
25
- :long => "--with-uri",
26
- :short => "-w",
27
- :description => "Show corresponding URIs"
26
+ long: "--with-uri",
27
+ short: "-w",
28
+ description: "Show corresponding URIs"
28
29
 
29
30
  option :all_orgs,
30
- :long => "--all-orgs",
31
- :short => "-a",
32
- :description => "Show auto-generated hidden orgs in output"
31
+ long: "--all-orgs",
32
+ short: "-a",
33
+ description: "Show auto-generated hidden orgs in output"
34
+
35
+ include Chef::Mixin::RootRestv0
33
36
 
34
37
  def run
35
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
36
- results = @chef_rest.get_rest("organizations")
37
- unless config[:all_orgs]
38
- results = results.select { |k,v| !(k.length == 20 && k =~ /^[a-z]+$/) }
38
+ results = root_rest.get("organizations")
39
+ unless config[:all_orgs]
40
+ results = results.select { |k, v| !(k.length == 20 && k =~ /^[a-z]+$/) }
39
41
  end
40
42
  ui.output(ui.format_list_for_display(results))
41
43
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,16 +15,18 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
+ require_relative "../mixin/root_rest"
18
19
 
19
20
  module Opc
20
21
  class OpcOrgShow < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
22
+ category "CHEF ORGANIZATION MANAGEMENT"
22
23
  banner "knife opc org show ORGNAME"
23
24
 
25
+ include Chef::Mixin::RootRestv0
26
+
24
27
  def run
25
28
  org_name = @name_args[0]
26
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
27
- ui.output @chef_rest.get_rest("organizations/#{org_name}")
29
+ ui.output root_rest.get("organizations/#{org_name}")
28
30
  end
29
31
  end
30
32
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Marc Paradise (<marc@getchef.com>)
3
- # Copyright:: Copyright 2014 Chef Software, Inc
2
+ # Author:: Marc Paradise (<marc@chef.io>)
3
+ # Copyright:: Copyright 2014-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,18 +18,18 @@
18
18
 
19
19
  module Opc
20
20
  class OpcOrgUserAdd < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
21
+ category "CHEF ORGANIZATION MANAGEMENT"
22
22
  banner "knife opc org user add ORG_NAME USER_NAME"
23
23
  attr_accessor :org_name, :username
24
24
 
25
25
  option :admin,
26
- :long => '--admin',
27
- :short => '-a',
28
- :description => 'Add user to admin group'
26
+ long: "--admin",
27
+ short: "-a",
28
+ description: "Add user to admin group"
29
29
 
30
30
  deps do
31
- require 'chef/org'
32
- require 'chef/org/group_operations'
31
+ require_relative "../org"
32
+ require_relative "../org/group_operations"
33
33
  end
34
34
 
35
35
  def run
@@ -52,8 +52,9 @@ module Opc
52
52
  end
53
53
  end
54
54
  if config[:admin]
55
- org.add_user_to_group('admins', @username)
56
- org.add_user_to_group('billing-admins', @username)
55
+ org.add_user_to_group("admins", @username)
56
+ org.add_user_to_group("billing-admins", @username)
57
+ ui.msg "User #{username} is added to admins and billing-admins group"
57
58
  end
58
59
  end
59
60
  end
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: Marc Paradise (<marc@getchef.com>)
3
- # Copyright:: Copyright 2014 Chef Software, Inc
3
+ # Copyright:: Copyright 2014-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,12 +18,19 @@
18
18
 
19
19
  module Opc
20
20
  class OpcOrgUserRemove < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
21
+ category "CHEF ORGANIZATION MANAGEMENT"
22
22
  banner "knife opc org user remove ORG_NAME USER_NAME"
23
23
  attr_accessor :org_name, :username
24
24
 
25
+ option :force_remove_from_admins,
26
+ long: "--force",
27
+ short: "-f",
28
+ description: "Force removal of user from the organization's admins and billing-admins group."
29
+
25
30
  deps do
26
- require 'chef/org'
31
+ require_relative "../org"
32
+ require_relative "../org/group_operations"
33
+ require "chef/json_compat"
27
34
  end
28
35
 
29
36
  def run
@@ -36,16 +43,61 @@ module Opc
36
43
  end
37
44
 
38
45
  org = Chef::Org.new(@org_name)
46
+
47
+ if config[:force_remove_from_admins]
48
+ if org.actor_delete_would_leave_admins_empty?
49
+ failure_error_message(org_name, username)
50
+ ui.msg <<~EOF
51
+ You ran with --force which force removes the user from the admins and billing-admins groups.
52
+ However, removing #{username} from the admins group would leave it empty, which breaks the org.
53
+ Please add another user to org #{org_name} admins group and try again.
54
+ EOF
55
+ exit 1
56
+ end
57
+ remove_user_from_admin_group(org, org_name, username, "admins")
58
+ remove_user_from_admin_group(org, org_name, username, "billing-admins")
59
+ end
60
+
39
61
  begin
40
62
  org.dissociate_user(@username)
41
63
  rescue Net::HTTPServerException => e
42
64
  if e.response.code == "404"
43
65
  ui.msg "User #{username} is not associated with organization #{org_name}"
44
66
  exit 1
67
+ elsif e.response.code == "403"
68
+ body = Chef::JSONCompat.from_json(e.response.body)
69
+ if body.key?("error") && body["error"] == "Please remove #{username} from this organization's admins group before removing him or her from the organization."
70
+ failure_error_message(org_name, username)
71
+ ui.msg <<~EOF
72
+ User #{username} is in the organization's admin group. Removing users from an organization without removing them from the admins group is not allowed.
73
+ Re-run this command with --force to remove this user from the admins prior to removing it from the organization.
74
+ EOF
75
+ exit 1
76
+ else
77
+ raise e
78
+ end
45
79
  else
46
80
  raise e
47
81
  end
48
82
  end
49
83
  end
84
+
85
+ def failure_error_message(org_name, username)
86
+ ui.error "Error removing user #{username} from organization #{org_name}."
87
+ end
88
+
89
+ def remove_user_from_admin_group(org, org_name, username, admin_group_string)
90
+ org.remove_user_from_group(admin_group_string, username)
91
+ rescue Net::HTTPServerException => e
92
+ if e.response.code == "404"
93
+ ui.warn <<~EOF
94
+ User #{username} is not in the #{admin_group_string} group for organization #{org_name}.
95
+ You probably don't need to pass --force.
96
+ EOF
97
+ else
98
+ raise e
99
+ end
100
+ end
101
+
50
102
  end
51
103
  end
@@ -1,6 +1,6 @@
1
1
  #
2
- # Author:: Steven Danna (<steve@opscode.com>)
3
- # Copyright:: Copyright 2011 Opscode, Inc.
2
+ # Author:: Steven Danna (<steve@chef.io>)
3
+ # Copyright:: Copyright 2011-2016 Chef Software, Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,21 +15,29 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
+ require_relative "../mixin/root_rest"
18
19
 
19
20
  module Opc
20
21
  class OpcUserCreate < Chef::Knife
21
- category "OPSCODE PRIVATE CHEF ORGANIZATION MANAGEMENT"
22
+ category "CHEF ORGANIZATION MANAGEMENT"
22
23
  banner "knife opc user create USERNAME FIRST_NAME [MIDDLE_NAME] LAST_NAME EMAIL PASSWORD"
23
24
 
24
25
  option :filename,
25
- :long => '--filename FILENAME',
26
- :short => '-f FILENAME',
27
- :description => 'Write private key to FILENAME rather than STDOUT'
26
+ long: "--filename FILENAME",
27
+ short: "-f FILENAME",
28
+ description: "Write private key to FILENAME rather than STDOUT"
28
29
 
29
30
  option :orgname,
30
- :long => '--orgname ORGNAME',
31
- :short => '-o ORGNAME',
32
- :description => 'Associate new user to an organization matching ORGNAME'
31
+ long: "--orgname ORGNAME",
32
+ short: "-o ORGNAME",
33
+ description: "Associate new user to an organization matching ORGNAME"
34
+
35
+ option :passwordprompt,
36
+ long: "--prompt-for-password",
37
+ short: "-p",
38
+ description: "Prompt for user password"
39
+
40
+ include Chef::Mixin::RootRestv0
33
41
 
34
42
  def run
35
43
  case @name_args.count
@@ -37,47 +45,57 @@ module Opc
37
45
  username, first_name, middle_name, last_name, email, password = @name_args
38
46
  when 5
39
47
  username, first_name, last_name, email, password = @name_args
48
+ when 4
49
+ username, first_name, last_name, email = @name_args
40
50
  else
41
51
  ui.fatal "Wrong number of arguments"
42
52
  show_usage
43
53
  exit 1
44
54
  end
55
+ password = prompt_for_password if config[:passwordprompt]
56
+ unless password
57
+ ui.fatal "You must either provide a password or use the --prompt-for-password (-p) option"
58
+ exit 1
59
+ end
45
60
  middle_name ||= ""
46
61
 
47
62
  user_hash = {
48
- :username => username,
49
- :first_name => first_name,
50
- :middle_name => middle_name,
51
- :last_name => last_name,
52
- :display_name => "#{first_name} #{last_name}",
53
- :email => email,
54
- :password => password
63
+ username: username,
64
+ first_name: first_name,
65
+ middle_name: middle_name,
66
+ last_name: last_name,
67
+ display_name: "#{first_name} #{last_name}",
68
+ email: email,
69
+ password: password,
55
70
  }
56
71
 
57
- # Check the file before creating the user so the api is more transactional.
58
- if config[:filename]
59
- file = config[:filename]
60
- unless File.exists?(file) ? File.writable?(file) : File.writable?(File.dirname(file))
61
- ui.fatal "File #{config[:filename]} is not writable. Check permissions."
62
- exit 1
63
- end
64
- end
72
+ # Check the file before creating the user so the api is more transactional.
73
+ if config[:filename]
74
+ file = config[:filename]
75
+ unless File.exist?(file) ? File.writable?(file) : File.writable?(File.dirname(file))
76
+ ui.fatal "File #{config[:filename]} is not writable. Check permissions."
77
+ exit 1
78
+ end
79
+ end
65
80
 
66
- @chef_rest = Chef::REST.new(Chef::Config[:chef_server_root])
67
- result = @chef_rest.post_rest("users/", user_hash)
81
+ result = root_rest.post("users/", user_hash)
68
82
  if config[:filename]
69
83
  File.open(config[:filename], "w") do |f|
70
- f.print(result['private_key'])
84
+ f.print(result["private_key"])
71
85
  end
72
86
  else
73
- ui.msg result['private_key']
87
+ ui.msg result["private_key"]
74
88
  end
75
89
  if config[:orgname]
76
- request_body = {:user => username}
77
- response = @chef_rest.post_rest "organizations/#{config[:orgname]}/association_requests", request_body
90
+ request_body = { user: username }
91
+ response = root_rest.post("organizations/#{config[:orgname]}/association_requests", request_body)
78
92
  association_id = response["uri"].split("/").last
79
- @chef_rest.put_rest "users/#{username}/association_requests/#{association_id}", { :response => 'accept' }
93
+ root_rest.put("users/#{username}/association_requests/#{association_id}", { response: "accept" })
80
94
  end
81
95
  end
96
+
97
+ def prompt_for_password
98
+ ui.ask("Please enter the user's password: ") { |q| q.echo = false }
99
+ end
82
100
  end
83
101
  end