-
Notifications
You must be signed in to change notification settings - Fork 84
/
client.go
58 lines (45 loc) · 1.2 KB
/
client.go
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
package director
import (
"context"
"time"
schema "github.com/kyma-incubator/compass/components/director/pkg/graphql"
gqlTools "github.com/kyma-incubator/compass/tests/director/pkg/gql"
"github.com/kyma-incubator/compass/tests/director/pkg/jwtbuilder"
gcli "github.com/machinebox/graphql"
)
type TenantsResponse struct {
Result []*schema.Tenant
}
func getToken(tenant string, scopes []string) (string, error) {
token, err := jwtbuilder.Build(tenant, scopes, &jwtbuilder.Consumer{})
if err != nil {
return "", err
}
return token, nil
}
func GetTenants(directorURL string, externalTenantID string) ([]*schema.Tenant, error) {
query := getTenantsQuery()
req := gcli.NewRequest(query)
token, err := getToken(externalTenantID, []string{"tenant:read"})
if err != nil {
return nil, err
}
client := gqlTools.NewAuthorizedGraphQLClientWithCustomURL(token, directorURL)
var response TenantsResponse
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Run(ctx, req, &response)
if err != nil {
return nil, err
}
return response.Result, nil
}
func getTenantsQuery() string {
return `query {
result: tenants {
id
name
internalID
}
}`
}