Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add location by sending more Model attributes as a body json/dict #200

Closed
fabiano-lima opened this issue May 18, 2023 · 4 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@fabiano-lima
Copy link

have not being able to add location by passing more zscaler location model attributes as such as: upBandwidth, country, sslScanEnabled, etc....

it is possible do send as json body all these location model settings using Postman to create a location. Is there an way to do the same using pyzscaler add_location function ?

@mitchos
Copy link
Owner

mitchos commented Jun 2, 2023

I've been looking into this one and I think I'll take three actions here:

  1. I will update the locations endpoint to accept the new attributes
  2. I'll try find a way to keep on top of modifications to Zscaler's API endpoints as they will randomly add new parameters without any obvious update to tell endusers
  3. I'm going to add a 'raw' override capability for all pyZscaler methods. This will allow you to skip args/kwargs and just pass a raw JSON structure in whatever manner you like. This would allow you to use pyZscaler and immediately implement any new parameters that may have come out before we have time to update the SDK

@fabiano-lima what do you think of the 3rd option, do you think you'd find that useful?

sorry for the late reply, I haven't had a lot of time lately but there are a lot of updates coming today hopefully 😃

@mitchos mitchos self-assigned this Jun 2, 2023
@mitchos mitchos added the bug Something isn't working label Jun 2, 2023
@mitchos
Copy link
Owner

mitchos commented Jun 2, 2023

Okay it's been a while since I've looked at the zia.locations module.

The code is pretty simple here:

        payload = {
            "name": name,
        }

        # Add optional parameters to payload
        for key, value in kwargs.items():
            payload[snake_to_camel(key)] = value

        return self._post("locations", json=payload)

We start the payload with the only required argument to create a location, which is name. From there any kwarg that you add will be converted to camelcase and fed into the payload. So if you wanted to set any of the parameters you've specified it might look like this:

zia.locations.add_location(name='My Location', up_bandwidth=30000, dn_bandwidth=30000, country='FIJI')

I'll update the docstrings so it's a bit clearer and I'll still consider the raw json option, but most of the methods will let you add an arbitrary number of kwargs over and above what I've actually documented.

Will that fit your use-case @fabiano-lima for now?

@fabiano-lima
Copy link
Author

Thank you @mitchos , appreciate you taking the time to check this issue. Also the pyZscaler is helping a lot on my project.

Have tested the line you suggested, but not working as expected, following some tests:

from pyzscaler.zia import ZIA

payload = {
   "name":"My Location",
   "upBandwidth":10000,
   "dnBandwidth":10000,
   "country":"FIJI",
   "vpnCredentials":[
      {
         "id":valid_id,
         "preSharedKey":"XXXXX",
         "type":"UFQDN",
         "fqdn":"my_location@test.com"
      }
   ]
}

with ZIA(api_key='key', cloud='zscaler', username='username', password='pass') as zia:

    # 1st try
	zia.locations.add_location(name='My Location', up_bandwidth=30000, dn_bandwidth=30000, country='FIJI')
	
	# 2nd try
	zia.locations.add_location(name='My Location', up_bandwidth=30000, dn_bandwidth=30000, country='FIJI', vpn_credentials={'id': valid_id, 'type': 'UFQDN'})
	
	# 3rd try
    zia.locations.add_location(name='My Location', ip_addresses=['1.1.1.1'], ports=[], vpn_credentials={'id': valid_id, 'type': 'UFQDN'})

	# 4th try
    zia.locations.add_location(name='My Location', ipAddresses=['1.1.1.1'], ports=[], vpnCredentials={'id': valid_id, 'type': 'UFQDN'})
    
	# 5th try
	zia.locations.add_location(name='My Location', ip_addresses=['1.1.1.1'], ports=[], vpnCredentials={'id': valid_id, 'type': 'UFQDN'})
	
	# 6th try
	zia.locations.add_location(name='My Location', ip_addresses=['1.1.1.1'], vpn_credentials={'id': valid_id, 'type': 'UFQDN'})
	
	7th try
	zia.locations.add_location(name='My Location', **payload)

	
Responses:

# 1st try
SDK PyZscaler> python .\add_location.py
[400: POST] https://zsapi.zscaler.net/api/v1/locations body=b'{"code":"INVALID_INPUT_ARGUMENT","message":"At least an IP Address, Port, VPN Credential or Virtual Zen/Cluster is required, name: My Location"}'
SDK PyZscaler>

# 2nd to 6th tries:
SDK PyZscaler> python .\add_location.py
[400: POST] https://zsapi.zscaler.net/api/v1/locations body=b'{"message":"Request body is invalid."}'
SDK PyZscaler>

# 7th try
# No response, neither any location created on zscaler cloud
SDK PyZscaler> python .\add_location.py
SDK PyZscaler>
  

I am using pyZscaler for GET locations and other functions, but for Add/Post using another methods as Postman where I am able to send all the parameters, including the location name on the Body as a Json (as the payload example on the tests)

@mitchos
Copy link
Owner

mitchos commented Jun 21, 2023

Hi @fabiano-lima so it just comes down to the vpn_credentials parameter, which needs to be a list as you can add multiple credentials to a single location.

This worked for me:

zia.locations.add_location(
        name="My Location",
        up_bandwidth=30000,
        dn_bandwidth=30000,
        country="FIJI",
        vpn_credentials=[{"id": "<id>", "type": "UFQDN"}],
    )

which results in:

 {
    "id": "<id>",
    "name": "My Location",
    "non_editable": False,
    "parent_id": 0,
    "up_bandwidth": 30000,
    "dn_bandwidth": 30000,
    "country": "FIJI",
    "language": "NONE",
    "tz": "GMT",
    "geo_override": False,
    "latitude": 0.0,
    "longitude": 0.0,
    "vpn_credentials": [{"id": "<id>", "type": "UFQDN", "fqdn": "<fqdn>"}],
    "auth_required": False,
    "ssl_scan_enabled": False,
    "zapp_ssl_scan_enabled": False,
    "xff_forward_enabled": False,
    "other_sub_location": False,
    "ec_location": False,
    "surrogate_ip": False,
    "idle_time_in_minutes": 0,
    "surrogate_ip_enforced_for_known_browsers": False,
    "surrogate_refresh_time_in_minutes": 0,
    "kerberos_auth": False,
    "digest_auth_enabled": False,
    "ofw_enabled": False,
    "ips_control": False,
    "aup_enabled": False,
    "caution_enabled": False,
    "aup_block_internet_until_accepted": False,
    "aup_force_ssl_inspection": False,
    "iot_discovery_enabled": False,
    "aup_timeout_in_days": 0,
    "child_count": 0,
    "match_in_child": False,
    "static_location_groups": [],
    "dynamiclocation_groups": [{"id": "<id>", "name": "Server Traffic Group"}],
    "exclude_from_dynamic_groups": False,
    "exclude_from_manual_groups": False,
    "profile": "SERVER",
}

I'll update the documentation and add an example as this is a pretty common usage.

Let me know how you get on with that?

mitchos added a commit that referenced this issue Jun 21, 2023
…fy params

Signed-off-by: mkelly <me@mkelly.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants