-
Notifications
You must be signed in to change notification settings - Fork 131
/
url_expander.go
87 lines (74 loc) · 2.67 KB
/
url_expander.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
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
/*
Copyright (c) 2019 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package urls
import (
"fmt"
)
// Expand returns full URI to UHC resources based on an alias. An alias
// allows for shortcuts on the CLI, such as replace "accts" with the
// full URI of the resource. Lists of resources require just the alias as
// a parameter, while getting/posting individual resources requires the additional
// ID of the resource.
func Expand(argv []string) (string, error) {
if len(argv) < 1 || len(argv) > 2 {
msg := fmt.Errorf("Expected 1 (for Lists) or 2 (for a specific resource) but got %d", len(argv))
return "", msg
}
preParsePath := argv[0]
path := preParsePath
var err error
switch preParsePath {
// List resources:
case "accounts", "accts":
path = "/api/accounts_mgmt/v1/accounts"
case "subscriptions", "subs":
path = "/api/accounts_mgmt/v1/subscriptions"
case "organizations", "orgs":
path = "/api/accounts_mgmt/v1/organizations"
case "clusters":
path = "/api/clusters_mgmt/v1/clusters"
case "role_bindings":
path = "/api/accounts_mgmt/v1/role_bindings"
case "resource_quota":
path = "/api/accounts_mgmt/v1/resource_quota"
case "roles":
path = "/api/accounts_mgmt/v1/roles"
case "skus":
path = "/api/accounts_mgmt/v1/skus"
// Individual resources:
case "account", "acct":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/accounts/", argv)
case "subscription", "sub":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/subscriptions/", argv)
case "organization", "org":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/organizations/", argv)
case "cluster":
path, err = expandResourceWithID("/api/clusters_mgmt/v1/clusters/", argv)
case "role_binding":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/role_bindings/", argv)
case "sku":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/skus/", argv)
case "role":
path, err = expandResourceWithID("/api/accounts_mgmt/v1/roles/", argv)
}
if err != nil {
return "", err
}
return path, nil
}
func expandResourceWithID(path string, argv []string) (string, error) {
if len(argv) != 2 {
return "", fmt.Errorf("Resource requires an ID, but got none")
}
return path + argv[1], nil
}