Skip to content

Instantly share code, notes, and snippets.

@dvu4
dvu4 / get_servicebus_connection_string.py
Created November 16, 2022 15:21
this script will retrieve the connection string for sending and receiving the message through Azure Servicebus
import requests
from azure.identity import DefaultAzureCredential
NAMESPACE_NAME = "namespace_name"
SUBSCRIPTION_ID = "subscription_id"
RESOURCE_GROUP = "resource-group"
AUTHORIZATION_RULE = "RootManageSharedAccessKey"
API_VERSION = "2021-11-01"
@dvu4
dvu4 / create_servicebus_queue.py
Last active November 16, 2022 15:48
this script will send event message to servicebus queue when service principals are expiry in `expires_within` time and the queue receiver will get the message
import uuid
from datetime import datetime
from typing import Optional
import typer
from azure.servicebus import ServiceBusClient, ServiceBusMessage
from my_api.service_principals import filter_my_service_principals, create_secret
from my_api.auth import get_token
from my_api.config import Settings
@dvu4
dvu4 / create_servicebus_topic_subscription.py
Last active November 16, 2022 15:49
this script will send event message to servicebus topic when service principals are expiry in `expires_within` time and the servicebus subscription will get the message
import uuid
from datetime import datetime
from typing import Optional
import typer
from azure.servicebus import ServiceBusClient, ServiceBusMessage
from my_api.api.service_principals import filter_my_service_principals, create_secret
from my_api.auth import get_token
from my_api.config import Settings
@dvu4
dvu4 / insert_entity_in_storage_table.py
Created November 28, 2022 21:41
this script will connect to a storage table via string connection and insert an entity into that table
import typer
from azure.identity import DefaultAzureCredential
from azure.mgmt.storage import StorageManagementClient
from azure.data.tables import TableServiceClient
from azure.core.exceptions import HttpResponseError
TABLE_NAME = "my_table"
SUBSCRIPTION_ID = "subscription_id"
RESOURCE_GROUP = "resource_group"
@dvu4
dvu4 / compare_datetime_strings.py
Created December 16, 2022 17:53
This script compares running time between "datetime.timedelta", "dateutil.relativedelta.relativedelta" and maya.add() when working only with days and compare 2 dates with 3 different approaches
import datetime
from dateutil.relativedelta import *
import maya
from dateutil import parser
import timeit
test = ['2023-01-15T20:56:24Z', '2023-12-15T14:47:33.049Z', '2023-01-21T15:28:50Z', '2023-01-21T15:29:33Z',
'2023-01-21T15:29:40Z', '2023-01-21T15:29:47Z', '2023-01-21T15:29:54Z', '2023-01-25T09:51:44Z',
'2023-01-25T09:51:51Z', '2023-01-25T09:51:57Z', '2023-01-31T14:05:43Z', '2023-01-31T14:05:48Z',
'2023-01-31T14:05:54Z', '2023-01-31T14:06:01Z', '2023-02-03T14:41:42Z', '2023-02-10T13:05:57Z',
@dvu4
dvu4 / get_storage_account_connection_string.py
Created January 2, 2023 21:11
this script will retrieve the connection string in Azure Storage Account
from azure.identity import DefaultAzureCredential
from azure.mgmt.storage import StorageManagementClient
STORAGE_ACCOUNT_NAME = "storage_account_name"
SUBSCRIPTION_ID = "subscription_id"
RESOURCE_GROUP = "resource_group"
def get_storage_account_connection_string() -> Optional[str]:
"""
Returns : connection string
@dvu4
dvu4 / read_in_storage_table.py
Last active January 2, 2023 21:16
this script will retrieve the keyvault name in Azure Storage Account by looking up the service principal name (display_name) in each entity in the table
from azure.data.tables import TableClient
import get_storage_account_connection_string
STORAGE_ACCOUNT_NAME = "storage_account_name"
def read_keyvault_from_storage_table(display_name: str) -> Optional[str]:
"""
Parameters
----------
display_name : service principal name
@dvu4
dvu4 / get_azure_access_token.md
Last active January 20, 2023 19:39
Get access token with az cli and print token on terminal

login azure

az login

Get an access token for Microsoft Graph

token=($(az account get-access-token --resource https://graph.microsoft.com))

print token

echo $token | jq

-->

@dvu4
dvu4 / convert_table_from_bash_command_into_json.md
Last active January 19, 2023 23:00
convert a table from bash command into json with jq

To check Disk Space On Linux and Unix operating systems, you can use the df command to get a detailed report on the system’s disk space usage.

df -h

The output

Filesystem       Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk3s1s1  926Gi   20Gi  803Gi     3%  502068 4293294021    0%   /
devfs           205Ki  205Ki    0Bi   100%     710          0  100%   /dev
/dev/disk3s6    926Gi  7.0Gi  803Gi     1%       7 8418661400    0%   /System/Volumes/VM
@dvu4
dvu4 / convert_kubernetes_namespace_output_to_json.md
Last active February 3, 2023 20:48
List the existing Kubernetes namespaces in a cluster and convert the output to list of dictionary with jq

List the existing namespaces in a cluster

kubectl get namespace

--->

NAME                STATUS   AGE
calico-system       Active   532d
default             Active   2y243d
ds-ado-agents-ns    Active   688d
ds-csi              Active   255d