Class: Umbreo::Models::Stack

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

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}, name_or_id = nil, status = false) ⇒ Stack

Returns a new instance of Stack.



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

def initialize(credentials = {}, name_or_id = nil, status = false)
  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
  @is_custom  = status
  @errors     = []
end

Instance Method Details

#allObject



31
32
33
34
35
36
37
38
39
# File 'lib/umbreo/models/stack.rb', line 31

def all
  retrive_data

    if @data['user_stacks'].present?
       Helpers::Table.show_table(@data['user_stacks'], "List My Stack", ['ID', 'Name', 'Description', 'Umbreo Stack'])
    else
      Helpers::AlertMessage.show_error_message("Stack is not found, you have to create your own stack.")
    end
end

#customObject



41
42
43
# File 'lib/umbreo/models/stack.rb', line 41

def custom
     @custom_blueprints = @data['my'] rescue []
end

#deploy(parameters = {}) ⇒ Object

deploy stack



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
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/umbreo/models/stack.rb', line 102

def deploy(parameters = {})
  
  @errors << "Name is required" if parameters[:name].blank?
  @errors << "You need to add a instance" if parameters[:user_node_attributes].blank?

  if parameters[:user_node_attributes].present?
    parameters[:user_node_attributes].each_with_index do |user_node, index|
      main_param      = parameters[:user_node_attributes][index]

      @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 = user_node[file_name]
        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
  end

  if valid?
    Helpers::ErrorException.rescue do

        response = Typhoeus.post(
          "#{@endpoint}/api/v1/stacks",
          body: {
            authenticate_token: @api_key,
            email:              @email,
            user_stack: {
             name:                     parameters[:name],
             description:              parameters[:desc],
             umbreo_stack_template_id: parameters[:umbreo_stack_template_id],
             user_nodes_attributes:    parameters[:user_node_attributes]
            }
          }
        )

        @response = JSON.parse response.response_body

        if @response["success"]
        Helpers::AlertMessage.show_success_message("Stack 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



175
176
177
# File 'lib/umbreo/models/stack.rb', line 175

def error
  @errors.first
end

#retrive_dataObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/umbreo/models/stack.rb', line 17

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

#search(keyword) ⇒ Object

search data stack



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/umbreo/models/stack.rb', line 78

def search(keyword)
  Helpers::ErrorException.rescue do
    data =  Typhoeus.get(
              "#{@endpoint}/api/v1/stacks/search",
              body: {
                authenticate_token: @api_key,
                email:              @email,
                keyword:            keyword
              }
            )

    @data = JSON.parse data.response_body

    if @data['success']
        Helpers::Table.show_table(@data['stacks'], "Search Result List My Stack", ['ID', 'Name', 'Description', 'Umbreo Stack'])
    else
      Helpers::AlertMessage.show_error_message(@data["message"])
    end
  end
end

#showObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/umbreo/models/stack.rb', line 45

def show
  if @name_or_id.present?
    Helpers::ErrorException.rescue do
      stack = Typhoeus.get(
          "#{@endpoint}/api/v1/stacks/#{@name_or_id}",
          body: {
            authenticate_token:   @api_key,
            email:                @email,
            name:                 @name_or_id,
            status:               @is_custom
          }
        ) 

        @data = JSON.parse stack.response_body rescue nil

        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_stack_template']) { "Stack Template: #message" }
          Helpers::Table.show_table(@data['user_nodes'], "List Nodes of #{@data['name']} stack", ['ID', 'Name', 'Description'])
        else
          Helpers::show_error_message("Stack is not found. Please try again with different name or try to search.")
        end
    end
  else
    Helpers::AlertMessage.show_error_message("Name or id is required")
  end
end

#valid?Boolean

Check no error from validation

Returns:



168
169
170
# File 'lib/umbreo/models/stack.rb', line 168

def valid?
  @errors.blank?
end