Scripts/ProtectionJobRun/set-cohesitysnapshotretention.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
function Set-CohesitySnapshotRetention {
    <#
        .SYNOPSIS
        Changes the retention of the snapshots associated with a Protection Job.
        .DESCRIPTION
        Changes the retention of the snapshots associated with a Protection Job. Returns success if the retention for snapshots is updated successfully.
        .NOTES
        Published by Cohesity
        .LINK
        https://cohesity.github.io/cohesity-powershell-module/#/README
        .EXAMPLE
        Set-CohesitySnapshotRetention -JobName Test-Job -JobRunId 2123 -ExtendByDays 30
        .EXAMPLE
        Set-CohesitySnapshotRetention -JobName Test-Job -JobRunId 2123 -ReduceByDays 30
    #>

    [CmdletBinding(DefaultParameterSetName = 'ExtendRetention', SupportsShouldProcess = $True, ConfirmImpact = "High")]
    Param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$JobName,
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [long]$JobRunId,
        [Parameter(Mandatory = $true, ParameterSetName = "ExtendRetention")]
        [ValidateNotNullOrEmpty()]
        [long]$ExtendByDays,
        [Parameter(Mandatory = $true, ParameterSetName = "ReduceRetention")]
        [ValidateNotNullOrEmpty()]
        [long]$ReduceByDays,
        [Parameter(Mandatory = $false)]
        [long[]]$SourceIds = $null
    )

    Begin {
        if (-not (Test-Path -Path "$HOME/.cohesity")) {
            throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'"
        }
        $cohesitySession = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json
        $cohesityCluster = $cohesitySession.ClusterUri
        $cohesityToken = $cohesitySession.Accesstoken.Accesstoken
    }

    Process {
        if ($PSCmdlet.ShouldProcess($JobRunId)) {
            $jobs = Get-CohesityProtectionJob
            $job = $jobs | Where-Object { $_.name -eq $JobName }
            if ($null -eq $job) {
                Write-Output "No job found with the name '$JobName'"
                return
            }
            $jobRuns = Get-CohesityProtectionJobRun -JobName $JobName
            if ($null -eq $jobRuns) {
                Write-Output "Job runs not found for job '$JobName'"
                return
            }
            $jobRun = $jobRuns | Where-Object { $_.BackupRun.JobRunId -eq $JobRunId }
            if ($null -eq $jobRun) {
                Write-Output "The job run '$JobRunId' does not exists for job '$JobName'"
                return
            }

            $cohesityClusterURL = $cohesityCluster + '/irisservices/api/v1/public/protectionRuns'
            $cohesityHeaders = @{'Authorization' = 'Bearer ' + $cohesityToken }

            [long]$retentionDays = 0
            if ($ExtendByDays) {
                $retentionDays = $ExtendByDays
            }
            elseif ($ReduceByDays) {
                $retentionDays = - $ReduceByDays
            }
            $targetObject = @{
                type                = "kLocal"
                daysToKeep          = $retentionDays
                holdForLegalPurpose = $null
            }
            $copyRunTargetsObject = @()
            $copyRunTargetsObject += $targetObject
            $jobUidObject = @{
                ClusterId            = $null
                ClusterIncarnationId = $null
                Id                   = $null
            }
            if ($job.IsActive -eq $false) {
                # using a private API for protection job in remote clusters
                # find out the job run for a partcular job
                $backupRunURL = $cohesityCluster + '/irisservices/api/v1/backupjobruns?allUnderHierarchy=true&id=' + $job.id
                $resp = Invoke-RestApi -Method Get -Uri $backupRunURL -Headers $cohesityHeaders
                $searchedJobRun = $resp | Where-Object { $_.backupJobRuns.jobDescription.name -eq $JobName }
                if ($null -eq $searchedJobRun) {
                    Write-Output "Could not find backup run details for inactive job '$JobName'"
                    return
                }
                $jobUidObject.ClusterId = $searchedJobRun.backupJobRuns.jobDescription.primaryJobUid.ClusterId
                $jobUidObject.ClusterIncarnationId = $searchedJobRun.backupJobRuns.jobDescription.primaryJobUid.ClusterIncarnationId
                $jobUidObject.Id = $searchedJobRun.backupJobRuns.jobDescription.primaryJobUid.objectId

            }
            else {
                $jobUidObject.ClusterId = $jobRun.JobUid.ClusterId
                $jobUidObject.ClusterIncarnationId = $jobRun.JobUid.ClusterIncarnationId
                $jobUidObject.Id = $jobRun.JobUid.Id
            }

            $jobRunObject = @{
                copyRunTargets    = $copyRunTargetsObject
                runStartTimeUsecs = $jobRun.CopyRun[0].RunStartTimeUsecs
                jobUid            = $jobUidObject
                sourceIds         = $SourceIds
            }
            $jobRunsObject = @()
            $jobRunsObject += $jobRunObject
            $payload = @{
                jobRuns = $jobRunsObject
            }
            $payloadJson = $payload | ConvertTo-Json -Depth 100
            $resp = Invoke-RestApi -Method Put -Uri $cohesityClusterURL -Headers $cohesityHeaders -Body $payloadJson

            $success = $false
            # there is no response to the API call. Therefore using the response status to identify
            if ($Global:CohesityAPIResponse) {
                if ($Global:CohesityAPIResponse.StatusCode -eq 204) {
                    $success = $true
                }
            }
            if ($success -eq $true) {
                $message = $null
                if ($ExtendByDays) {
                    $message = "Extended the snapshot retention successfully"
                }
                elseif ($ReduceByDays) {
                    $message = "Reduced the snapshot retention successfully"
                }
                $message
            }
            else {
                $errorMsg = "Snapshot retention : Failed to update the snapshot"
                Write-Output $errorMsg
                CSLog -Message $errorMsg
            }
        }
    }

    End {
    }
}