Hello,
We are trying to mimic something similar to what vRA7 allowed to us to do in regards to allow the user to specify the number of days they would like to lease. The goal is to allow them to request a minimum of (60 days) and maximum (120 days) and let them set their preference from within that range 60-120.
The article below does a great job and covers exactly what we are trying to do, however we still need to set a min/max limit to the user input so they are aware of that they need to be between the 60-120 days, something outside of that range shouldn't be allowed.
I don't know we can do this with some integer value like in vRA7 (the user will have to type the number of days between 60-120) or the only way is to manage the input with using date-time format.
Any thoughts?
Thanks!
(...)
Using date-format and using a min (numeric 60) and max (numeric 120) to try to limit errors out with the following:
The article for reference is here:
Putting this into Yaml may not currently available but you can put inside the code whether the entered date is less than 60 or beyond
What about making the input an integer (instead of date-time) so that you could specify the minimum/maximum on the request form, and then altering the ABX python script to set the expiration date to "today" + the input lease days?
Something like:
inputs:
[...]
leaseDays:
type: integer
minimum: 60
maximum: 120
Cloud_vSphere_Machine_1:
type: Cloud.vSphere.Machine
properties:
[...]
leaseDays: '${input.leaseDays}'
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from datetime import datetime, timedelta
def handler(context, inputs):
token = inputs['vratoken']
vrafqdn = inputs['vrafqdn']
deploymentid = inputs['deploymentId']
print(token)
print(deploymentid)
leasedays = inputs['requestInputs']['leaseDays']
leasedate = f'{datetime.now() + timedelta(days=leasedays) :%Y-%m-%dT%H:%M:%SZ}'
url = f'https://{vrafqdn}/deployment/api/deployments/{deploymentid}/requests'
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': token
}
data = {
"actionId": "Deployment.ChangeLease",
"inputs": {
'Lease Expiration Date': leasedate
}
}
jdata = json.dumps(data)
apioutput = requests.post(url, headers=headers,data=jdata, verify=False)
print(apioutput)
print(apioutput.json())
Note I haven't tested this... but it seems to work in my head.
Putting this into Yaml may not currently available but you can put inside the code whether the entered date is less than 60 or beyond
Hello,
Thanks for your reply. What do you mean, could you please provide an example? You are referring to the yaml code?
No I meant abx action If you are comfortable with Python then putting a logic should be easy
Ok, the problem with doing it at the action code and not the yaml is that the users will not be aware of the limitation that we are enforcing (yeah we can add some description or info code). In vRA7 if they typed something outside of a valid range they will get an error. something to let them know that their value needed to be between A-Z. It's unfortunate that simple things like this need now to be recreated, I understand the flexibility is needed and useful but some basic functions (out of the box) should be have been left in place.
What about making the input an integer (instead of date-time) so that you could specify the minimum/maximum on the request form, and then altering the ABX python script to set the expiration date to "today" + the input lease days?
Something like:
inputs:
[...]
leaseDays:
type: integer
minimum: 60
maximum: 120
Cloud_vSphere_Machine_1:
type: Cloud.vSphere.Machine
properties:
[...]
leaseDays: '${input.leaseDays}'
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from datetime import datetime, timedelta
def handler(context, inputs):
token = inputs['vratoken']
vrafqdn = inputs['vrafqdn']
deploymentid = inputs['deploymentId']
print(token)
print(deploymentid)
leasedays = inputs['requestInputs']['leaseDays']
leasedate = f'{datetime.now() + timedelta(days=leasedays) :%Y-%m-%dT%H:%M:%SZ}'
url = f'https://{vrafqdn}/deployment/api/deployments/{deploymentid}/requests'
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': token
}
data = {
"actionId": "Deployment.ChangeLease",
"inputs": {
'Lease Expiration Date': leasedate
}
}
jdata = json.dumps(data)
apioutput = requests.post(url, headers=headers,data=jdata, verify=False)
print(apioutput)
print(apioutput.json())
Note I haven't tested this... but it seems to work in my head.
Hi John, That sounds great, let me give it a go and update the feed. Thanks!
Thanks John,
I've updated the yaml code with your suggestions as well as the the action.
The action runs and shows as completed but when I look at the Log I see the following:
<Response [400]>
{'message': "Lease Expiration Date: Input '2022-06-05T16:0426Z' does not match specified format 'date-time'", 'statusCode': 400, 'errorCode': 10006}
When I look at the details TAB I see it's passing the input
"name": "vmclient201", "vm-size": "Medium (2vCPU|4Gb)", "os-image": "2K16", "leaseDays": 60, "sysLocale": "winlocale-en" }, "failureMessage": "", "blueprintVersion": "", "catalogItemVersion": ""
When I look at the Expires on for the resource I still see the Never
Thanks!
@vmsiul I would recommend you to not to provide bearer token in public community , Anyone can decode and do security attacks.
example:
https://jwt.io/
Ah, yep, I missed the required colon between the minutes and seconds fields in the formatting string. That line should be:
leasedate = f'{datetime.now() + timedelta(days=leasedays) :%Y-%m-%dT%H:%M:%SZ}'
(Assuming that it's wanting the date-time string to look like "2022-06-05T16:04:26Z", which is somewhat of a guess on my part.)
Works great. thank you so much John. I learned one more thing today. 🙂
Awesome, I'm glad I could help! I learned a bit in the process too so it's a win-win.