Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Configure DHCP Relay for Logical Router #602

Open
F3rC opened this issue Sep 25, 2019 · 13 comments
Open

Configure DHCP Relay for Logical Router #602

F3rC opened this issue Sep 25, 2019 · 13 comments

Comments

@F3rC
Copy link

F3rC commented Sep 25, 2019

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
Copy link
Contributor

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
Copy link
Author

F3rC commented Sep 25, 2019

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
Copy link
Author

F3rC commented Sep 25, 2019

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

@alagoutte
Copy link
Contributor

Yes, it is the idea

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

@F3rC F3rC closed this as completed Sep 26, 2019
@F3rC
Copy link
Author

F3rC commented Sep 26, 2019

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 F3rC reopened this Sep 26, 2019
@F3rC
Copy link
Author

F3rC commented Sep 26, 2019

@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><title>HTTP Status 405 – Method Not Allowed</title><style type="text/css">h1
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body
{font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line
{height:1px;background-color:#525D76;border:none;}</style>

HTTP Status 405 – Method Not Allowed


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

  • Invoke-NsxWebRequest -method post -URI '/api/4.0/edges/edge-11/dhcp/c ...
  •   + CategoryInfo          : InvalidResult: (Invoke-NsxWebRequest:String) [Invoke-NsxWebRequest], InternalNsxApiException
      + FullyQualifiedErrorId : NsxAPIFailureResult,Invoke-NsxWebRequest`
    

@alagoutte
Copy link
Contributor

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
Copy link
Author

F3rC commented Sep 27, 2019

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
Copy link
Contributor

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
Copy link
Author

F3rC commented Sep 27, 2019

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
Copy link
Contributor

Thanks for feedback

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

@F3rC
Copy link
Author

F3rC commented Oct 1, 2019

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
Copy link
Contributor

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

There is some internal function for add field or XML body

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants