VMware Cloud Community
giffjo
Contributor
Contributor

REST API Add Cloud Zone to project

I am trying to create a project using the API and even though I can get it to work I cannot get cloud zones to populate. I know the format and I know the zoneID but it is not showing in the actual UI when I check on projects. What am I doing wrong. 

            "zones": [

                {

                    "zoneId": "MY ID For Cloud Zone",

                    "priority": 0,

                    "maxNumberInstances": 0,

                    "allocatedInstancesCount": 2,

                    "memoryLimitMB": 0,

                    "allocatedMemoryMB": 0,

                    "cpuLimit": 0,

                    "allocatedCpu": 0,

                    "storageLimitGB": 0,

                    "allocatedStorageGB": 250.0

                }

            ],

 

I enter the right cloud zone ID and it just doesn't populate cloud zones at all. Any help is appreciated. 

Labels (4)
0 Kudos
9 Replies
RebeccaW
Enthusiast
Enthusiast

For onprem 8.8 this looks to be not in Project API but in IaaS API /iaas/api/projects. The Project api POST looks to not have a section for setting the zones just the base Project shell.

In IaaS API instead of zones it is listed as zoneAssignmentConfigurations. Not sure if the url is similar for SaaS version but for onprem check with the swagger to check https://<your vra url>/iaas/api/swagger/ui/#/Project/createProject

0 Kudos
Ankush11s
VMware Employee
VMware Employee


Have a look at below sample 

    def add_Cloudzone_Project(self, projectid, cloudzoneid):
        url = f'https://{self.vrafqdn}/iaas/api/projects/{projectid}?apiVersion={self.apiversion}'
        headers = {
            'accept': "application/json",
            'content-type': "application/json",
            'authorization': self.vratoken()
        }
        existing_data = requests.get(url=url, headers=headers, verify=False)
        if existing_data.status_code == 200:
            if existing_data.json():
                payload = {
                    "zoneAssignmentConfigurations": existing_data.json()['zones']
                }
                new_zone = {
                    "storageLimitGB": 0,
                    "cpuLimit": 0,
                    "memoryLimitMB": 0,
                    "zoneId": f'{cloudzoneid}',
                    "maxNumberInstances": 0,
                    "priority": 0
                }
                payload['zoneAssignmentConfigurations'].append(new_zone)
                output = requests.patch(url=url, headers=headers, data=json.dumps(payload), verify=False)
                if output.status_code == 200:
                    print(f'Adding Cloud zone: {cloudzoneid} is successful')
                    print(output.json())
0 Kudos
Ankush11s
VMware Employee
VMware Employee

Also to note , if there are existing cloud zone , we need to add those also in body .

Tags (1)
0 Kudos
Enter123
Enthusiast
Enthusiast

Hi @Ankush11s ,

I am trying to add new CloudZone to all projects in via vRO javascript code. Do we also need to add all existing CloudZone somewhere in code if I need to add new one?

0 Kudos
Enter123
Enthusiast
Enthusiast

It seems to be the case.

Now I have a challenge how to add this new CloudZone to all projects even if some projects have different CloudZones than others. 

Why is it required to add all existing cloudzones? Dont see logic in that. 

0 Kudos
Ankush11s
VMware Employee
VMware Employee

@Enter123 there is already a Code sample provided which does the requirement 
I am not sure I follow when you say challenge and how to do it  , when logic is in thread already 

0 Kudos
Enter123
Enthusiast
Enthusiast

If I have 300 projects and a lot of them have different number of cloudZones associated, I find it challenging to add 1 new CloudZone to all of them in one go. 

Since I just want to append only 1 new cloudzone to all Projects, I don't understand the logic why I need to add existing CloudZones in the Body. Since many of Projects can have different number of cloudzones, I need to customize each API call with different values in Body - add existing CloudZones plus append new one. 

I have pretty straight forward vRO workflow which gets all projects and for each project adds new cloudzone:

Enter123_0-1681802173866.png

but in "Update Project with new CloudZone" workflow, I have to add following Variables:

- new cloudZone I want to add

- existing CloudZones in the Project which I want to update

Enter123_1-1681802344193.png

As you can see, this is fine if I want to update just one Project or if all my Projects have the same CloudZones. But this is not the case.

This is where I dont understand logic, why I need to add existing cloudzones plus new one to be able to update Project with 1 additional cloudzone.

And yes, tested, it doesn't work if I don't add existing cloudzone + new one.

 

 

0 Kudos
Ankush11s
VMware Employee
VMware Employee

The out of box workflow may not be right fit , You would need to write custom code 
if you understand python , you can look at my code snippet which adds additional cloud zone along with existing payload 
or you may want to adapt it and convert it in javascript or Powershell or Jodejs according to your language preference .

0 Kudos
Enter123
Enthusiast
Enthusiast

If I were on good terms with Python, I wouldn't be asking what I was asking 🙂

But thank a lot, will check if some colleagues can help. 

0 Kudos