summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsnehaldwivedi <sdwivedi@msystechnologies.com>2020-10-09 03:56:16 -0700
committersnehaldwivedi <sdwivedi@msystechnologies.com>2021-02-16 02:45:13 -0800
commit3c1bf664db5c20b02aa5d2f0b1a1f4b18d19d750 (patch)
tree5117e08efc39ac07c1adcf19e6161c0947ee9711
parentee812455a8f4cf8164bc26384e07dcdc8745cdd1 (diff)
downloadchef-3c1bf664db5c20b02aa5d2f0b1a1f4b18d19d750.tar.gz
Updated API verion back to 1 and used user_v1
Signed-off-by: snehaldwivedi <sdwivedi@msystechnologies.com>
-rw-r--r--lib/chef/abstract_user.rb106
-rw-r--r--lib/chef/knife.rb2
-rw-r--r--lib/chef/knife/user_create.rb4
-rw-r--r--lib/chef/user.rb48
-rw-r--r--lib/chef/user_v1.rb74
-rw-r--r--spec/unit/knife/org_delete_spec.rb2
-rw-r--r--spec/unit/knife/org_edit_spec.rb2
-rw-r--r--spec/unit/knife/org_list_spec.rb2
-rw-r--r--spec/unit/knife/org_show_spec.rb2
-rw-r--r--spec/unit/knife/user_create_spec.rb14
-rw-r--r--spec/unit/knife/user_edit_spec.rb3
-rw-r--r--spec/unit/knife/user_password_spec.rb3
-rw-r--r--spec/unit/knife/user_show_spec.rb8
13 files changed, 135 insertions, 135 deletions
diff --git a/lib/chef/abstract_user.rb b/lib/chef/abstract_user.rb
deleted file mode 100644
index dc53488349..0000000000
--- a/lib/chef/abstract_user.rb
+++ /dev/null
@@ -1,106 +0,0 @@
-#
-# Author:: Snehal Dwivedi (sdwivedi@chef.io)
-# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-# common methods of User and UserV1
-class Chef
- class AbstractUser
-
- def initialize
- @name = ""
- @password = nil
- @admin = false
- @username = nil
- @display_name = nil
- @first_name = nil
- @middle_name = nil
- @last_name = nil
- @email = nil
- @public_key = nil
- @private_key = nil
- @create_key = nil
- end
-
- def chef_root_rest_v0
- @chef_root_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "0" })
- end
-
- def chef_root_rest_v1
- @chef_root_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "1" })
- end
-
- def name(arg = nil)
- set_or_return(:name, arg,
- regex: /^[a-z0-9\-_]+$/)
- end
-
- def admin(arg = nil)
- set_or_return(:admin,
- arg, kind_of: [TrueClass, FalseClass])
- end
-
- def username(arg = nil)
- set_or_return(:username, arg,
- regex: /^[a-z0-9\-_]+$/)
- end
-
- def display_name(arg = nil)
- set_or_return(:display_name,
- arg, kind_of: String)
- end
-
- def first_name(arg = nil)
- set_or_return(:first_name,
- arg, kind_of: String)
- end
-
- def middle_name(arg = nil)
- set_or_return(:middle_name,
- arg, kind_of: String)
- end
-
- def last_name(arg = nil)
- set_or_return(:last_name,
- arg, kind_of: String)
- end
-
- def email(arg = nil)
- set_or_return(:email,
- arg, kind_of: String)
- end
-
- def create_key(arg = nil)
- set_or_return(:create_key, arg,
- kind_of: [TrueClass, FalseClass])
- end
-
- def public_key(arg = nil)
- set_or_return(:public_key,
- arg, kind_of: String)
- end
-
- def private_key(arg = nil)
- set_or_return(:private_key,
- arg, kind_of: String)
- end
-
- def password(arg = nil)
- set_or_return(:password,
- arg, kind_of: String)
- end
- end
-end
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index 9e2578ef3a..a5ed415fee 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -665,7 +665,7 @@ class Chef
def root_rest
@root_rest ||= begin
require_relative "server_api"
- Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "2" })
+ Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "1" })
end
end
end
diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb
index bd9103d958..aa1d4d54f2 100644
--- a/lib/chef/knife/user_create.rb
+++ b/lib/chef/knife/user_create.rb
@@ -26,7 +26,7 @@ class Chef
attr_accessor :user_field
deps do
- require_relative "../user"
+ require_relative "../user_v1"
end
option :file,
@@ -57,7 +57,7 @@ class Chef
banner "knife user create USERNAME DISPLAY_NAME FIRST_NAME LAST_NAME EMAIL PASSWORD (options)"
def user
- @user_field ||= Chef::User.new
+ @user_field ||= Chef::UserV1.new
end
def run
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
index 8691bb4a50..4ebcb3a463 100644
--- a/lib/chef/user.rb
+++ b/lib/chef/user.rb
@@ -22,7 +22,6 @@ require_relative "mash"
require_relative "json_compat"
require_relative "search/query"
require_relative "server_api"
-require_relative "abstract_user"
# TODO
# DEPRECATION NOTE
@@ -36,10 +35,47 @@ require_relative "abstract_user"
# This file and corresponding osc_user knife files
# should be removed once client support for Open Source Chef Server 11 expires.
class Chef
- class User < Chef::AbstractUser
+ class User
include Chef::Mixin::FromFile
include Chef::Mixin::ParamsValidate
+ def initialize
+ @name = ""
+ @public_key = nil
+ @private_key = nil
+ @password = nil
+ @admin = false
+ end
+
+ def chef_rest_v0
+ @chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" })
+ end
+
+ def name(arg = nil)
+ set_or_return(:name, arg,
+ regex: /^[a-z0-9\-_]+$/)
+ end
+
+ def admin(arg = nil)
+ set_or_return(:admin,
+ arg, kind_of: [TrueClass, FalseClass])
+ end
+
+ def public_key(arg = nil)
+ set_or_return(:public_key,
+ arg, kind_of: String)
+ end
+
+ def private_key(arg = nil)
+ set_or_return(:private_key,
+ arg, kind_of: String)
+ end
+
+ def password(arg = nil)
+ set_or_return(:password,
+ arg, kind_of: String)
+ end
+
def to_h
result = {
"name" => @name,
@@ -58,13 +94,13 @@ class Chef
end
def destroy
- chef_root_rest_v0.delete("users/#{@name}")
+ chef_rest_v0.delete("users/#{@name}")
end
def create
payload = { name: name, admin: admin, password: password }
payload[:public_key] = public_key if public_key
- new_user = chef_root_rest_v0.post("users", payload)
+ new_user = chef_rest_v0.post("users", payload)
Chef::User.from_hash(to_h.merge(new_user))
end
@@ -72,7 +108,7 @@ class Chef
payload = { name: name, admin: admin }
payload[:private_key] = new_key if new_key
payload[:password] = password if password
- updated_user = chef_root_rest_v0.put("users/#{name}", payload)
+ updated_user = chef_rest_v0.put("users/#{name}", payload)
Chef::User.from_hash(to_h.merge(updated_user))
end
@@ -87,7 +123,7 @@ class Chef
end
def reregister
- reregistered_self = chef_root_rest_v0.put("users/#{name}", { name: name, admin: admin, private_key: true })
+ reregistered_self = chef_rest_v0.put("users/#{name}", { name: name, admin: admin, private_key: true })
private_key(reregistered_self["private_key"])
self
end
diff --git a/lib/chef/user_v1.rb b/lib/chef/user_v1.rb
index e5c3f07eff..945f0197df 100644
--- a/lib/chef/user_v1.rb
+++ b/lib/chef/user_v1.rb
@@ -24,7 +24,6 @@ require_relative "search/query"
require_relative "mixin/api_version_request_handling"
require_relative "exceptions"
require_relative "server_api"
-require_relative "abstract_user"
# OSC 11 BACKWARDS COMPATIBILITY NOTE (remove after OSC 11 support ends)
#
@@ -34,7 +33,7 @@ require_relative "abstract_user"
#
# Exception: self.list is backwards compatible with OSC 11
class Chef
- class UserV1 < Chef::AbstractUser
+ class UserV1
include Chef::Mixin::FromFile
include Chef::Mixin::ParamsValidate
@@ -42,6 +41,77 @@ class Chef
SUPPORTED_API_VERSIONS = [0, 1].freeze
+ def initialize
+ @username = nil
+ @display_name = nil
+ @first_name = nil
+ @middle_name = nil
+ @last_name = nil
+ @email = nil
+ @password = nil
+ @public_key = nil
+ @private_key = nil
+ @create_key = nil
+ end
+
+ def chef_root_rest_v0
+ @chef_root_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "0" })
+ end
+
+ def chef_root_rest_v1
+ @chef_root_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "1" })
+ end
+
+ def username(arg = nil)
+ set_or_return(:username, arg,
+ regex: /^[a-z0-9\-_]+$/)
+ end
+
+ def display_name(arg = nil)
+ set_or_return(:display_name,
+ arg, kind_of: String)
+ end
+
+ def first_name(arg = nil)
+ set_or_return(:first_name,
+ arg, kind_of: String)
+ end
+
+ def middle_name(arg = nil)
+ set_or_return(:middle_name,
+ arg, kind_of: String)
+ end
+
+ def last_name(arg = nil)
+ set_or_return(:last_name,
+ arg, kind_of: String)
+ end
+
+ def email(arg = nil)
+ set_or_return(:email,
+ arg, kind_of: String)
+ end
+
+ def create_key(arg = nil)
+ set_or_return(:create_key, arg,
+ kind_of: [TrueClass, FalseClass])
+ end
+
+ def public_key(arg = nil)
+ set_or_return(:public_key,
+ arg, kind_of: String)
+ end
+
+ def private_key(arg = nil)
+ set_or_return(:private_key,
+ arg, kind_of: String)
+ end
+
+ def password(arg = nil)
+ set_or_return(:password,
+ arg, kind_of: String)
+ end
+
def to_h
result = {
"username" => @username,
diff --git a/spec/unit/knife/org_delete_spec.rb b/spec/unit/knife/org_delete_spec.rb
index 38a9032bf4..94c224f1e6 100644
--- a/spec/unit/knife/org_delete_spec.rb
+++ b/spec/unit/knife/org_delete_spec.rb
@@ -32,7 +32,7 @@ describe Chef::Knife::OrgDelete do
end
it "should confirm that you want to delete and then delete organizations" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(@knife.ui).to receive(:confirm).with("Do you want to delete the organization #{@org_name}")
expect(root_rest).to receive(:delete).with("organizations/#{@org_name}")
expect(@knife.ui).to receive(:output)
diff --git a/spec/unit/knife/org_edit_spec.rb b/spec/unit/knife/org_edit_spec.rb
index 25be4bf03c..3badaf5168 100644
--- a/spec/unit/knife/org_edit_spec.rb
+++ b/spec/unit/knife/org_edit_spec.rb
@@ -31,7 +31,7 @@ describe Chef::Knife::OrgEdit do
end
it "loads and edits the organisation" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
original_data = { "org_name" => "my_org" }
data = { "org_name" => "my_org1" }
expect(root_rest).to receive(:get).with("organizations/foobar").and_return(original_data)
diff --git a/spec/unit/knife/org_list_spec.rb b/spec/unit/knife/org_list_spec.rb
index b332bdbe8d..1c6fb956af 100644
--- a/spec/unit/knife/org_list_spec.rb
+++ b/spec/unit/knife/org_list_spec.rb
@@ -33,7 +33,7 @@ describe Chef::Knife::OrgList do
before :each do
@org = double("Chef::Org")
@knife = Chef::Knife::OrgList.new
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
allow(root_rest).to receive(:get).with("organizations").and_return(orgs)
end
diff --git a/spec/unit/knife/org_show_spec.rb b/spec/unit/knife/org_show_spec.rb
index e3d5fc1fdb..6a6260273f 100644
--- a/spec/unit/knife/org_show_spec.rb
+++ b/spec/unit/knife/org_show_spec.rb
@@ -28,7 +28,7 @@ describe Chef::Knife::OrgShow do
@org_name = "foobar"
@knife.name_args << @org_name
@org = double("Chef::Org")
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
allow(@org).to receive(:root_rest).and_return(root_rest)
end
diff --git a/spec/unit/knife/user_create_spec.rb b/spec/unit/knife/user_create_spec.rb
index 9149eebb3d..ec221fb8c0 100644
--- a/spec/unit/knife/user_create_spec.rb
+++ b/spec/unit/knife/user_create_spec.rb
@@ -86,7 +86,7 @@ describe Chef::Knife::UserCreate do
end
it "creates an user" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
expect(knife.ui).to receive(:ask).with("Please enter the user's password: ", echo: false).and_return("password")
knife.run
@@ -102,7 +102,7 @@ describe Chef::Knife::UserCreate do
end
it "sets all the mandatory fields" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
knife.run
expect(knife.user.username).to eq("some_user")
@@ -135,7 +135,7 @@ describe Chef::Knife::UserCreate do
end
it "does not set user.create_key" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
knife.run
expect(knife.user.create_key).to be_falsey
@@ -144,7 +144,7 @@ describe Chef::Knife::UserCreate do
context "when --prevent-keygen is not passed" do
it "sets user.create_key to true" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
knife.run
expect(knife.user.create_key).to be_truthy
@@ -159,7 +159,7 @@ describe Chef::Knife::UserCreate do
end
it "sets user.public_key" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
knife.run
expect(knife.user.public_key).to eq("some_key")
@@ -168,7 +168,7 @@ describe Chef::Knife::UserCreate do
context "when --user-key is not passed" do
it "does not set user.public_key" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
knife.run
expect(knife.user.public_key).to be_nil
@@ -180,7 +180,7 @@ describe Chef::Knife::UserCreate do
before :each do
@user = double("Chef::User")
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:post).and_return(@user)
@key = "You don't come into cooking to get rich - Ramsay"
allow(@user).to receive(:[]).with("private_key").and_return(@key)
diff --git a/spec/unit/knife/user_edit_spec.rb b/spec/unit/knife/user_edit_spec.rb
index 63ff89eee1..1467d05af3 100644
--- a/spec/unit/knife/user_edit_spec.rb
+++ b/spec/unit/knife/user_edit_spec.rb
@@ -37,7 +37,8 @@ describe Chef::Knife::UserEdit do
result = {}
@key = "You don't come into cooking to get rich - Ramsay"
allow(result).to receive(:[]).with("private_key").and_return(@key)
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(root_rest).to receive(:get).with("users/my_user2").and_return(data)
expect(knife).to receive(:get_updated_user).with(data).and_return(edited_data)
expect(root_rest).to receive(:put).with("users/my_user2", edited_data).and_return(result)
diff --git a/spec/unit/knife/user_password_spec.rb b/spec/unit/knife/user_password_spec.rb
index d582801780..853e21cd6d 100644
--- a/spec/unit/knife/user_password_spec.rb
+++ b/spec/unit/knife/user_password_spec.rb
@@ -31,7 +31,6 @@ describe Chef::Knife::UserPassword do
@password = "abc123"
@user = double("Chef::User")
allow(@user).to receive(:root_rest).and_return(root_rest)
- allow(Chef::User).to receive(:new).and_return(@user)
@key = "You don't come into cooking to get rich - Ramsay"
end
@@ -44,7 +43,7 @@ describe Chef::Knife::UserPassword do
result = { "password" => [], "recovery_authentication_enabled" => true }
allow(@user).to receive(:[]).with("organization")
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(@user.root_rest).to receive(:get).with("users/#{@user_name}").and_return(result)
expect(@user.root_rest).to receive(:put).with("users/#{@user_name}", result)
expect(@knife.ui).to receive(:msg).with("Authentication info updated for #{@user_name}.")
diff --git a/spec/unit/knife/user_show_spec.rb b/spec/unit/knife/user_show_spec.rb
index 31f35b65ef..941592cb56 100644
--- a/spec/unit/knife/user_show_spec.rb
+++ b/spec/unit/knife/user_show_spec.rb
@@ -31,7 +31,7 @@ describe Chef::Knife::UserShow do
@password = "abc123"
@user = double("Chef::User")
allow(@user).to receive(:root_rest).and_return(root_rest)
- allow(Chef::User).to receive(:new).and_return(@user)
+ # allow(Chef::User).to receive(:new).and_return(@user)
@key = "You don't come into cooking to get rich - Ramsay"
end
@@ -42,13 +42,13 @@ describe Chef::Knife::UserShow do
end
it "should load the user" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(@user.root_rest).to receive(:get).with("users/my_user")
knife.run
end
it "loads and displays the user" do
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
expect(@user.root_rest).to receive(:get).with("users/my_user")
expect(knife).to receive(:format_for_display)
knife.run
@@ -81,7 +81,7 @@ describe Chef::Knife::UserShow do
result = { "organizations" => [] }
knife.config[:with_orgs] = true
- expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "2" }).and_return(root_rest)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url], { api_version: "1" }).and_return(root_rest)
allow(@org).to receive(:[]).with("organization").and_return({ "name" => "test" })
expect(@user.root_rest).to receive(:get).with("users/#{@user_name}").and_return(result)
expect(@user.root_rest).to receive(:get).with("users/#{@user_name}/organizations").and_return(orgs)