lib/kaname/adapter/real.rb in kaname-0.2.0 vs lib/kaname/adapter/real.rb in kaname-0.3.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'net/http' + module Kaname module Adapter class Real def find_user(name) user = Kaname::Resource.users.find_by_name(name) @@ -15,9 +17,29 @@ def create_user_role(tenant_name, user_hash, role_name) tenant = Kaname::Resource.tenants.find{|t| t.name == tenant_name} role = Kaname::Resource.roles.find{|r| r.name == role_name} Fog::Identity[:openstack].create_user_role(tenant.id, user_hash["id"], role.id) + end + + def update_user_password(credentials, old_password, new_password) + if old_password && new_password + # TODO: need to confirm port number of endpoint + endpoint = "http://#{URI(credentials[:openstack_management_url]).hostname}:5000/v2.0" + url = URI.parse("#{endpoint}/OS-KSCRUD/users/#{credentials[:openstack_current_user_id]}") + req = Net::HTTP::Patch.new(url.path) + req["Content-type"] = "application/json" + req["X-Auth-Token"] = credentials[:openstack_auth_token] + req.body = JSON.generate({'user' => {'password' => new_password, 'original_password' => old_password}}) + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + if res.code == "200" + puts "Your password is updated. Please update your ~/.fog configuration too." + else + raise "password updating is failed" + end + end end def delete_user(name) user = find_user(name) Fog::Identity[:openstack].delete_user(user["id"])