There's a 3rd option and that is to PUT the JSON response from GET
GET api/v2/operatingsystems/24
Part of the response includes architectures as a child-node
{
"id": 24,
"name": "CentOs",
"architectures": [
{
"name": "x86_64",
"id": 1
}
]
}
PUT api/v2/operatingsystems/24
And ad another OS in the JSON data
{
"id": 24,
"name": "CentOs",
"architectures": [
{
"name": "x86_64",
"id": 1
},
{
"name": "i386",
"id": 2
}
]
}
Actually, most of the JSON data above is unnecessary. I can just pass in the relationship node with a collection of ids
PUT api/v2/operatingsystems/24
{
"architectures": [
{
"id": 1
},
{
"id": 2
}
]
}
Note that you can NOT add or remove a SINGLE id as in your option #2. I agree with Walden not to implement this.
Below, this example CREATES a record in has_and_belongs_to_many relationship table
PUT api/v2/operatingsystems/24
{
"architectures": [
{
"id": 1
},
{
"id": 2
}
},
{
"id": 3
}
]
}
This DELETES a record in has_and_belongs_to_many relationship table
PUT api/v2/operatingsystems/24
{
"architectures": [
{
"id": 1
}
]
}
Note that this should work with every has_many or has_and_belongs_to_many relationship even if nested routes or child nodes may not appear for in RABL templates for all objects.
In summary, Foreman supports
#1) Array on attribute operatingsystem_ids on PUT /architecture/2
{
"operatingsystem_ids": [24,25,26]
}
#3) Array on child not operatingsystems PUT /architecture/2
{
"operatingsystems": [{"id": 24},{"id": 25},{"id": 26}]
}
David, I hope this helps. I agree that the documentation needs to be better. Can you take the initiative on updating the docs once we reach a final decision.
Thanks,
Joseph
···
----- Original Message -----
> From: "Walden Raines"
> To: foreman-dev@googlegroups.com
> Sent: Tuesday, January 28, 2014 10:20:15 PM
> Subject: Re: [foreman-dev] Updating many-to-many relationships
>
> Even though I just followed an existing pattern and implemented a new
> occurrence of #2, I prefer #1 as it is more RESTful.
>
> Cheers,
> Walden
>
> ----- Original Message -----
> From: "David Davis"
> To: "foreman-dev"
> Sent: Tuesday, January 28, 2014 3:01:29 PM
> Subject: [foreman-dev] Updating many-to-many relationships
>
> So there seems to be a divergence in how Foreman and Katello handle
> many-to-many relationships. To give a couple examples of these, one would be
> architectures which have and belong to many OSes in Foreman. In Katello,
> content views has and belongs to many repositories There 2-3 options for
> updating these relationships:
>
> 1. Send in operating_system_ids on PUT /architecture/2
> 2. Have add_operating_systems and remove_operating_systems actions that take
> ids
> 3. Both 2 and 3
>
> Foreman looks like it mostly supports 1 while Katello has a combination in
> its v2 api of 1 and 2 (but not 3). I can see advantages to all: 1 is simpler
> while 2 would maybe be easier to use for the user. 3 seems like a good
> option but would be the most work.
>
> Thoughts?
>
> David
>
> --
> You received this message because you are subscribed to the Google Groups
> "foreman-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to foreman-dev+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
> --
> You received this message because you are subscribed to the Google Groups
> "foreman-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to foreman-dev+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>