Class: Umbreo::Models::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/umbreo/models/instance.rb

Constant Summary collapse

DEPLOYMENT_TYPE =

list deployment type of instance

["test", "manual", "provider"]

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}, name_or_id = nil) ⇒ Instance

Returns a new instance of Instance.



10
11
12
13
14
15
16
17
18
19
# File 'lib/umbreo/models/instance.rb', line 10

def initialize(credentials = {}, name_or_id = nil)
	if credentials.present?
		@email    = credentials[:email]
		@api_key  = credentials[:api_key]
		@endpoint = credentials[:end_point] || SERVER_END_POINT
	end

	@name_or_id = name_or_id
	@errors     = []
end

Instance Method Details

#all(filter = {}) ⇒ Object

callback for retrieve data



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/umbreo/models/instance.rb', line 42

def all(filter = {})
	@errors << "Please choose correct one of regiter type |all, registered, unregistered|" if ["registered", "unregistered"].exclude?(filter[:register_type]) && filter[:register_type].present?
	@errors << "Please choose correct one of type deployment |all, #{DEPLOYMENT_TYPE.join(", ")}|" if DEPLOYMENT_TYPE.exclude?(filter[:type]) && filter[:type].present?
	@errors << "Please choose correct one of cloud provider |all, #{CloudProvider::CLOUDS.join(", ")}|" if CloudProvider::CLOUDS.exclude?(filter[:provider]) && filter[:provider].present?
	@errors << "Search keyword is required." if filter[:keyword].blank? && filter[:search]

	if valid?
		retrieve_data(filter)

   	if @data['user_nodes'].present?
        Helpers::Table.show_table(@data['user_nodes'], "List My Instance#{ (' | Page: ' + filter[:page].to_s) if @data['total_page'] > 1 }", ['ID', 'Name', 'Hostname', 'Description'])
   	else
   		Helpers::AlertMessage.show_error_message(@data["message"])
   	end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#deploy(parameters = {}) ⇒ Object

deploy blueprint



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/umbreo/models/instance.rb', line 101

def deploy(parameters = {})
	
	@error << "Name is required" if parameters[:name].blank?
	@error << "Blueprint Json file is required" if parameters[:blueprint].blank?
	@error << "Deployment type is required" if parameters[:type].blank?
	@error << "Compute Provider Json file is required" if parameters[:provider].blank? && parameters[:type] == "provider"

	file_parameter = {}

	[:blueprint, :service_logging, :service_monitoring, :service_backup, :provider].each do |file_name|
		file_path = parameters[file_name]

		if file_path.present?
			begin
				file    = File.open(file_path, "r")
				content = file.read
        json    = JSON.parse(content)
        file_parameter[file_name] = Helpers::JsonBaseConvert.encode(json)
			rescue
				@errors << "Error on #{ file_path } file. Please check again."
			end
		end
	end

	if valid?
		Helpers::ErrorException.rescue do

        response = Typhoeus.post(
          "#{@endpoint}/api/v1/instances/deploy",
          body: {
            authenticate_token: @api_key,
            email:              @email,
            name:               parameters[:name],
            description:        parameters[:desc],
            type:               parameters[:type],
            custom:             parameters[:custom],
            file_parameter:     file_parameter
          }
        )

        @response = JSON.parse response.response_body

        if @response["success"]
				Helpers::AlertMessage.show_success_message(@response["message"])
        else
				Helpers::AlertMessage.show_error_message(@response["message"])
        end
		end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#errorObject

Get first error



164
165
166
# File 'lib/umbreo/models/instance.rb', line 164

def error
	@errors.first
end

#retrieve_data(filter = {}) ⇒ Object

retrieve data instance



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/umbreo/models/instance.rb', line 24

def retrieve_data(filter = {})
	Helpers::ErrorException.rescue do
		data = 	Typhoeus.get(
		          "#{@endpoint}/api/v1/instances",
		          body: {
		            authenticate_token: @api_key,
		            email:              @email,
		            filter:             filter
		          }
		        )
			    
   	@data = JSON.parse data.response_body
	end
end

#showObject

get instance data



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/umbreo/models/instance.rb', line 64

def show
	@errors << "ID or Name is required." if @name_or_id.blank?

	if valid?
		Helpers::ErrorException.rescue do
			data = 	Typhoeus.get(
			          "#{@endpoint}/api/v1/instances/show",
			          body: {
			            authenticate_token: @api_key,
			            email:              @email,
			            name_or_id:         @name_or_id
			          }
			        )

    	@data = JSON.parse data.response_body

    	if @data['success']
    		type = @data['type_deployment'] == 'barebone' ? 'manual' : @data['type_deployment']
        	Helpers::AlertMessage.(@data['id']) { "ID: #message" }
        	Helpers::AlertMessage.(@data['name']) { "Name: #message" }
        	Helpers::AlertMessage.(@data['description']) { "Description: #message" }
        	Helpers::AlertMessage.(@data['role']) { "Blueprint: #message" }
        	Helpers::AlertMessage.(type) { "Deployment type: #message" }
        	Helpers::AlertMessage.(@data['hostname']) { "Hostname: #message" }
        	Helpers::AlertMessage.(Date.parse(@data['created_at']).to_s) { "Created At: #message" }
    	else
    		Helpers::AlertMessage.show_error_message(@data['message'])
    	end
		end			
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#valid?Boolean

Check no error from validation

Returns:

  • (Boolean)


157
158
159
# File 'lib/umbreo/models/instance.rb', line 157

def valid?
	@errors.blank?
end