How to Access the Directory API in Admin SDK - python

Trying in vain to access the Directory API in the Google Admin SDK (Reference). Upgrading the "google-api-python-client" package doesn't resolve it, as the Downloads > Installation > Python link instructs.
I also don't see in the documentation where it lists the programmatic name of the API, so I'm assuming it's "directory", but have also tried "directory.admin" and "admin.directory." I get the following exception when I try to run my test code:
apiclient.errors.UnknownApiNameOrVersion: name: directory version: v1
Could someone please assist with what I may be overlooking? Unfortunately, the "Quickstart" that provides the shell to access the API seems to be broken (Reference). When I select Python > Command-Line > Configure Project, I get an error that states "Sorry, unable to configure a project right now."
Here's the pertinent piece of code I'm using in my custom Directory library class:
class Directory(object):
def __init__(self, httpConnection):
self.service = build('directory', 'v1', http=httpConnection)
Is this just a case of no client library being available yet for the API? If so, when might that be available?

There's an issue where this new API isn't appearing in the list of supported APIs, but it is indeed there, you can access it from:
service = build('admin', 'directory_v1')

Related

How to get all starred projects by a user from the Gitlab API

I want to get a list of all projects that a certain user on Gitlab has starred using the Gitlab API and Python (via python-gitlab).
The documentation from python-gitlab on users does not mention starred projects. It may be a user activity, but the documentation on user activities just refers to user activity managers and about that no further documentation is available.
With the Github API and pyGithub it's possible to achieve that using
import github
g = github.Github()
u = g.get_user()
u.get_starred()
However, with the Gitlab APi and python-gitlab I only get as far as:
import gitlab
gl = gitlab.Gitlab()
u = gl.users.get()
u.get_starred() # AttributeError
u.starred() # AttributeError
I think it might be possible because on a Gitlab website I can see the starred projects of a user. Example: https://gitlab.com/users/username/starred
I'm not sure if gitlab python package has it. But there is a REST API for the same. Refer this link https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
You can use requests module to do the GET and POST requests using python.

listing the azure locations that support virtual network

I'd like to create virtual networks in every location in Azure that can support them, using Azure python SDK.
In the code below I'm limiting only to location germanynorth, but that is just to help reproduce the issue.
from azure.common.client_factory import get_client_from_auth_file
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.subscription import SubscriptionClient
from azure.mgmt.network import NetworkManagementClient
get_client_from_auth_file(ComputeManagementClient)
for location in get_client_from_auth_file(SubscriptionClient).subscriptions.list_locations(get_subscription_id()):
if location.name == 'germanynorth':
get_client_from_auth_file(NetworkManagementClient).virtual_networks.create_or_update(
resource_group_name=RESOURCE_GROUP_NAME,
virtual_network_name='test-network',
parameters={'location': location.name, 'address_space': {'address_prefixes': ['10.0.0.0/16']}, }
)
When running this I get the error:
msrestazure.azure_exceptions.CloudError: Azure Error: LocationNotAvailableForResourceType
Message: The provided location 'germanynorth' is not available for resource type 'Microsoft.Network/virtualNetworks'. List of available regions for the resource type is 'westus,eastus,northeurope,westeurope,eastasia,southeastasia,northcentralus,southcentralus,centralus,eastus2,japaneast,japanwest,brazilsouth,australiaeast,australiasoutheast,centralindia,southindia,westindia,canadacentral,canadaeast,westcentralus,westus2,ukwest,uksouth,koreacentral,koreasouth,francecentral,australiacentral,southafricanorth,uaenorth,switzerlandnorth,germanywestcentral,norwayeast'.
Very helpfully, the error includes a list of all the regions where virtualNetworks could be created, but of course this list will change over time.
What API in Azure can I use to figure out what locations (regions?) support virtual networks?
Thanks!
You can use Azure resource providers and types. You can refer the Microsoft article on the resource provider.
PowerShell script to get all supported azure regions to create Azure key Vault.
$locations = (((Get-AzResourceProvider -ProviderNamespace Microsoft.KeyVault)| Where-Object RegistrationState -eq "Registered").ResourceTypes | Where-Object ResourceTypeName -eq vaults).Locations
I actually figured out my own bounty.
https://learn.microsoft.com/en-us/rest/api/resources/providers/get
"Gets the specified resource provider."
I don't think this describes what it actually does, which is why I didn't find it. I had to just basically test a bunch of APIs to see what returned what.
This API will return a list of available locations for the provided resource type (in your subscription).
I just wish it didn't only return a list (East US) but also with the with a short code (code:location), for example (eastus:East US).
So to answer the actual question, if you can't use your python library for this, an option would be to use this REST API:
GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Network?api-version=2021-04-01
Your list of locations will be under the json path: {response}.resourceTypes[0].locations, where resourceType eq "virtualNetworks"
To actually get the locationCode (short location code) you can query this API:
https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list-locations
then map your location from above with this response to get the short code ('East US' -> 'eastus'), which can be used in other rest APIs to create for example a virtual network.
There is an API with which we can list out all the available location under a given subscription id (I am not sure if there is an equivalent version of this API that can filter by resource type) -
API -
GET https://management.azure.com/subscriptions/{subscriptionId}/locations?api-version=2020-01-01
Quoting the documentation
This operation provides all the locations that are available for resource providers; however, each resource provider may support a subset of this list.
Perhaps, you can iterate through the list of available locations in your subscription and put it in a try/except block to create Vnet against all available regions in your subscription?
EDIT: Apologies, I realized it late, you are already iterating through the list of locations under your subscription id. I guess it's a matter of writing the code beneath the for loop in a try/except block, wherein you can except that particular error type and continue with your loop?

Google API. Library imported has no attribute

I'm trying to use Google API and the build instance has no attributes. Not even the sample project from Google works: https://developers.google.com/calendar/quickstart/python
I have tried to reinstall the libraries and forcing reinstall using:
"pip install --force-reinstall google-api-python-client".
Code from another Google site which does not work either:
from googleapiclient.discovery import build
API_KEY='my_apiKey'
GPLUS = build('plus', 'v1', developerKey=API_KEY)
TMPL = '''
User: %s
Date: %s
Post: %s
'''
items = GPLUS.event() # AttributeError: 'Resource' object has no attribute 'event'
The service you're building with the code you provided is the Google+ API Client. According to the docs for this client there's no attribute or function called event(), and so you get the corresponding error message when running your code. Take a look at those docs to see what you should be able to do with this client. There appears to be a quick sample on Github.
After some more testing i can verify that there exist attributes since the code is working but pylint in VS Code does not find the attributes and therefore mark them as non existing.
I have not idea though why this is happening.

IBM Cloud functions - Unable to create an action

I'm unable to create an IBM Cloud action. I have no idea why.
My IBM data that I get in console is as follows:
API endpoint: https://api.eu-gb.bluemix.net
Region: eu-gb
User: my-name#my-company.com
Account: My Name's Account (12fcae9b137946b8bbfe481448612345)
Resource group: Default
CF API endpoint: https://api.eu-gb.bluemix.net (API version: 2.92.0)
Org: my-org
Space: dev
That look fine to me.
When I execute the test action as the docs says here as follows:
ibmcloud wsk action invoke whisk.system/utils/echo -p message hello --result
I get correct result:
{
"message": "hello"
}
But I'm not able to create my own actions.
The folder looks as follows:
Thus there is .zip file that I created with
zip -r as24-crawler.zip virtualenv commmon.py __main__.py
And now when I try to create an action as the docs says here with
ibmcloud wsk action create my-action-name --kind python:3 as24-crawler.zip
I got an error as follows:
error: Unable to create action 'my-action-name': The connection
failed, or timed out. (HTTP status code 413)
Any idea what am I doing wrong?
UPDATE
Is the file size to big? I'm not sure. The error message is not clear but the meaning could be that the file to big is.
They say in the docs that the limit is 48MB. But my created .zip file is 43.6MB. Thus I assume that that not the problem is.
I am one of the guys responsible for IBM Cloud Functions.
We have been looking into this issue and must admit that this is a bug (causing zip files to be rejected even if the are smaller than the documented 48MB limit); we are already working on a fix to get this resolved asap.
In the meantime you may want to have a look at the workaround (which allows you to "outsource" some of your dependencies etc.) described here:
http://jamesthom.as/blog/2017/08/04/large-applications-on-openwhisk/
I hope this helps - feel free to contact me (e.g. via Linkedin/Twitter, see my profile information) directly in case you have further questions or in case you want to discuss anything else.
Thanks for having found and reported this issue and sorry for inconvenience.
it seems according to the return code '413' the provided data used to create the action is too large. To get an indication about the system limits you might want to check IBM Cloud Functions documentation here: https://console.bluemix.net/docs/openwhisk/openwhisk_reference.html#openwhisk_syslimits.

Azure Batch Pool: How do I use a custom VM Image via Python?

I want to create my Pool using Python. I can do this when using an image (Ubuntu Server 16.04) from the marketplace, but I want to use a custom image (but also Ubuntu Server 16.04) -- one which I have prepared with the desired libraries and setup.
This is how I am creating my pool:
new_pool = batch.models.PoolAddParameter(
id=pool_id,
virtual_machine_configuration=batchmodels.VirtualMachineConfiguration(
image_reference=image_ref_to_use, # ??
node_agent_sku_id=sku_to_use),
vm_size=_POOL_VM_SIZE,
target_dedicated_nodes=_POOL_NODE_COUNT,
start_task=start_task,
max_tasks_per_node=_CORES_PER_NODE
)
I imaging that I need to use batch.models.ImageReference() to create my image reference... but I do not know how to use it.
Yes, I checked the documentation, which says the following:
A reference to an Azure Virtual Machines Marketplace image or a custom
Azure Virtual Machine image.
It lists the parameters as:
publisher (str)
offer (str)
sku (str)
version (str)
virtual_machine_image_id (str)
However, the parameter virtual_machine_image_id does not exists... In other words, batch.models.ImageReference(virtual_machine_image_id) is not allowed.
How can I use a custom image for my Pool?
UPDATE
So I figured out how to use a custom image... it turns out that no matter how many times I uninstall the azure python libraries and re-install them, the virtual_machine_image_id is never available.
I then went here downloaded the zip. Opened it up, checked the ImageReference class and low-and-behold, the virtual_machine_image_id was available in the __init__ function of the ImageReference class. I then downloaded the python wheel and used pip to install it. Boom it worked.
Or so I thought.
I then had to fight though trying to figure out what the node_agent_sku_id is... only by manually creating a Pool and seeing the Batch Node Agent SKU ID field did I manage to find it.
Now I am struggling with the Authentication...
The error I am getting is:
Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
AuthenticationErrorDetail: The specified type of authentication
SharedKey is not allowed when external resources of type Compute are
linked.
azure.batch.models.batch_error.BatchErrorException: {'lang':
'en-US', 'value': 'Server failed to authenticate the request. Make
sure the value of Authorization header is formed correctly including
the
signature.\nRequestId:f8c1a3b3-65c4-4efd-9c4f-75c5c253f992\nTime:2017-10-15T20:36:06.7898187Z'}
From the error, I understand that I am not allowed to use SharedKeyCredentials:
credentials = batchauth.SharedKeyCredentials(_BATCH_ACCOUNT_NAME,
_BATCH_ACCOUNT_KEY)
batch_client = batch.BatchServiceClient(
credentials,
base_url=_BATCH_ACCOUNT_URL)
What must I do?
UPDATE 2
OK. User fpark has informed me that I need to use:
from azure.batch import BatchServiceClient
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id=CLIENT_ID,
secret=SECRET,
tenant=TENANT_ID,
resource="https://batch.core.windows.net/"
)
batch_client = BatchServiceClient(
credentials,
base_url=BATCH_ACCOUNT_URL
)
to authenticate. Unfortunately, that the code above is described here and makes no reference to what CLIENT_ID et. al are.
I then managed to find another piece of documentation which appears to be the same thing: https://azure-sdk-for-python.readthedocs.io/en/v2.0.0rc3/resourcemanagementauthentication.html
That page pointed me to another webpage: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal
I followed that tutorial and managed to finally authenticate my application...
NOTE
When creating your application, the tutorial will tell you:
Provide a name and URL for the application. Select either Web app /
API or Native for the type of application you want to create. After
setting the values, select Create.
DO NOT select Native as you will not have the option to get an application key...
Required Minimum Azure Batch SDK
The azure-batch Python SDK v4.0.0 or higher is required. Typically with pip install --upgrade azure-batch you should just get the newest version. If that doesn't work you can add the --force-reinstall option to pip to force it (with --upgrade).
Node Agent Sku Id
Regarding the proper value for node_agent_sku_id, you need to use the list_node_agent_skus operation to see the mapping between operating systems and the node agent skus supported.
Azure Active Directory Authentication Required
Regarding the auth issue, you must use Azure Active Directory authentication to use this feature. It will not work with shared key auth.
Documentation
More information can be found in this guide, including all pre-requisites needed to enable custom images.
I am using azure-batch==9.0.0, and it turns out the docs are not updated as per the package itself. Using id instead of virtual_machine_image_id fixes the problem for me.

Categories