project_credential.go 3.5 KB
Newer Older
R
runzexia 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
Copyright 2019 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.
*/

package v1alpha2

import (
	"github.com/emicklei/go-restful"
	"k8s.io/klog"
H
hongming 已提交
19
	"kubesphere.io/kubesphere/pkg/api"
R
runzexia 已提交
20 21 22 23 24 25 26 27 28 29 30 31
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/simple/client/devops"
)

func (h ProjectPipelineHandler) CreateDevOpsProjectCredentialHandler(request *restful.Request, resp *restful.Response) {

	projectId := request.PathParameter("devops")
	username := request.HeaderParameter(constants.UserNameHeader)
	var credential *devops.Credential
	err := request.ReadEntity(&credential)
	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
32
		api.HandleBadRequest(resp, err)
R
runzexia 已提交
33 34 35 36 37 38
		return
	}
	credentialId, err := h.projectCredentialOperator.CreateProjectCredential(projectId, username, credential)

	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
39
		api.HandleInternalError(resp, err)
R
runzexia 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		return
	}

	resp.WriteAsJson(struct {
		Name string `json:"name"`
	}{Name: credentialId})
	return
}

func (h ProjectPipelineHandler) UpdateDevOpsProjectCredentialHandler(request *restful.Request, resp *restful.Response) {

	projectId := request.PathParameter("devops")
	credentialId := request.PathParameter("credential")
	var credential *devops.Credential
	err := request.ReadEntity(&credential)
	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
57
		api.HandleBadRequest(resp, err)
R
runzexia 已提交
58 59 60 61 62 63
		return
	}
	credentialId, err = h.projectCredentialOperator.UpdateProjectCredential(projectId, credentialId, credential)

	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
64
		api.HandleInternalError(resp, err)
R
runzexia 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		return
	}

	resp.WriteAsJson(struct {
		Name string `json:"name"`
	}{Name: credentialId})
	return
}

func (h ProjectPipelineHandler) DeleteDevOpsProjectCredentialHandler(request *restful.Request, resp *restful.Response) {

	projectId := request.PathParameter("devops")
	credentialId := request.PathParameter("credential")

	credentialId, err := h.projectCredentialOperator.DeleteProjectCredential(projectId, credentialId)

	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
83
		api.HandleInternalError(resp, err)
R
runzexia 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		return
	}

	resp.WriteAsJson(struct {
		Name string `json:"name"`
	}{Name: credentialId})
	return
}

func (h ProjectPipelineHandler) GetDevOpsProjectCredentialHandler(request *restful.Request, resp *restful.Response) {

	projectId := request.PathParameter("devops")
	credentialId := request.PathParameter("credential")
	getContent := request.QueryParameter("content")
	response, err := h.projectCredentialOperator.GetProjectCredential(projectId, credentialId, getContent)

	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
102
		api.HandleInternalError(resp, err)
R
runzexia 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
		return
	}

	resp.WriteAsJson(response)
	return
}

func (h ProjectPipelineHandler) GetDevOpsProjectCredentialsHandler(request *restful.Request, resp *restful.Response) {
	projectId := request.PathParameter("devops")

	jenkinsCredentials, err := h.projectCredentialOperator.GetProjectCredentials(projectId)
	if err != nil {
		klog.Errorf("%+v", err)
H
hongming 已提交
116
		api.HandleInternalError(resp, err)
R
runzexia 已提交
117 118 119 120 121
		return
	}
	resp.WriteAsJson(jenkinsCredentials)
	return
}