repos.go 9.4 KB
Newer Older
H
hongming 已提交
1
/*
H
hongming 已提交
2 3 4 5 6 7 8 9 10 11 12
Copyright 2020 The KubeSphere Authors.
Licensed 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.
*/
H
hongming 已提交
13

H
hongming 已提交
14
package openpitrix
H
hongming 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28

import (
	"fmt"
	"github.com/golang/protobuf/ptypes/wrappers"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"k8s.io/klog"
	"kubesphere.io/kubesphere/pkg/models"
	"kubesphere.io/kubesphere/pkg/server/params"
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
	"openpitrix.io/openpitrix/pkg/pb"
	"strings"
)

H
hongming 已提交
29 30 31 32 33 34 35 36
type RepoInterface interface {
	CreateRepo(request *CreateRepoRequest) (*CreateRepoResponse, error)
	DeleteRepo(id string) error
	ModifyRepo(id string, request *ModifyRepoRequest) error
	DescribeRepo(id string) (*Repo, error)
	ListRepos(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
	ValidateRepo(request *ValidateRepoRequest) (*ValidateRepoResponse, error)
	DoRepoAction(repoId string, request *RepoActionRequest) error
P
pengcong06 已提交
37
	ListEvents(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
H
hongming 已提交
38 39 40 41 42 43 44 45 46 47
	ListRepoEvents(repoId string, conditions *params.Conditions, limit, offset int) (*models.PageableResponse, error)
}

type repoOperator struct {
	opClient openpitrix.Client
}

func newRepoOperator(opClient openpitrix.Client) RepoInterface {
	return &repoOperator{
		opClient: opClient,
H
hongming 已提交
48
	}
H
hongming 已提交
49 50 51
}

func (c *repoOperator) CreateRepo(request *CreateRepoRequest) (*CreateRepoResponse, error) {
H
hongming 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	createRepoRequest := &pb.CreateRepoRequest{
		Name:             &wrappers.StringValue{Value: request.Name},
		Description:      &wrappers.StringValue{Value: request.Description},
		Type:             &wrappers.StringValue{Value: request.Type},
		Url:              &wrappers.StringValue{Value: request.URL},
		Credential:       &wrappers.StringValue{Value: request.Credential},
		Visibility:       &wrappers.StringValue{Value: request.Visibility},
		CategoryId:       &wrappers.StringValue{Value: request.CategoryId},
		AppDefaultStatus: &wrappers.StringValue{Value: request.AppDefaultStatus},
	}

	if request.Providers != nil {
		createRepoRequest.Providers = request.Providers
	}
	if request.Workspace != nil {
		createRepoRequest.Labels = &wrappers.StringValue{Value: fmt.Sprintf("workspace=%s", *request.Workspace)}
	}

H
hongming 已提交
70
	resp, err := c.opClient.CreateRepo(openpitrix.SystemContext(), createRepoRequest)
H
hongming 已提交
71 72 73 74
	if err != nil {
		klog.Error(err)
		return nil, err
	}
H
hongming 已提交
75
	return &CreateRepoResponse{
H
hongming 已提交
76 77 78 79
		RepoID: resp.GetRepoId().GetValue(),
	}, nil
}

H
hongming 已提交
80 81
func (c *repoOperator) DeleteRepo(id string) error {
	_, err := c.opClient.DeleteRepos(openpitrix.SystemContext(), &pb.DeleteReposRequest{
H
hongming 已提交
82 83 84 85 86 87 88 89 90
		RepoId: []string{id},
	})
	if err != nil {
		klog.Error(err)
		return err
	}
	return nil
}

H
hongming 已提交
91
func (c *repoOperator) ModifyRepo(id string, request *ModifyRepoRequest) error {
H
hongming 已提交
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
	modifyRepoRequest := &pb.ModifyRepoRequest{
		RepoId: &wrappers.StringValue{Value: id},
	}

	if request.Name != nil {
		modifyRepoRequest.Name = &wrappers.StringValue{Value: *request.Name}
	}
	if request.Description != nil {
		modifyRepoRequest.Description = &wrappers.StringValue{Value: *request.Description}
	}
	if request.Type != nil {
		modifyRepoRequest.Type = &wrappers.StringValue{Value: *request.Type}
	}
	if request.URL != nil {
		modifyRepoRequest.Url = &wrappers.StringValue{Value: *request.URL}
	}
	if request.Credential != nil {
		modifyRepoRequest.Credential = &wrappers.StringValue{Value: *request.Credential}
	}
	if request.Visibility != nil {
		modifyRepoRequest.Visibility = &wrappers.StringValue{Value: *request.Visibility}
	}

	if request.CategoryID != nil {
		modifyRepoRequest.CategoryId = &wrappers.StringValue{Value: *request.CategoryID}
	}
	if request.AppDefaultStatus != nil {
		modifyRepoRequest.AppDefaultStatus = &wrappers.StringValue{Value: *request.AppDefaultStatus}
	}
	if request.Providers != nil {
		modifyRepoRequest.Providers = request.Providers
	}
	if request.Workspace != nil {
		modifyRepoRequest.Labels = &wrappers.StringValue{Value: fmt.Sprintf("workspace=%s", *request.Workspace)}
	}

H
hongming 已提交
128
	_, err := c.opClient.ModifyRepo(openpitrix.SystemContext(), modifyRepoRequest)
H
hongming 已提交
129 130 131 132 133 134 135
	if err != nil {
		klog.Error(err)
		return err
	}
	return nil
}

H
hongming 已提交
136 137
func (c *repoOperator) DescribeRepo(id string) (*Repo, error) {
	resp, err := c.opClient.DescribeRepos(openpitrix.SystemContext(), &pb.DescribeReposRequest{
H
hongming 已提交
138 139 140 141 142 143 144 145
		RepoId: []string{id},
		Limit:  1,
	})
	if err != nil {
		klog.Error(err)
		return nil, err
	}

H
hongming 已提交
146
	var repo *Repo
H
hongming 已提交
147 148

	if len(resp.RepoSet) > 0 {
H
hongming 已提交
149
		repo = convertRepo(resp.RepoSet[0])
H
hongming 已提交
150 151 152 153 154 155 156 157
		return repo, nil
	} else {
		err := status.New(codes.NotFound, "resource not found").Err()
		klog.Error(err)
		return nil, err
	}
}

H
hongming 已提交
158
func (c *repoOperator) ListRepos(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
H
hongming 已提交
159 160
	req := &pb.DescribeReposRequest{}

H
hongming 已提交
161
	if keyword := conditions.Match[Keyword]; keyword != "" {
H
hongming 已提交
162 163
		req.SearchWord = &wrappers.StringValue{Value: keyword}
	}
H
hongming 已提交
164
	if status := conditions.Match[Status]; status != "" {
H
hongming 已提交
165 166
		req.Status = strings.Split(status, "|")
	}
H
hongming 已提交
167
	if typeStr := conditions.Match[Type]; typeStr != "" {
H
hongming 已提交
168 169
		req.Type = strings.Split(typeStr, "|")
	}
H
hongming 已提交
170
	if visibility := conditions.Match[Visibility]; visibility != "" {
H
hongming 已提交
171 172
		req.Visibility = strings.Split(visibility, "|")
	}
H
hongming 已提交
173
	if status := conditions.Match[Status]; status != "" {
H
hongming 已提交
174 175
		req.Status = strings.Split(status, "|")
	}
H
hongming 已提交
176
	if workspace := conditions.Match[WorkspaceLabel]; workspace != "" {
H
hongming 已提交
177 178 179 180 181
		req.Label = &wrappers.StringValue{Value: fmt.Sprintf("workspace=%s", workspace)}
	}
	if orderBy != "" {
		req.SortKey = &wrappers.StringValue{Value: orderBy}
	}
H
hongming 已提交
182
	req.Reverse = &wrappers.BoolValue{Value: reverse}
H
hongming 已提交
183 184
	req.Limit = uint32(limit)
	req.Offset = uint32(offset)
H
hongming 已提交
185
	resp, err := c.opClient.DescribeRepos(openpitrix.SystemContext(), req)
H
hongming 已提交
186 187 188 189 190 191 192 193
	if err != nil {
		klog.Error(err)
		return nil, err
	}

	items := make([]interface{}, 0)

	for _, item := range resp.RepoSet {
H
hongming 已提交
194
		items = append(items, convertRepo(item))
H
hongming 已提交
195 196 197 198 199
	}

	return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
}

H
hongming 已提交
200 201
func (c *repoOperator) ValidateRepo(request *ValidateRepoRequest) (*ValidateRepoResponse, error) {
	resp, err := c.opClient.ValidateRepo(openpitrix.SystemContext(), &pb.ValidateRepoRequest{
H
hongming 已提交
202 203 204 205 206 207 208 209 210 211
		Type:       &wrappers.StringValue{Value: request.Type},
		Credential: &wrappers.StringValue{Value: request.Credential},
		Url:        &wrappers.StringValue{Value: request.Url},
	})

	if err != nil {
		klog.Error(err)
		return nil, err
	}

H
hongming 已提交
212
	return &ValidateRepoResponse{
H
hongming 已提交
213 214 215 216 217
		ErrorCode: int64(resp.ErrorCode),
		Ok:        resp.Ok.Value,
	}, nil
}

H
hongming 已提交
218 219
func (c *repoOperator) DoRepoAction(repoId string, request *RepoActionRequest) error {
	var err error
H
hongming 已提交
220
	switch request.Action {
H
hongming 已提交
221
	case ActionIndex:
H
hongming 已提交
222 223 224
		indexRepoRequest := &pb.IndexRepoRequest{
			RepoId: &wrappers.StringValue{Value: repoId},
		}
H
hongming 已提交
225
		_, err := c.opClient.IndexRepo(openpitrix.SystemContext(), indexRepoRequest)
H
hongming 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239

		if err != nil {
			klog.Error(err)
			return err
		}

		return nil
	default:
		err = status.New(codes.InvalidArgument, "action not support").Err()
		klog.Error(err)
		return err
	}
}

H
hongming 已提交
240
func (c *repoOperator) ListRepoEvents(repoId string, conditions *params.Conditions, limit, offset int) (*models.PageableResponse, error) {
H
hongming 已提交
241 242 243 244 245 246 247 248 249 250 251 252
	describeRepoEventsRequest := &pb.DescribeRepoEventsRequest{
		RepoId: []string{repoId},
	}
	if eventId := conditions.Match["repo_event_id"]; eventId != "" {
		describeRepoEventsRequest.RepoEventId = strings.Split(eventId, "|")
	}
	if status := conditions.Match["status"]; status != "" {
		describeRepoEventsRequest.Status = strings.Split(status, "|")
	}
	describeRepoEventsRequest.Limit = uint32(limit)
	describeRepoEventsRequest.Offset = uint32(offset)

H
hongming 已提交
253
	resp, err := c.opClient.DescribeRepoEvents(openpitrix.SystemContext(), describeRepoEventsRequest)
H
hongming 已提交
254 255 256 257 258 259 260 261 262

	if err != nil {
		klog.Error(err)
		return nil, err
	}

	items := make([]interface{}, 0)

	for _, item := range resp.RepoEventSet {
H
hongming 已提交
263
		items = append(items, convertRepoEvent(item))
H
hongming 已提交
264 265 266 267
	}

	return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
}
P
pengcong06 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

func (c *repoOperator) ListEvents(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
	describeRepoEventsRequest := &pb.DescribeRepoEventsRequest{}
	if repoId := conditions.Match["repo_id"]; repoId != "" {
		describeRepoEventsRequest.RepoId = strings.Split(repoId, "|")
	}
	if eventId := conditions.Match["repo_event_id"]; eventId != "" {
		describeRepoEventsRequest.RepoEventId = strings.Split(eventId, "|")
	}
	if status := conditions.Match["status"]; status != "" {
		describeRepoEventsRequest.Status = strings.Split(status, "|")
	}
	describeRepoEventsRequest.Limit = uint32(limit)
	describeRepoEventsRequest.Offset = uint32(offset)

	resp, err := c.opClient.DescribeRepoEvents(openpitrix.SystemContext(), describeRepoEventsRequest)

	if err != nil {
		klog.Error(err)
		return nil, err
	}

	items := make([]interface{}, 0)

	for _, item := range resp.RepoEventSet {
		items = append(items, convertRepoEvent(item))
	}

	return &models.PageableResponse{Items: items, TotalCount: int(resp.TotalCount)}, nil
}