Open F3rC opened 5 years ago
Hi @F3rC, there is not a lot of active dev on PowerNSX
But about configure DHCP Relay Agent on Edge, you can look nsx_64_api.pdf (Google can help for found file) and look page 430 : Working with DHCP Relay
Hi @alagoutte. Thanks for your reply.
I found the document and the section you mentioned. I will take a look. Thanks for the help.
@alagoutte I have no experience with API calls. Can I use the 'Invoke-NsxRestMethod' function to configure the DHCP Relay Agents?
Yes, it is the idea
You can look on PowerNSX.psm1 for example Get-NsxSSLVpn or Set-NsxSslVpn for example
Thanks. I accomplished what I needed using the API guide you suggested and the 'Invoke-NsxWebRequest' function and by pulling the existing xml configuration file.
$Body =<edited xml file content>
Invoke-NsxWebRequest -method put -URI '/api/4.0/edges/edge-11/dhcp/config/relay' -body $Body
@alagoutte Sorry for the multiple questions. Is the POST method not allowed / available for DHCP Relay Agents? It is not documented in the API and when I tried running the same script I got the following error:
`Invoke-NsxWebRequest : Invoke-NsxWebRequest : The NSX API response received indicates a failure. 405 : Method Not Allowed : Response Body: <!doctype html>
Type Status Report
Message Request method 'POST' not supported
Description The method received in the request-line is known by the origin server but not supported by the target resource.
+ CategoryInfo : InvalidResult: (Invoke-NsxWebRequest:String) [Invoke-NsxWebRequest], InternalNsxApiException
+ FullyQualifiedErrorId : NsxAPIFailureResult,Invoke-NsxWebRequest`
Do you have try to use PUT ?
(Do you plan to try to include direclty on powernsx), can be reuse by some other people...
Yes, I tried PUT and it works but that means I need to include the whole XML in the body as opposed to just adding the new / additional required entries. With my basic understanding of the REST methods I thought that with POST I would be able to add just the new DHCP Relay Agents I needed but I might be wrong.
I did run the script with PowerNSX using the Invoke-NsxWebRequest function. But not sure how I understand your question regarding including it directly in PowerNSX.
First I got the XML content using GET:
(Invoke-NsxWebRequest -method GET -URI '/api/4.0/edges/edge-11/dhcp/config/relay').content >> C:\DHCPRelay_xml.txt
Then I edited the XML content and finally I used the edited XML with PUT method. Worked like a charm but again, I had to completely replace the XML instead of being able to add just some additional entries (DHCP Relay Agents).
The actual script I used is the following:
$Body =<edited xml file content>
Invoke-NsxWebRequest -method PUT -URI '/api/4.0/edges/edge-11/dhcp/config/relay' -body $Body
I may be confused about what POST actually does, maybe I need to do some more reading about REST.
the NSX(-V) API work only with this method... need to extract actually body and PUT edited body...
Only some API call support to add...
Got it. Thanks for the clarification. Despite not being able to add entries, I still got the task done so I am happy. I really appreciate your help with this. I will definitely keep exploring NSX (and probably vSphere) API as it seems the most powerful way of automating tasks.
Thanks for feedback
a good idea, it will be to add missing cmdlet for DHCP Relay (can be usefull for other...)
I have tried using the New-NsxLogicalSwitch function as a reference to build one that adds a new DHCP Relay. I noticed 'New-NsxLogicalSwitch' uses the 'Add-XmlElement' function to add a new logical switch to the 'virtualWireCreateSpec' resource. However, this function uses the POST method which is not allowed/supported by the API for DHCP Relays (relay resource). I did build a function and tried both POST and PUT but with POST I always get the 'HTTP Status 405 – Method Not Allowed' message and with PUT I get the following:
invoke-nsxwebrequest : Invoke-NsxWebRequest : The NSX API response received indicates a failure. 400 :
Bad Request : Response Body: <?xml version="1.0" encoding="UTF-8"?>
<error><errorCode>223</errorCode><details>Invalid request. Could not find mapping for element :
'description'.</details></error>
At line:86 char:21
+ ... $response = invoke-nsxwebrequest -method "put" -uri $URI -body $body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (Invoke-NsxWebRequest:String) [Invoke-NsxWebRequest], Intern
alNsxApiException
+ FullyQualifiedErrorId : NsxAPIFailureResult,Invoke-NsxWebRequest
`
Get-NsxLogicalSwitch : Cannot validate argument on parameter 'ObjectId'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At line:89 char:45
+ Get-NsxLogicalSwitch -virtualWireId $response.content -connec ...
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Get-NsxLogicalSwitch], ParameterBindingValidationExceptio
n
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Get-NsxLogicalSwitch`
I am more than glad to continue working on a function that works but may need some guidance as I haven't worked with APIs and XMLs before. If you or anyone else has the availability to work on this I can do the "legwork", may only need someone to point me in the right direction.
Since POST is not an option, the first and most important question I may need an answer for is how can the PUT method be used to update a resource as it is supposed to replace the whole thing? One option would be getting the existing resource, locally updating it and using PUT to replace it on the server side. But I am pretty there must be a better approach.
hi @F3rC, do you have found how to fix the issue ?
There is some internal function for add field or XML body
I was looking for a way to configure DHCP Relay Agents via PowerNSX but there doesn't seem to be one at the moment. Can this be added in a future version? Thanks.