forked from redhat-developer/odo
/
project.go
135 lines (118 loc) · 3.71 KB
/
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
128
129
130
131
132
133
134
135
package project
import (
"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/pkg/errors"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/occlient"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetCurrent return current project
func GetCurrent(client *occlient.Client) string {
project := client.GetCurrentProjectName()
return project
}
// SetCurrent sets the projectName as current project
func SetCurrent(client *occlient.Client, projectName string) error {
err := client.SetCurrentProject(projectName)
if err != nil {
return errors.Wrap(err, "unable to set current project to"+projectName)
}
return nil
}
func Create(client *occlient.Client, projectName string, wait bool) error {
err := client.CreateNewProject(projectName, wait)
if err != nil {
return errors.Wrap(err, "unable to create new project")
}
return nil
}
// Delete deletes the project with name projectName and returns errors if any
func Delete(client *occlient.Client, projectName string) error {
// Loading spinner
s := log.Spinnerf("Deleting project %s", projectName)
defer s.End(false)
// Delete the requested project
err := client.DeleteProject(projectName)
if err != nil {
return errors.Wrap(err, "unable to delete project")
}
s.End(true)
return nil
}
func DescribeProjects(client *occlient.Client) (ProjectList, error) {
currentProject := client.GetCurrentProjectName()
allProjects, err := client.GetProjectNames()
if err != nil {
return ProjectList{}, errors.Wrap(err, "cannot get all the projects")
}
// Get apps from project
var projects []Project
for _, project := range allProjects {
isActive := false
if project == currentProject {
isActive = true
}
apps, _ := application.ListInProject(client)
projects = append(projects, GetMachineReadableFormat(project, isActive, apps))
}
return getMachineReadableFormatForList(projects), nil
}
// Exists Checks whether a project with the given name exists or not
// projectName is the project name to perform check for
// The first returned parameter is a bool indicating if a project with the given name already exists or not
// The second returned parameter is the error that might occurs while execution
func Exists(client *occlient.Client, projectName string) (bool, error) {
project, err := client.GetProject(projectName)
if err != nil || project == nil {
return false, err
}
return true, nil
}
// GetMachineReadableFormat gathers the readable format and output a Project struct
// for json to marshal
func GetMachineReadableFormat(projectName string, isActive bool, apps []string) Project {
return Project{
TypeMeta: metav1.TypeMeta{
Kind: "Project",
APIVersion: "odo.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: projectName,
Namespace: projectName,
},
Spec: ProjectSpec{
Applications: apps,
},
Status: ProjectStatus{
Active: isActive,
},
}
}
// MachineReadableSuccessOutput outputs a success output that includes
// project information and namespace
func MachineReadableSuccessOutput(projectName string, message string) {
machineOutput := machineoutput.Success{
TypeMeta: metav1.TypeMeta{
Kind: "Project",
APIVersion: "odo.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: projectName,
Namespace: projectName,
},
Message: message,
}
machineoutput.OutputSuccess(machineOutput)
}
// getMachineReadableFormatForList returns application list in machine readable format
func getMachineReadableFormatForList(projects []Project) ProjectList {
return ProjectList{
TypeMeta: metav1.TypeMeta{
Kind: "List",
APIVersion: "odo.openshift.io/v1alpha1",
},
ListMeta: metav1.ListMeta{},
Items: projects,
}
}