class HP::Cloud::Mangler::Cli
Public Instance Methods
add_sec_grp_to_sec_grp()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 67 def add_sec_grp_to_sec_grp end
create_sec_group()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 30 def create_sec_group auth= HP::Cloud::Mangler::Client.userdata @token = authenticate(auth) suffix = "os-security-groups" create_sg_url = generate_uri(@options[:hp_compute_uri], auth[:auth][:tenantId].to_s, suffix) sg_name = ask("Please provide a security group name (Example: my-sec-group): ") sg_desc = ask("Please provide a security group description: ") sg_data = { 'security_group' => { 'description' => sg_desc, 'name' => sg_name } } begin clientconnect = RestClient.post( create_sg_url, sg_data.to_json, rest_header(@token[:access][:token][:id]) ) rescue RestClient::ExceptionWithResponse => e puts "HTTP failed: #{e.http_code}: #{e.http_body}" end resp = MultiJson.decode(clientconnect, :symbolize_names => true) tabledata = {:security_group => [ resp[:security_group] ] } Formatador.display_table(tabledata[:security_group], [:tenant_id, :id, :name, :description]) end
delete_sec_group()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 53 def delete_sec_group Process.exit unless options[:groupid] suffix = "os-security-groups" + '/' + "#{options[:groupid]}" auth= HP::Cloud::Mangler::Client.userdata @token = authenticate(auth) delete_sg_url = generate_uri(@options[:hp_compute_uri], auth[:auth][:tenantId].to_s, suffix) begin resp = RestClient.delete(delete_sg_url, rest_header(@token[:access][:token][:id])) rescue RestClient::ExceptionWithResponse => e puts "HTTP failed: #{e.http_code}: #{e.http_body}" end Formatador.display_line('[green]' + "Security group successfully deleted." + " <#{resp.code}>" + '[/]') end
getauthtoken()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 94 def getauthtoken auth = HP::Cloud::Mangler::Client.userdata @token = authenticate(auth) pp @token[:access][:token][:id] end
list_sec_groups()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 21 def list_sec_groups @auth_opts = HP::Cloud::Mangler::Client.userdata @token = authenticate(@auth_opts) sg_list_url = generate_uri(@options[:hp_compute_uri], @auth_opts[:auth][:tenantId].to_s, @options[:suffix]) sg_list = call_rest_uri(sg_list_url, "get", @token[:access][:token][:id]) data = parse_resp(sg_list) Formatador.display_table(data[:security_groups], [:id, :name, :description]) end
list_sg_details()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 82 def list_sg_details Process.exit unless options[:groupid] suffix = "os-security-groups" + '/' + "#{options[:groupid]}" @auth_opts = HP::Cloud::Mangler::Client.userdata @token = authenticate(@auth_opts) sg_detail_url = generate_uri(@options[:hp_compute_uri], @auth_opts[:auth][:tenantId].to_s, suffix) data = parse_resp(call_rest_uri(sg_detail_url, "get", @token[:access][:token][:id])) Formatador.display_line Formatador.display_line('[bold]' + "Rules for Security Group: " + data[:security_group][:name] + '[/]') Formatador.display_table(data[:security_group][:rules], [:id, :from_port, :to_port, :ip_range]) end
list_tenants()
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 71 def list_tenants auth = HP::Cloud::Mangler::Client.userdata @token = authenticate(auth) tenants_list_url = generate_uri(@options[:hp_auth_uri], "tenants") tenants_list = call_rest_uri(tenants_list_url, "get", @token[:access][:token][:id]) data = parse_resp(tenants_list) pp tenants_list.code Formatador.display_table(data[:tenants], [:id, :name, :description]) end
Private Instance Methods
authenticate(opts)
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 100 def authenticate(opts) config_file = File.expand_path("~/pso-ops-tools/fog-utils/data/hpcs.json") @options = HP::Cloud::Mangler::Settings.load!( config_file, {:suffix => 'os-security-groups'} ) auth_uri = generate_uri(@options[:hp_auth_uri], "tokens") auth_response = call_rest_uri(auth_uri, "post", opts) if auth_response.code == 200 Formatador.display_line('[green]' + "Authentication successful." + '[/]') return parse_resp(auth_response) else Formatador.display_line('[red]' + "Authentication failed with a " + "#{auth_response.code} error code." +'[/]') exit end end
call_rest_uri(uri, request_type="get", *auth_creds)
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 129 def call_rest_uri (uri, request_type="get", *auth_creds) if request_type =~ /get/ begin return RestClient.get(uri, rest_header(auth_creds[0])) rescue => e e.response end elsif request_type =~ /post/ begin return RestClient.post(uri, auth_creds.to_json, rest_header) rescue => e e.response end end end
generate_uri(base, *tenant, suffix)
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 113 def generate_uri (base, *tenant, suffix) if tenant[0].nil? || tenant[0] == 0 return base + suffix else return base + tenant[0].to_s + '/' + suffix end end
parse_resp(response)
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 144 def parse_resp (response) MultiJson.decode(response, :symbolize_names => true) end
rest_header(*token)
click to toggle source
# File lib/hpcloudmangler/cli.rb, line 121 def rest_header (*token) if token[0].nil? || token[0] == 0 return {:content_type => :json, :accept => :json} else return {:content_type => :json, :accept => :json, 'X-Auth-Token' => token[0]} end end