Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Created August 24, 2015 03:53
Show Gist options
  • Save yukpiz/c3b76ff5a9066ef107fc to your computer and use it in GitHub Desktop.
Save yukpiz/c3b76ff5a9066ef107fc to your computer and use it in GitHub Desktop.

Conohaオブジェクトストレージを使う

# ClientToken.rb
module Conoha
    require "net/http"
    require "uri"
    require "json"

    class ClientToken
        attr_reader :identity_end_point
        attr_reader :user_name
        attr_reader :password
        attr_reader :tenant_id
        attr_reader :token
        attr_reader :debug

        def initialize(identity_end_point:, user_name:,
                       password:, tenant_id:, debug: false)
            begin
                raise ArgumentError if
                !identity_end_point || !user_name ||
                    !password || !tenant_id

                @identity_end_point = identity_end_point
                @user_name = user_name
                @password = password
                @tenant_id = tenant_id
                @debug = debug

                @token = _getToken

                STDOUT.puts("Identity End Point: [#{@identity_end_point}]") if debug
                STDOUT.puts("API User Name: [#{@user_name}]") if debug
                STDOUT.puts("API Password: [#{@password}]") if debug
                STDOUT.puts("Tenant ID: [#{@tenant_id}]") if debug
                STDOUT.puts("Request Token: [#{@token}]") if debug

            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
            end
        end

        class AuthTokenError < StandardError; end

        private
        def _genRequestJson
            {
                "auth" => {
                    "passwordCredentials" => {
                        "username" => @user_name,
                        "password" => @password,
                    },
                    "tenantId" => @tenant_id,
                },
            }.to_json
        end

        def _getToken
            uri = URI.parse(@identity_end_point)

            #Https client
            https = Net::HTTP.new(uri.host, uri.port)
            https.use_ssl = true

            #Request object
            request = Net::HTTP::Post.new(uri.request_uri)
            request["Accept"] = "application/json"
            request.body = _genRequestJson

            #Execute request
            response = https.request(request)
            if response.code == "200" then
                STDOUT.puts("Succeeded to get the request token.") if @debug
                object = JSON.parse(response.body)
                object["access"]["token"]["id"]
            else
                STDERR.puts("Error of HTTPS request.")
                STDERR.puts(response.entity)
            end
        end
    end
end
# ObjectStorage.rb
module Conoha
    require_relative "./ClientToken.rb"
    class ObjectStorage < ClientToken
        def initialize(identity_end_point:, user_name:,
                       password:, tenant_id:, debug: false,
                       object_storage_end_point:)
            super(
                identity_end_point: identity_end_point,
                user_name: user_name,
                password: password,
                tenant_id: tenant_id,
                debug: debug)
            @end_point = object_storage_end_point
        end

        def createContainer(container_name:)
            begin
                raise ArgumentError if !@end_point || !container_name
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)

                uri = URI.parse("#{@end_point}/#{container_name}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Put.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "201" then
                    STDOUT.puts("Succeeded to create the container.") if @debug
                    true
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    false
                end
            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
                false
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                false
            end
        end

        def removeContainer(container_name:)
            begin
                raise ArgumentError if !@end_point || !container_name
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)

                uri = URI.parse("#{@end_point}/#{container_name}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Delete.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "204" then
                    STDOUT.puts("Succeeded to remove the container.") if @debug
                    true
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    false
                end
            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
                false
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                false
            end
        end

        def getContainerList
            begin
                raise ArgumentError if !@end_point
                raise AuthTokenError if !@token

                uri = URI.parse(@end_point)

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Get.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "200" then
                    STDOUT.puts("Succeeded to get container list.") if @debug
                    JSON.parse(response.body)
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    nil
                end
            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
                nil
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                nil
            end
        end

        def getContainerInfo(container_name:)
            begin
                raise ArgumentError if !@end_point || !container_name
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)

                uri = URI.parse("#{@end_point}/#{container_name}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Get.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "200" then
                    STDOUT.puts("Succeeded to get container infomation.") if @debug
                    JSON.parse(response.body)
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    nil
                end
            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
                nil
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                nil
            end
        end

        def getObjectInfo(container_name:, object_name:)
            begin
                raise ArgumentError if !@end_point || !container_name || !object_name
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)
                object_name = URI.encode(object_name)

                uri = URI.parse("#{@end_point}/#{container_name}/#{object_name}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Get.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "200"
                    STDOUT.puts("Succeeded to get object infomation.") if @debug
                    JSON.parse(response.body)
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    nil
                end
            rescue ArgumentError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Arguments are all required.")
                nil
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                nil
            end
        end

        def createDirectory(container_name:, directory_name:)
            begin
                raise ArgumentError if !@end_point || !container_name || !directory_name
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)
                directory_name = URI.encode(directory_name)

                uri = URI.parse("#{@end_point}/#{container_name}/#{directory_name}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Put.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["Content-Type"] = "application/directory"
                request["Content-Length"] = 0
                request["X-Auth-Token"] = @token

                #Execute request
                response = https.request(request)
                if response.code == "201"
                    STDOUT.puts("Succeeded to create a directory.") if @debug
                    true
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    false
                end
            rescue ArgumentError => e
                STDERR.puts(e.isnpect)
                STDERR.puts("Arguments are all required.")
                false
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                false
            end
        end

        def uploadObject(container_name:, upload_file:, filename:)
            begin
                raise ArgumentError if !@end_point ||
                    !container_name || !upload_file || !filename
                raise AuthTokenError if !@token

                container_name = URI.encode(container_name)
                filename = URI.encode(filename)

                uri = URI.parse("#{@end_point}/#{container_name}/#{filename}")

                #Https client
                https = Net::HTTP.new(uri.host, uri.port)
                https.use_ssl = true

                #Request object
                request = Net::HTTP::Put.new(uri.request_uri)
                request["Accept"] = "application/json"
                request["X-Auth-Token"] = @token

                img = File.binread(upload_file)
                request.body = img

                #Execute request
                response = https.request(request)
                if response.code == "201"
                    STDOUT.puts("Succeeded to upload a object.") if @debug
                    true
                else
                    STDERR.puts("Error of HTTPS request.")
                    STDERR.puts(response.entity)
                    false
                end
            rescue ArgumentError => e
                STDERR.puts(e.isnpect)
                STDERR.puts("Arguments are all required.")
                false
            rescue AuthTokenError => e
                STDERR.puts(e.inspect)
                STDERR.puts("Token has not been acquired.")
                false
            end
        end
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment