Method: Umbreo::Models::Service#create

Defined in:
lib/umbreo/models/service.rb

#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