doctl icon indicating copy to clipboard operation
doctl copied to clipboard

create droplet in a specific project

Open viorel-anghel opened this issue 5 years ago • 11 comments

digitalocean can group droplets into "projects".

"doctl compute droplet create" will create droplets into the default project.

how can I create a droplet in a specific project?

viorel-anghel avatar Oct 04 '19 11:10 viorel-anghel

Hey @viorel-anghel,

To create a Droplet in a specific project, you'll need to use an API token for that project. There are two ways to do this.

Through a CLI argument

doctl allows you to pass a -t flag to provide an API access token for that command. For example you could create a Droplet by running

doctl compute droplet create <name> -t <project-api-token> --image ubuntu-18-04-x64 --region nyc3 --size s-1vcpu-1gb

where,

  • <name> is the name of the Droplet you want to create (as you probably already know),
  • <project-api-token> is the API token belonging to the specific project you want to create the Droplet in.

Using an authentication context

doctl allows you to create named authentication contexts so that you don't need to pass a -t flag every time. To do this,

  • Run doctl auth init --context=<project-name>, where <project-name> is the name of your project.
  • Provide your project's API token at the prompt.
  • Switch to that context by running doctl auth switch --context=<project-name>.

Now, anytime you create a Droplet, it will be created using your project's API token until you switch back to your default context. To do that, run doctl auth switch --context=default. You can also view the different contexts you've created at any time by running doctl auth list.

Hope this helps!

bentranter avatar Oct 04 '19 14:10 bentranter

I have a question.

How to create the <project-api-token> ?

I have only a personal API token.

lavir avatar Jan 31 '20 22:01 lavir

Same question here.

angristan avatar Apr 27 '20 19:04 angristan

Hi all,

It looks like there was a bit of miscommunication around the different features and terminology. The approach @bentranter describes above is for managing multiple DigitalOcean team or accounts. The DigitalOcean API does not currently support assigning resources to a specific project when the resource is created. Currently, this require a second API call. Using doctl, that command would look like:

doctl projects resources assign <project-id> --resource=<urn> [--resource=<urn> ...]

The --resource flag can be repeated multiple times to assign more than one resource in a single request. It takes a resource's uniform resource name (URN). For a Droplet, that looks like do:droplet:123456

See: https://developers.digitalocean.com/documentation/v2/#project-resources

Putting it all together, here's an example of what this might look like in a script:

PROJECT_ID="c82f8368-8dc5-45fa-8ffe-28038935f5ce"
DROPLET_ID=$(doctl compute droplet create example.com --size s-1vcpu-1gb --image ubuntu-20-04-x64 --region nyc3 --wait --no-header --format=ID)
doctl projects resources assign ${PROJECT_ID} --resource=do:droplet:${DROPLET_ID}

In the Droplet create request, passing --no-header --format=ID means that only the ID is printed in response. This allows you to save it into a variable for later use.

andrewsomething avatar Apr 27 '20 20:04 andrewsomething

Thanks for the tips!

angristan avatar Apr 27 '20 22:04 angristan

Thanks @andrewsomething! It would be nice to have a version of your explanation in the official documentation as well, or to be able to use

doctl compute droplet create example.com \
  --size s-1vcpu-1gb \
  --image ubuntu-20-04-x64 \
  --region nyc3 \
  --wait \
  --format ID,Name,Tags,PublicIPv4,PrivateIPv4,Status \
  --context $CONTEXT \
  --project $PROJECT_NAME

For example.

woudsma avatar May 26 '20 18:05 woudsma

Hi guys, is it means doctl not support to get project ID yet?

yckwan avatar Dec 06 '20 13:12 yckwan

@andrewsomething do you know if the --project flag for creating resources is on the development schedule?

BBudnicki avatar Jan 05 '21 01:01 BBudnicki

@andrewsomething Can I take this task of allowing the project id to be passed in create droplet command. I would like to contribute.

Utsavk avatar May 23 '21 07:05 Utsavk

Hello @BBudnicki ! Adding resources by project is one of the very important features we at DO are working, albeit it is only in the planning stages as of now. We look forward to rolling this feature out in the not too distant future. Thank you.

ChiefMateStarbuck avatar May 25 '21 17:05 ChiefMateStarbuck

bump

brandonscholet avatar Sep 07 '22 19:09 brandonscholet

bump Being able to specify a project ID/name is optimal when creating resources in DO.

senilegenius avatar Aug 18 '23 07:08 senilegenius

Bump,

Asking for project-id to be specified during droplet creation

shkarface avatar Jan 24 '24 08:01 shkarface

3 years and a feature that probably shouldn't be that hard to implement?

brandonscholet avatar Jan 24 '24 15:01 brandonscholet

Hey @brandonscholet and @shkarface, this is available in our latest release – you can provide a project UUID when creating a Droplet with a new --project-id flag.

bentranter avatar Jan 26 '24 22:01 bentranter

@bentranter thank you for letting us know! Appreciate it.

shkarface avatar Jan 26 '24 22:01 shkarface