-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (57 loc) · 2.49 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"github.com/spf13/pflag"
"github.com/cloudwan/goten-sdk/runtime/api/view"
caccpoint "github.com/cloudwan/ztna-sdk/client/v1alpha/access_point"
raccpoint "github.com/cloudwan/ztna-sdk/resources/v1alpha/access_point"
"github.com/cloudwan/edgelq-sdk/examples/utils"
)
/* This is simple illustration of how to construct a client and send a request.
*
* Based on provided parameters (endpoint, credentials, projectID) it lists AccessPoint resources
* from ZTNA service.
*
* In order to use execute example, you need either user account (prepare access token, like one from web browser),
* or credentials for service account (JSON format, as defined as in edgelq-sdk/common/api/credentials.proto -
* ServiceAccount). Of course, user or service account will need permission to list AccessPoints in project you
* select, so this is pre-requirement.
*/
func main() {
ctx := context.Background()
// ------------------------- Get params and just verify they were provided -------------------------
endpoint := pflag.StringP("endpoint", "e", "", "API endpoint (for example, ztna.stg01b.edgelq.com:443)")
projectId := pflag.StringP("project", "p", "", "Name of the project to list access points from")
accessToken := pflag.StringP("access-token", "a", "", "Active token for your user")
credsFile := pflag.StringP("credentials", "c", "", "Path to service account credentials file")
pflag.Parse()
if *projectId == "" {
pflag.Usage()
panic(fmt.Errorf("no project ID was provided"))
}
if *endpoint == "" {
pflag.Usage()
panic(fmt.Errorf("no endpoint was provided"))
}
if *accessToken == "" && *credsFile == "" {
pflag.Usage()
panic(fmt.Errorf("access token OR credentials file are necessary in order to execute this example"))
}
// ------------------------- Create GRPC connection -------------------------
grpcConn := utils.Dial(ctx, *endpoint, *accessToken, *credsFile)
apClient := caccpoint.NewAccessPointServiceClient(grpcConn)
// ------------------------- Lets begin! -------------------------
listAccessPointsResp, err := apClient.ListAccessPoints(ctx, &caccpoint.ListAccessPointsRequest{
Parent: raccpoint.NewNameBuilder().SetProjectId(*projectId).Parent(),
View: view.View_BASIC,
})
if err != nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("failed to list access points: %s\n", err))
os.Exit(1)
}
for _, ap := range listAccessPointsResp.GetAccessPoints() {
_, _ = os.Stderr.WriteString(fmt.Sprintf("received access point %s\n\n", ap))
}
}