/
versionclient.go
126 lines (97 loc) · 3.05 KB
/
versionclient.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cx
import (
"context"
"errors"
"fmt"
cx "cloud.google.com/go/dialogflow/cx/apiv3beta1"
cxpb "cloud.google.com/go/dialogflow/cx/apiv3beta1/cxpb"
"github.com/xavidop/dialogflow-cx-cli/internal/global"
"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
func CreateVersionRESTClient(locationId string) (*cx.VersionsClient, error) {
ctx := context.Background()
endpointString := fmt.Sprintf("%s-dialogflow.googleapis.com:443", locationId)
endpoint := option.WithEndpoint(endpointString)
if global.Credentials != "" {
credentials := option.WithCredentialsFile(global.Credentials)
return cx.NewVersionsRESTClient(ctx, credentials, endpoint)
} else {
return cx.NewVersionsRESTClient(ctx, endpoint)
}
}
func CreateVersionGRPCClient(locationId string) (*cx.VersionsClient, error) {
ctx := context.Background()
endpointString := fmt.Sprintf("%s-dialogflow.googleapis.com:443", locationId)
endpoint := option.WithEndpoint(endpointString)
if global.Credentials != "" {
credentials := option.WithCredentialsFile(global.Credentials)
return cx.NewVersionsClient(ctx, credentials, endpoint)
} else {
return cx.NewVersionsClient(ctx, endpoint)
}
}
func CreateVersion(versionClient *cx.VersionsClient, flow *cxpb.Flow, description, name string) (*cxpb.Version, error) {
ctx := context.Background()
reqCreateVersion := &cxpb.CreateVersionRequest{
Parent: flow.GetName(),
Version: &cxpb.Version{
DisplayName: name,
Description: description,
},
}
op, err := versionClient.CreateVersion(ctx, reqCreateVersion)
if err != nil {
return nil, err
}
resp, err := op.Wait(ctx)
if err != nil {
return nil, err
}
return resp, nil
}
func UpdateVersion(versionClient *cx.VersionsClient, flow *cxpb.Flow, description, name string) (*cxpb.Version, error) {
ctx := context.Background()
version, err := GetVersionIdByName(versionClient, flow, name)
if err != nil {
return nil, err
}
if description != "" {
version.Description = description
}
reqUpdateVersion := &cxpb.UpdateVersionRequest{
Version: version,
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"description"},
},
}
return versionClient.UpdateVersion(ctx, reqUpdateVersion)
}
func GetVersionIdByName(versionClient *cx.VersionsClient, flow *cxpb.Flow, name string) (*cxpb.Version, error) {
ctx := context.Background()
reqVersionList := &cxpb.ListVersionsRequest{
Parent: flow.GetName(),
}
versions := versionClient.ListVersions(ctx, reqVersionList)
for version, err := versions.Next(); err == nil; {
if version.DisplayName == name {
return version, nil
}
version, err = versions.Next()
if err != nil {
return nil, err
}
}
return nil, errors.New("version not found")
}
func DeleteVersion(versionClient *cx.VersionsClient, flow *cxpb.Flow, name string) error {
ctx := context.Background()
version, err := GetVersionIdByName(versionClient, flow, name)
if err != nil {
return err
}
reqDeleteVersion := &cxpb.DeleteVersionRequest{
Name: version.GetName(),
}
return versionClient.DeleteVersion(ctx, reqDeleteVersion)
}