Subscribe
Accepted Solution

Update Annotations using REST API in OCI

I got the error when I tried to invoke rest-api put method with the URL. URL for put is : "https://localhost/rest/v1/assets/internalVolumes/{id}/annotations" .

When I invoked rest api it gave error as 500 : Internal Server Error. Has anybody tried to update (put) fields(annotations) in OCI using rest Api?

 

 

 

 

Re: Update Annotations using REST API in OCI

Hi,

 

Here is the PowerShell code from a WFA workflow i've used in my environment to successfully set OCI annotations on internal volumes.

 

Param(
   [Parameter(Mandatory = $True, HelpMessage="The OCI hostname to poll")]
   [String]$Host,
   [Parameter(Mandatory = $True, HelpMessage="The OCI port number (default: 443)")]
   [Int]$Port=443,
   [Parameter(Mandatory = $True, HelpMessage="The cluster name or IP Address")]
   [String]$ClusterName,
   [Parameter(Mandatory = $True, HelpMessage="The volume name")]
   [String]$VolumeName,
   [Parameter(Mandatory = $True, HelpMessage="The vserver name")]
   [String]$VserverName,
   [Parameter(Mandatory = $True, HelpMessage="The OCI annotation name")]
   [String]$AnnotationName,
   [Parameter(Mandatory = $True, HelpMessage="The OCI annotation value")]
   [String]$AnnotationValue
)
#'------------------------------------------------------------------------------
#'Function to create an OCI connection
#'------------------------------------------------------------------------------
Function New-OCIConnection{
   Param(
      [parameter(mandatory = $False, position=0)]
      [String]$Hostname,
      [parameter(Mandatory = $False, position=1)]
      [String]$Port,
      [parameter(Mandatory = $False, position=2)]
      [String]$Username,
      [parameter(Mandatory = $False, position=3)]
      [String]$Password
   )
   #'---------------------------------------------------------------------------
   #'use command line paramters if specified
   #'---------------------------------------------------------------------------  
   If($Username -And $Password){
      $AuthInfo = ( "{0}:{1}" -f $Username, $Password )
   }Else{
      Try{
         $Hostname    = Get-WfaRestParameter "host"
         $Port        = Get-WfaRestParameter "port"
 	     $Credentials = Get-WfaCredentials -Host $HostName
      }Catch{
         Throw "Failed enumerating data source credentials for ""$HostName"""
      }
      $AuthInfo = ("{0}:{1}" -f $Credentials.Username, $Credentials.Password )
   }
   #'---------------------------------------------------------------------------   
   #'Build authentication header
   #'---------------------------------------------------------------------------   
   $AuthInfo = [System.Text.Encoding]::UTF8.GetBytes( $AuthInfo )
   $AuthInfo = [System.Convert]::ToBase64String( $AuthInfo )
   $Headers  = @{ Authorization=( "Basic {0}" -f $AuthInfo ) }
   #'---------------------------------------------------------------------------
   #'Ignore not trusted certificate - from OCI PS samples
   #'---------------------------------------------------------------------------   
   Add-Type @"
      using System.Net;
      using System.Security.Cryptography.X509Certificates;
      public class OCIDontCarePolicy : ICertificatePolicy {
      public OCIDontCarePolicy() {}
      public bool CheckValidationResult(
         ServicePoint sPoint, X509Certificate cert,
         WebRequest wRequest, int certProb) {
         return true;
      }
   }
"@
   [System.Net.ServicePointManager]::CertificatePolicy = New-Object OCIDontCarePolicy
   $BaseUri = $( "https://" + $Hostname + ":" + $Port )
   If($Port -eq 80 -or $Port -eq 8080 ) {
      $BaseUri = $( "http://" + $Hostname + ":" + $Port )
   }
   #'---------------------------------------------------------------------------
   #'return the values
   #'---------------------------------------------------------------------------   
   $BaseUri, $Headers
}
#'------------------------------------------------------------------------------
#'Function to set the OCI annotation value.
#'------------------------------------------------------------------------------
Function Set-OCIAnnotationFields{
   Param(
      [Parameter(Mandatory = $True, position=0)]
      [String]$AnnotationName,
      [Parameter(Mandatory = $True, position=1)]
      [String]$AnnotationValue
   )
   $objDefinition = [PSCustomObject]@{"name"=$AnnotationName}
   $objList       = [PSCustomObject]@{"rawValue"=$AnnotationValue; "definition"=$objDefinition}
   $OCIList       = [PSCustomObject]@($objList)
   ConvertTo-Json @($OCIList)
}
#'------------------------------------------------------------------------------
#'Enumerate the OCI credentials
#'------------------------------------------------------------------------------
Try{
   $Credentials = Get-WfaCredentials -Host $Host
   $Username    = $Credentials.Username
   $Password    = ConvertFromSecureToPlain -SecurePassword $Credentials.Password
}Catch{
   Throw "Failed enumerating credentials for ""$Host"""
}
#'------------------------------------------------------------------------------
#'create initial OCI connection.
#'------------------------------------------------------------------------------
($BaseUri, $Headers) = New-OCIConnection $Host $Port $Username $Password
#'------------------------------------------------------------------------------
#'Enumerate the the API version.
#'------------------------------------------------------------------------------
Try{
   $Uri   = $BaseUri + "/rest/v1/login"
   $Login = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers
}Catch{
   Throw $("Failed enumerating API login information from OCI server ""$Host"". Error " + $_.Exception.Message)
}
#'------------------------------------------------------------------------------
#'Save it as a double so we get the minor version
#'------------------------------------------------------------------------------
$APIVersion = [double]$Login.apiVersion
Try{
   Get-WFALogger -Info -Message "Enumerating storage information on OCI server ""$Host"""
   $Uri = $BaseUri + "/rest/v1/assets/storages"
   $S   = Invoke-RestMethod -Uri $Uri -Headers $Headers
}Catch{
   Throw $("Failed enumerating storage asset information from OCI server ""$Host"". Error " + $_.Exception.Message)
}
#'------------------------------------------------------------------------------
#'now we have to rotate through the entire list with extensions
#'------------------------------------------------------------------------------
For($i = 0; $i -lt $S.count; $i++){
   If($S[$i].name -eq $ClusterName){
      Try{
         Get-WFALogger -Info -Message "Enumerating internal volume information"
         $Uri = $($BaseUri + "/rest/v1/assets/storages/" + $S[$i].id + "/internalVolumes")
         $IVs = Invoke-RestMethod -Uri $Uri -Headers $Headers
      }Catch{
         Throw $("Failed enumerating internal volume information from OCI server ""$Host"". Error " + $_.Exception.Message)
      }
      #'------------------------------------------------------------------------
      #'match the internal volume
      #'------------------------------------------------------------------------
      If(-Not($VolumeName.Contains(":"))){
         $Name = $($ClusterName + ":" + $VserverName + ":" + $VolumeName)
      }Else{
         $Name = $VolumeName
      }
      Get-WFALogger -Debug -Message "The internal volume name is`: $Name"
      For($j = 0; $j -lt $IVs.count; $j++){
         If($IVs[$j].name -eq $Name){
            #'------------------------------------------------------------------
            #'found the internal volume - set the annotation
            #'------------------------------------------------------------------
           Try{
               Get-WFALogger -Info -Message "Setting the annotation name ""$AnnotationName"" to value ""$AnnotationValue"""
               $Uri = $($BaseUri + "/rest/v1/assets/internalVolumes/" + $IVs[$j].id + "/annotations")
               $A   = Invoke-RestMethod -Method PUT -Uri $Uri -Headers $Headers -Body (Set-OCIAnnotationFields $AnnotationName $AnnotationValue)
            }Catch{
               Throw $("Failed setting annotation ""$AnnotationName"" to value ""$AnnotationValue"". Error " + $_.Exception.Message)
            }
            $j = $IVs.count
         }Else{
            Get-WFALogger -Debug -Message $("The volume """ + $IVs[$j].name + """ does not match internal volume ""$Name""")
         }
      }
      $i = $S.count
   }
}
#'------------------------------------------------------------------------------

Hope that helps

 

/matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

Re: Update Annotations using REST API in OCI

I have added sanscreen  host creadential in WFA and tried to run it and its failing at "Failed enumerating API login information from OCI server", what I might be missing?

also, what is the way to provide credential directly in CLI?

 

the $username and $password parameters are not discovered automatically when I click on discover parameter?

 

Re: Update Annotations using REST API in OCI

Hi,

 

The username and password are enumerated from the credentials varialbe which is set by retrieving them from the WFA credential cache using:

 

$Credentials = Get-WfaCredentials -Host $Host

 Have you added credentials for the OCI host and set the port and timeout?

 

oci.png

 

Note: You might need to modify the port number to meet your requirements

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

Re: Update Annotations using REST API in OCI

Matt, I did added as you showed above, also I throw the user name in script  and confirmed it can get right user name also. I think the issue may be in header. can you give me a command where i can simply replace the put comamnd you ahev with my user name and password for testing purpose and see if it can work, then I can confirm it must be something with the creadentials.

 

for ex:

 $A   = Invoke-RestMethod -Method PUT -Uri $Uri -Headers $Headers -Body (Set-OCIAnnotationFields $AnnotationName $AnnotationValue)

 

insted of $header what should i do to supply user name and password in above line directely?

 

appriciate your help.