-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
42 lines (37 loc) · 1.36 KB
/
utils.go
File metadata and controls
42 lines (37 loc) · 1.36 KB
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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0
package gophercloudext
import (
"errors"
"fmt"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/tokens"
)
// GetProjectIDFromTokenScope returns the project ID from the client's token scope.
//
// This is useful in applications that usually operate on the cloud-admin level,
// when using an API endpoint that requires a project ID in its URL.
// Usually this is then overridden by a query parameter like "?all_projects=True".
func GetProjectIDFromTokenScope(provider *gophercloud.ProviderClient) (string, error) {
result, ok := provider.GetAuthResult().(tokens.CreateResult)
if !ok {
return "", fmt.Errorf("%T is not a %T", provider.GetAuthResult(), tokens.CreateResult{})
}
project, err := result.ExtractProject()
if err != nil {
return "", err
}
if project == nil || project.ID == "" {
return "", fmt.Errorf(`expected "id" attribute in "project" section, but got %#v`, project)
}
return project.ID, nil
}
// UnpackError is usually a no-op, but for some Gophercloud errors, it removes
// the outer layer that obscures the better error message hidden within.
func UnpackError(err error) error {
var innerErr gophercloud.ErrUnexpectedResponseCode
if errors.As(err, &innerErr) {
return innerErr
}
return err
}