lib/hyperkit/client/profiles.rb in hyperkit-1.0.0 vs lib/hyperkit/client/profiles.rb in hyperkit-1.0.2
- old
+ new
@@ -3,52 +3,125 @@
module Hyperkit
class Client
# Methods for the profiles API
- #
+ #
# @see Hyperkit::Client
# @see https://github.com/lxc/lxd/blob/master/specs/rest-api.md
module Profiles
- # GET /profiles
+ # List of profiles on the server
+ #
+ # @return [Array<String>] An array of profile names
+ #
+ # @example Get list of profiles
+ # Hyperkit.profiles #=> ["default", "docker"]
def profiles
response = get(profiles_path)
response.metadata.map { |path| path.split('/').last }
end
- # POST /profiles
+ # Create a profile
+ #
+ # @param name [String] Profile name
+ # @param options [Hash] Additional data to be passed
+ # @option options [Hash] :config Profile configuration
+ # @option options [String] :description Profile description
+ # @option options [Hash] :devices Profile devices
+ # @return [Sawyer::Resource]
+ #
+ # @example Create profile with config
+ # Hyperkit.create_profile("test-profile", config: {
+ # "limits.memory" => "2GB",
+ # "limits.cpu" => 2,
+ # "raw.lxc" => "lxc.aa_profile = unconfined"
+ # })
+ #
+ # @example Create profile with devices
+ # Hyperkit.create_profile("test-profile", devices: {
+ # eth0: {
+ # nictype: "bridged",
+ # parent: "br-ext",
+ # type: "nic"
+ # }
+ # })
def create_profile(name, options={})
- options = options.merge(name: name)
- post(profiles_path, options).metadata
+ opts = options.merge(name: name)
+ opts[:config] = stringify_hash(opts[:config]) if opts[:config]
+ post(profiles_path, opts).metadata
end
- # GET /profiles/<name>
+ # Retrieve a profile
+ #
+ # @param name [String] Profile name
+ # @return [Sawyer::Resource] Profile
+ #
+ # @example Retrieve profile 'test-profile'
+ # Hyperkit.profile("test-profile")
def profile(name)
get(profile_path(name)).metadata
end
- # PUT /profiles/<name>
+ # Update an existing profile
+ #
+ # @param name [String] Profile name
+ # @param options [Hash] Additional data to be passed
+ # @option options [Hash] :config Profile configuration. Existing configuration will be overwritten.
+ # @option options [String] :description Profile description
+ # @option options [Hash] :devices Profile devices. Existing devices will be overwritten.
+ # @return [Sawyer::Resource]
+ #
+ # @example Update profile with config (config is overwritten -- not merged)
+ # Hyperkit.update_profile("test-profile", config: {
+ # "limits.memory" => "4GB",
+ # "limits.cpu" => 4,
+ # "raw.lxc" => "lxc.aa_profile = unconfined"
+ # })
+ #
+ # @example Create profile with devices (devices are overwritten -- not merged)
+ # Hyperkit.create_profile("test-profile", devices: {
+ # eth0: {
+ # nictype: "bridged",
+ # parent: "br-int",
+ # type: "nic"
+ # }
+ # })
def update_profile(name, options={})
- put(profile_path(name), options.except(:name)).metadata
+ opts = options.except(:name)
+ opts[:config] = stringify_hash(opts[:config]) if opts[:config]
+ put(profile_path(name), opts).metadata
end
- # POST /profiles/<name>
+ # Rename a profile
+ #
+ # @param old_name [String] Existing profile name
+ # @param new_name [String] New profile name
+ # @return [Sawyer::Resource]
+ #
+ # @example Rename profile 'test' to 'test2'
+ # Hyperkit.rename_profile("test", "test2")
def rename_profile(old_name, new_name)
post(profile_path(old_name), { name: new_name }).metadata
end
- # DELETE /profiles/<name>
+ # Delete a profile
+ #
+ # @param name [String] Profile name
+ # @return [Sawyer::Resource]
+ #
+ # @example Delete profile 'test-profile'
+ # Hyperkit.delete_profile("test-profile")
def delete_profile(name)
delete(profile_path(name)).metadata
end
private
def profiles_path
"/1.0/profiles"
end
-
+
def profile_path(name)
File.join(profiles_path, name)
end
end