Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProjectResource assign_resources raises NoMethodError (undefined method `try') #251

Closed
joaomarceloods opened this issue Aug 2, 2020 · 1 comment · Fixed by #274
Closed

Comments

@joaomarceloods
Copy link

joaomarceloods commented Aug 2, 2020

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

@joaomarceloods
Copy link
Author

joaomarceloods commented Aug 2, 2020

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}>}>] 

fwitzke pushed a commit to fwitzke/droplet_kit that referenced this issue Apr 17, 2021
- It also adds tests to `assign_reources` method, which was lacking.
- Ths fixes digitalocean#251 (digitalocean#251)
fwitzke pushed a commit to fwitzke/droplet_kit that referenced this issue Apr 17, 2021
- It also adds tests to `assign_resources` method, which was lacking.
- This fixes digitalocean#251 (digitalocean#251)
andrewsomething pushed a commit that referenced this issue Apr 19, 2021
- It also adds tests to `assign_resources` method, which was lacking.
- This fixes #251 (#251)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant