digitalocean/droplet_kit

ProjectResource assign_resources raises NoMethodError (undefined method `try')

joaomarceloods opened this issue · 1 comments

When using plain ruby (not Ruby on Rails), if you assign a resource to a project, it raises NoMethodError. This happens only if the resource is an object (not urn string). Example:

db_cluster = client.databases.find_cluster(id: "cluster_id")
client.projects.assign_resources([db_cluster], id: project.id)
# => NoMethodError (undefined method `try' for #<DropletKit::DatabaseCluster:0x00007fa3bb5cb528>)

This is because try is a method introduced by Ruby on Rails, and it's not available in plain ruby.

elsif resource.try(:urn) && DropletKit::BaseModel.valid_urn?(resource.urn)

To play well with plain ruby, the line above could be replaced with this:

elsif resource.respond_to?(:urn) && resource.urn && DropletKit::BaseModel.valid_urn?(resource.urn)

Using droplet_kit-3.8.0

Also, I'm not sure if this is related, but if I use the actual urn string of the db cluster, I get an API error:

db_cluster = client.databases.find_cluster(id: "cluster_id")
client.projects.assign_resources([db_cluster.urn], id: project.id)
# => DropletKit::Error (400: {"id":"invalid_argument","message":"resource objects must have an urn in the following format: do:resource_type:resource_id"})
db_cluster.urn
# => "do:database_cluster:c023fcd4-xxxx-xxxx-xxxx-90a776d3e537"

UPDATE:

It looks like this problem is only happening with db clusters. I can assign a droplet to a project without trouble:

droplet = client.droplets.find(id: "xxxxx")
client.projects.assign_resources([droplet.urn], id: project.id)
# => [<DropletKit::ProjectAssignment {:@urn=>"do:droplet:xxxxx", :@assigned_at=>"2020-08-02T00:11:01Z", :@links=><DropletKit::Links {:@myself=>"https://api.digitalocean.com/v2/droplets/xxxxx", :@first=>nil, :@next=>nil, :@prev=>nil, :@last=>nil}>}>]