in backend/helpers/pluginhelper/api/remote_api_helper.go [136:247]
func (r *RemoteApiHelper[Conn, Scope, ApiScope, Group]) GetScopesFromRemote(input *plugin.ApiResourceInput,
getGroup func(basicRes coreContext.BasicRes, gid string, queryData *RemoteQueryData, connection Conn) ([]Group, errors.Error),
getScope func(basicRes coreContext.BasicRes, gid string, queryData *RemoteQueryData, connection Conn) ([]ApiScope, errors.Error),
) (*plugin.ApiResourceOutput, errors.Error) {
connectionId, err := errors.Convert01(strconv.ParseUint(input.Params["connectionId"], 10, 64))
if err != nil || connectionId == 0 {
return nil, errors.BadInput.New("invalid connectionId")
}
var connection Conn
err = r.connHelper.First(&connection, input.Params)
if err != nil {
return nil, err
}
groupId, ok := input.Query["groupId"]
if !ok || len(groupId) == 0 {
groupId = []string{""}
}
pageToken, ok := input.Query["pageToken"]
if !ok || len(pageToken) == 0 {
pageToken = []string{""}
}
// get gid and pageData
gid := groupId[0]
queryData, err := getPageDataFromPageToken(pageToken[0])
if err != nil {
return nil, errors.BadInput.New("failed to get page token")
}
outputBody := &RemoteScopesOutput{}
// list groups part
if queryData.Tag == TypeGroup {
var resBody []Group
if getGroup != nil {
resBody, err = getGroup(r.basicRes, gid, queryData, connection)
}
if err != nil {
return nil, err
}
// if len(resBody) == 0, will skip the following steps, this will happen in some plugins which don't have group
// append group to output
for _, group := range resBody {
child := RemoteScopesChild{
Type: TypeGroup,
Id: group.GroupId(),
Name: group.GroupName(),
// don't need to save group into data
Data: nil,
}
child.ParentId = &gid
if *child.ParentId == "" {
child.ParentId = nil
}
outputBody.Children = append(outputBody.Children, child)
}
// check groups count
if len(resBody) < queryData.PerPage {
queryData.Tag = TypeProject
queryData.Page = 1
queryData.PerPage = queryData.PerPage - len(resBody)
}
}
// list projects part
if queryData.Tag == TypeProject && getScope != nil {
var resBody []ApiScope
resBody, err = getScope(r.basicRes, gid, queryData, connection)
if err != nil {
return nil, err
}
// append project to output
for _, project := range resBody {
scope := project.ConvertApiScope()
child := RemoteScopesChild{
Type: TypeProject,
Id: scope.ScopeId(),
Name: scope.ScopeName(),
FullName: scope.ScopeFullName(),
Data: &scope,
}
child.ParentId = &gid
if *child.ParentId == "" {
child.ParentId = nil
}
outputBody.Children = append(outputBody.Children, child)
}
// check project count
if len(resBody) < queryData.PerPage {
queryData = nil
}
}
// get the next page token
outputBody.NextPageToken = ""
if queryData != nil {
queryData.Page += 1
outputBody.NextPageToken, err = getPageTokenFromPageData(queryData)
if err != nil {
return nil, err
}
}
return &plugin.ApiResourceOutput{Body: outputBody, Status: http.StatusOK}, nil
}