1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
# Go Discover Nodes for Cloud Providers [](https://travis-ci.org/hashicorp/go-discover) [](https://godoc.org/github.com/hashicorp/go-discover)
`go-discover` is a Go (golang) library and command line tool to discover
ip addresses of nodes in cloud environments based on meta information
like tags provided by the environment.
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.
### Supported Providers
The following cloud providers have implementations in the go-discover/provider
sub packages. Additional providers can be added through the
[Register](https://godoc.org/github.com/hashicorp/go-discover#Register)
function.
* Aliyun (Alibaba) Cloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/aliyun/aliyun_discover.go#L15-L28)
* Amazon AWS [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/aws/aws_discover.go#L19-L33)
* DigitalOcean [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/digitalocean/digitalocean_discover.go#L16-L24)
* Google Cloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/gce/gce_discover.go#L17-L37)
* Linode [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/linode/linode_discover.go#L30-L41)
* mDNS [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/mdns/mdns_provider.go#L19-L31)
* Microsoft Azure [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/azure/azure_discover.go#L16-L37)
* Openstack [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/os/os_discover.go#L23-L38)
* Scaleway [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/scaleway/scaleway_discover.go#L14-L22)
* SoftLayer [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/softlayer/softlayer_discover.go#L16-L25)
* Triton [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/triton/triton_discover.go#L17-L27)
* vSphere [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/vsphere/vsphere_discover.go#L148-L155)
* Packet [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/packet/packet_discover.go#L25-L35)
The following providers are implemented in the go-discover/provider subdirectory
but aren't automatically registered. If you want to support these providers,
register them manually:
* Kubernetes [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/k8s/k8s_discover.go#L32-L51)
HashiCorp maintains acceptance tests that regularly allocate and run tests with
real resources to verify the behavior of several of these providers. Those
currently are: Amazon AWS, Microsoft Azure, Google Cloud, DigitalOcean, Triton, Scaleway, AliBaba Cloud, vSphere, and Packet.net.
### Config Example
```
# Aliyun (Alibaba) Cloud
provider=aliyun region=... tag_key=consul tag_value=... access_key_id=... access_key_secret=...
# Amazon AWS
provider=aws region=eu-west-1 tag_key=consul tag_value=... access_key_id=... secret_access_key=...
# DigitalOcean
provider=digitalocean region=... tag_name=... api_token=...
# Google Cloud
provider=gce project_name=... zone_pattern=eu-west-* tag_value=consul credentials_file=...
# Linode
provider=linode tag_name=... region=us-east address_type=private_v4 api_token=...
# mDNS
provider=mdns service=consul domain=local
# Microsoft Azure
provider=azure tag_name=consul tag_value=... tenant_id=... client_id=... subscription_id=... secret_access_key=...
# Openstack
provider=os tag_key=consul tag_value=server username=... password=... auth_url=...
# Scaleway
provider=scaleway organization=my-org tag_name=consul-server token=... region=...
# SoftLayer
provider=softlayer datacenter=dal06 tag_value=consul username=... api_key=...
# Triton
provider=triton account=testaccount url=https://us-sw-1.api.joyentcloud.com key_id=... tag_key=consul-role tag_value=server
# vSphere
provider=vsphere category_name=consul-role tag_name=consul-server host=... user=... password=... insecure_ssl=[true|false]
# Packet
provider=packet auth_token=token project=uuid url=... address_type=...
# Kubernetes
provider=k8s label_selector="app = consul-server"
```
## Command Line Tool Usage
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 ...
```
## Library Usage
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](https://godoc.org/github.com/hashicorp/go-discover). The configuration
for the supported providers is documented in the
[providers](https://godoc.org/github.com/hashicorp/go-discover/provider)
sub-package.
## Testing
**Note: Due to the `go.sum` checksum errors referenced in [#68](https://github.com/hashicorp/go-discover/issues/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](https://www.terraform.io) 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.**
## Retrieving Test Credentials
Below are instructions for retrieving credentials in order to run
tests for some of the providers.
<details>
<summary>Google Cloud</summary>
1. Go to https://console.cloud.google.com/
1. IAM & Admin / Settings:
* Create Project, e.g. `discover`
* Write down the `Project ID`, e.g. `discover-xxx`
1. Billing: Ensure that the project is linked to a billing account
1. API Manager / Dashboard: Enable the following APIs
* Google Compute Engine API
1. IAM & Admin / Service Accounts: Create Service Account
* Service account name: `admin`
* Roles:
* `Project/Service Account Actor`
* `Compute Engine/Compute Instance Admin (v1)`
* `Compute Engine/Compute Security Admin`
* Furnish a new private key: `yes`
* Key type: `JSON`
1. The credentials file `discover-xxx.json` will have been downloaded
automatically to your machine
1. Source the contents of the credentials file into the `GOOGLE_CREDENTIALS`
environment variable
</details>
<details>
<summary>Azure</summary>
See also the [Terraform provider documentation](https://www.terraform.io/docs/providers/azurerm/index.html#creating-credentials).
```shell
# Install Azure CLI (https://github.com/Azure/azure-cli)
curl -L https://aka.ms/InstallAzureCli | bash
# 1. Login
$ az login
# 2. Get SubscriptionID
$ az account list
[
{
"cloudName": "AzureCloud",
"id": "subscription_id",
"isDefault": true,
"name": "Gratis versie",
"state": "Enabled",
"tenantId": "tenant_id",
"user": {
"name": "user@email.com",
"type": "user"
}
}
]
# 3. Switch to subscription
$ az account set --subscription="subscription_id"
# 4. Create ClientID and Secret
$ az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/subscription_id"
{
"appId": "client_id",
"displayName": "azure-cli-2017-07-18-16-51-43",
"name": "http://azure-cli-2017-07-18-16-51-43",
"password": "client_secret",
"tenant": "tenant_id"
}
# 5. Export the Credentials for the client
export ARM_CLIENT_ID=client_id
export ARM_CLIENT_SECRET=client_secret
export ARM_TENANT_ID=tenant_id
export ARM_SUBSCRIPTION_ID=subscription_id
# 6. Test the credentials
$ az vm list-sizes --location 'West Europe'
```
</details>
|