glamorous-australia-21342
06/14/2022, 3:25 PMjsonObj
isnt blank outside of the output.apply(...
. What am I doing wrong? If I log it inside the output.apply(..
it behaves as expected. Some kind of scope thing I am not understanding I believe.
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 = {}
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
}
}
})
);
const jsonObjOutput = pulumi.output(jsonObj)
return jsonObjOutput.apply(v => console.log(v))
}
Output of createRbacJson()
{}
gorgeous-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 PMgorgeous-egg-16927
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>gorgeous-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 PMgorgeous-egg-16927
06/14/2022, 3:41 PMapply(input => { return value })
glamorous-australia-21342
06/14/2022, 3:41 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))
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
}
}
})
return jsonObj
});
const jsonObjOutput = pulumi.output(createRbacJson())
console.log(jsonObjOutput.apply(v => v))
OutputImpl {
__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 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))
billowy-army-68599
glamorous-australia-21342
06/14/2022, 4:15 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'
}
}
apply
needed a return
in the function 🤦♂️