Class: Umbreo::Models::Service

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Service.



5
6
7
8
9
10
11
12
13
14
# File 'lib/umbreo/models/service.rb', line 5

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/umbreo/models/service.rb', line 37

def all(filter = {})
	@errors << "Please choose correct state |all, active, unactive|" if ['active', 'unactive'].exclude?(filter[:state]) && filter[:state].present?
	@errors << "Search keyword is required." if filter[:keyword].blank? && filter[:search]
	
	if valid?
		retrieve_data(filter)

   	if @data['user_services'].present?
   		page = " | Page: #{filter[:page]} of #{@data['total_page']} pages"
        Helpers::Table.show_table(@data['user_services'], "List Service#{ page if @data['total_page'] > 1 }", ['ID', 'Name', 'Service Provider', 'Link Type', 'Active'])
   	else
   		Helpers::AlertMessage.show_error_message(@data["message"])
   	end
	else
   	Helpers::AlertMessage.show_error_message(error)
	end
end

#create(parameters) ⇒ Object

create user provider



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/umbreo/models/service.rb', line 161

def create(parameters)
	
	@errors << "Name is required" if parameters[:name].blank?

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

		@errors << "#{main_param[:name]} ##{index} Blueprint Json file is required" if main_param[:name].blank?
		@errors << "#{main_param[:name]} ##{index} Deployment type is required" if main_param[:type].blank?
		@errors << "#{main_param[:name]} ##{index} Compute Provider Json file is required" if main_param[:provider].blank? && main_param[:type] == "provider"

		main_param[:file_parameter] = {}

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

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

	if valid?
		Helpers::ErrorException.rescue do

        response = Typhoeus.post(
          "#{@endpoint}/api/v1/services",
          body: {
            authenticate_token: @api_key,
            email:              @email,
            user_service:       parameters
          }
        )

        @response = JSON.parse response.response_body

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

end

#errorObject

Get first error



238
239
240
# File 'lib/umbreo/models/service.rb', line 238

def error
	@errors.first
end

#exportObject

export umbreo stack template params



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/umbreo/models/service.rb', line 93

def export
	Helpers::ErrorException.rescue do
		response = Typhoeus.get(
          "#{@endpoint}/api/v1/services/export",
          body: {
            authenticate_token:   @api_key,
            email:                @email,
            id:                   @name_or_id
          }
        ) 
        
      @response = JSON.parse response.response_body rescue nil
      
      if @response["user_service"].present?
      	name_file = @response["user_service"]["name"].downcase.titleize.delete(" ").underscore
      	Helpers::FileGenerator.create(name_file, @response["user_service"])

         Helpers::AlertMessage.show_success_message("Success export service. your service is saved on json file with name #{name_file}.json")
      else
      	Helpers::AlertMessage.show_error_message(@response["message"])
      end
	end
end

#retrieve_data(filter = {}) ⇒ Object

retrieve data instance



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/umbreo/models/service.rb', line 19

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

#showObject

get instance data



58
59
60
61
62
63
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
# File 'lib/umbreo/models/service.rb', line 58

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/services/show",
			          body: {
			            authenticate_token: @api_key,
			            email:              @email,
			            id:                 @name_or_id
			          }
			        )

    	@data = JSON.parse data.response_body

    	if @data['success']
        	Helpers::AlertMessage.(@data['id']) { "ID: #message" }
        	Helpers::AlertMessage.(@data['name']) { "Name: #message" }
        	Helpers::AlertMessage.(@data['umbreo_service']) { "Service Provider: #message" }
        	Helpers::AlertMessage.(@data['link_type']) { "Link type: #message" }
        	Helpers::AlertMessage.(@data['is_active']) { "Active: #message" }
        	Helpers::AlertMessage.(@data['description']) { "Description: #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)


231
232
233
# File 'lib/umbreo/models/service.rb', line 231

def valid?
	@errors.blank?
end

#validate(file_path) ⇒ Object

check valid json params



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
153
154
155
156
# File 'lib/umbreo/models/service.rb', line 120

def validate(file_path)
	@errors << "File directory is required." if file_path.blank?

	begin
		file    = File.open(file_path, "r")
		content = file.read
      json    = JSON.parse(content)
      encode  = Helpers::JsonBaseConvert.encode(json)
	rescue
		@errors << "Error on file. Please check again."
	end
	@errors << "Content of file is required." if content.blank?

	if valid?
		Helpers::ErrorException.rescue do

        response = Typhoeus.get(
          "#{@endpoint}/api/v1/services/validate",
          body: {
            authenticate_token: @api_key,
            email:              @email,
            content:            encode
          }
        )

        @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