sparse-intern-71089
06/14/2022, 3:25 PMgorgeous-egg-16927
06/14/2022, 3:35 PMapply
is a callback, so it doesn’t run at the same time as your other code; it runs when the input is ready. In this case, your code basically skips the apply block and returns the empty result immediately.
You can fix this by returning the result of the output.apply
directly.
export function createRbacJson() {
// Generate list of valid resources names
const getK8sApiOutput = new local.Command("get-k8s-api-output", {
create: `kubectl api-resources --no-headers -o wide`,
});
const output = getK8sApiOutput.stdout;
//output.apply(v => console.log(v))
let jsonObj: any = {}
return output.apply(row =>
row.split("\n").forEach(function (row) {
const splitRow = row.match(/"[^"]*"|\[[^\][]*]|[^\s\][]+/g)
if (splitRow != null) {
const resourceName = splitRow[0]
// if resource has a shortname skip it
var columnModifier = 0
if (splitRow.length == 6) {
columnModifier++
}
const resourceApi = splitRow[1 + columnModifier]
const namespaced = splitRow[2 + columnModifier]
const resourceVerbs = splitRow[4 + columnModifier]?.replace(/[\[\]']+/g,'').split(/[ ,]+/)
const verbs: string[] = []
resourceVerbs.forEach(verb => {
verbs.push(verb)
});
// console.log(resourceName)
// console.log(resourceApi)
// console.log(verbs)
// console.log(namespaced)
jsonObj[resourceName] = {
api: resourceApi,
verbs: verbs,
namespaced: namespaced
}
}
})
);
}
glamorous-australia-21342
06/14/2022, 3:36 PMglamorous-australia-21342
06/14/2022, 3:36 PMglamorous-australia-21342
06/14/2022, 3:37 PMgorgeous-egg-16927
06/14/2022, 3:38 PMglamorous-australia-21342
06/14/2022, 3:38 PMglamorous-australia-21342
06/14/2022, 3:38 PMconsole.log(createRbacJson().apply(v => v))
and its still an Output<t>glamorous-australia-21342
06/14/2022, 3:39 PMglamorous-australia-21342
06/14/2022, 3:39 PMgorgeous-egg-16927
06/14/2022, 3:39 PMjsonObj
from that.glamorous-australia-21342
06/14/2022, 3:40 PMgorgeous-egg-16927
06/14/2022, 3:40 PMoutput.apply(row => {
row.split("\n").forEach(function (row) {
const splitRow = row.match(/"[^"]*"|\[[^\][]*]|[^\s\][]+/g)
if (splitRow != null) {
const resourceName = splitRow[0]
// if resource has a shortname skip it
var columnModifier = 0
if (splitRow.length == 6) {
columnModifier++
}
const resourceApi = splitRow[1 + columnModifier]
const namespaced = splitRow[2 + columnModifier]
const resourceVerbs = splitRow[4 + columnModifier]?.replace(/[\[\]']+/g,'').split(/[ ,]+/)
const verbs: string[] = []
resourceVerbs.forEach(verb => {
verbs.push(verb)
});
// console.log(resourceName)
// console.log(resourceApi)
// console.log(verbs)
// console.log(namespaced)
return jsonObj[resourceName] = {
api: resourceApi,
verbs: verbs,
namespaced: namespaced
}
}
})
});
glamorous-australia-21342
06/14/2022, 3:40 PMglamorous-australia-21342
06/14/2022, 3:40 PMgorgeous-egg-16927
06/14/2022, 3:41 PMapply(input => { return value })
glamorous-australia-21342
06/14/2022, 3:41 PMglamorous-australia-21342
06/14/2022, 3:43 PM.apply
does not exit for type void
gorgeous-egg-16927
06/14/2022, 3:43 PMelse
caseglamorous-australia-21342
06/14/2022, 3:43 PM:any
and console.log(createRbacJson().apply((v: any) => v))
glamorous-australia-21342
06/14/2022, 3:46 PMglamorous-australia-21342
06/14/2022, 3:46 PMglamorous-australia-21342
06/14/2022, 3:47 PMglamorous-australia-21342
06/14/2022, 3:48 PMglamorous-australia-21342
06/14/2022, 3:48 PMoutput.apply(row => {
row.split("\n").forEach(function (row) {
const splitRow = row.match(/"[^"]*"|\[[^\][]*]|[^\s\][]+/g)
if (splitRow != null) {
const resourceName = splitRow[0]
// if resource has a shortname skip it
var columnModifier = 0
if (splitRow.length == 6) {
columnModifier++
}
const resourceApi = splitRow[1 + columnModifier]
const namespaced = splitRow[2 + columnModifier]
const resourceVerbs = splitRow[4 + columnModifier]?.replace(/[\[\]']+/g,'').split(/[ ,]+/)
const verbs: string[] = []
resourceVerbs.forEach(verb => {
verbs.push(verb)
});
// console.log(resourceName)
// console.log(resourceApi)
// console.log(verbs)
// console.log(namespaced)
jsonObj[resourceName] = {
api: resourceApi,
verbs: verbs,
namespaced: namespaced
}
}
})
return jsonObj
});
glamorous-australia-21342
06/14/2022, 3:48 PMglamorous-australia-21342
06/14/2022, 3:52 PMconst jsonObjOutput = pulumi.output(createRbacJson())
console.log(jsonObjOutput.apply(v => v))
glamorous-australia-21342
06/14/2022, 3:52 PMglamorous-australia-21342
06/14/2022, 3:52 PMOutputImpl {
__pulumiOutput: true,
resources: [Function (anonymous)],
allResources: [Function (anonymous)],
isKnown: Promise { <pending> },
isSecret: Promise { <pending> },
promise: [Function (anonymous)],
toString: [Function (anonymous)],
toJSON: [Function (anonymous)]
}
billowy-army-68599
glamorous-australia-21342
06/14/2022, 3:53 PMglamorous-australia-21342
06/14/2022, 3:53 PMglamorous-australia-21342
06/14/2022, 3:53 PMglamorous-australia-21342
06/14/2022, 3:55 PMTypeError: Cannot read properties of undefined (reading 'apply')
createRbacJson().apply((v: any) => console.log(v))
billowy-army-68599
jsonObjOutput.apply(v => console.log(v))
glamorous-australia-21342
06/14/2022, 3:57 PMundefined
const jsonObjOutput = pulumi.output(createRbacJson())
jsonObjOutput.apply(v => console.log(v))
glamorous-australia-21342
06/14/2022, 4:12 PMbillowy-army-68599
glamorous-australia-21342
06/14/2022, 4:15 PMglamorous-australia-21342
06/14/2022, 4:17 PMglamorous-australia-21342
06/14/2022, 4:43 PMexport function createRbacJson(): any {
// Generate list of valid resources names
const getK8sApiOutput = new local.Command("get-k8s-api-output", {
create: `kubectl api-resources --no-headers -o wide`,
});
const output = getK8sApiOutput.stdout;
//output.apply(v => console.log(v))
let jsonObj: any = {}
output.apply(row => {
row.split("\n").forEach(function (row) {
const splitRow = row.match(/"[^"]*"|\[[^\][]*]|[^\s\][]+/g)
...
...
jsonObj[resourceName] = {
api: resourceApi,
verbs: verbs,
namespaced: namespaced
}
}
})
console.log(jsonObj)
});
}
Output
...
...
securitygrouppolicies: {
api: 'vpcresources.k8s.aws/v1beta1',
verbs: [
'delete',
'deletecollection',
'get',
'list',
'patch',
'create',
'update',
'watch'
],
namespaced: 'true'
}
}
glamorous-australia-21342
06/16/2022, 8:05 PMapply
needed a return
in the function 🤦♂️