Class: Umbreo::Models::ServiceProvider

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

Constant Summary collapse

TYPE =

service type

["logging", "monitoring", "backup"]
RESOURCE =

resource type

["master", "client"]

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ServiceProvider.



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

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



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

def all(filter = {})
	@errors << "Please choose correct option service type |all, logging, monitoring, backup|" if TYPE.exclude?(filter[:type]) && filter[:type].present?
	@errors << "Please choose correct option resource type |all, master, client|" if RESOURCE.exclude?(filter[:resource]) && filter[:resource].present?
	@errors << "Search keyword is required." if filter[:keyword].blank? && filter[:search]
	
	if valid?
		retrieve_data(filter)

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

#errorObject

Get first error



179
180
181
# File 'lib/umbreo/models/service_provider.rb', line 179

def error
	@errors.first
end

#exportObject

export umbreo stack template params



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/umbreo/models/service_provider.rb', line 104

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

         Helpers::AlertMessage.show_success_message("Success export umbreo service provider. your service provider 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



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

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

#show(need_show = true) ⇒ Object

get instance data



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
97
98
99
# File 'lib/umbreo/models/service_provider.rb', line 65

def show(need_show = true)
	@errors << "ID or Name is required." if @name_or_id.blank?

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

    	@data = JSON.parse data.response_body

    	if @data['success']
    		if need_show
         	Helpers::AlertMessage.(@data['id']) { "ID: #message" }
         	Helpers::AlertMessage.(@data['name']) { "Name: #message" }
         	Helpers::AlertMessage.(@data['resource_type']) { "Resource type: #message" }
         	Helpers::AlertMessage.(@data['umbreo_service_type']) { "Service Type: #message" }
         	Helpers::AlertMessage.(@data['slug']) { "Slug: #message" }
         	Helpers::AlertMessage.(@data['description']) { "Description: #message" }
    		else
    			@data
    		end
    	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)


172
173
174
# File 'lib/umbreo/models/service_provider.rb', line 172

def valid?
	@errors.blank?
end

#validate(file_path) ⇒ Object

check valid json params



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
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/umbreo/models/service_provider.rb', line 131

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/service_providers/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