forked from apache/devlake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_api.go
More file actions
177 lines (154 loc) · 5.11 KB
/
Copy pathremote_api.go
File metadata and controls
177 lines (154 loc) · 5.11 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 api
import (
"net/url"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
dsmodels "github.com/apache/incubator-devlake/helpers/pluginhelper/api/models"
"github.com/apache/incubator-devlake/plugins/argocd/models"
)
type ArgocdRemotePagination struct {
Page int `json:"page"`
}
type ArgocdRemoteApplication struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Project string `json:"project"`
}
func listArgocdRemoteScopes(
connection *models.ArgocdConnection,
apiClient plugin.ApiClient,
groupId string,
page ArgocdRemotePagination,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.ArgocdApplication],
nextPage *ArgocdRemotePagination,
err errors.Error,
) {
if groupId == "" {
query := url.Values{}
res, err := apiClient.Get("applications", query, nil)
if err != nil {
return nil, nextPage, err
}
var argocdApps struct {
Items []struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"metadata"`
Spec struct {
Project string `json:"project"`
Source struct {
RepoURL string `json:"repoURL"`
} `json:"source"`
} `json:"spec"`
} `json:"items"`
}
if err = api.UnmarshalResponse(res, &argocdApps); err != nil {
return nil, nextPage, err
}
projectMap := make(map[string]bool)
for _, item := range argocdApps.Items {
projectName := item.Spec.Project
if projectName == "" {
projectName = "default"
}
projectMap[projectName] = true
}
// Return projects as groups
for projectName := range projectMap {
children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.ArgocdApplication]{
Type: api.RAS_ENTRY_TYPE_GROUP,
Id: projectName,
Name: projectName,
FullName: projectName,
})
}
return children, nextPage, nil
}
query := url.Values{}
res, err := apiClient.Get("applications", query, nil)
if err != nil {
return nil, nextPage, err
}
var argocdApps struct {
Items []struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"metadata"`
Spec struct {
Project string `json:"project"`
Source struct {
RepoURL string `json:"repoURL"`
} `json:"source"`
} `json:"spec"`
} `json:"items"`
}
if err = api.UnmarshalResponse(res, &argocdApps); err != nil {
return nil, nextPage, err
}
for _, item := range argocdApps.Items {
projectName := item.Spec.Project
if projectName == "" {
projectName = "default"
}
if projectName == groupId {
app := &models.ArgocdApplication{
Name: item.Metadata.Name,
Namespace: item.Metadata.Namespace,
Project: item.Spec.Project,
RepoURL: item.Spec.Source.RepoURL,
}
app.ConnectionId = connection.ID
parentId := groupId
children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.ArgocdApplication]{
Type: api.RAS_ENTRY_TYPE_SCOPE,
ParentId: &parentId,
Id: item.Metadata.Name,
Name: item.Metadata.Name,
FullName: item.Metadata.Name,
Data: app,
})
}
}
return children, nextPage, nil
}
// RemoteScopes list all available applications for the given connection
// @Summary list all available applications for the given connection
// @Description list all available applications for the given connection
// @Tags plugins/argocd
// @Accept application/json
// @Param connectionId path int false "connection ID"
// @Param groupId query string false "group ID"
// @Success 200 {object} dsmodels.DsRemoteApiScopeList[models.ArgocdApplication]
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/argocd/connections/{connectionId}/remote-scopes [GET]
func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raScopeList.Get(input)
}
// @Summary Remote server API proxy
// @Description Forward API requests to the ArgoCD server
// @Param connectionId path int true "connection ID"
// @Param path path string true "path to a API endpoint"
// @Tags plugins/argocd
// @Router /plugins/argocd/connections/{connectionId}/proxy/{path} [GET]
func Proxy(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raProxy.Proxy(input)
}