Skip to content

Commit 439ebf9

Browse files
author
Oleg Sucharevich
committed
move api access to be resoruce based, breaking change
1 parent 23e53b3 commit 439ebf9

File tree

11 files changed

+90
-52
lines changed

11 files changed

+90
-52
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.4
1+
0.4.0

cmd/create_runtime_environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var createRuntimeEnvironmentCmd = &cobra.Command{
4040
Namespace: namespace,
4141
HasAgent: true,
4242
}
43-
re, err := codefreshClient.CreateRuntimeEnvironment(opt)
43+
re, err := codefreshClient.RuntimeEnvironments().Create(opt)
4444
if err == nil {
4545
fmt.Printf("Runtime-Environment %s created\n", re.Metadata.Name)
4646
}

cmd/create_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var createTokenCmd = &cobra.Command{
2828
Run: func(cmd *cobra.Command, args []string) {
2929
client := viper.Get("codefresh")
3030
codefreshClient := utils.CastToCodefreshOrDie(client)
31-
token, err := codefreshClient.GenerateToken("TestToken", "hybrid/codefresh-re")
31+
token, err := codefreshClient.Tokens().Create("TestToken", "hybrid/codefresh-re")
3232
internal.DieOnError(err)
3333
table := internal.CreateTable()
3434
table.SetHeader([]string{"name", "token"})

cmd/get_pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var getPipelineCmd = &cobra.Command{
3939
table := internal.CreateTable()
4040
table.SetHeader([]string{"Pipeline Name", "Created At", "Updated At"})
4141
table.Append([]string{"", "", ""})
42-
pipelines, err := codefreshClient.GetPipelines()
42+
pipelines, err := codefreshClient.Pipelines().List()
4343
internal.DieOnError(err)
4444
for _, p := range pipelines {
4545
table.Append([]string{

cmd/get_runtime_environment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ var getRuntimeEnvironmentCmd = &cobra.Command{
2929
Run: func(cmd *cobra.Command, args []string) {
3030
client := viper.Get("codefresh")
3131
codefreshClient, _ := client.(codefresh.Codefresh)
32+
api := codefreshClient.RuntimeEnvironments()
3233
if len(args) > 0 {
33-
re, _ := codefreshClient.GetRuntimeEnvironment(args[0])
34+
re, _ := api.Get(args[0])
3435
fmt.Printf(re.Metadata.Name)
3536
} else {
36-
res, _ := codefreshClient.GetRuntimeEnvironments()
37+
res, _ := api.List()
3738
for _, re := range res {
3839
fmt.Println(re.Metadata.Name)
3940
}

cmd/get_tokens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var getTokensCmd = &cobra.Command{
3434
table := internal.CreateTable()
3535
table.SetHeader([]string{"Created", "ID", "Name", "Reference Subject", "Reference Type", "Token"})
3636
table.Append([]string{"", "", ""})
37-
tokens, err := codefreshClient.GetTokens()
37+
tokens, err := codefreshClient.Tokens().Get()
3838
internal.DieOnError(err)
3939
for _, t := range tokens {
4040
table.Append([]string{

cmd/run_pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var runPipelineCmd = &cobra.Command{
4040
client := viper.Get("codefresh")
4141
codefreshClient := utils.CastToCodefreshOrDie(client)
4242
for _, name := range args {
43-
build, err := codefreshClient.RunPipeline(name)
43+
build, err := codefreshClient.Pipelines().Run(name)
4444
internal.DieOnError(err)
4545
fmt.Printf("Pipeline started with ID: %s\n", build)
4646
}

pkg/codefresh/codefresh.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import (
1212
type (
1313
Codefresh interface {
1414
requestAPI(*requestOptions) (*http.Response, error)
15-
ITokenAPI
16-
IPipelineAPI
17-
IRuntimeEnvironmentAPI
15+
decodeResponseInto(*http.Response, interface{}) error
16+
getBodyAsString(*http.Response) (string, error)
17+
getBodyAsBytes(*http.Response) ([]byte, error)
18+
Pipelines() IPipelineAPI
19+
Tokens() ITokenAPI
20+
RuntimeEnvironments() IRuntimeEnvironmentAPI
1821
}
1922
)
2023

@@ -27,6 +30,18 @@ func New(opt *ClientOptions) Codefresh {
2730
}
2831
}
2932

33+
func (c *codefresh) Pipelines() IPipelineAPI {
34+
return newPipelineAPI(c)
35+
}
36+
37+
func (c *codefresh) Tokens() ITokenAPI {
38+
return newTokenAPI(c)
39+
}
40+
41+
func (c *codefresh) RuntimeEnvironments() IRuntimeEnvironmentAPI {
42+
return newRuntimeEnvironmentAPI(c)
43+
}
44+
3045
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
3146
var body []byte
3247
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)

pkg/codefresh/pipeline.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
type (
1010
// IPipelineAPI declers Codefresh pipeline API
1111
IPipelineAPI interface {
12-
GetPipelines() ([]*Pipeline, error)
13-
RunPipeline(string) (string, error)
12+
List() ([]*Pipeline, error)
13+
Run(string) (string, error)
1414
}
1515

1616
PipelineMetadata struct {
@@ -58,26 +58,34 @@ type (
5858
Docs []*Pipeline `json:"docs"`
5959
Count int `json:"count"`
6060
}
61+
62+
pipeline struct {
63+
codefresh Codefresh
64+
}
6165
)
6266

63-
// GetPipelines - returns pipelines from API
64-
func (c *codefresh) GetPipelines() ([]*Pipeline, error) {
67+
func newPipelineAPI(codefresh Codefresh) IPipelineAPI {
68+
return &pipeline{codefresh}
69+
}
70+
71+
// Get - returns pipelines from API
72+
func (p *pipeline) List() ([]*Pipeline, error) {
6573
r := &getPipelineResponse{}
66-
resp, err := c.requestAPI(&requestOptions{
74+
resp, err := p.codefresh.requestAPI(&requestOptions{
6775
path: "/api/pipelines",
6876
method: "GET",
6977
})
70-
err = c.decodeResponseInto(resp, r)
78+
err = p.codefresh.decodeResponseInto(resp, r)
7179
return r.Docs, err
7280
}
7381

74-
func (c *codefresh) RunPipeline(name string) (string, error) {
75-
resp, err := c.requestAPI(&requestOptions{
82+
func (p *pipeline) Run(name string) (string, error) {
83+
resp, err := p.codefresh.requestAPI(&requestOptions{
7684
path: fmt.Sprintf("/api/pipelines/run/%s", url.PathEscape(name)),
7785
method: "POST",
7886
})
7987
if err != nil {
8088
return "", err
8189
}
82-
return c.getBodyAsString(resp)
90+
return p.codefresh.getBodyAsString(resp)
8391
}

pkg/codefresh/runtime_enrionment.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
type (
1111
// IRuntimeEnvironmentAPI declers Codefresh runtime environment API
1212
IRuntimeEnvironmentAPI interface {
13-
CreateRuntimeEnvironment(*CreateRuntimeOptions) (*RuntimeEnvironment, error)
14-
ValidateRuntimeEnvironment(*ValidateRuntimeOptions) error
15-
SignRuntimeEnvironmentCertificate(*SignCertificatesOptions) ([]byte, error)
16-
GetRuntimeEnvironment(string) (*RuntimeEnvironment, error)
17-
GetRuntimeEnvironments() ([]*RuntimeEnvironment, error)
13+
Create(*CreateRuntimeOptions) (*RuntimeEnvironment, error)
14+
Validate(*ValidateRuntimeOptions) error
15+
SignCertificate(*SignCertificatesOptions) ([]byte, error)
16+
Get(string) (*RuntimeEnvironment, error)
17+
List() ([]*RuntimeEnvironment, error)
1818
}
1919

2020
RuntimeEnvironment struct {
@@ -76,13 +76,21 @@ type (
7676
CSR string
7777
}
7878

79-
createRuntimeEnvironmentResponse struct {
79+
CreateResponse struct {
8080
Name string
8181
}
82+
83+
runtimeEnvironment struct {
84+
codefresh Codefresh
85+
}
8286
)
8387

84-
// CreateRuntimeEnvironment - create Runtime-Environment
85-
func (c *codefresh) CreateRuntimeEnvironment(opt *CreateRuntimeOptions) (*RuntimeEnvironment, error) {
88+
func newRuntimeEnvironmentAPI(codefresh Codefresh) IRuntimeEnvironmentAPI {
89+
return &runtimeEnvironment{codefresh}
90+
}
91+
92+
// Create - create Runtime-Environment
93+
func (r *runtimeEnvironment) Create(opt *CreateRuntimeOptions) (*RuntimeEnvironment, error) {
8694
re := &RuntimeEnvironment{
8795
Metadata: RuntimeMetadata{
8896
Name: fmt.Sprintf("%s/%s", opt.Cluster, opt.Namespace),
@@ -95,7 +103,7 @@ func (c *codefresh) CreateRuntimeEnvironment(opt *CreateRuntimeOptions) (*Runtim
95103
if opt.HasAgent {
96104
body["agent"] = true
97105
}
98-
resp, err := c.requestAPI(&requestOptions{
106+
resp, err := r.codefresh.requestAPI(&requestOptions{
99107
path: "/api/custom_clusters/register",
100108
method: "POST",
101109
body: body,
@@ -111,39 +119,39 @@ func (c *codefresh) CreateRuntimeEnvironment(opt *CreateRuntimeOptions) (*Runtim
111119
return nil, fmt.Errorf("Error during runtime environment creation")
112120
}
113121

114-
func (c *codefresh) ValidateRuntimeEnvironment(opt *ValidateRuntimeOptions) error {
122+
func (r *runtimeEnvironment) Validate(opt *ValidateRuntimeOptions) error {
115123
body := map[string]interface{}{
116124
"clusterName": opt.Cluster,
117125
"namespace": opt.Namespace,
118126
}
119-
_, err := c.requestAPI(&requestOptions{
127+
_, err := r.codefresh.requestAPI(&requestOptions{
120128
path: "/api/custom_clusters/validate",
121129
method: "POST",
122130
body: body,
123131
})
124132
return err
125133
}
126134

127-
func (c *codefresh) SignRuntimeEnvironmentCertificate(opt *SignCertificatesOptions) ([]byte, error) {
135+
func (r *runtimeEnvironment) SignCertificate(opt *SignCertificatesOptions) ([]byte, error) {
128136
body := map[string]interface{}{
129137
"reqSubjectAltName": opt.AltName,
130138
"csr": opt.CSR,
131139
}
132-
resp, err := c.requestAPI(&requestOptions{
140+
resp, err := r.codefresh.requestAPI(&requestOptions{
133141
path: "/api/custom_clusters/signServerCerts",
134142
method: "POST",
135143
body: body,
136144
})
137145
if err != nil {
138146
return nil, err
139147
}
140-
return c.getBodyAsBytes(resp)
148+
return r.codefresh.getBodyAsBytes(resp)
141149
}
142150

143-
func (c *codefresh) GetRuntimeEnvironment(name string) (*RuntimeEnvironment, error) {
151+
func (r *runtimeEnvironment) Get(name string) (*RuntimeEnvironment, error) {
144152
re := &RuntimeEnvironment{}
145153
path := fmt.Sprintf("/api/runtime-environments/%s", url.PathEscape(name))
146-
resp, err := c.requestAPI(&requestOptions{
154+
resp, err := r.codefresh.requestAPI(&requestOptions{
147155
path: path,
148156
method: "GET",
149157
qs: map[string]string{
@@ -155,17 +163,17 @@ func (c *codefresh) GetRuntimeEnvironment(name string) (*RuntimeEnvironment, err
155163
fmt.Println(err.Error())
156164
return nil, err
157165
}
158-
c.decodeResponseInto(resp, re)
166+
r.codefresh.decodeResponseInto(resp, re)
159167
return re, nil
160168
}
161169

162-
func (c *codefresh) GetRuntimeEnvironments() ([]*RuntimeEnvironment, error) {
170+
func (r *runtimeEnvironment) List() ([]*RuntimeEnvironment, error) {
163171
emptySlice := make([]*RuntimeEnvironment, 0)
164-
resp, err := c.requestAPI(&requestOptions{
172+
resp, err := r.codefresh.requestAPI(&requestOptions{
165173
path: "/api/runtime-environments",
166174
method: "GET",
167175
})
168-
tokensAsBytes, err := c.getBodyAsBytes(resp)
176+
tokensAsBytes, err := r.codefresh.getBodyAsBytes(resp)
169177
if err != nil {
170178
return nil, err
171179
}

0 commit comments

Comments
 (0)