Authenticate with a clouds.yaml#2892
Authenticate with a clouds.yaml#2892EmilienM merged 1 commit intogophercloud:masterfrom pierreprinetti:auth_clouds
Conversation
EmilienM
left a comment
There was a problem hiding this comment.
I haven't spot any problems with it, although I haven't tested it yet. Before approving, I'll give it a try. In the meantime, I was wondering if you could have a least one e2e test for this one?
|
Replaced generic code with typed code in the function |
|
Replaced a call to |
|
There may be more work required here, if e.g. we want |
|
Finally managed to test this, really cool work! ~/gophercloud-playground via 🐹 v1.21.6
❯ go run main.go
panic: cloud "" not found in clouds.yaml
goroutine 1 [running]:
main.main()
/home/emilien/gophercloud-playground/main.go:20 +0x7f5
exit status 2
~/gophercloud-playground via 🐹 v1.21.6
❯ export OS_CLOUD=foch_openshift
~/gophercloud-playground via 🐹 v1.21.6 on ☁️ foch_openshift(openshift)
❯ go run main.go
~/gophercloud-playground via 🐹 v1.21.6 on ☁️ foch_openshift(openshift)
❯ openstack server list
+--------------------------------------+--------------+--------+----------------------------------------+--------------------------+-----------+
| ID | Name | Status | Networks | Image | Flavor |
+--------------------------------------+--------------+--------+----------------------------------------+--------------------------+-----------+
| 17833e16-466d-4fbc-875a-cd36bc235f5f | test-emacchi | ACTIVE | hostonly=192.168.68.120, 2001:db8::221 | N/A (booted from volume) | m1.medium |
+--------------------------------------+--------------+--------+----------------------------------------+--------------------------+-----------+ |
|
@EmilienM is that panic coming from your code, or from gophercloud? |
gophercloud I think, my code is here: https://paste.centos.org/view/65f08709 it's because I didn't export |
line 20? |
This commit imports the clouds.yaml parsing code from the utils module, and exposes it in a way that fits the natural Gophercloud authentication flow.
Unlike the code from utils, this new `clouds.Parse` function keeps the separation between the ProviderClient (holding the cloud coordinates and a Keystone token) and the ServiceClient (holding specific endpoint configuration).
By default, `clouds.Parse` fetches its configuration from the environment, just like the openstack client would do.
Example use:
```Go
func main() {
ctx := context.Background()
ao, eo, tlsConfig, err := clouds.Parse()
if err != nil {
panic(err)
}
providerClient, err := config.NewProviderClient(ctx, ao, config.WithTLSConfig(tlsConfig))
if err != nil {
panic(err)
}
networkClient, err := openstack.NewNetworkV2(providerClient, eo)
if err != nil {
panic(err)
}
}
```
The `clouds.Parse` function accepts several functional options that can modify its behaviour. For example, to use a `clouds.yaml` that exists in a non-standard path:
```Go
ao, eo, tlsConfig, err := clouds.Parse(clouds.WithLocations("/my/path/clouds2.yaml"))
```
It is also possible to pass a reader directly. Note that any number of options can be passed, with each of them taking precedence of the previous if there is conflict.
```Go
const exampleClouds = `clouds:
openstack:
auth:
auth_url: https://example.com:13000`
ao, eo, tlsConfig, err := clouds.Parse(
clouds.WithCloudsYAML(strings.NewReader(exampleClouds)),
clouds.WithIdentityEndpoint("https://example.com:13001"),
clouds.WithCloudName("osp1"),
clouds.WithUsername("alice"),
)
```
I think this could be done later, right? I'm going to approve this PR for now. |
mandre
left a comment
There was a problem hiding this comment.
This is really cool 😎
LGTM
This commit imports the clouds.yaml parsing code from the utils module, and exposes it in a way that fits the natural Gophercloud authentication flow.
Unlike the code from utils, this new
clouds.Parsefunction keeps the separation between the ProviderClient (holding the cloud coordinates and a Keystone token) and the ServiceClient (holding specific endpoint configuration).By default,
clouds.Parsefetches its configuration from the environment, just like the openstack client would do.Example use:
The
clouds.Parsefunction accepts several functional options that can modify its behaviour. For example, to use aclouds.yamlthat exists in a non-standard path:It is also possible to pass a reader directly. Note that any number of options can be passed, with each of them taking precedence of the previous if there is conflict.