Skip to content

Commit 949bffa

Browse files
Merge pull request #13 from codefresh-io/CR-2431
support codefresh api from agent
2 parents f3f51bf + 8b5e35f commit 949bffa

File tree

6 files changed

+433
-3
lines changed

6 files changed

+433
-3
lines changed

main.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,33 @@
1414

1515
package main
1616

17-
import "github.com/codefresh-io/go-sdk/cmd"
17+
import (
18+
"fmt"
19+
"github.com/codefresh-io/go-sdk/pkg/codefresh"
20+
"github.com/codefresh-io/go-sdk/pkg/utils"
21+
"os"
22+
)
1823

1924
func main() {
20-
cmd.Execute()
25+
//cmd.Execute()
26+
27+
path := fmt.Sprintf("%s/.cfconfig", os.Getenv("HOME"))
28+
options, err := utils.ReadAuthContext(path, "")
29+
if err != nil {
30+
fmt.Println("Failed to read codefresh config file")
31+
panic(err)
32+
}
33+
clientOptions := codefresh.ClientOptions{Host: options.URL,
34+
Auth: codefresh.AuthOptions{Token: options.Token}}
35+
cf := codefresh.New(&clientOptions)
36+
err, ctxts := cf.Contexts().GetGitContexts()
37+
for _, ctx := range *ctxts {
38+
fmt.Println(ctx.Metadata.Name)
39+
_, ctxd := cf.Contexts().GetGitContextByName(ctx.Metadata.Name)
40+
if ctxd.Spec.Data.Auth.SshPrivateKey != "" {
41+
fmt.Println(ctxd.Metadata.Name)
42+
}
43+
44+
}
45+
2146
}

pkg/codefresh/argo.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package codefresh
2+
3+
import "fmt"
4+
5+
type (
6+
ArgoApi interface {
7+
CreateIntegration(integration IntegrationPayloadData) error
8+
UpdateIntegration(name string, integration IntegrationPayloadData) error
9+
GetIntegrations() ([]*IntegrationPayload, error)
10+
GetIntegrationByName(name string) (*IntegrationPayload, error)
11+
DeleteIntegrationByName(name string) error
12+
HeartBeat(error string, version string, integration string) error
13+
SendResources(kind string, items interface{}, amount int, integration string) error
14+
}
15+
16+
argo struct {
17+
codefresh Codefresh
18+
}
19+
20+
IntegrationItem struct {
21+
Amount int `json:"amount"`
22+
}
23+
24+
IntegrationPayloadData struct {
25+
Name string `json:"name"`
26+
Url string `json:"url"`
27+
Clusters IntegrationItem `json:"clusters"`
28+
Applications IntegrationItem `json:"applications"`
29+
Repositories IntegrationItem `json:"repositories"`
30+
Username *string `json:"username"`
31+
Password *string `json:"password"`
32+
Token *string `json:"token"`
33+
ClusterName *string `json:"clusterName"`
34+
ServerVersion *string `json:"serverVersion"`
35+
Provider *string `json:"provider"`
36+
}
37+
38+
IntegrationPayload struct {
39+
Type string `json:"type"`
40+
Data IntegrationPayloadData `json:"data"`
41+
}
42+
43+
Heartbeat struct {
44+
Error string `json:"error"`
45+
AgentVersion string `json:"agentVersion"`
46+
}
47+
AgentState struct {
48+
Kind string `json:"type"`
49+
Items interface{} `json:"items"`
50+
}
51+
)
52+
53+
func newArgoAPI(codefresh Codefresh) ArgoApi {
54+
return &argo{codefresh}
55+
}
56+
57+
func (a *argo) CreateIntegration(integration IntegrationPayloadData) error {
58+
59+
_, err := a.codefresh.requestAPI(&requestOptions{
60+
path: "/argo",
61+
method: "POST",
62+
body: &IntegrationPayload{
63+
Type: "argo-cd",
64+
Data: integration,
65+
},
66+
})
67+
68+
return err
69+
}
70+
71+
func (a *argo) UpdateIntegration(name string, integration IntegrationPayloadData) error {
72+
_, err := a.codefresh.requestAPI(&requestOptions{
73+
method: "PUT",
74+
path: fmt.Sprintf("/argo/%s", name),
75+
body: &IntegrationPayload{
76+
Type: "argo-cd",
77+
Data: integration,
78+
},
79+
})
80+
if err != nil {
81+
return err
82+
}
83+
84+
return nil
85+
}
86+
87+
func (a *argo) GetIntegrations() ([]*IntegrationPayload, error) {
88+
var result []*IntegrationPayload
89+
90+
resp, err := a.codefresh.requestAPI(&requestOptions{
91+
method: "GET",
92+
path: "/argo",
93+
})
94+
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
err = a.codefresh.decodeResponseInto(resp, &result)
100+
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
return result, nil
106+
}
107+
108+
func (a *argo) GetIntegrationByName(name string) (*IntegrationPayload, error) {
109+
var result IntegrationPayload
110+
111+
resp, err := a.codefresh.requestAPI(&requestOptions{
112+
method: "GET",
113+
path: fmt.Sprintf("/argo/%s", name),
114+
})
115+
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
err = a.codefresh.decodeResponseInto(resp, &result)
121+
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
return &result, nil
127+
}
128+
129+
func (a *argo) DeleteIntegrationByName(name string) error {
130+
_, err := a.codefresh.requestAPI(&requestOptions{
131+
method: "DELETE",
132+
path: fmt.Sprintf("/argo/%s", name),
133+
})
134+
135+
if err != nil {
136+
return err
137+
}
138+
139+
return nil
140+
}
141+
142+
func (a *argo) HeartBeat(error string, version string, integration string) error {
143+
var body = Heartbeat{}
144+
145+
if error != "" {
146+
body.Error = error
147+
}
148+
149+
if version != "" {
150+
body.AgentVersion = version
151+
}
152+
153+
_, err := a.codefresh.requestAPI(&requestOptions{
154+
method: "POST",
155+
path: fmt.Sprintf("/argo-agent/%s/heartbeat", integration),
156+
body: body,
157+
})
158+
if err != nil {
159+
return err
160+
}
161+
162+
return nil
163+
}
164+
165+
func (a *argo) SendResources(kind string, items interface{}, amount int, integration string) error {
166+
if items == nil {
167+
return nil
168+
}
169+
170+
_, err := a.codefresh.requestAPI(&requestOptions{
171+
method: "POST",
172+
path: fmt.Sprintf("/argo-agent/%s", integration),
173+
body: &AgentState{Kind: kind, Items: items},
174+
})
175+
if err != nil {
176+
return err
177+
}
178+
179+
return nil
180+
}

pkg/codefresh/codefresh.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func (c *codefresh) Contexts() IContextAPI {
6666
return newContextAPI(c)
6767
}
6868

69+
func (c *codefresh) Argo() ArgoApi {
70+
return newArgoAPI(c)
71+
}
72+
73+
func (c *codefresh) Gitops() GitopsApi {
74+
return newGitopsAPI(c)
75+
}
76+
6977
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
7078
var body []byte
7179
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)

pkg/codefresh/contexts.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type (
44
IContextAPI interface {
55
GetGitContexts() (error, *[]ContextPayload)
66
GetGitContextByName(name string) (error, *ContextPayload)
7+
GetDefaultGitContext() (error, *ContextPayload)
78
}
89

910
context struct {
@@ -74,3 +75,20 @@ func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
7475

7576
return nil, &result
7677
}
78+
79+
func (c context) GetDefaultGitContext() (error, *ContextPayload) {
80+
var result ContextPayload
81+
82+
resp, err := c.codefresh.requestAPI(&requestOptions{
83+
method: "GET",
84+
path: "/contexts/git/default",
85+
})
86+
87+
if err != nil {
88+
return err, nil
89+
}
90+
91+
err = c.codefresh.decodeResponseInto(resp, &result)
92+
93+
return err, &result
94+
}

0 commit comments

Comments
 (0)