How to update the network profile in VRA 7.X using Rest API -External Profile

Recently I was working on issue where we want to update the network ranges of specific network profile.

Unfortunately I was having difficulty because it was greyed out as below screenshot.

I also tried to implement the KB68142 , which does the work 90% , But unfortunately it did not work for me , But no worries , VMware has given API for almost every piece of work in VRA .

So now question is how do we achieve this using API and how do we do this in good easy manner.

I have been working on VRA7.X APIs and manage to make one master class in Module.

No need to worry about if you are not from python background or do not know code, Just follow the below instruction and we are good .

  1. Please follow the detailed instruction here python installation and required module before we use this module -do not skip this step.
  2. Download the file and unzip and copy the file by name VRA76 and paste the file in directory where python is installed
    general path in windows is : C:\Users\Administrator\AppData\Local\Programs\Python\Python39
  3. I am using Python 3.9 for example ,It will be different as per your version.
  4. Once the step 1-3 is completed , all we need to do is run the below commands.
from VRA76 import vraclass
obj = vraclass('vrafqdn', 'Username@domain.com', 'password', "tenantname")
obj.list_Network_Profile()

It will list the all the network profile with details of ID , start IP and End IP etc , Below is the sample.

As you see I have 2 external network profile in my environment, as per GUI screenshot above there is no edit button which is enabled , But still we will update the details with below mentioned command.

All we need to do is copy the ID from above list command and provide it in update command. I am using b4d7e25c-ca31-4aa5-b6aa-a222b5dd3756

obj.update_Netork_Profile(profileid='b4d7e25c-ca31-4aa5-b6aa-a222b5dd3756', beginIP='192.168.0.200', endIP='192.168.0.250', defaultGateway='192.168.0.2')

Now If you see in GUI , we see changes for default gateway which is greyed out now also ,but has all the update we gave and Ips are also changed.

Below is the actual source code for the same, If anyone wanted to review and understand.

import requests
import json
from prettytable import PrettyTable
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class vraclass:

    def __init__(self, vraurl, username, password, tenant):
        self.username = username
        self.password = password
        self.vraurl = vraurl
        self.tenant = tenant

    def vralogin(self):
        url = f"https://{self.vraurl}/identity/api/tokens"
        payload = f'{{"username":"{self.username}","password":"{self.password}","tenant":"{self.tenant}"}}'
        headers = {
            'accept': "application/json",
            'content-type': "application/json",
        }
        response = requests.request("POST", url, data=payload, headers=headers, verify=False)
        outp = response.json()['id']
        token = "Bearer " + outp
        return token

    def list_tenant(self):
        mainurl = f"https://{self.vraurl}/identity/api/tenants"
        vraheaders = {
            'accept': "application/json",
            "authorization": self.vralogin()
        }
        output = requests.request("GET", mainurl, headers=vraheaders, verify=False)
        result = output.json()['content']
        resulTable = PrettyTable(['Type', 'ID', 'URLName', 'Name', 'Description', 'ContactEmail', 'DefaultTenant'])
        print(resulTable)
        for i in result:
            resulTable.add_row(
                (i['@type'], i['id'], i['urlName'], i['name'], i['description'], i['contactEmail'], i['defaultTenant']))
        print(resulTable)

    def create_tenant(self, tenantname, urlName, Description, ContactEmail):
        url = f"https://{self.vraurl}/identity/api/tenants/{urlName}"

        payload = f'{{"@type":"Tenant","id":"{tenantname}","urlName":"{urlName}","name":"{tenantname}","description":"{Description}","contactEmail":"{ContactEmail}","defaultTenant":"false"}}'
        print(type(payload))
        headers = {
            'accept': "application/json",
            'content-type': "application/json",
            "authorization": self.vralogin()

        }
        output = requests.request("PUT", url, data=payload, headers=headers, verify=False)

    
    def list_Network_Profile(self):
        url = f"https://{self.vraurl}/iaas-proxy-provider/api/network/profiles"
        headers = {
            'accept': "application/json",
            'authorization': self.vralogin(),
            'content-type': "application/json"
        }
        output = requests.request("GET", url, headers=headers, verify=False)
        result = output.json()['content']

        result_table = PrettyTable(
            ['ID', 'NAME', 'CreationDate', 'LastModifiedDate', 'StartIPAddress', 'EndIPAddress', 'SubnetMask',
             'GatewayAddress', 'PrimaryDnsAddress'])
        for i in result:

            result_table.add_row(
                (i['id'], i['name'], i['createdDate'], i['lastModifiedDate'], i['definedRanges'][0]['beginIPv4Address'],
                 i['definedRanges'][0]['endIPv4Address'], i['subnetMask'], i['gatewayAddress'], i['primaryDnsAddress'])
            )
        print(result_table)

    def update_Netork_Profile(self, profileid, beginIP, endIP, defaultGateway):
        url = f"https://{self.vraurl}/iaas-proxy-provider/api/network/profiles/{profileid}"
        headers = {
            'accept': "application/json",
            'authorization': self.vralogin(),
            'content-type': "application/json"
        }
        output = (requests.request("GET", url, headers=headers, verify=False)).json()
        output['definedRanges'][0]['beginIPv4Address'] = beginIP
        output['definedRanges'][0]['endIPv4Address'] = endIP
        output['gatewayAddress'] = defaultGateway
        #output['subnetMask'] = subnetMask
        payload = json.dumps(output)
        finalcall = requests.request("PUT", url, data=payload, headers=headers, verify=False)
        print(finalcall)





    def list_catalog(self, catalogname=None,ServiceName=None):
        if not (catalogname is None) and (ServiceName is None):
            #url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews"
            url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews?$filter=name+eq+%27{catalogname}%27"
        elif (catalogname is None) and not (ServiceName is None):
            url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews?$filter=service/name+eq+%27{ServiceName}%27"
        elif (catalogname is None) and (ServiceName is None):
            url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews"

        headers = {
            'accept': "application/json",
            'authorization': self.vralogin(),
            'content-type': "application/json"
        }
        output = requests.request("GET", url, headers=headers, verify=False).json()['content']
        view = PrettyTable(
            ['TenantName', 'CatalogName', 'CatalogItemID', 'Description', 'CreationDate', 'LastModified', 'ServiceName',
             'RequestTemplateLink', 'SubmitRequestLink'])
        for i in output:
            view.add_row(
                (i['entitledOrganizations'][0]['tenantLabel'], i['name'], i['catalogItemId'], i['description'],
                 i['dateCreated'], i['lastUpdatedDate'], i['serviceRef']['label'], i['links'][0]['href'],
                 i['links'][1]['href'])
            )

        return print(view)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s