Module: CloudmunchService

Includes:
Util
Included in:
AppAbstract, AssetHelper, IntegrationHelper, RoleHelper, ServerHelper
Defined in:
lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb,
lib/cloudmunch_Ruby_sdk_v3/CloudmunchService.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

getJSONArgs, log, logClose, logInit, logIt, openJSONFile

Class Method Details

.getCustomDataContext(server, endpoint, param) ⇒ Object



208
209
210
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 208

def self.getCustomDataContext(server, endpoint, param)
    return self.http_get(server, endpoint, param)
end

.http_get(server, path, params) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 212

def self.http_get(server,path,params)
  if params.nil?
   return Net::HTTP.get(server, path)
  else
     queryStr =  "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))
     puts ("SDKDEBUG: Calling URL " + server+queryStr)
     uri = URI(server + "/" + queryStr)
     return Net::HTTP.get(uri) 
  end
end

.http_post(server, path, params) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 223

def self.http_post(server,path,params)
  queryStr =  "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))
  puts ("SDKDEBUG: Calling URL " + server+queryStr)
  if params.nil?
      return Net::HTTP.post(server, path)
  else
      uri = URI(server +  path)
      return Net::HTTP.post_form(uri, params)
  end
end

.putCustomDataContext(server, endpoint, param) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 198

def self.putCustomDataContext(server, endpoint, param)
  result = self.http_post(server, endpoint, param)
  
  if result.code.to_s == "200"
    return true 
  else
    return false 
  end     
end

Instance Method Details

#deleteCloudmunchData(paramHash) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 106

def deleteCloudmunchData(paramHash)
  paramContext = paramHash["context"]
  paramContextID = paramHash["contextID"]

  if !paramContext.nil? && !paramContext.empty? && !paramContextID.nil? && !paramContextID.empty?
      serverurl=@appContext.get_data("{master_url}")+"/applications/"+@appContext.get_data("{application}")+"/"+context+"/"+contextID;
      serverurl=serverurl+"?apikey="+@appContext.get_data("{api_key}")
      uri = URI.parse(serverurl)
      Net::HTTP::Delete(uri)
      return 1
  else
      return nil
  end
end

#deleteKeysObject



53
54
55
56
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 53

def deleteKeys()
  fileName = @appContext.get_data("{workspace}")+"/keyfile_*"
  File.delete(fileName)
end

#downloadKeys(filekey, context, contextid) ⇒ Object

downloadKeys(filekey, context, contextid) deleteKeys() getCloudmunchData(paramHash) updateCloudmunchData(paramHash, method = “POST”) deleteCloudmunchData(paramHash) parseResponse(responseJson) generateServerURL(paramHash, appendQueryParams = nil)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 25

def downloadKeys(filekey, context, contextid)

  paramHash = Hash.new
  paramHash["context"]     = context
  paramHash["contextID"]   = contextid
  paramHash["file"]        = filekey

  keyString = getCloudmunchData(paramHash)
  
  if !keyString.to_s == ""
    log("ERROR", "downloaded key content is empty, please re-upload key and try")
    return nil
  end
  
  filename = "keyfile_" + Time.now.strftime('%Y%m%d%H%M%S%L')
  file     = @appContext.get_data("{workspace}")+"/"+filename
  
  File.open(file, 'w') { |file| file.write(keyString) }
  system('chmod 400 '+file)
  
  if @keyArray.nil?
    @keyArray = []
  end

  @keyArray.push(file)
  return file;
end

#generateServerURL(paramHash, appendQueryParams = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 146

def generateServerURL(paramHash, appendQueryParams = nil)

  serverurl = ""
  
  if !paramHash["url"].nil?
      serverurl = @appContext.get_data("{master_url}")+"/"+paramHash["url"]
  elsif !paramHash["context"].nil?
      paramContext       = paramHash["context"].nil? ? nil : paramHash["context"]
      paramContextID     = paramHash["contextID"].nil? ? nil : paramHash["contextID"]
      paramSubContext    = paramHash["subContext"].nil? ? nil : paramHash["subContext"]
      paramSubContextID  = paramHash["subContextID"].nil? ? nil : paramHash["subContextID"]
      paramLeafContext   = paramHash["leafContext"].nil? ? nil : paramHash["leafContext"]
      paramLeafContextID = paramHash["leafContextID"].nil? ? nil : paramHash["leafContextID"]

      serverurl=@appContext.get_data("{master_url}")+"/applications/"+@appContext.get_data("{application}")+"/"+paramContext

      if !paramContextID.nil? && !paramContextID.empty?
          serverurl=serverurl+"/"+paramContextID;
          if !paramSubContext.nil? && !paramSubContext.empty?
              serverurl=serverurl+"/"+paramSubContext;              
              if !paramSubContextID.nil? && !paramSubContextID.empty?
                  serverurl=serverurl+"/"+paramSubContextID;
                  if !paramLeafContext.nil? && !paramLeafContext.empty?
                      serverurl=serverurl+"/"+paramLeafContext;
                      if !paramLeafContextID.nil? && !paramLeafContextID.empty?
                          serverurl=serverurl+"/"+paramLeafContextID;
                      end
                  end
              end
          end
      end
  else
      log("DEBUG", "No context provided for get api call");
      return nil
  end

  queryString = ""
  
  if !appendQueryParams.nil?
    queryString = queryString + "filter=" + paramHash["filter"].to_json + "&" if !paramHash["filter"].nil?
    queryString = queryString + "file="   + paramHash["file"].to_s      + "&" if !paramHash["file"].nil?
    queryString = queryString + "fields=" + paramHash["fields"].to_s    + "&" if !paramHash["fields"].nil?
    queryString = queryString + "count="  + paramHash["count"].to_s     + "&" if !paramHash["count"].nil?
    queryString = queryString + "offset=" + paramHash["offset"].to_s    + "&" if !paramHash["offset"].nil?
    queryString = queryString + "request_category=" + paramHash["requestCategory"].to_s + "&" if !paramHash["requestCategory"].nil?
    queryString = queryString + "order_by=" + paramHash["orderBy"].to_s + "&" if !paramHash["orderBy"].nil?
    queryString = queryString + "group_by=" + paramHash["groupBy"].to_s + "&" if !paramHash["groupBy"].nil?
  end
  serverurl = serverurl+"?"+queryString+"apikey="+@appContext.get_data("{api_key}")
  return serverurl
end

#getCloudmunchData(paramHash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 58

def getCloudmunchData(paramHash)
  serverurl = nil
  serverurl = generateServerURL(paramHash,true)

  if serverurl.nil?
      log("DEBUG", "Unable to generate server url")
      log("ERROR", "Unable to get data from cloudmunch")    
      return nil
  end

  uri = URI.parse(serverurl)              
  
  log("DEBUG", "URI for get : ")
  log("DEBUG", uri)          
  
  responseJson = Net::HTTP.get(uri)
  
  parseResponse(responseJson)
end

#getCloudmunchDataBKUP(paramHash) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cloudmunch_Ruby_sdk_v3/CloudmunchService.rb', line 109

def getCloudmunchDataBKUP(paramHash)
  serverurl = nil
  serverurl = generateServerURL(paramHash,true)

  if serverurl.nil?
      log("DEBUG", "Unable to generate server url")
      log("ERROR", "Unable to get data from cloudmunch")    
      return nil
  end

  uri = URI.parse(serverurl)              
  responseJson = Net::HTTP.get(uri)
  
  parseResponse(responseJson)
end

#getDataForContext(server, endpoint, param) ⇒ Object

def self.updateDataContext(server, endpoint, param)

putCustomDataContext(server, endpoint, param)

end



242
243
244
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 242

def getDataForContext(server, endpoint, param)
    getCustomDataContext(server, endpoint, param)    
end

#getIntegrationWithID(integrationID) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cloudmunch_Ruby_sdk_v3/CloudmunchService.rb', line 52

def getIntegrationWithID(integrationID)
    if !integrationID.nil?
      paramHash = Hash.new
      paramHash["context"] = "integrations"
      paramHash["contextID"] = integrationID

      integration = getCloudmunchData(paramHash)

#        log("DEBUG",integration)
      if integration.nil?
        return nil
      else
        if integration["configuration"]
            return integration["configuration"]
        else
            return nil
        end
      end
    else
      return nil
    end        
end

#getResource(type) ⇒ Object

getResource(type) Return resources available for a given type



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudmunch_Ruby_sdk_v3/CloudmunchService.rb', line 31

def getResource(type)
  if !type.nil?
    paramHash = Hash.new
    paramHash["context"] = "resources"
    paramHash["filter"] = {"type" => type}
    paramHash["fields"] = "*"

    resource = getCloudmunchData(paramHash)

    if resource.nil?
      log("DEBUG", "Unable to retrieve resource for this type")
      return nil
    else
      return resource
    end
  else
    log("DEBUG", "Type needs to be provided to retrieve resource!")
    return nil
  end
end

#parseResponse(responseJson) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 121

def parseResponse(responseJson)
  begin
      JSON.parse(responseJson)
  rescue
      return responseJson
  end

  requestDetails = (JSON.load(responseJson))['request']
  responseData   = (JSON.load(responseJson))['data']

  log("DEBUG", "Response : ")
  log("DEBUG", responseJson)
  
  if !requestDetails['status'].nil? && requestDetails['status'].casecmp('success') == 0    
      return responseData
  else
      if !requestDetails['message'].nil?
        log("ERROR", requestDetails['message'])
      end
      return nil
  end
end

#updateCloudmunchData(paramHash, method = "POST") ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 78

def updateCloudmunchData(paramHash, method = "POST") 
    paramData = Hash.new
    paramData["data"] = paramHash["data"]
    
    serverurl = nil
    serverurl = generateServerURL(paramHash)

    if serverurl.nil?
        log("DEBUG", "Unable to generate server url")
        log("ERROR", "Unable to "+method+" data on cloudmunch")    
        return nil
    end

    uri = URI.parse(serverurl)              
    
    log("DEBUG", "URI for "+method+" : ")
    log("DEBUG", uri)          
    
    if method.casecmp("post") == 0
      responseJson = Net::HTTP.post_form(uri,"data" => paramData.to_json)
    elsif method.casecmp("patch") == 0
      #code for patch
    elsif method.casecmp("put") == 0
      #code for put
    end
    return parseResponse(responseJson.body)      
end

#updateDataForContext(server, endpoint, param) ⇒ Object



246
247
248
# File 'lib/cloudmunch_Ruby_sdk_v2/CloudmunchService.rb', line 246

def updateDataForContext(server, endpoint, param)
    putCustomDataContext(server, endpoint, param)  
end