/
main.go
94 lines (76 loc) · 2.21 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/containership/go-containership.cloud/client"
"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin"
)
var (
version = "unknown"
)
func main() {
fmt.Printf("Drone ContainerShip Plugin built from %s\n", version)
repo := drone.Repo{}
vargs := Params{}
plugin.Param("repo", &repo)
plugin.Param("vargs", &vargs)
plugin.MustParse()
if vargs.Application == "" {
vargs.Application = repo.Name
}
if vargs.Image == "" {
vargs.Image = repo.FullName + ":latest"
}
if vargs.ApiKey == "" {
fmt.Println("Error: ContainerShip Cloud API Key is required!")
os.Exit(1)
}
if vargs.ClusterId == "" {
fmt.Println("Error: ContainerShip Cloud Cluster ID is required!")
os.Exit(1)
}
if vargs.Organization == "" {
fmt.Println("Error: ContainerShip Cloud Organization is required!")
os.Exit(1)
}
client := client.NewContainerShipCloudClient(vargs.Organization, vargs.ApiKey)
response, err := client.GetApplication(vargs.ClusterId, vargs.Application)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
application := map[string]interface{}{
"id": vargs.Application,
"image": vargs.Image,
}
switch response.StatusCode {
case 404:
create_response, err := client.CreateApplication(vargs.ClusterId, vargs.Application, application)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch create_response.StatusCode {
case 201:
fmt.Printf("Success: ContainerShip Cloud created %s\n", vargs.Application)
default:
fmt.Printf("Error: ContainerShip Cloud returned a %d response when creating %s\n", create_response.StatusCode, vargs.Application)
}
case 200:
update_response, err := client.UpdateApplication(vargs.ClusterId, vargs.Application, application)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch update_response.StatusCode {
case 200:
fmt.Printf("Success: ContainerShip Cloud updated %s\n", vargs.Application)
default:
fmt.Printf("Error: ContainerShip Cloud returned a %d response when updating %s\n", update_response.StatusCode, vargs.Application)
}
default:
fmt.Printf("Error: ContainerShip Cloud returned a %d response when fetching %s\n", response.StatusCode, vargs.Application)
os.Exit(1)
}
}