lib/cli/group.rb in cf-uaac-3.0.0 vs lib/cli/group.rb in cf-uaac-3.1.0

- old
+ new

@@ -1,8 +1,8 @@ #-- -# Cloud Foundry 2012.02.03 Beta -# Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved. +# Cloud Foundry +# Copyright (c) [2009-2014] Pivotal Software, Inc. All Rights Reserved. # # This product is licensed to you under the Apache License, Version 2.0 (the "License"). # You may not use this product except in compliance with the License. # # This product includes a number of subcomponents with @@ -40,10 +40,61 @@ scim.delete(:group, scim.id(:group, gname(name))) "success" } end + define_option :start, "--start <start>", "show results starting at this index" + define_option :count, "--count <count>", "number of results to show" + desc "group mappings", "List all the mappings between uaa scopes and external groups", :start, :count do + start, count = opts[:start], opts[:count] + return gripe "Please enter a valid start index" if start unless is_integer?(start) + return gripe "Please enter a valid count" if count unless is_natural_number?(count) + + response = scim_request { |scim| scim.list_group_mappings(start, count) } + + if response + grouped_group_mappings = [] + response["resources"].each do |resource| + grouped_group_mappings << {resource['displayname'] => resource['externalgroup']} + end + response["resources"] = grouped_group_mappings + pp response + end + end + + define_option :id, "--id <id>", "map uaa group using group id" + define_option :name, "--name <name>", "map uaa scope using group name" + desc "group map [external_group]", "Map uaa groups to external groups", :id, :name do |external_group| + return gripe "Please provide a group name or id" unless opts[:id] || opts[:name] + return gripe "Please provide an external group" unless external_group + + group = opts[:id] ? opts[:id] : opts[:name] + is_id = opts[:id] ? true : false + pp scim_request { |ua| + response = ua.map_group(group, is_id, external_group) + raise BadResponse, "no group id found in response of external group mapping" unless response["groupid"] + "Successfully mapped #{response["displayname"]} to #{external_group}" + } + end + + desc "group unmap [group_name] [external_group]", "Unmaps an external group from a uaa group" do |group_name, external_group| + return gripe "Please provide a group name and external group" unless group_name && external_group + + group_id = nil + response = Cli.run("group get #{group_name}") + if response + group_id = response['id'] + else + return gripe "Group #{group_name} not found" + end + + pp scim_request { |ua| + ua.unmap_group(group_id, external_group) + "Successfully unmapped #{external_group} from #{group_name}" + } + end + def id_set(objs) objs.each_with_object(Set.new) {|o, s| id = o.is_a?(String)? o: (o["id"] || o["value"] || o["memberid"]) raise BadResponse, "no id found in response of current members" unless id s << id @@ -90,8 +141,18 @@ desc "group writer delete [name] [users...]", "remove user who can modify group" do |name, *users| pp scim_request { |scim| update_members(scim, name, "writers", users, false) } end + private + + def is_natural_number?(input) + is_integer?(input) && input.to_i > -1 + end + + def is_integer?(input) + input && (input.to_i.to_s == input) + end end end +