rust-discover
is a Rust library and command line tool to discover
ip addresses of nodes in cloud environments based on meta information
like tags provided by the environment. It is a port to Rust of the excellent go-discover library.
The configuration for the providers is provided as a list of key=val key=val
...
tuples. If either the key or the value contains a space (), a backslash
(
\
) or double quotes ("
) then it needs to be quoted with double quotes.
Within a quoted string you can use the backslash to escape double quotes or the
backslash itself, e.g. key=val "some key"="some value"
Duplicate keys are reported as error and the provider is determined through the
provider
key.
The following cloud providers have implementations in the rust-discover/provider sub packages. Additional providers can be added through the Register function.
```
provider=aliyun region=... tagkey=consul tagvalue=... accesskeyid=... accesskeysecret=...
provider=aws region=eu-west-1 tagkey=consul tagvalue=... accesskeyid=... secretaccesskey=...
provider=digitalocean region=... tagname=... apitoken=...
provider=gce projectname=... zonepattern=eu-west-* tagvalue=consul credentialsfile=...
provider=linode tagname=... region=us-east addresstype=privatev4 apitoken=...
provider=mdns service=consul domain=local
provider=azure tagname=consul tagvalue=... tenantid=... clientid=... subscriptionid=... secretaccess_key=...
provider=os tagkey=consul tagvalue=server username=... password=... auth_url=...
provider=scaleway organization=my-org tag_name=consul-server token=... region=...
provider=softlayer datacenter=dal06 tagvalue=consul username=... apikey=...
provider=tencentcloud region=ap-guangzhou tagkey=consul tagvalue=... accesskeyid=... accesskeysecret=...
provider=triton account=testaccount url=https://us-sw-1.api.joyentcloud.com keyid=... tagkey=consul-role tag_value=server
provider=vsphere categoryname=consul-role tagname=consul-server host=... user=... password=... insecure_ssl=[true|false]
provider=packet authtoken=token project=uuid url=... addresstype=...
provider=k8s label_selector="app = consul-server" ```
Install the command line tool with:
go get -u github.com/hashicorp/go-discover/cmd/discover
Then run it with:
$ discover addrs provider=aws region=eu-west-1 ...
Install the library with:
go get -u github.com/hashicorp/go-discover
You can then either support discovery for all available providers or only for some of them.
```go // support discovery for all supported providers d := discover.Discover{}
// support discovery for AWS and GCE only d := discover.Discover{ Providers : map[string]discover.Provider{ "aws": discover.Providers["aws"], "gce": discover.Providers["gce"], } }
// use ioutil.Discard for no log output l := log.New(os.Stderr, "", log.LstdFlags)
cfg := "provider=aws region=eu-west-1 ..." addrs, err := d.Addrs(cfg, l) ```
You can also add support for providers that aren't registered by default:
```go // Imports at top of file import "github.com/hashicorp/go-discover/provider/k8s"
// support discovery for all supported providers d := discover.Discover{}
// support discovery for AWS and GCE only d := discover.Discover{ Providers : map[string]discover.Provider{ "k8s": &k8s.Provider{}, } }
// ... ```
For complete API documentation, see GoDoc. The configuration for the supported providers is documented in the providers sub-package.
Note: Due to the go.sum
checksum errors referenced in #68,
you will need Go 1.11.4+ to build/test go-discover.
Configuration tests can be run with Go:
$ go test ./...
By default tests that communicate with providers do not run unless credentials are set for that provider. To run provider tests you must set the necessary environment variables.
Note: This will make real API calls to the account provided by the credentials.
$ AWS_ACCESS_KEY_ID=... AWS_ACCESS_KEY_SECRET=... AWS_REGION=... go test -v ./provider/aws
This requires resources to exist that match those specified in tests
(eg instance tags in the case of AWS). To create these resources,
there are sets of Terraform configuration
in the test/tf
directory for supported providers.
You must use the same account and access credentials above. The same environment variables should be applicable and read by Terraform.
$ cd test/tf/aws
$ export AWS_ACCESS_KEY_ID=... AWS_ACCESS_KEY_SECRET=... AWS_REGION=...
$ terraform init
...
$ terraform apply
...
After Terraform successfully runs, you should be able to successfully run the tests, assuming you have exported credentials into your environment:
$ go test -v ./provider/aws
To destroy the resources you need to use Terraform again:
$ cd test/tf/aws
$ terraform destroy
...
Note: There should be no requirements to create and test these resources other than credentials and Terraform. This is to ensure tests can run in development and CI environments consistently across all providers.
Below are instructions for retrieving credentials in order to run tests for some of the providers.
Google Cloud
discover
Project ID
, e.g. discover-xxx
admin
Project/Service Account Actor
Compute Engine/Compute Instance Admin (v1)
Compute Engine/Compute Security Admin
yes
JSON
discover-xxx.json
will have been downloaded
automatically to your machineGOOGLE_CREDENTIALS
environment variableAzure
See also the Terraform provider documentation.
```shell
curl -L https://aka.ms/InstallAzureCli | bash
$ az login
$ az account list [ { "cloudName": "AzureCloud", "id": "subscriptionid", "isDefault": true, "name": "Gratis versie", "state": "Enabled", "tenantId": "tenantid", "user": { "name": "user@email.com", "type": "user" } } ]
$ az account set --subscription="subscription_id"
$ az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/subscriptionid" { "appId": "clientid", "displayName": "azure-cli-2017-07-18-16-51-43", "name": "http://azure-cli-2017-07-18-16-51-43", "password": "clientsecret", "tenant": "tenantid" }
export ARMCLIENTID=clientid export ARMCLIENTSECRET=clientsecret export ARMTENANTID=tenantid export ARMSUBSCRIPTIONID=subscriptionid
$ az vm list-sizes --location 'West Europe' ```