|
| 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 | +} |
0 commit comments