forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_project.go
127 lines (100 loc) · 3.8 KB
/
request_project.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
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
package cmd
import (
"errors"
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/client"
cliconfig "github.com/openshift/origin/pkg/cmd/cli/config"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
projectapi "github.com/openshift/origin/pkg/project/api"
)
type NewProjectOptions struct {
ProjectName string
DisplayName string
Description string
NodeSelector string
Client client.Interface
ProjectOptions *ProjectOptions
Out io.Writer
}
const (
requestProject_long = `Create a new project for yourself in OpenShift with you as the project admin.
Assuming your cluster admin has granted you permission, this command will
create a new project for you and assign you as the project admin. You must
be logged in, so you might have to run %[1]s first.
After your project is created you can switch to it using %[2]s <project name>.`
requestProject_example = ` // Create a new project with minimal information
$ %[1]s web-team-dev
// Create a new project with a description and node selector
$ %[1]s web-team-dev --display-name="Web Team Development" --description="Development project for the web team." --node-selector="env=dev"`
)
func NewCmdRequestProject(name, fullName, oscLoginName, oscProjectName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &NewProjectOptions{}
options.Out = out
cmd := &cobra.Command{
Use: fmt.Sprintf("%s NAME [--display-name=DISPLAYNAME] [--description=DESCRIPTION] [--node-selector=<label selector>]", name),
Short: "Request a new project",
Long: fmt.Sprintf(requestProject_long, oscLoginName, oscProjectName),
Example: fmt.Sprintf(requestProject_example, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.complete(cmd, f); err != nil {
kcmdutil.CheckErr(err)
}
var err error
if options.Client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
}
if err := options.Run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.SetOutput(out)
cmd.Flags().StringVar(&options.DisplayName, "display-name", "", "project display name")
cmd.Flags().StringVar(&options.Description, "description", "", "project description")
cmd.Flags().StringVar(&options.NodeSelector, "node-selector", "", "Restrict pods onto nodes matching given label selector. Format: '<key1>=<value1>, <key2>=<value2>...'")
return cmd
}
func (o *NewProjectOptions) complete(cmd *cobra.Command, f *clientcmd.Factory) error {
args := cmd.Flags().Args()
if len(args) != 1 {
cmd.Help()
return errors.New("must have exactly one argument")
}
o.ProjectName = args[0]
o.ProjectOptions = &ProjectOptions{}
o.ProjectOptions.PathOptions = cliconfig.NewPathOptions(cmd)
if err := o.ProjectOptions.Complete(f, []string{""}, o.Out); err != nil {
return err
}
return nil
}
func (o *NewProjectOptions) Run() error {
// TODO eliminate this when we get better forbidden messages
_, err := o.Client.ProjectRequests().List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
projectRequest := &projectapi.ProjectRequest{}
projectRequest.Name = o.ProjectName
projectRequest.DisplayName = o.DisplayName
projectRequest.Annotations = make(map[string]string)
projectRequest.Annotations["description"] = o.Description
projectRequest.Annotations["openshift.io/node-selector"] = o.NodeSelector
project, err := o.Client.ProjectRequests().Create(projectRequest)
if err != nil {
return err
}
if o.ProjectOptions != nil {
o.ProjectOptions.ProjectName = project.Name
o.ProjectOptions.ProjectOnly = true
if err := o.ProjectOptions.RunProject(); err != nil {
return err
}
}
return nil
}