Skip to content

Instantly share code, notes, and snippets.

@agracey
Created October 26, 2020 20:51
Show Gist options
  • Save agracey/574787b2c5dac83ab6302ed1649b6326 to your computer and use it in GitHub Desktop.
Save agracey/574787b2c5dac83ab6302ed1649b6326 to your computer and use it in GitHub Desktop.
import axios from 'axios'
import qs from 'qs'
const uaa_uri = 'https://uaa.cap.staging.explore.suse.dev'
const cf_uri = 'https://api.cap.staging.explore.suse.dev'
const cf_api_user = 'admin'
const cf_api_password = 'REDACTED'
const login = async ()=>{
try {
const options = {
url: `${uaa_uri}/oauth/token`,
method: 'post',
headers: {
Authorization: 'Basic Y2Y6',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
data: qs.stringify({
grant_type: 'password',
client_id: 'cf',
username: cf_api_user,
password: cf_api_password
})
}
const ret = await axios(options)
return `Bearer ${ret.data.access_token}`
} catch(e) {
console.error(`Error Logging in`, e)
}
}
const getServiceBindings = async (Authorization) => {
const url = `${cf_uri}/v2/service_bindings`
const options = {
headers:{
Authorization
},
url
}
const {data} = await axios(options)
return data.resources.map(res=>({
app_guid: res.entity.app_guid,
service_instance_guid: res.entity.service_instance_guid,
binding_guid: res.metadata.guid
}))
}
const rebind = async ({binding_guid, app_guid, service_instance_guid}, Authorization) => {
console.error(`DELETING: ${binding_guid}`)
const options_delete = {
method: 'delete',
headers:{
Authorization
},
url: `${cf_uri}/v2/service_bindings/${binding_guid}`
}
await axios(options_delete)
console.error(`DELETED: ${binding_guid}`)
console.error(`CREATING: ${service_instance_guid} --> ${app_guid}`)
const options_create = {
method: 'post',
headers:{
Authorization
},
url: `${cf_uri}/v2/service_bindings`,
data:{
app_guid,
service_instance_guid
}
}
await axios(options_create)
console.error(`CREATED: ${service_instance_guid} --> ${app_guid}`)
}
const restage = async ({app_guid}, Authorization) => {
console.error(`RESTAGING: ${app_guid}`)
const options_restage = {
method: 'post',
headers:{
Authorization
},
url: `${cf_uri}/v2/apps/${app_guid}/restage`
}
await axios(options_restage)
console.error(`RESTAGED: ${app_guid}`)
}
const sleepTime = (timeout) => {
return new Promise((res)=>{
setTimeout(()=>{res()},timeout)
})
}
(async () => {
const auth = await login()
const list = await getServiceBindings(auth)
console.log(list)
for(let i=0; i<list.length; i++){
await rebind(list[i], auth)
await restage(list[i],auth)
await sleepTime(10000)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment