Class: NCC::Client

Inherits:
NOMS::HttpClient show all
Defined in:
lib/ncc/client.rb

Instance Method Summary collapse

Methods inherited from NOMS::HttpClient

#allow_partial_updates, #allow_put_to_create, #dbg, #default_content_type, #handle_mock, #initialize, #ltrim, #method_missing, mock!, mockery, #myconfig, #opt, #rtrim, #trim

Constructor Details

This class inherits a constructor from NOMS::HttpClient

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NOMS::HttpClient

Instance Method Details

#clouds(cloudname = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ncc/client.rb', line 55

def clouds(cloudname=nil)
    unless cloudname
        cloudnames = do_request :GET => "clouds"
        cloudnames.map do |cloudname|
            if cloudname.respond_to? :keys
                cloudname
            else
                do_request :GET => "clouds/#{cloudname}"
            end
        end
    else
        do_request :GET => "clouds/#{cloudname}"
    end
end

#config_keyObject



27
28
29
# File 'lib/ncc/client.rb', line 27

def config_key
    'ncc'
end

#console(cloud, id) ⇒ Object



104
105
106
# File 'lib/ncc/client.rb', line 104

def console(cloud, id)
    do_request :GET => "clouds/#{cloud}/instances/#{id}/console"
end

#create(cloud, attrs) ⇒ Object



70
71
72
73
# File 'lib/ncc/client.rb', line 70

def create(cloud, attrs)
    do_request :POST => "clouds/#{cloud}/instances",
        :body => attrs
end

#delete(cloud, attrs) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ncc/client.rb', line 75

def delete(cloud, attrs)
    if attrs.has_key? :id
        do_request :DELETE => "clouds/#{cloud}/instances/#{attrs[:id]}"
    elsif attrs.has_key? :name
        # For now I have to do this--not optimal, should be in NCC-API
        instobj = find_by_name(cloud, attrs[:name])
        if instobj
            do_request :DELETE => "clouds/#{cloud}/instances/#{instobj['id']}"
        else
            raise "No instance found in cloud #{cloud} with name #{attrs[:name]}"
        end
    else
        raise "Need to delete instance by name or id"
    end
end

#find_by_name(cloud, name) ⇒ Object



91
92
93
# File 'lib/ncc/client.rb', line 91

def find_by_name(cloud, name)
    instobj = (list cloud).find { |i| i['name'] == name }
end

#ignore_content_typeObject

ncc-api (probably due to nginx) returns bad content-types



32
33
34
# File 'lib/ncc/client.rb', line 32

def ignore_content_type
    true
end

#infoObject



36
37
38
# File 'lib/ncc/client.rb', line 36

def info
    do_request :GET => ''
end

#instance(cloud, id) ⇒ Object



51
52
53
# File 'lib/ncc/client.rb', line 51

def instance(cloud, id)
    do_request :GET => "clouds/#{cloud}/instances/#{id}"
end

#list(cloud = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/ncc/client.rb', line 40

def list(cloud=nil)
    if cloud.nil?
        clouds = do_request "clouds"
        clouds.map do |c|
            list c
        end.flatten
    else
        do_request :GET => "clouds/#{cloud}/instances"
    end
end