summaryrefslogtreecommitdiff
path: root/lib/chef/user/base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/user/base.rb')
-rw-r--r--lib/chef/user/base.rb123
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/chef/user/base.rb b/lib/chef/user/base.rb
new file mode 100644
index 0000000000..e12bda38e3
--- /dev/null
+++ b/lib/chef/user/base.rb
@@ -0,0 +1,123 @@
+#
+# Copyright:: Copyright 2012-2017, 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.
+#
+
+class Chef
+ class User
+ class Base
+
+ 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
+ @admin = false
+ end
+
+ def chef_rest
+ @chef_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { :api_version => @minimum_api_version })
+ end
+
+ def username(arg = nil)
+ set_or_return(:username, arg,
+ :regex => /^[a-z0-9\-_]+$/)
+ end
+ alias name username
+
+ def admin(arg = nil)
+ set_or_return(:admin,
+ arg, :kind_of => [TrueClass, FalseClass])
+ 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_json(*a)
+ Chef::JSONCompat.to_json(to_hash, *a)
+ end
+
+ def destroy
+ # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
+ chef_rest.delete("users/#{@username}")
+ end
+
+ def save(new_key = false)
+ begin
+ create
+ rescue Net::HTTPServerException => e
+ if e.response.code == "409"
+ update(new_key)
+ else
+ raise e
+ end
+ end
+ end
+
+ def to_s
+ "user[#{@username}]"
+ end
+
+ end
+ end
+end