project_credential.go 3.4 KB
Newer Older
R
runzexia 已提交
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
package devops

import (
	"github.com/asaskevich/govalidator"
	"time"
)

const (
	CredentialTypeUsernamePassword = "username_password"
	CredentialTypeSsh              = "ssh"
	CredentialTypeSecretText       = "secret_text"
	CredentialTypeKubeConfig       = "kubeconfig"
)

type JenkinsCredential struct {
	Id          string `json:"id"`
	Type        string `json:"type"`
	DisplayName string `json:"display_name"`
	Fingerprint *struct {
		FileName string `json:"file_name,omitempty"`
		Hash     string `json:"hash,omitempty"`
		Usage    []*struct {
			Name   string `json:"name,omitempty"`
			Ranges struct {
				Ranges []*struct {
					Start int `json:"start"`
					End   int `json:"end"`
				} `json:"ranges"`
			} `json:"ranges"`
		} `json:"usage,omitempty"`
	} `json:"fingerprint,omitempty"`
	Description                string                      `json:"description"`
	Domain                     string                      `json:"domain"`
	CreateTime                 *time.Time                  `json:"create_time,omitempty"`
	Creator                    string                      `json:"creator,omitempty"`
	UsernamePasswordCredential *UsernamePasswordCredential `json:"username_password"`
	SshCredential              *SshCredential              `json:"ssh"`
	SecretTextCredential       *SecretTextCredential       `json:"secret_text"`
	KubeconfigCredential       *KubeconfigCredential       `json:"kubeconfig"`
}

type UsernamePasswordCredential struct {
	Id          string `json:"id"`
	Username    string `json:"username"`
	Password    string `json:"password,omitempty"`
	Description string `json:"description"`
}

type SshCredential struct {
	Id          string `json:"id"`
	Username    string `json:"username"`
	Passphrase  string `json:"passphrase"`
	PrivateKey  string `json:"private_key" mapstructure:"private_key"`
	Description string `json:"description"`
}

type SecretTextCredential struct {
	Id          string `json:"id"`
	Secret      string `json:"secret"`
	Description string `json:"description"`
}

type KubeconfigCredential struct {
	Id          string `json:"id"`
	Content     string `json:"content"`
	Description string `json:"description"`
}

type DeleteCredentialRequest struct {
	Domain string `json:"domain"`
}

type CopySshCredentialRequest struct {
	Id string `json:"id"`
}

const (
	ProjectCredentialTableName       = "project_credential"
	ProjectCredentialIdColumn        = "credential_id"
	ProjectCredentialDomainColumn    = "domain"
	ProjectCredentialProjectIdColumn = "project_id"
)

var CredentialTypeMap = map[string]string{
	"SSH Username with private key":         CredentialTypeSsh,
	"Username with password":                CredentialTypeUsernamePassword,
	"Secret text":                           CredentialTypeSecretText,
	"Kubernetes configuration (kubeconfig)": CredentialTypeKubeConfig,
}

type ProjectCredential struct {
	ProjectId    string    `json:"project_id"`
	CredentialId string    `json:"credential_id"`
	Domain       string    `json:"domain"`
	Creator      string    `json:"creator"`
	CreateTime   time.Time `json:"create_time"`
}

var ProjectCredentialColumns = GetColumnsFromStruct(&ProjectCredential{})

func NewProjectCredential(projectId, credentialId, domain, creator string) *ProjectCredential {
	if govalidator.IsNull(domain) {
		domain = "_"
	}
	return &ProjectCredential{
		ProjectId:    projectId,
		CredentialId: credentialId,
		Domain:       domain,
		Creator:      creator,
		CreateTime:   time.Now(),
	}
}