forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
99 lines (86 loc) · 3.84 KB
/
provider.go
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
package gitlab
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/rancher/norman/store/subtype"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/pipeline/providers/common"
"github.com/rancher/rancher/pkg/pipeline/remote/model"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/rancher/types/apis/project.cattle.io/v3/schema"
"github.com/rancher/types/client/project/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type GlProvider struct {
common.BaseProvider
}
func (g *GlProvider) CustomizeSchemas(schemas *types.Schemas) {
scpConfigBaseSchema := schemas.Schema(&schema.Version, client.SourceCodeProviderConfigType)
configSchema := schemas.Schema(&schema.Version, client.GitlabPipelineConfigType)
configSchema.ActionHandler = g.ActionHandler
configSchema.Formatter = g.Formatter
configSchema.Store = subtype.NewSubTypeStore(client.GitlabPipelineConfigType, scpConfigBaseSchema.Store)
providerBaseSchema := schemas.Schema(&schema.Version, client.SourceCodeProviderType)
providerSchema := schemas.Schema(&schema.Version, client.GitlabProviderType)
providerSchema.Formatter = g.providerFormatter
providerSchema.ActionHandler = g.providerActionHandler
providerSchema.Store = subtype.NewSubTypeStore(client.GitlabProviderType, providerBaseSchema.Store)
}
func (g *GlProvider) GetName() string {
return model.GitlabType
}
func (g *GlProvider) TransformToSourceCodeProvider(config map[string]interface{}) map[string]interface{} {
m := g.BaseProvider.TransformToSourceCodeProvider(config, client.GitlabProviderType)
m[client.GitlabProviderFieldRedirectURL] = formGitlabRedirectURLFromMap(config)
return m
}
func (g *GlProvider) GetProviderConfig(projectID string) (interface{}, error) {
scpConfigObj, err := g.SourceCodeProviderConfigs.ObjectClient().UnstructuredClient().GetNamespaced(projectID, model.GitlabType, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to retrieve GitlabConfig, error: %v", err)
}
u, ok := scpConfigObj.(runtime.Unstructured)
if !ok {
return nil, fmt.Errorf("failed to retrieve GitlabConfig, cannot read k8s Unstructured data")
}
storedGitlabPipelineConfigMap := u.UnstructuredContent()
storedGitlabPipelineConfig := &v3.GitlabPipelineConfig{}
if err := mapstructure.Decode(storedGitlabPipelineConfigMap, storedGitlabPipelineConfig); err != nil {
return nil, fmt.Errorf("failed to decode the config, error: %v", err)
}
metadataMap, ok := storedGitlabPipelineConfigMap["metadata"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed to retrieve GitlabConfig metadata, cannot read k8s Unstructured data")
}
typemeta := &metav1.ObjectMeta{}
//time.Time cannot decode directly
delete(metadataMap, "creationTimestamp")
if err := mapstructure.Decode(metadataMap, typemeta); err != nil {
return nil, fmt.Errorf("failed to decode the config, error: %v", err)
}
storedGitlabPipelineConfig.ObjectMeta = *typemeta
storedGitlabPipelineConfig.APIVersion = "project.cattle.io/v3"
storedGitlabPipelineConfig.Kind = v3.SourceCodeProviderConfigGroupVersionKind.Kind
return storedGitlabPipelineConfig, nil
}
func formGitlabRedirectURLFromMap(config map[string]interface{}) string {
hostname := convert.ToString(config[client.GitlabPipelineConfigFieldHostname])
clientID := convert.ToString(config[client.GitlabPipelineConfigFieldClientID])
tls := convert.ToBool(config[client.GitlabPipelineConfigFieldTLS])
return gitlabRedirectURL(hostname, clientID, tls)
}
func gitlabRedirectURL(hostname, clientID string, tls bool) string {
redirect := ""
if hostname != "" {
scheme := "http://"
if tls {
scheme = "https://"
}
redirect = scheme + hostname
} else {
redirect = gitlabDefaultHostName
}
return fmt.Sprintf("%s/oauth/authorize?client_id=%s&response_type=code", redirect, clientID)
}