RavenDB - Update Properties within a Document

Octopus.Script exported 2015-11-26 by timhunt303 belongs to ‘RavenDB’ category.

Retrieves the specified document by a Raven Query, updates selected variables with values from octopus Variables. Replaces the current document with the newly created document with the updated values. IMPORTANT: Any variable that is being updated MUST have an Octopus Variable that has exactly the same name (including capitals, any special characters, etc.) prefixed with “Property_”. This is the case of BOTH document variables and Metadata variables. I.E. if you wanted TestMode change, you MUST have an Octopus Variable named Property_TestMode (same name, and capitals, etc.).

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

URL of the Database

ravenDatabaseURL = http://localhost:8080/

The URL of the database.

For example: http://localhost:8080/

Name of the Database

ravenDatabaseName

Name of the database in Raven

Name of the Document

ravenDocumentName

Name of the document in Raven that the program will retrieve.

List of Included Document Variables

includeSettingList

A List of document variables that the program will update based on values within Octopus Variables

IMPORTANT: The names of the variables MUST be the same (including capitals, special characters, etc.)

List of Excluded Document Variables

excludeSettingList

A list of document variables that the step will exclude from the updated version.

For example: if TestMode is in both the include and exclude document list, then TestMode will be excluded from the update.

List of Included Metadata Variables

includeMetadataList

A List of Metadata variables that the program will update based on values within Octopus Variables

IMPORTANT: The names of the variables MUST be the same (including capitals, special characters, etc.)

List of Excluded Metadata Variables

excludeMetadataList

A list of Metadata variables that the step will exclude from the updated version.

For example: if Raven-Entity-Name is in both the include and exclude metadata lists, then Raven-Entity-Name will be excluded from the update.

Script body

Steps based on this template will execute the following PowerShell script.


#Variables

#--------------------------------------------------------------------
#RavenDB database variables

#URL address of RavenDB
$ravenDatabaseURL = $OctopusParameters["ravenDatabaseURL"]

#Name of the database
$ravenDatabaseName = $OctopusParameters["ravenDatabaseName"]

#--------------------------------------------------------------------
#RavenDB Query variables

#Raven Query
#$ravenQuery = $OctopusParameters["ravenQuery"]

#Name of the settings document
$ravenDocumentName = $OctopusParameters["ravenDocumentName"]

#--------------------------------------------------------------------
#Setting Variables

#list of settings variables that are to be changed
$includeSettingList = $OctopusParameters["includeSettingList"]

#list of settings variables that are NOT to be changed
$excludeSettingList = $OctopusParameters["excludeSettingList"]

#--------------------------------------------------------------------
#Metadata variables

#list of metadata variables that are to be changed
$includeMetadataList = $OctopusParameters["includeMetadataList"]

#list of metadata variables that are NOT to be changed
$excludeMetadataList = $OctopusParameters["excludeMetadataList"]


#--------------------------------------------------------------------
#other variables

$octopusVariableList = $OctopusParameters.GetEnumerator()



Write-Output "`n-------------------------`n"
#--------------------------------------------------------------------
#checks to see if the entered database exists, return a Boolean value depending on the outcome
function doesRavenDBExist([string] $databaseChecking, [string]$URL)
{
    #retrieves the list of databases at the specified URL
    $database_list = Invoke-RestMethod -Uri "$ravenDatabaseURL/databases" -Method Get
    #checks if the database is at the specified URL
    if ($database_list -contains $databaseChecking.ToString()) 
    {
        return $TRUE
    }
    else 
    {
        return $FALSE
    }

    

}#ends does ravenDB exist function


Write-Output "`n-------------------------`n"
#--------------------------------------------------------------------    
#check to see if the database exists
       

Write-Output "Checking if $ravenDatabaseName exists"

$database_exists = doesRavenDBExist -databaseChecking $ravenDatabaseName -URL $ravenDatabaseURL


#only proceeds if database exists
if ($database_exists -eq $TRUE)
{
    Write-Output "$ravenDatabaseName exists"
            
}#ends database exists if statement 
else 
{
    Write-Error "$ravenDatabaseName doesn't exists. `nMake sure the database exists before continuing" -ErrorId E4
    Exit 1
}


Write-Output "`n-------------------------`n"   
         
#--------------------------------------------------------------------
#Get current setings and change them accordingly

$allSettingsJSON = $null

Write-Output "Getting Document: $ravenDatabaseName"

$settingsURI = "$ravenDatabaseURL/databases/$ravenDatabaseName/docs/$ravenDocumentName"

    

try {
    #Gets settings from the specific Uri
    $allSettings = Invoke-RestMethod -Uri $settingsURI -Method Get

} catch {
    if ($_.Exception.Response.StatusCode.Value__ -ne 404) {
  
    $_.Exception
    }
}

#check to make sure the query return some results
if($allSettings -eq $null)
{
    Write-Error "An error occurred while querying the database. `nThe query did not return any values. `nPlease enter a new query" -ErrorId E4
    Exit 1
}

$includeList = @()

($includeSettingList.Split(", ") | ForEach {
    $includeList += $_.ToString()
})

     
Write-Output "Updating the Settings document"
try
{
    

    #changes the values of the included settings within the original settings document to values from Octopus Variables
    for($i = 0; $i -lt $includeList.length; $i++)
    {
        
        
        #checks if the any of the include setting list is in the exclude setting list
        if($excludeSettingList -notcontains $includeList[$i])
        {
            
            
            $octopusVariableList = $OctopusParameters.GetEnumerator()
            
            #loops through the variable list to find the corresponding value to the settings variable
            foreach($varKey in $octopusVariableList)
            {
                
                
                $newSettingVar = $includeList[$i].ToString()
                
                $newSettingVar = "Property_$newSettingVar"
                
                #sets the setting variable to the correct variable in octopus
                if($varKey.Key -eq $newSettingVar)
                {
                    
                    

                    $allSettings.($includeList[$i]) = $varKey.Value 

                }#ends if

            }#ends for each



        }#ends check if settings in excluded list


    }#ends for
}#ends try
catch
{
    Write-Error "An error occurred while trying to find the Setting Variables." -ErrorId E4
    Exit 1
}


Write-Output "Update complete"

Write-Output "`n-----------------------------"

#--------------------------------------------------------------------
#set update metadata information

Write-Output "Updating the Metadata of the document"

$metadata = @{}

$metadataList = @()

($includeMetadataList.Split(", ") | Foreach {
    $metadataList += $_.ToString()
})


try
{
    for($i = 0; $i -lt $metadataList.length; $i++)
    {
    
        if($excludeMetadataList -notcontains $metadataList[$i])
        {
        
            $octopusVariableList = $OctopusParameters.GetEnumerator()
        
            foreach($varKey in $octopusVariableList)
            {
                
                $newMetadataVar = $metadataList[$i]
                
                $newMetadataVar = "Property_$newMetadataVar"

                if($varKey.Key -eq $newMetadataVar)
                {
                    
                    $temp = $metadataList[$i].ToString()
                    
                    $metadata.Add("$temp", $varKey.Value)
                    
                    
                }
            
            }#ends foreach

        }#ends if

    }#Ends for 
}#ends try
catch
{
    Write-Error "An error occurred while trying to find the Metadata Variables." -ErrorId E4
    Exit 1
}


Write-Output "Metadata update complete"



#--------------------------------------------------------------------
#converting settings to a JSON document

Write-Output "Converting settings to a JSON document"

#Converts allSettings to JSON so it can be added to RavenDB
if ($allSettingsJSON -eq $null) 
{
    $allSettingsJSON = ConvertTo-Json -InputObject $allSettings
}



Write-Output "`n-------------------------`n"

#--------------------------------------------------------------------
#inserting settings document

Write-Output "Restoring Document: $ravenDatabaseName . Inserting the new settings document to the database"

#URL to put the JSON document
$putSettingsURI = "$ravenDatabaseURL/databases/$ravenDatabaseName/docs/$ravenDocumentName"

#Puts the settings and metadata in the specified RavenDB
try
{

    Invoke-RestMethod -Uri $putSettingsURI -Headers $metadata -Body $allSettingsJSON -Method Put
        
    Write-Output "New settings have been successfully added to the database"
}
catch
{
    Write-Error "An error occurred while inserting the new settings document to the database" -ErrorId E4
} 


Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "89806198-6216-4034-a934-6de6a3f445b0",
  "Name": "RavenDB - Update Properties within a Document",
  "Description": "Retrieves the specified document by a Raven Query, updates selected variables with values from octopus Variables. Replaces the current document with the newly created document with the updated values.\n**IMPORTANT**: Any variable that is being updated **MUST** have an Octopus Variable that has exactly the same name (including capitals, any special characters, etc.) prefixed with “Property_”. This is the case of **BOTH** document variables and Metadata variables. I.E. if you wanted TestMode change, you **MUST** have an Octopus Variable named Property_TestMode (same name, and capitals, etc.).\n",
  "Version": 50,
  "ExportedAt": "2015-11-26T22:00:53.359+00:00",
  "ActionType": "Octopus.Script",
  "Author": "timhunt303",
  "Parameters": [
    {
      "Name": "ravenDatabaseURL",
      "Label": "URL of the Database",
      "HelpText": "The URL of the database.\n\nFor example: **http://localhost:8080/**",
      "DefaultValue": "http://localhost:8080/",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "ravenDatabaseName",
      "Label": "Name of the Database",
      "HelpText": "Name of the database in Raven",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "ravenDocumentName",
      "Label": "Name of the Document",
      "HelpText": "Name of the document in Raven that the program will retrieve.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "includeSettingList",
      "Label": "List of Included Document Variables",
      "HelpText": "A List of document variables that the program will update based on values within Octopus Variables\n\n**IMPORTANT:** The names of the variables **MUST** be the same (including capitals, special characters, etc.)",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "excludeSettingList",
      "Label": "List of Excluded Document Variables",
      "HelpText": "A list of document variables that the step will exclude from the updated version. \n\nFor example: if TestMode is in both the include and exclude document list, then TestMode will be excluded from the update.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "includeMetadataList",
      "Label": "List of Included Metadata Variables",
      "HelpText": "A List of Metadata variables that the program will update based on values within Octopus Variables\n\n**IMPORTANT:** The names of the variables **MUST** be the same (including capitals, special characters, etc.)",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "excludeMetadataList",
      "Label": "List of Excluded Metadata Variables",
      "HelpText": "A list of Metadata variables that the step will exclude from the updated version. \n\nFor example: if Raven-Entity-Name is in both the include and exclude metadata lists, then Raven-Entity-Name will be excluded from the update.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "\n#Variables\n\n#--------------------------------------------------------------------\n#RavenDB database variables\n\n#URL address of RavenDB\n$ravenDatabaseURL = $OctopusParameters[\"ravenDatabaseURL\"]\n\n#Name of the database\n$ravenDatabaseName = $OctopusParameters[\"ravenDatabaseName\"]\n\n#--------------------------------------------------------------------\n#RavenDB Query variables\n\n#Raven Query\n#$ravenQuery = $OctopusParameters[\"ravenQuery\"]\n\n#Name of the settings document\n$ravenDocumentName = $OctopusParameters[\"ravenDocumentName\"]\n\n#--------------------------------------------------------------------\n#Setting Variables\n\n#list of settings variables that are to be changed\n$includeSettingList = $OctopusParameters[\"includeSettingList\"]\n\n#list of settings variables that are NOT to be changed\n$excludeSettingList = $OctopusParameters[\"excludeSettingList\"]\n\n#--------------------------------------------------------------------\n#Metadata variables\n\n#list of metadata variables that are to be changed\n$includeMetadataList = $OctopusParameters[\"includeMetadataList\"]\n\n#list of metadata variables that are NOT to be changed\n$excludeMetadataList = $OctopusParameters[\"excludeMetadataList\"]\n\n\n#--------------------------------------------------------------------\n#other variables\n\n$octopusVariableList = $OctopusParameters.GetEnumerator()\n\n\n\nWrite-Output \"`n-------------------------`n\"\n#--------------------------------------------------------------------\n#checks to see if the entered database exists, return a Boolean value depending on the outcome\nfunction doesRavenDBExist([string] $databaseChecking, [string]$URL)\n{\n    #retrieves the list of databases at the specified URL\n    $database_list = Invoke-RestMethod -Uri \"$ravenDatabaseURL/databases\" -Method Get\n    #checks if the database is at the specified URL\n    if ($database_list -contains $databaseChecking.ToString()) \n    {\n        return $TRUE\n    }\n    else \n    {\n        return $FALSE\n    }\n\n    \n\n}#ends does ravenDB exist function\n\n\nWrite-Output \"`n-------------------------`n\"\n#--------------------------------------------------------------------    \n#check to see if the database exists\n       \n\nWrite-Output \"Checking if $ravenDatabaseName exists\"\n\n$database_exists = doesRavenDBExist -databaseChecking $ravenDatabaseName -URL $ravenDatabaseURL\n\n\n#only proceeds if database exists\nif ($database_exists -eq $TRUE)\n{\n    Write-Output \"$ravenDatabaseName exists\"\n            \n}#ends database exists if statement \nelse \n{\n    Write-Error \"$ravenDatabaseName doesn't exists. `nMake sure the database exists before continuing\" -ErrorId E4\n    Exit 1\n}\n\n\nWrite-Output \"`n-------------------------`n\"   \n         \n#--------------------------------------------------------------------\n#Get current setings and change them accordingly\n\n$allSettingsJSON = $null\n\nWrite-Output \"Getting Document: $ravenDatabaseName\"\n\n$settingsURI = \"$ravenDatabaseURL/databases/$ravenDatabaseName/docs/$ravenDocumentName\"\n\n    \n\ntry {\n    #Gets settings from the specific Uri\n    $allSettings = Invoke-RestMethod -Uri $settingsURI -Method Get\n\n} catch {\n    if ($_.Exception.Response.StatusCode.Value__ -ne 404) {\n  \n    $_.Exception\n    }\n}\n\n#check to make sure the query return some results\nif($allSettings -eq $null)\n{\n    Write-Error \"An error occurred while querying the database. `nThe query did not return any values. `nPlease enter a new query\" -ErrorId E4\n    Exit 1\n}\n\n$includeList = @()\n\n($includeSettingList.Split(\", \") | ForEach {\n    $includeList += $_.ToString()\n})\n\n     \nWrite-Output \"Updating the Settings document\"\ntry\n{\n    \n\n    #changes the values of the included settings within the original settings document to values from Octopus Variables\n    for($i = 0; $i -lt $includeList.length; $i++)\n    {\n        \n        \n        #checks if the any of the include setting list is in the exclude setting list\n        if($excludeSettingList -notcontains $includeList[$i])\n        {\n            \n            \n            $octopusVariableList = $OctopusParameters.GetEnumerator()\n            \n            #loops through the variable list to find the corresponding value to the settings variable\n            foreach($varKey in $octopusVariableList)\n            {\n                \n                \n                $newSettingVar = $includeList[$i].ToString()\n                \n                $newSettingVar = \"Property_$newSettingVar\"\n                \n                #sets the setting variable to the correct variable in octopus\n                if($varKey.Key -eq $newSettingVar)\n                {\n                    \n                    \n\n                    $allSettings.($includeList[$i]) = $varKey.Value \n\n                }#ends if\n\n            }#ends for each\n\n\n\n        }#ends check if settings in excluded list\n\n\n    }#ends for\n}#ends try\ncatch\n{\n    Write-Error \"An error occurred while trying to find the Setting Variables.\" -ErrorId E4\n    Exit 1\n}\n\n\nWrite-Output \"Update complete\"\n\nWrite-Output \"`n-----------------------------\"\n\n#--------------------------------------------------------------------\n#set update metadata information\n\nWrite-Output \"Updating the Metadata of the document\"\n\n$metadata = @{}\n\n$metadataList = @()\n\n($includeMetadataList.Split(\", \") | Foreach {\n    $metadataList += $_.ToString()\n})\n\n\ntry\n{\n    for($i = 0; $i -lt $metadataList.length; $i++)\n    {\n    \n        if($excludeMetadataList -notcontains $metadataList[$i])\n        {\n        \n            $octopusVariableList = $OctopusParameters.GetEnumerator()\n        \n            foreach($varKey in $octopusVariableList)\n            {\n                \n                $newMetadataVar = $metadataList[$i]\n                \n                $newMetadataVar = \"Property_$newMetadataVar\"\n\n                if($varKey.Key -eq $newMetadataVar)\n                {\n                    \n                    $temp = $metadataList[$i].ToString()\n                    \n                    $metadata.Add(\"$temp\", $varKey.Value)\n                    \n                    \n                }\n            \n            }#ends foreach\n\n        }#ends if\n\n    }#Ends for \n}#ends try\ncatch\n{\n    Write-Error \"An error occurred while trying to find the Metadata Variables.\" -ErrorId E4\n    Exit 1\n}\n\n\nWrite-Output \"Metadata update complete\"\n\n\n\n#--------------------------------------------------------------------\n#converting settings to a JSON document\n\nWrite-Output \"Converting settings to a JSON document\"\n\n#Converts allSettings to JSON so it can be added to RavenDB\nif ($allSettingsJSON -eq $null) \n{\n    $allSettingsJSON = ConvertTo-Json -InputObject $allSettings\n}\n\n\n\nWrite-Output \"`n-------------------------`n\"\n\n#--------------------------------------------------------------------\n#inserting settings document\n\nWrite-Output \"Restoring Document: $ravenDatabaseName . Inserting the new settings document to the database\"\n\n#URL to put the JSON document\n$putSettingsURI = \"$ravenDatabaseURL/databases/$ravenDatabaseName/docs/$ravenDocumentName\"\n\n#Puts the settings and metadata in the specified RavenDB\ntry\n{\n\n    Invoke-RestMethod -Uri $putSettingsURI -Headers $metadata -Body $allSettingsJSON -Method Put\n        \n    Write-Output \"New settings have been successfully added to the database\"\n}\ncatch\n{\n    Write-Error \"An error occurred while inserting the new settings document to the database\" -ErrorId E4\n} \n\n"
  },
  "Category": "RavenDB",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/ravendb-update-properties-within-a-document.json",
  "Website": "/step-templates/89806198-6216-4034-a934-6de6a3f445b0",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRF0Y6OjR8iHx8fW1la0szNmVZZZB8k5crLkIy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iKTrgjH5WPOdxFaFhG71pjR9QM5A3l96FYL4CzGziESn30O6E5C3FX/J0tsWhA6QQxNJm0+N7jaICBcuwy/SshgRsRuyWOmy4VbSu7uTFSBsrItYSmGwfRD/sX3/PYgKq7ozRMgDkINrW1UhKSC7IEfvCteCtNOjXZDjKw4VIFZqqc3cfD9DvrmLWnfnw1wjrGg4akOc8YrmA+TL1Z+6EbGBsQ35dpmh8qaaKSKyULO6CxKLpHl7GX9iUbEyRMfcevdR98zqVWVImEnipXixbnUT5BEiwtch51Z4a0NaHxG5dhf77Jp7bQj3ETErtX56cbc2xPjcYq+1fn43R20Ii93XFi2PWyCuCRfC5sg9xL8T4ud2+bx1q8jeh+qQ1te6u2APR3WIbJaZVWp/UH0IX2QWc78KYSbrWQV3NlaHuCwgsuAe4Bs2+bvy2+fugbTF0+oeiLtgi+ktkLb0/tK7utYV4bhlZi/brG6DyPK74m+BOHnVO+M/iwECCCCAAAIIIIAAAggggAACCCCAAAIIIIAAAggggAACCCCAAAIIIIAAAggggAACCCCAAAIIIIAAAggggAACSM3xR4ABAI1fHCI0qDDgAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, November 26, 2015