How to fix unexpected node type with ksonnet? - ksonnet

When I try to set a parameter with ksonnet I get an error
ks param set --env=prow workflows name some-name
ERROR Invalid params schema -- did not expect node type: *ast.ApplyBrace
My parameters file looks like
local params = import "../../components/params.libsonnet";
params {
components+: {
// Insert component parameter overrides here. Ex:
// guestbook +: {
// name: "guestbook-dev",
// replicas: params.global.replicas,
// },
workflows +: {
name: "some-name",
},
},
}

Adding a plus to params fixed it.
local params = import "../../components/params.libsonnet";
params +{
components+: {
// Insert component parameter overrides here. Ex:
// guestbook +: {
// name: "guestbook-dev",
// replicas: params.global.replicas,
// },
workflows +: {
name: "some-name",
},
},
}

Related

Include an object from another file into main bicep template

Trying to do something, I don't know if it's possible, and if it is, I am asking for some help on how to do it.
I have a file "test.bicep" that has an object:
{
name: 'testRafaelRule'
priority: 1001
ruleCollectionType: 'FirewallPolicyFilterRuleCollection'
action: {
type: 'Allow'
}
rules: [
{
name: 'deleteme-1'
ipProtocols: [
'Any'
]
destinationPorts: [
'*'
]
sourceAddresses: [
'192.168.0.0/16'
]
sourceIpGroups: []
destinationIpGroups: []
destinationAddresses: [
'AzureCloud.EastUS'
]
ruleType: 'NetworkRule'
destinationFqdns: []
}
]
}
and I have another file, in which I am trying to somehow input the object in test.bicep into a specific property called "ruleCollections":
resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups#2020-11-01' = {
name: 'netrules'
properties: {
priority: 200
ruleCollections: [
**ADD_OBJECT_FROM_TEST.BICEP_HERE_HOW?**
]
}
}
any suggestions or links to useful documentation would be helpful.
I have looked at outputs and parameters, but I am trying to add just an object into an existing property, I am not adding an entire resource on its own, otherwise, I would output the resouce and consume it with the "module" keyword.
It's not possible straightforward, but you can leverage variables or module's output.
var RULE = {
name: 'testRafaelRule'
priority: 1001
(...)
}
resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups#2020-11-01' = {
name 'netrules'
properties: {
ruleCollections: [
RULE
]
}
}
or
rule.bicep
output rule object = {
name: 'testRafaelRule'
priority: 1001
(...)
}
main.bicep
module fwrule 'rule.bicep' = {
name: 'fwrule'
}
resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups#2020-11-01' = {
name 'netrules'
properties: {
ruleCollections: [
fwrule.outputs.rule
]
}
}

How to edit a pulumi resource after it's been declared

I've declared a kubernetes deployment like:
const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", {
spec: {
template: {
metadata: {
labels: {name: "ledger"},
name: "ledger",
// namespace: namespace,
},
spec: {
containers: [
...
],
volumes: [
{
emptyDir: {},
name: "gunicorn-socket-dir"
}
]
}
}
}
});
Later on in my index.ts I want to conditionally modify the volumes of the deployment. I think this is a quirk of pulumi I haven't wrapped my head around but here's my current attempt:
if(myCondition) {
ledgerDeployment.spec.template.spec.volumes.apply(volumes =>
volumes.push(
{
name: "certificates",
secret: {
items: [
{key: "tls.key", path: "proxykey"},
{key: "tls.crt", path: "proxycert"}],
secretName: "star.builds.qwil.co"
}
})
)
)
When I do this I get the following error: Property 'mode' is missing in type '{ key: string; path: string; }' but required in type 'KeyToPath'
I suspect I'm using apply incorrectly. When I try to directly modify ledgerDeployment.spec.template.spec.volumes.push() I get an error Property 'push' does not exist on type 'Output<Volume[]>'.
What is the pattern for modifying resources in Pulumi? How can I add a new volume to my deployment?
It's not possible to modify the resource inputs after you created the resource. Instead, you should place all the logic that defines the shape of inputs before you call the constructor.
In your example, this could be:
let volumes = [
{
emptyDir: {},
name: "gunicorn-socket-dir"
}
]
if (myCondition) {
volumes.push({...});
}
const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", {
// <-- use `volumes` here
});

Cloudformation - how to set filter policy of SNS subscription in code?

UPDATE: Cloudformation now supports SNS Topic Filters, so this question is not relevant anymore, no custom plugins or code is needed.
I am building a system with a number of SNS topics, and a number of Lambdas which are each reading messages from their assigned SQS queue. The SQS queues are subscribed to the SNS topics, but also have a filter policy so the messages will end up in the relevant SQS queues.
It works well when I set up the subscriptions in the AWS console.
Now I'm trying to do the same in my code, but the AWS Cloudformation documentation does not describe how to add a filter policy to a subscription. Based on the python examples here, I tried the following:
StopOperationSubscription:
Type: "AWS::SNS::Subscription"
Properties:
Protocol: sqs
TopicArn:
Ref: StatusTopic
Endpoint:
Fn::GetAtt: [StopActionQueue, Arn]
FilterPolicy: '{"value": ["stop"]}'
But then I get this error:
An error occurred: StopOperationSubscription - Encountered unsupported property FilterPolicy.
How can I set the filter policy that I need, using CloudFormation? And If that's not supported, what do you suggest as an alternative?
I want it to be set up automatically when I deploy my serverless app, with no manual steps required.
Cloudformation just started to support FilterPolicy yesterday. I have been struggling for a while too :)
Syntax
JSON
{
"Type" : "AWS::SNS::Subscription",
"Properties" : {
"DeliveryPolicy" : JSON object,
"Endpoint" : String,
"FilterPolicy" : JSON object,
"Protocol" : String,
"RawMessageDelivery" : Boolean,
"Region" : String,
"TopicArn" : String
}
}
YAML
Type: "AWS::SNS::Subscription"
Properties:
DeliveryPolicy: JSON object
Endpoint: String
FilterPolicy: JSON object
Protocol: String
RawMessageDelivery: Boolean,
Region: String
TopicArn: String
Ref:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy
https://aws.amazon.com/blogs/compute/managing-amazon-sns-subscription-attributes-with-aws-cloudformation/
I fixed it like this:
serverless.yml
plugins:
- serverless-plugin-scripts
custom:
scripts:
commands:
update-topic-filters: sls invoke local -f configureSubscriptions --path resources/lambdaTopicFilters.json
hooks:
before:deploy:finalize: sls update-topic-filters
functions:
configureSubscriptions:
handler: src/configurationLambdas/configureSubscriptions.main
# Only invoked when deploying - therefore, no permissions or triggers are needed.
configureSubscriptions.js
import AWS from 'aws-sdk'
const nameFromArn = arn => arn.split(':').pop()
const lambdaNameFromArn = arn =>
nameFromArn(arn)
.split('-')
.pop()
exports.main = async event => {
const sns = new AWS.SNS({ apiVersion: '2010-03-31' })
const params = {}
const { Topics } = await sns.listTopics(params).promise()
for (const { TopicArn } of Topics) {
const topicName = nameFromArn(TopicArn)
const filtersForTopic = event[topicName]
if (!filtersForTopic) {
continue
}
const { Subscriptions } = await sns.listSubscriptionsByTopic({ TopicArn }).promise()
for (const { Protocol, Endpoint, SubscriptionArn } of Subscriptions) {
if (Protocol === 'lambda') {
const lambdaName = lambdaNameFromArn(Endpoint)
const filterForLambda = filtersForTopic[lambdaName]
if (!filterForLambda) {
continue
}
const setPolicyParams = {
AttributeName: 'FilterPolicy',
SubscriptionArn,
AttributeValue: JSON.stringify(filterForLambda),
}
await sns.setSubscriptionAttributes(setPolicyParams).promise()
// eslint-disable-next-line no-console
console.log('Subscription filters has been set')
}
}
}
}
Top level is the different topic names, next level is the lambda names, and the third level is the filter policies for the related subscriptions:
lambdaTopicFilters.json
{
"user-event": {
"activateUser": {
"valueType": ["status"],
"value": ["awaiting_activation"]
},
"findActivities": {
"messageType": ["event"],
"value": ["awaiting_activity_data"],
"valueType": ["status"]
}
},
"system-event": {
"startStopProcess": {
"valueType": ["status"],
"value": ["activated", "aborted", "limit_reached"]
}
}
}
If you are using serverless it is now supporting sns filter natively
functions:
pets:
handler: pets.handler
events:
- sns:
topicName: pets
filterPolicy:
pet:
- dog
- cat
https://serverless.com/framework/docs/providers/aws/events/sns#setting-a-filter-policy

graphql-tools, how can I use mockServer to mock a "Mutation"?

I try to use mockServer of graphql-tools to mock an Mutation.
here is my unit test:
it('should update user name correctly', () => {
mockserver
.query(
`{
Mutation {
updateUserName(id: 1, name: "du") {
id
name
}
}
}`
)
.then(res => {
console.log(res);
expect(1).to.be.equal(1);
});
});
But, got an error:
mock server test suites
✓ should get users correctly
✓ should get user by id correctly
✓ should update user name correctly
{ errors:
[ { GraphQLError: Cannot query field "Mutation" on type "Query".
at Object.Field (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:65:31)
at Object.enter (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/language/visitor.js:324:29)
at Object.enter (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/language/visitor.js:366:25)
at visit (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/language/visitor.js:254:26)
at visitUsingRules (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/validation/validate.js:74:22)
at validate (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/validation/validate.js:59:10)
at graphqlImpl (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/graphql.js:106:50)
at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/graphql.js:66:223
at new Promise (<anonymous>)
at Object.graphql (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/graphql.js:63:10)
at Object.query (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql-tools/dist/mock.js:19:63)
at Context.it (/Users/ldu020/workspace/apollo-server-express-starter/src/mockServer/index.spec.js:95:8)
at callFn (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runnable.js:383:21)
at Test.Runnable.run (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runnable.js:375:7)
at Runner.runTest (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:446:10)
at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:564:12
at next (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:360:14)
at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:370:7
at next (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:294:14)
at Immediate.<anonymous> (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/mocha/lib/runner.js:338:5)
at runCallback (timers.js:763:18)
at tryOnImmediate (timers.js:734:5)
at processImmediate (timers.js:716:5)
message: 'Cannot query field "Mutation" on type "Query".',
locations: [Array],
path: undefined } ] }
and, I read graphql-tools interface.d.ts file.
export interface IMockServer {
query: (query: string, vars?: {
[key: string]: any;
}) => Promise<ExecutionResult>;
}
Obviously, there is no mutation function in mockServer.
Does mockServer support Mutation?
https://github.com/apollographql/graphql-tools/issues/279
It's the structure of your query. The query should be structured much like you would in GraphiQL such as:
mutation {
updateUserName(id: 1, name: "du") {
id
name
}
}
Hence, your code should look something like this, with the mutation keyword as the first thing in your query before your opening brace { :
it('should update user name correctly', () => {
mockserver
.query(`mutation {
updateUserName(id: 1, name: "du") {
id
name
}
}`)
.then(res => {
console.log(res);
expect(1).to.be.equal(1);
});
});

Retrieve docs from MongoDB using Array

I have a route:
router.get('/:bar', (req, res) => {
Style.distinct('id', {'foo': req.params.bar })
.then(ids =>
// [00001, 00002, 00003, 00004, ... 99999]]
// get Style
// get Config
// etc..
});
});
In distinct then function, I would like to replace id 00001 with it's object & configuration (to do it, I have to query the Schema):
Style.find({'id', id})
Config.find({'id', id})
I'm stuck in callback hell.
I am trying to achieve the following output:
{
00001 : {
style: {}
config: {}
},
00002 : {
style: {}
config: {}
},
00003 : {
style: {}
config: {}
}
}
However, I am unsure how to return the response of two concurrent API calls in an object, and return an array of objects when complete.