What is a good way to get the latest version of a app engine cloud secret? - google-secret-manager

I can get the secret versions with
from google.cloud import secretmanager
secretmanager_client = secretmanager.SecretManagerServiceClient()
secret_pages = secretmanager_client.list_secret_versions(parent=f"projects/{project-number}/secrets/{secret-name}").pages
version = next(version for page in secret_pages for version in page.versions if version.state.value == 1)
data = secretmanager_client.access_secret_version(name=version.name).payload.data
But all this is very awkward to put in all my apps. Isn't there a better way?

Secret Manger allows the string "latest" as a version id for this case.
projects/{project-number}/secrets/{secret-name}/versions/latest
https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets#access

Related

How to filter Google Cloud Instances using google-compute-engine python client?

I am using
Package Version
------------------------ ---------
google-api-core 2.0.1
google-auth 2.0.2
google-cloud-compute 0.5.0
google-compute-engine 2.8.13
to retrieve google cloud instances data. I was referring to the docs to get aggregated list of instances. I wasnt able to filter instances based on tags of the compute VM instances. Is there a particular way to do it using python as there is no particular mention about it in the documentation. ?
Please include your code.
You should be able to apply a Filter to AggregatedListInstancesRequest and you should be able to specify labels, see Filtering searches using labels. I'm confident (!?) the API is consistent.
The documentation refers to the filter query parameter. I didn't manage to get it to work with tags. But you can still list all the instances and filter it directly.
In the example below I'm looking for the gke-nginx-1-cluster-846d7440-node tag.
from googleapiclient import discovery
from google.oauth2 import service_account
scopes = ['https://www.googleapis.com/auth/compute']
sa_file = 'alon-lavian-0474ca3b9309.json'
credentials = service_account.Credentials.from_service_account_file(sa_file, scopes=scopes)
service = discovery.build('compute', 'v1', credentials=credentials)
project = 'alon-lavian'
zone = 'us-central1-a'
request = service.instances().list(project=project, zone=zone)
while request is not None:
response = request.execute()
for instance in response['items']:
if 'gke-nginx-1-cluster-846d7440-node' in instance['tags']['items']:
print(instance)
request = service.instances().list_next(previous_request=request, previous_response=response)

What is suggested method to get service versions

What is the best way to get list of service versions in google app engine in flex env? (from service instance in Python 3). I want to authenticate using service account json keys file. I need to find currently default version (with most of traffic).
Is there any lib I can use like googleapiclient.discovery, or google.appengine.api.modules? Or I should build it from scratches and request REST api on apps.services.versions.list using oauth? I couldn't not find any information in google docs..
https://cloud.google.com/appengine/docs/standard/python3/python-differences#cloud_client_libraries
Finally I was able to solve it. Simple things on GAE became big problems..
SOLUTION:
I have path to service_account.json set in GOOGLE_APPLICATION_CREDENTIALS env variable. Then you can use google.auth.default
from googleapiclient.discovery import build
import google.auth
creds, project = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform.read-only'])
service = build('appengine', 'v1', credentials=creds, cache_discovery=False)
data = service.apps().services().get(appsId=APPLICATION_ID, servicesId=SERVICE_ID).execute()
print data['split']['allocations']
Return value is allocations dictionary with versions as keys and traffic percents in values.
All the best!
You can use Google's Python Client Library to interact with the Google App Engine Admin API, in order to get the list of a GAE service versions.
Once you have google-api-python-client installed, you might want to use the list method to list all services in your application:
list(appsId, pageSize=None, pageToken=None, x__xgafv=None)
The arguments of the method should include the following:
appsId: string, Part of `name`. Name of the resource requested. Example: apps/myapp. (required)
pageSize: integer, Maximum results to return per page.
pageToken: string, Continuation token for fetching the next page of results.
x__xgafv: string, V1 error format. Allowed values: v1 error format, v2 error format
You can find more information on this method in the link mentioned above.

Get list of application packages available for a batch account of Azure Batch

I'm making a python app that launches a batch.
I want, via user inputs, to create a pool.
For simplicity, I'll just add all the applications present in the batch account to the pool.
I'm not able to get the list of available application packages.
This is the portion of code:
import azure.batch.batch_service_client as batch
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id='xxxxx',
secret='xxxxx',
tenant='xxxx',
resource="https://batch.core.windows.net/"
)
batch_client = batch.BatchServiceClient(
credentials,
base_url=self.AppData['CloudSettings'][0]['BatchAccountURL'])
# Get list of applications
batchApps = batch_client.application.list()
I can create a pool, so credentials are good and there are applications but the returned list is empty.
Can anybody help me with this?
Thank you,
Guido
Update:
I tried:
import azure.batch.batch_service_client as batch
batchApps = batch.ApplicationOperations.list(batch_client)
and
import azure.batch.operations as batch_operations
batchApps = batch_operations.ApplicationOperations.list(batch_client)
but they don't seem to work. batchApps is always empty.
I don't think it's an authentication issue since I'd get an error otherwise.
At this point I wonder if it just a bug in the python SDK?
The SDK version I'm using is:
azure.batch: 4.1.3
azure: 4.0.0
This is a screenshot of the empty batchApps var:
Is this the link you are looking for:
Understanding the application package concept here: https://learn.microsoft.com/en-us/azure/batch/batch-application-packages
Since its python SDK in action here: https://learn.microsoft.com/en-us/python/api/azure-batch/azure.batch.operations.applicationoperations?view=azure-python
list operation and here is get
hope this helps.
I haven't tried lately using the Azure Python SDK but the way I solved this was to use the Azure REST API:
https://learn.microsoft.com/en-us/rest/api/batchservice/application/list
For the authorization, I had to create an application and give it access to the Batch services and the I programmatically generated the token with the following request:
data = {'grant_type': 'client_credentials',
'client_id': clientId,
'client_secret': clientSecret,
'resource': 'https://batch.core.windows.net/'}
postReply = requests.post('https://login.microsoftonline.com/' + tenantId + '/oauth2/token', data)

node, azuresb.createServiceBusService provide connection string instead of environment variable

I'm using the package azure-sb to send a message to an Azure Service Bus topic. My problem is that the constructor expects a key for a connection string for example
azuresb.createServiceBusService(KEY_NAME)
Is it possible to provide the connection string itself instead of a KEY_NAME?
Yes, you can. From the documentation of this package, you do need to pass the connection string.
var connStr = process.argv[2] || process.env.CONNECTION_STRING;
var sbService = azure.createServiceBusService(connStr);
Microsoft Azure SDK for Node.js - Gallery https://www.npmjs.com/package/azure-sb
Also, azuresb seems an older SDKs. If you use Azure SDK for Node.js, here is how you can pass the connection string (quite similar!)
var serviceBusService = azure.createServiceBusService(connectionString)
You might have found the answer already... adding it here if someone else visits your question in future. :)
As mentioned in the other answers, you can pass the connection string. Your code seems to be using the older azure-sb library.
I'd recommend using the latest #azure/service-bus library which is based on AMQP protocol and is more performant.
I know this is a late reply, but just in case someone is running into issues with the older service-bus node SDK and landed here, refer to the links below.
The latest version 7.0.0 of #azure/service-bus has been released recently.
#azure/service-bus - 7.0.0
Samples for 7.0.0
Guide to migrate from #azure/service-bus v1 to v7

Upgrading boto2 to boto3: block_device_mapping structure

I am currently upgrading an old code using boto2 and I want to use boto3. A part of the old code is this:
EC2 = boto.ec2.connect_to_region("eu-west-1", **AUTH)
AMI = EC2.get_all_images(owners="297649722856")
amiList = []
for image in AMI:
ami_snapshot_id = image.block_device_mapping.current_value.snapshot_id
amiList.append(ami_snapshot_id)
My issue is about how to get the snapshot_id of the block_device_mapping. I've gone through the documentation of both boto2 and boto3 and I didn't find how to upgrade this.
Is there a way to have the same function in boto3?

Resources