vmsiul
Enthusiast
Enthusiast

vRA 8.6 | date-time input set minimum and maximum values

Jump to solution

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!

inputs:
  leaseDate:
    type: string
    title: Lease Date
    format: date-time

(...)

Cloud_vSphere_Machine_1:
    type: Cloud.vSphere.Machine
    properties:
      image: Images
      abxaction: 'yes'
      leaseDate: '${input.leaseDate}'

 

Using date-format and using a min (numeric 60) and max (numeric 120) to try to limit errors out with the following:

vmsiul_0-1649189222824.png

The article for reference is here:

https://vmwarecode.com/2021/08/20/how-to-provide-vm-lease-date-during-the-submission-of-vm-build-in-...

 

 

0 Kudos
2 Solutions

Accepted Solutions
Ankush11s
VMware Employee
VMware Employee

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 

View solution in original post

0 Kudos
johnbowdre
Enthusiast
Enthusiast

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.

View solution in original post

0 Kudos
11 Replies
Ankush11s
VMware Employee
VMware Employee

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 

0 Kudos
vmsiul
Enthusiast
Enthusiast

Hello, 

Thanks for your reply. What do you mean, could you please provide an example? You are referring to the yaml code? 

0 Kudos
Ankush11s
VMware Employee
VMware Employee

No I meant abx action  If you are comfortable with Python then putting a logic should be easy 

0 Kudos
vmsiul
Enthusiast
Enthusiast

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. 

0 Kudos
johnbowdre
Enthusiast
Enthusiast

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.

0 Kudos
vmsiul
Enthusiast
Enthusiast

Hi John, That sounds great, let me give it a go and update the feed. Thanks! 

0 Kudos
vmsiul
Enthusiast
Enthusiast

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

vmsiul_0-1649261437121.png

Thanks! 

0 Kudos
Ankush11s
VMware Employee
VMware Employee

@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/

 

 

 

johnbowdre
Enthusiast
Enthusiast

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

0 Kudos
vmsiul
Enthusiast
Enthusiast

Works great. thank you so much John. I learned one more thing today. 🙂 

0 Kudos
johnbowdre
Enthusiast
Enthusiast

Awesome, I'm glad I could help! I learned a bit in the process too so it's a win-win.