Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.

If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!

Update either Server or Role Variables



  • Is it possible to update a variable for a server based on code? For example, can I create a variable called OS and have it populate based on CIM_OperatingSystem or WMI? More specifically edit a variable on a server?

    Product: Otter
    Version: 2.0.13



  • You could use the Variables Management API for this:

    https://inedo.com/support/documentation/otter/reference/api/variables



  • So I have been working on different ways to update a server, and I am having some issues. I can create a new server, or add a new server to a role no problem with the code below. But Updating variables, I am getting this:

    Invoke-RestMethod : 547160Variables_CreateOrUpdateVariable57`The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Variables__Servers".

    $Key= 'blahblah'
    $Server = 'hdarsintsv1'

    $Body =
    '{
    "variables": {
    "Viking": "True",
    },
    }'

    Invoke-RestMethod $URI -Method Post -Body $Body -Verbose

    Any ideas? Thanks!





  • So I am able to update all of the attributes of a server, with the exception of variables. I am able to edit roles, environments, temppath, drift, all without issue. I am not able to change server variables, though. Can anyone test this to confirm it may be a bug, or perhaps I am doing something wrong? Being I can update everything else, I am going to side with the former, but anything's possible.

    $Key= 'blahblah'

    $Server = 'hdarsintsv1'

    $URI = "https://otterserver.test:8626/api/infrastructure/servers/update/$Server`?key=$key"

    $Body =

    '{

     "variables": 
     
     {
         "Viking": "True",
         
     },
    

    }'

    Invoke-RestMethod $URI -Method Post -Body $Body -Verbose


  • inedo-engineer

    That is indeed a bug in that particular API, but in the meantime the variables API can be used as follows:

    $VariableObj = @{
        name = "Viking"
        value = "True"
        server = "hdarsintsv1"	
    }
    
    $Headers = @{
        "X-ApiKey" = "blahblah"
    }
    
    Invoke-RestMethod "http://otterint/api/variables/scoped/single" -Method Post `
        -Body ($VariableObj | ConvertTo-Json) `
        -Headers $Headers -Verbose


  • I am not familiar with this way to do it, and it does not appear to work for me:

    Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.

    This appears to be an issue when using headers, but I have not found a reliable way to fix this.


  • inedo-engineer

    You can adapt it to your method (i.e. just use JSON directly as you did in your previous example), I just already had this script handy and don't like putting the API key in the query string if I can avoid it.



  • Thanks John. I did try a few variations, and after I ended up renaming a server trying to get your method to work, I decided to stop trying. I am not very familiar with the format you are using, and it is different from the documentation. However, I agree that not adding the key to the URL string is probably a safer way to do it, and that process and examples should be added to the documentation.

    I still am not able to update a variable, but if one creates a new server, then you can add variables as long as it's a create. Maybe that will help whoever is going to fix it.


  • inedo-engineer

    Ahh I see the confusion now. The important bit is that I am using a completely different API (i.e. the Variables Management API that Tod originally mentioned at /api/variables, not the Infrastructure API at /api/infrastructure) to accomplish the same task as a workaround. These endpoints are also far more powerful for variables specifically (i.e. you can edit multiple at once, handle multi-scoping, etc.)

    As I mentioned, what you did originally is supposed to work, and there is a bug filed to fix for the next release: https://inedo.myjetbrains.com/youtrack/issue/OT-257



  • Alright, after messing around with that information, I was able to get it to work. I am able to update the variables. Interesting thing is that it doesn't appear I can change just one variable, I have to change all of them, or at least enter all of them as they are?

    Thanks for your help!


Log in to reply
 

Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation