Skip to content

Instantly share code, notes, and snippets.

@kveber
Created September 9, 2023 21:58
Show Gist options
  • Save kveber/409b346b0dead02263c6bd05548a0ab9 to your computer and use it in GitHub Desktop.
Save kveber/409b346b0dead02263c6bd05548a0ab9 to your computer and use it in GitHub Desktop.
api to automatize authorised networks via gcloud command
const got = require('got')
const token = process.env.ACCESS_TOKEN
const headers = { Authorization: `Bearer ${token}`}
if (!token) {
console.error(`Missing env $ACCESS_TOKEN value is ${token}`)
return (1)
}
const projects = [
{id: 'project-id', instance: 'intanceName'},
// other projects
]
;(async() => {
try {
const url = `https://www.googleapis.com/sql/v1beta4/projects/${projects[0].id}/instances/${projects[0].instance}`
let response1 = await got('https://ipinfo.io', { json: true})
const hqIp = response1.body.ip
let response2 = await got(url + '?fields=settings', {
headers,
json: true
})
const currentIps = response2.body.settings.ipConfiguration.authorizedNetworks
//console.log(currentIps)
if (!currentIps.some(ip => ip.value === hqIp)) {
// hq ip change
const newIps = [
...currentIps.filter(a => a.value !== 'HQ'),
{kind: 'sql#aclEntry', value: hqIp, name: 'HQ'}
]
let response3 = await got.patch(url, {
headers,
json: true,
body: {
settings: {ipConfiguration: {
authorizedNetworks: newIps
}}}
})
console.log(response3.body)
} else {
console.log('hq ip is already whitelisted')
}
} catch (err) {
console.log(err)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment