Class: Umbreo::Models::Provider

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

Instance Method Summary collapse

Constructor Details

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

init params which required for create cloud provider



8
9
10
11
12
13
14
15
16
17
# File 'lib/umbreo/models/provider.rb', line 8

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

	@errors     = []
	@name_or_id = name_or_id
end

Instance Method Details

#all(filter = {}) ⇒ Object

callback for retrieve data



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/umbreo/models/provider.rb', line 40

def all(filter = {})
	@errors << "Please choose correct option cloud provider |all, aws, gcompute, docean, openstack, XenServer|" if CloudProvider::CLOUDS.exclude?(filter[:umbreo_cloud_provider_id].try(:to_sym)) && filter[:umbreo_cloud_provider_id].present?

	if valid?
		retrieve_data(filter)

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

#check_validation!Object

validation params



95
96
97
98
# File 'lib/umbreo/models/provider.rb', line 95

def check_validation!
	@errors << 'Invalid cloud provider' if CloudProvider::CLOUDS.exclude?(@cloud_provider.to_sym)
	@errors << 'Invalid cloud provider credential' if @provider_credential.blank?
end

#create(cloud, name, description = nil, file_path) ⇒ Object

create provider



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
153
154
155
156
157
158
159
160
# File 'lib/umbreo/models/provider.rb', line 117

def create(cloud, name, description = nil, file_path)
	@name           = name
	@desc           = description
	@cloud_provider = cloud

	@errors << "File directory is required." if file_path.blank?
	@errors << "Name is required." if name.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?
		url  = "#{@endpoint}/api/v1/providers"

		Helpers::ErrorException.rescue do
 			data = Typhoeus.post(
          url,
          body: {
            authenticate_token:   @api_key,
            email:                @email,
            provider:             @cloud_provider,
           name_provider:        @name,
           description_provider: @desc,
            provider_credential:  @encode
          }
        )
        data = JSON.parse data.response_body

        if data['success']
        	Helpers::AlertMessage.show_success_message(data['message'])
        else
        	Helpers::AlertMessage.show_error_message(data['message'])
        end
      end
	else
       Helpers::AlertMessage.show_error_message(error)
	end
end

#errorObject

call errors



110
111
112
# File 'lib/umbreo/models/provider.rb', line 110

def error
	@errors.first
end

#retrieve_data(filter = {}) ⇒ Object

get list of my providers



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/umbreo/models/provider.rb', line 22

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

#showObject

get instance data



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
89
90
# File 'lib/umbreo/models/provider.rb', line 61

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/providers/show",
			          body: {
			            authenticate_token: @api_key,
			            email:              @email,
			            name_or_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['description']) { "Description: #message" }
        	Helpers::AlertMessage.(@data['umbreo_cloud_provider']) { "Cloud provider: #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 errors

Returns:

  • (Boolean)


103
104
105
# File 'lib/umbreo/models/provider.rb', line 103

def valid?
	@errors.blank?
end