vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 89 forks source link

Configure DHCP Relay for Logical Router #602

Open F3rC opened 5 years ago

F3rC commented 5 years ago

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.

alagoutte commented 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

F3rC commented 5 years ago

Hi @alagoutte. Thanks for your reply.

I found the document and the section you mentioned. I will take a look. Thanks for the help.

F3rC commented 5 years ago

@alagoutte I have no experience with API calls. Can I use the 'Invoke-NsxRestMethod' function to configure the DHCP Relay Agents?

alagoutte commented 5 years ago

Yes, it is the idea

You can look on PowerNSX.psm1 for example Get-NsxSSLVpn or Set-NsxSslVpn for example

F3rC commented 5 years ago

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

F3rC commented 5 years ago

@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>HTTP Status 405 – Method Not Allowed

HTTP Status 405 – Method Not Allowed

<hr class="line" />

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.


At line:22 char:1

alagoutte commented 5 years ago

Do you have try to use PUT ?

(Do you plan to try to include direclty on powernsx), can be reuse by some other people...

F3rC commented 5 years ago

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.

alagoutte commented 5 years ago

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...

F3rC commented 5 years ago

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.

alagoutte commented 5 years ago

Thanks for feedback

a good idea, it will be to add missing cmdlet for DHCP Relay (can be usefull for other...)

F3rC commented 5 years ago

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 : &apos;description&apos;.</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.

alagoutte commented 4 years ago

hi @F3rC, do you have found how to fix the issue ?

There is some internal function for add field or XML body