-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
102 lines (90 loc) · 2.39 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
95
96
97
98
99
100
101
102
package main
import (
"context"
"flag"
"fmt"
eLog "github.com/labstack/gommon/log"
"net/http"
"os"
"github.com/dirien/pulumi-connector/internal/port"
"github.com/dirien/pulumi-connector/internal/pulumi"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
var (
Port int
PortClientId string
PortClientSecret string
PortBaseUrl string
portClient *port.Client
pu *pulumi.Pulumi
)
func main() {
e := echo.New()
if debug, ok := os.LookupEnv("DEBUG"); ok && debug == "true" {
e.Logger.SetLevel(eLog.DEBUG)
} else {
e.Logger.SetLevel(eLog.ERROR)
}
flag.IntVar(&Port, "port", 8080, "Port for test HTTP server")
PortClientId, _ = os.LookupEnv("PORT_CLIENT_ID")
if PortClientId == "" {
e.Logger.Fatal("PORT_CLIENT_ID is not set")
}
PortClientSecret, _ = os.LookupEnv("PORT_CLIENT_SECRET")
if PortClientSecret == "" {
e.Logger.Fatal("PORT_CLIENT_SECRET is not set")
}
PortBaseUrl, _ = os.LookupEnv("PORT_BASE_URL")
if PortBaseUrl == "" {
PortBaseUrl = "https://api.getport.io"
}
if os.Getenv("PULUMI_ACCESS_TOKEN") == "" {
e.Logger.Fatal("PULUMI_ACCESS_TOKEN is set")
}
flag.Parse()
e.Use(middleware.Logger())
e.Logger.Info("Authenticating with Port")
portClient = port.New(PortBaseUrl)
_, err := portClient.Authenticate(context.Background(), PortClientId, PortClientSecret)
if err != nil {
e.Logger.Fatalf("failed to authenticate with Port: %v", err)
}
pu = pulumi.NewPulumi(&e.Logger)
e.POST("/", func(c echo.Context) error {
err = actionHandler(c)
if err != nil {
e.Logger.Errorf("%v", err)
c.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()})
return nil
}
c.JSON(http.StatusOK, echo.Map{"message": "ok"})
return nil
})
e.Logger.Fatal(e.Start(fmt.Sprintf("0.0.0.0:%d", Port)))
}
func actionHandler(c echo.Context) (err error) {
body := port.ActionBody{}
err = c.Bind(&body)
if err != nil {
return err
}
ctx := context.Background()
switch body.Payload.Action.Trigger {
case "CREATE", "DAY-2":
err = pu.Up(ctx, &body)
case "DELETE":
err = pu.Destroy(ctx, &body)
default:
return fmt.Errorf("unknown action: %s", body.Payload.Action.Identifier)
}
if err != nil {
portClient.PatchActionRun(ctx, body.Context.RunID, port.ActionStatusFailure)
return err
}
err = portClient.PatchActionRun(ctx, body.Context.RunID, port.ActionStatusSuccess)
if err != nil {
return err
}
return nil
}