creating AWS resource group using boto3 - aws-sdk

I would like to create AWS resource group using boto3. In the resource group I would like to add ec2 instances having tags "name":"Jenkins".Below is the syntax suggested in boto3 documentation.
response = client.create_group(
Name='string',
Description='string',
ResourceQuery={
'Type': 'TAG_FILTERS_1_0'|'CLOUDFORMATION_STACK_1_0',
'Query': 'string'
},
Tags={
'string': 'string'
}
)
I read the documentation but I did not understand what query is in my case and couldn't find any example of creating resource groups using boto3 online. In the ResourceQuery dictionary, I can use 'Type' as 'TAG_FILTERS_1_0' but not sure what 'Query' would be. It would be great if I can get an example explanation of creating a resource group. Thank you
Update
After following #Jarmod suggestion, I tried the following code
client = boto3.client('resource-groups', **conn_args)
response = client.create_group(
Name='JenkinsResource',
Description='JenkinsResourceGrp',
ResourceQuery={
'Type': 'TAG_FILTERS_1_0',
'Query': [{"Key": "name", "Values": "Jenkins"}]
}
)
I ended up with the following error.
Invalid type for parameter ResourceQuery.Query, value: [{'Key': 'name', 'Values': 'Jenkins'}], type: , valid types:

I was able to get it to work with the Query object being:
{
'ResourceTypeFilters': ['AWS::AllSupported'],
'TagFilters': [{
'Values': ['Jenkins'],
'Key': 'name'
}]
}
And then as it's expecting a string and not a json object I did a json.dumps(query).
I discovered this by generating it through the web console and then having a look at the CloudTrail logs to see what the console did :)

Related

Is there an alternative to "type": "undefined" in JSON?

I'm working with Amazon API Gateway. I am creating a model for an REST API. The model gets hung up on:
"tiers": {
"type": "array",
"items": {
"type": "undefined"
}
}
The API data model uses JSON schema draft 4.
The error returned is:
Invalid model specified: Validation Result: warnings : [], errors :
[Invalid model schema specified]
Anyone run into this before?
Things I've tried:
Removing this property = script creates model
Changing "Undefined" to "null" = script creates model
The "null" seems like the right option but, I've not been able to back it up. Some guidance and/or clarification would be greatly appreciated.
Thanks,
Todd
You don't seem to be actually defining a schema for your data, refer to the API gateway docs to re-define your model.
undefined is not a valid json value, even though it is valid in javascript. From the official json standard (ECMA-404, Section 5):
A JSON value can be an object, array, number, string, true, false, or
null.
For json, use null instead of undefined: { "something": null }
Using null instead of undefined is definitely not ideal, but it's a standard you can count on when consuming third-party services.

Ignoring => when converting hash in ruby? How to properly convert hash?

I'm sending a post request, and this is the payload:
data = {updates: [{action: "create", user_id: "2", type: "view"}]}
but when I send it, the API says it was expecting:
{ action: \"create\", user_id: \"id\", type: \"type\" }
but it getting:
{:action=\u003e\\\"create\\\", :user_id=\u003e2\\\"2\\\", :type=\u003e\\\"view\\\"}\
The => is getting converted. I tried using to_json but that didn't help me.
How do I convert this properly? I think it has something to do with the nested array/hash because all the others work fine.
Also, I'm setting my request body and sending like so:
request.set_form_data(body)
https.request(request)
Looks like I need to use this syntax in order to pass set_form_data a nested hash:
{'a[b]': 'c'} is the same as {'a' => {'b' => 'c'}}
but is there a way to include the array? I need to have:
data = {updates: [{action: "create", user_id: "2", type: "view"}]}
in this notation.
As you mentioned, set_form_data does not accept arrays. It looks like a workaround if you wanted to continue using the Net::HTTP library is to encode the data and pass it to request.body.
request.body = "updates[][action]=create&updates[][type]=view&updates[][user_id]=2"
request.content_type = 'application/x-www-form-urlencoded'
You could also create a custom function like they did in this post: http://cloudlines.tumblr.com/post/653645115/postput-arrays-with-ruby-nethttp-setformdata
Check out this post on changing complex objects into a querystring: https://coderwall.com/p/uh8kiw/pass-arrays-objects-via-querystring-the-rack-rails-way.
Hope that helps!

How can I serialize a Simple array payload in ember?

I am pretty new to Ember. I have a service which returns a simple array like
[
"abc",
"bcd",
"cde",
"def",
"efg"
]
My model is somewhat like this
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
value: attr()
});
In the serializer(I am trying with RESTSerializer), I want this data to be sent back to route.js where the service call is made. The service call is to an API which I am not allowed to change in any way.
I tried a lot of ways that might be stupid and googled a lot. Sadly I could not find a solution though I believe it may not be too tough.
I got the payload in serializer as pasted above and was able to log the response. From there what is to be returned and what serializer is apt is my current problem. Kindly ask me if any further details are required to figure this out. I am not posting much so that I can keep it simple and understandable. Any help is appreciated.
You may not want to use Ember Data. However, you can by implementing normalizeResponse in your Serializer.
For example, if your model name is "account":
export default DS.RESTSerializer.extend({
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
let newPayload= {
accounts: [{
value: payload
}]
};
return this._super(store, primaryModelClass, newPayload, id, requestType);
}
});

JSON Expectations for Ember Data Polymorphic Relationships

DEBUG: Ember : 1.8.1
ember.js:15373DEBUG: Ember Data : 1.0.0-beta.18
ember.js:15373DEBUG: Handlebars : 1.3.0
ember.js:15373DEBUG: jQuery : 1.11.3
So I have a "Request" model which can depend on an account or other Requests that stand in for and account when it is fulfilled.
I'm a bit confused about how to get the JSON right for Ember to pick this up.
On the server-side, ActiveModelSeriailzers seems to want to serialize polymorphic relationships like this:
{
"id" : 1,
"account_id" : { "id": 1, "type": "account"}
}
I have the following ED Models:
// abstract-account.js
export default DS.Model.extend();
//account.js
export default AbstractAccount.extend();
//request.js
export default DS.Model.extend({
account: DS.belongsTo('abstractAccount', { polymorphic: true} )
});
When I try to load the record using the default json, I get
Error: Assertion Failed: You must include an `id` for account in an object passed to `push`
In my reading, I had gotten the impression that belongsTo polymorphic assocaitions should get sent like:
{
"id" : 1,
"account_id" : 1,
"account_type" : "Account"
}
If I change the server to serialize like that, I instead get
Uncaught Error: Assertion Failed: You may not pass `null` as id to the store's find method
I'm not really sure what I'm missing,or what the current recommended practice is.
I am using a similar setup with the first convention. Keep in mind Ember expects JSON objects to be namespaced under the class name, so for a GET to /requests you would need to return something like
{
'requests': [
{
'id': 1,
'account': { id: 10, type: 'account' }
},
{
'id': 2,
'account': { id: 20, type: 'some_other_abstract_account' }
}
]
}
This is with the RESTAdapter declared in application/adapter.js, I haven't tried the ActiveModelAdapter. I have seen documents advising another convention, but it's working for me with ember data 1.0.0-beta.16

Is there an easy way to make GORM errors restful api friendly

I'm working on a simple Restful API in GRAILS, I want users to be able to create an entry on one of my domain classes, so they can hit an entry point /rest/v1/create/event?params
In the receiving controller if the GORM entry fails, !event.save()
I have code like this:
def result = [
'status' : 'error',
'data' : event.errors.fieldErrors.toList()
]
render result as JSON
Is there a way to easily make event.errors.fieldErrors JSON friendly, something with just the field error and message, or will I have to write a parser method to handle this?
Ending up writing a short method to parse through and make friendly errors
If anyone finds this useful, here it is:
def gorm_errors(results) {
results = results.fieldErrors.toList()
def errors = []
for(error in results) {
errors.add([
'type' : 'invalid_entry',
'field' : error.field,
'rejected_value' : error.rejectedValue,
'message' : error.defaultMessage
])
}
return errors
}
Here is a more "groovy-er" version of the above example:
def gorm_errors(errors) {
errors.fieldErrors.toList().collect {error ->
[
'type': 'invalid_entry',
'field': error.field,
'rejected_value': error.rejectedValue,
'message': error.defaultMessage
]
}