Skip to content

Instantly share code, notes, and snippets.

View gbvanrenswoude's full-sized avatar

Gijs van Renswoude gbvanrenswoude

View GitHub Profile
@gbvanrenswoude
gbvanrenswoude / main.py
Created September 16, 2022 12:24
LeetCode Common Prefix Length
# Given a string, split the string into two substrings at every possible point.
# The rightmost substring is a suffix. The beginning of the string is the prefix.
# Determine the lengths of the common prefix between each suffix and the original string.
# Sum and return the lengths of the common prefixes. Return an array where each element i is the sum for string i.
# Complete the 'commonPrefix' function below.
# The function is expected to return an INTEGER_ARRAY.
# The function accepts STRING_ARRAY inputs as parameter.
# Let's break this problem down in several stages to make it easier to solve:
@gbvanrenswoude
gbvanrenswoude / policy.yaml
Last active September 28, 2022 12:26
kyverno-prevent-updates-to-service-loadbalancer
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: service-no-type-load-balancer-enforce-extention
spec:
validationFailureAction: enforce
background: false
rules:
- name: default
match:
@gbvanrenswoude
gbvanrenswoude / script.ts
Last active November 14, 2022 12:35
Script to remove all Route53 Hosted Zones with records from an AWS account
// Run with ts-node script.ts --no-cache
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-console */
import {
ChangeBatch,
ChangeResourceRecordSetsCommand,
DeleteHostedZoneCommand,
ListHostedZonesCommand,
ListResourceRecordSetsCommand,
@gbvanrenswoude
gbvanrenswoude / pi.lisp
Created December 8, 2022 12:39
pi.lisp
(require :cl)
(defun calc-pi (n)
(let ((in-circle 0))
(loop for i from 1 to n do
(let ((x (random 1.0))
(y (random 1.0)))
(when (< (+ (* x x) (* y y)) 1)
(incf in-circle))))
(* 4 (/ in-circle n))))
@gbvanrenswoude
gbvanrenswoude / frozen_dict.py
Created December 12, 2022 08:40
FrozenDict
class FrozenDict(dict):
def __setitem__(self, key, value):
raise TypeError("This dictionary is frozen. You cannot add or modify items.")
def __hash__(self):
items = tuple(self.items())
return hash(items)
my_dict = {"a": 1, "b": 2, "c": 3}
my_frozen_dict = FrozenDict(my_dict)
@gbvanrenswoude
gbvanrenswoude / ESClientBuilder.ts
Created December 15, 2022 16:38
Expand OpenSearch SDK with aws sigv4 signing of requests on the Client using aws-opensearch-connector
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { Client } from '@opensearch-project/opensearch';
// tslint:disable-next-line:no-var-requires
const createAwsOpensearchConnector = require('aws-opensearch-connector');
export async function getEsClient(node: string = `https://${process.env.elasticsearch}`) {
const es = new Client({
...createAwsOpensearchConnector({
credentials: await defaultProvider()(),
@gbvanrenswoude
gbvanrenswoude / EsClientBuilder.ts
Last active December 15, 2022 21:24
Make plain https requests to an OpenSearch domain using AWS Sigv4 signing
import { Sha256 } from '@aws-crypto/sha256-browser';
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { NodeHttpHandler } from '@aws-sdk/node-http-handler';
import { HttpRequest } from '@aws-sdk/protocol-http';
import { SignatureV4 } from '@aws-sdk/signature-v4';
// import { region } from './http-agent'; use an aws region string
// import { getEnv } from './utils'; just calls process.env or returns default
export async function indexDocumentonAwsES(body, index, type, id, domain) {
const request = new HttpRequest({
@gbvanrenswoude
gbvanrenswoude / handler.ts
Last active December 19, 2022 11:04
OpenSearch ISM handler in CDK - CR example
import { Sha256 } from '@aws-crypto/sha256-browser';
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { NodeHttpHandler } from '@aws-sdk/node-http-handler';
import { HttpRequest } from '@aws-sdk/protocol-http';
import { SignatureV4 } from '@aws-sdk/signature-v4';
import { CdkCustomResourceEvent, CdkCustomResourceHandler } from 'aws-lambda';
export async function signRequest(request: HttpRequest, region = 'eu-west-1') {
const signer = new SignatureV4({
credentials: defaultProvider(),
@gbvanrenswoude
gbvanrenswoude / requests_mock.py
Created January 18, 2023 14:50
Simple Python requests mock example
import logging
import os
import requests
import unittest
from unittest.mock import patch
import requests_mock
def send_slack_message(message, slack_url):
slack_response = requests.post(
@gbvanrenswoude
gbvanrenswoude / example.ts
Created April 6, 2023 13:34
cdk snapshot testing with the ability to exclude or include assets
import { SynthUtils } from '@aws-cdk/assert';
import * as cdk from 'aws-cdk-lib';
import { Stack, StageSynthesisOptions } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { InfrastructureStack } from '../../src/lib/infrastructure-stack';
type Options = StageSynthesisOptions & {
/**
* Ignore Assets
*/