knife-opc 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,7 +15,7 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
- require 'chef/mixin/root_rest'
18
+ require "chef/mixin/root_rest"
19
19
 
20
20
  module Opc
21
21
  class OpcUserShow < Chef::Knife
@@ -30,10 +30,10 @@ module Opc
30
30
 
31
31
  def run
32
32
  user_name = @name_args[0]
33
- results = root_rest.get("users/#{user_name}")
33
+ results = root_rest.get("users/#{user_name}")
34
34
  if config[:with_orgs]
35
- orgs = root_rest.get("users/#{user_name}/organizations")
36
- results["organizations"] = orgs.map {|o| o['organization']['name']}
35
+ orgs = root_rest.get("users/#{user_name}/organizations")
36
+ results["organizations"] = orgs.map { |o| o["organization"]["name"] }
37
37
  end
38
38
  ui.output results
39
39
  end
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: Steven Danna (<steve@chef.io>)
3
- # Copyright:: Copyright 2011 Chef Software, Inc
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,7 +15,7 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
- require 'chef/server_api'
18
+ require "chef/server_api"
19
19
  class Chef
20
20
  module Mixin
21
21
  module RootRestv0
@@ -24,7 +24,7 @@ class Chef
24
24
  # Rather than upgrade all of this code to move to v1, the goal is to remove the
25
25
  # need for this plugin. See
26
26
  # https://github.com/chef/chef/issues/3517
27
- @root_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], {:api_version => "0"})
27
+ @root_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { :api_version => "0" })
28
28
  end
29
29
  end
30
30
  end
@@ -1,7 +1,7 @@
1
- require 'chef/json_compat'
2
- require 'chef/mixin/params_validate'
3
- require 'chef/rest'
4
- require 'chef/org/group_operations'
1
+ require "chef/json_compat"
2
+ require "chef/mixin/params_validate"
3
+ require "chef/server_api"
4
+ require "chef/org/group_operations"
5
5
 
6
6
  class Chef
7
7
  class Org
@@ -9,9 +9,9 @@ class Chef
9
9
  include Chef::Mixin::ParamsValidate
10
10
  include Chef::Org::GroupOperations
11
11
 
12
- def initialize(name='')
12
+ def initialize(name = "")
13
13
  @name = name
14
- @full_name = ''
14
+ @full_name = ""
15
15
  # The Chef API returns the private key of the validator
16
16
  # client on create
17
17
  @private_key = nil
@@ -19,25 +19,25 @@ class Chef
19
19
  end
20
20
 
21
21
  def chef_rest
22
- @chef_rest ||= Chef::REST.new(Chef::Config[:chef_server_root])
22
+ @chef_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root])
23
23
  end
24
24
 
25
- def name(arg=nil)
25
+ def name(arg = nil)
26
26
  set_or_return(:name, arg,
27
27
  :regex => /^[a-z0-9\-_]+$/)
28
28
  end
29
29
 
30
- def full_name(arg=nil)
30
+ def full_name(arg = nil)
31
31
  set_or_return(:full_name,
32
32
  arg, :kind_of => String)
33
33
  end
34
34
 
35
- def private_key(arg=nil)
35
+ def private_key(arg = nil)
36
36
  set_or_return(:private_key,
37
37
  arg, :kind_of => String)
38
38
  end
39
39
 
40
- def guid(arg=nil)
40
+ def guid(arg = nil)
41
41
  set_or_return(:guid,
42
42
  arg, :kind_of => String)
43
43
  end
@@ -45,7 +45,7 @@ class Chef
45
45
  def to_hash
46
46
  result = {
47
47
  "name" => @name,
48
- "full_name" => @full_name
48
+ "full_name" => @full_name,
49
49
  }
50
50
  result["private_key"] = @private_key if @private_key
51
51
  result["guid"] = @guid if @guid
@@ -57,15 +57,15 @@ class Chef
57
57
  end
58
58
 
59
59
  def create
60
- payload = {:name => self.name, :full_name => self.full_name}
60
+ payload = { :name => name, :full_name => full_name }
61
61
  new_org = chef_rest.post_rest("organizations", payload)
62
- Chef::Org.from_hash(self.to_hash.merge(new_org))
62
+ Chef::Org.from_hash(to_hash.merge(new_org))
63
63
  end
64
64
 
65
65
  def update
66
- payload = {:name => self.name, :full_name => self.full_name}
66
+ payload = { :name => name, :full_name => full_name }
67
67
  new_org = chef_rest.put_rest("organizations/#{name}", payload)
68
- Chef::Org.from_hash(self.to_hash.merge(new_org))
68
+ Chef::Org.from_hash(to_hash.merge(new_org))
69
69
  end
70
70
 
71
71
  def destroy
@@ -73,22 +73,20 @@ class Chef
73
73
  end
74
74
 
75
75
  def save
76
- begin
77
- create
78
- rescue Net::HTTPServerException => e
79
- if e.response.code == "409"
80
- update
81
- else
82
- raise e
83
- end
76
+ create
77
+ rescue Net::HTTPServerException => e
78
+ if e.response.code == "409"
79
+ update
80
+ else
81
+ raise e
84
82
  end
85
83
  end
86
84
 
87
85
  def associate_user(username)
88
- request_body = {:user => username}
86
+ request_body = { :user => username }
89
87
  response = chef_rest.post_rest "organizations/#{@name}/association_requests", request_body
90
88
  association_id = response["uri"].split("/").last
91
- chef_rest.put_rest "users/#{username}/association_requests/#{association_id}", { :response => 'accept' }
89
+ chef_rest.put_rest "users/#{username}/association_requests/#{association_id}", { :response => "accept" }
92
90
  end
93
91
 
94
92
  def dissociate_user(username)
@@ -98,10 +96,10 @@ class Chef
98
96
  # Class methods
99
97
  def self.from_hash(org_hash)
100
98
  org = Chef::Org.new
101
- org.name org_hash['name']
102
- org.full_name org_hash['full_name']
103
- org.private_key org_hash['private_key'] if org_hash.key?('private_key')
104
- org.guid org_hash['guid'] if org_hash.key?('guid')
99
+ org.name org_hash["name"]
100
+ org.full_name org_hash["full_name"]
101
+ org.private_key org_hash["private_key"] if org_hash.key?("private_key")
102
+ org.guid org_hash["guid"] if org_hash.key?("guid")
105
103
  org
106
104
  end
107
105
 
@@ -114,12 +112,12 @@ class Chef
114
112
  end
115
113
 
116
114
  def self.load(org_name)
117
- response = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest("organizations/#{org_name}")
115
+ response = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get_rest("organizations/#{org_name}")
118
116
  Chef::Org.from_hash(response)
119
117
  end
120
118
 
121
- def self.list(inflate=false)
122
- orgs = Chef::REST.new(Chef::Config[:chef_server_root]).get_rest('organizations')
119
+ def self.list(inflate = false)
120
+ orgs = Chef::ServerAPI.new(Chef::Config[:chef_server_root]).get_rest("organizations")
123
121
  if inflate
124
122
  orgs.inject({}) do |org_map, (name, _url)|
125
123
  org_map[name] = Chef::Org.load(name)
@@ -1,19 +1,59 @@
1
- require 'chef/org'
1
+ require "chef/org"
2
2
 
3
3
  class Chef
4
4
  class Org
5
5
  module GroupOperations
6
+ def group(groupname)
7
+ @group ||= {}
8
+ @group[groupname] ||= chef_rest.get_rest "organizations/#{name}/groups/#{groupname}"
9
+ end
10
+
11
+ def user_member_of_group?(username, groupname)
12
+ group = group(groupname)
13
+ group["actors"].include? username
14
+ end
15
+
6
16
  def add_user_to_group(groupname, username)
7
- group = chef_rest.get_rest "organizations/#{name}/groups/#{groupname}"
17
+ group = group(groupname)
8
18
  body_hash = {
9
19
  :groupname => "#{groupname}",
10
20
  :actors => {
11
21
  "users" => group["actors"].concat([username]),
12
- "groups" => group["groups"]
13
- }
22
+ "groups" => group["groups"],
23
+ },
14
24
  }
15
25
  chef_rest.put_rest "organizations/#{name}/groups/#{groupname}", body_hash
16
26
  end
27
+
28
+ def remove_user_from_group(groupname, username)
29
+ group = group(groupname)
30
+ group["actors"].delete(username)
31
+ body_hash = {
32
+ :groupname => "#{groupname}",
33
+ :actors => {
34
+ "users" => group["actors"],
35
+ "groups" => group["groups"],
36
+ },
37
+ }
38
+ chef_rest.put_rest "organizations/#{name}/groups/#{groupname}", body_hash
39
+ end
40
+
41
+ def actor_delete_would_leave_admins_empty?
42
+ admins = group("admins")
43
+ if admins["groups"].empty?
44
+ # exclude 'pivotal' but don't mutate the group since we're caching it
45
+ if admins["actors"].include? "pivotal"
46
+ admins["actors"].length <= 2
47
+ else
48
+ admins["actors"].length <= 1
49
+ end
50
+ else
51
+ # We don't check recursively. If the admins group contains a group,
52
+ # and the user is the only member of that group,
53
+ # we'll still turn up a 'safe to delete'.
54
+ false
55
+ end
56
+ end
17
57
  end
18
58
  include Chef::Org::GroupOperations
19
59
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeOPC
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,73 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-opc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Danna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: chef
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: sdoc
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- description: Knife Tools for Opscode Chef Server
70
- email: steve@opscode.com
11
+ date: 2018-06-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Knife Tools for Chef Server
14
+ email: steve@chef.io
71
15
  executables: []
72
16
  extensions: []
73
17
  extra_rdoc_files:
@@ -93,8 +37,9 @@ files:
93
37
  - lib/chef/org.rb
94
38
  - lib/chef/org/group_operations.rb
95
39
  - lib/knife-opc/version.rb
96
- homepage: http://wiki.opscode.com/display/chef
97
- licenses: []
40
+ homepage: https://github.com/knife-opc
41
+ licenses:
42
+ - Apache-2.0
98
43
  metadata: {}
99
44
  post_install_message:
100
45
  rdoc_options: []
@@ -112,9 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
57
  version: '0'
113
58
  requirements: []
114
59
  rubyforge_project:
115
- rubygems_version: 2.4.4
60
+ rubygems_version: 2.7.6
116
61
  signing_key:
117
62
  specification_version: 4
118
- summary: Knife Tools for Opscode Chef Server
63
+ summary: Knife Tools for Chef Server
119
64
  test_files: []
120
- has_rdoc: