Sha256: e41c3b4bee0a1239bdbde01bd5090173477829c8c8b596d2e4c81173eb07c4a8

Contents?: true

Size: 1.87 KB

Versions: 13

Compression:

Stored size: 1.87 KB

Contents

require 'chef/knife/joyent_base'

class Chef
  class Knife
    class JoyentServerMetadataDelete < Knife

      include Knife::JoyentBase

      attr_reader :server

      banner 'knife joyent server metadata delete <server_id> <options>'

      option :keys,
        :short => "-k KEY",
        :long => "--key KEY",
        :description => "Key (or comma separated keys) to delete",
        :proc => lambda { |o| o.split(/[\s,]+/) },
        :default => []

      option :all,
        :long => "--all",
        :description => "Delete all metadata",
        :boolean => true,
        :default => false

      def run
        unless name_args.size === 1
          show_usage
          exit 1
        end

        if config[:keys].empty? and not config[:all]
          show_usage
          exit 1
        end

        id = name_args.first

        @server = self.connection.servers.get(id)
        unless server
          puts ui.error("Server with id: #{id} not found")
          exit 1
        end

        delete_all_keys if config[:all]
        delete_keys

        if server.update_metadata(config[:metadata])
          puts ui.color("Updated metadata on #{id}", :cyan)
          exit 0
        else
          puts ui.error("Metadata update failed")
          exit 1
        end
      end

      private

      def delete_all_keys
        if server.delete_all_metadata
          msg("Deleted all metadata on #{server.id}")
          exit 0
        else
          fail
        end
      end

      def delete_keys
        config[:keys].each do |key|
          if server.delete_metadata(key)
            msg("Deleted metadata key: #{key} on #{server.name}")
          else
            fail
          end
        end
        exit 0
      end

      def msg(msg)
        puts ui.color(msg, :cyan)
      end

      def fail
        puts ui.error("Metadata delete failed")
        exit 1
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
knife-joyent-0.4.0 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.6 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.5 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.4 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.3 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.2 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.1 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.3.0 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.2.2 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.2.1 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.2.0 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.1.4 lib/chef/knife/joyent_server_metadata_delete.rb
knife-joyent-0.1.3 lib/chef/knife/joyent_server_metadata_delete.rb