Class: AssetHelper

Inherits:
Object
  • Object
show all
Includes:
CloudmunchService, Util
Defined in:
lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb,
lib/cloudmunch_Ruby_sdk_v3/AssetHelper.rb

Instance Method Summary collapse

Methods included from Util

getJSONArgs, log, logClose, logInit, logIt, openJSONFile

Methods included from CloudmunchService

#deleteCloudmunchData, #deleteKeys, #downloadKeys, #generateServerURL, #getCloudmunchData, #getCloudmunchDataBKUP, getCustomDataContext, #getDataForContext, #getIntegrationWithID, #getResource, http_get, http_post, #parseResponse, putCustomDataContext, #updateCloudmunchData, #updateDataForContext

Constructor Details

#initialize(appContext) ⇒ AssetHelper

Returns a new instance of AssetHelper.



23
24
25
26
# File 'lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb', line 23

def initialize(appContext)
    @appContext = appContext
    @cloudmunchDataService = CloudmunchService.new 
end

Instance Method Details

#addAsset(assetName, assetType, assetStatus, assetExternalRef, assetData) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb', line 51

def addAsset(assetName, assetType, assetStatus, assetExternalRef, assetData)
    if assetName.empty? || assetType.empty? || assetStatus.empty? then
        log("ERROR", "Asset name ,status and type need to be provided") 
        return false
    end

    statusArray = [::STATUS_RUNNING, ::STATUS_STOPPED, ::STATUS_NIL]
    if !statusArray.include?(assetStatus) then
        log("ERROR", "Invalid status sent. Allowed values " + ::STATUS_RUNNING, ::STATUS_STOPPED, ::STATUS_NIL) 
        return false
    end

    assetData["name"] = assetName
    assetData["type"] = assetType
    assetData["status"] = assetStatus
    assetData["external_reference"] = assetExternalRef

    serverUrl = @appContext.getMasterURL() + "/applications/" + @appContext.getProject() + "/assets/"
    retResult = @cloudmunchDataService.updateDataForContext(serverUrl, @appContext.getAPIKey(), assetData)

    if retResult == nil then 
        return nil
    else
        retResult = JSON.parse(retResult)
        retData = retResult["data"]
        if retData.nil? then 
            return nil
        else
            return retData
        end
    end

end

#checkIfAssetExists(assetID) ⇒ Object



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

def checkIfAssetExists(assetID)
    serverUrl = @appContext.getMasterURL() + "/applications/" + @appContext.getProject() + "/assets/" + assetID

    assetResult = @cloudmunchDataService.getDataForContext(serverUrl, @appContext.getAPIKey(), "")
    if assetResult == nil then  
        return nil
    else
        assetResult = JSON.parse(assetResult)
        assetData = assetResult["data"]
        if assetData.nil? then
            log("ERROR", "Asset does not exist") 
            return false
        else
            return true
        end
    end
end

#deleteAsset(assetID) ⇒ Object



91
92
93
94
# File 'lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb', line 91

def deleteAsset(assetID)
    serverUrl = @appContext.getMasterURL() + "/applications/" + @appContext.getProject() + "/assets/" + assetID
    @cloudmunchDataService.deleteDataForContext(serverUrl, @appContext.getAPIKey())
end

#getAsset(assetID, filterData) ⇒ Object



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

def getAsset(assetID, filterData)
    paramHash = {}
    if !filterData.nil?
        paramHash["filter"] = filterData.to_json
    end
    serverUrl = @appContext.getMasterURL() + "/applications/" + @appContext.getProject() + "/assets/" + assetID
    assetResult = @cloudmunchDataService.getDataForContext(serverUrl, @appContext.getAPIJey(), paramHash)

    if assetResult == nil then 
        log("ERROR", "Unable to get data from cloudmunch") 
        return nil
    else
        assetResult = JSON.parse(assetResult)
        assetData = assetResult["data"]
        if assetData.nil? then
            log("ERROR", "Asset does not exist") 
            return nil
        else
            return assetData
        end
    end
end

#updateAsset(assetID, assetData) ⇒ Object



86
87
88
89
# File 'lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb', line 86

def updateAsset(assetID, assetData)
    serverUrl = @appContext.getMasterURL() + "/applications/" + @appContext.getProject() + "/assets/" + assetID
    @cloudmunchDataService.updateDataForContext(serverUrl, @appContext.getAPIKey(), assetData)
end

#updateStatus(assetID, status) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cloudmunch_Ruby_sdk_v2/AssetHelper.rb', line 96

def updateStatus(assetID, status)
    statusArray = [::STATUS_RUNNING, ::STATUS_STOPPED, ::STATUS_NIL]
    if !statusArray.include?(status) then
        log("ERROR", "Invalid status") 
        return false
    end

    assetData = {}
    assetData["status"] = status
    updateAsset(assetID, assetData)
end