-
Notifications
You must be signed in to change notification settings - Fork 13
/
service.go
130 lines (110 loc) · 2.33 KB
/
service.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
127
128
129
130
package cd
import (
"fmt"
"strings"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
)
type ServiceClient struct {
ApiClient *ApiClient
}
func (ac *ServiceClient) ListServices(limit int, offset int, filters []*graphql.ServiceFilter) ([]*graphql.Service, *graphql.PageInfo, error) {
query := &GraphQLQuery{
Query: fmt.Sprintf(`query($filters: [ServiceFilter]) {
services(limit: %[3]d, offset: %[4]d. filters: $filters) {
nodes {
%[1]s
}
%[2]s
}
}`, standardServiceFields, paginationFields, limit, offset),
Variables: map[string]interface{}{
"filters": filters,
},
}
res := struct {
Services struct {
Nodes []*graphql.Service
PageInfo *graphql.PageInfo
}
}{}
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)
if err != nil {
return nil, nil, err
}
return res.Services.Nodes, res.Services.PageInfo, nil
}
func (ac *ServiceClient) ListServicesByApplicationId(appId string, limit int, offset int) ([]*graphql.Service, *graphql.PageInfo, error) {
filter := []*graphql.ServiceFilter{
{
Application: &graphql.IdFilter{
Operator: graphql.IdOperatorTypes.Equals,
Values: []string{appId},
},
},
}
return ac.ListServices(limit, offset, filter)
}
func (ac *ServiceClient) GetServiceById(id string) (*graphql.Service, error) {
query := &GraphQLQuery{
Query: fmt.Sprintf(`query($id: String!) {
service(serviceId: $id) {
%s
}
}`, standardServiceFields),
Variables: map[string]interface{}{
"id": id,
},
}
res := struct {
Service graphql.Service
}{}
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)
if err != nil {
return nil, nil
}
return &res.Service, nil
}
func (ac *ServiceClient) GetServiceByName(appId string, name string) (*graphql.Service, error) {
limit := 25
offset := 0
hasMore := true
for hasMore {
services, pagination, err := ac.ListServicesByApplicationId(appId, limit, offset)
if err != nil {
return nil, err
}
for _, service := range services {
if strings.EqualFold(service.Name, name) {
return service, nil
}
}
hasMore = pagination.HasMore
offset += limit
}
return nil, nil
}
const (
standardServiceFields = `
name
artifactType
artifactSources {
id
name
}
createdAt
createdBy {
email
externalUserId
id
name
}
deploymentType
description
id
name
tags {
name
value
}
`
)