-
Notifications
You must be signed in to change notification settings - Fork 12
/
deployment_invokeoperation_create.go
139 lines (114 loc) · 4.04 KB
/
deployment_invokeoperation_create.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package handlers
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/labstack/echo/v4"
"github.com/microsoft/commercial-marketplace-offer-deploy/internal/config"
"github.com/microsoft/commercial-marketplace-offer-deploy/internal/data"
"github.com/microsoft/commercial-marketplace-offer-deploy/internal/hook"
"github.com/microsoft/commercial-marketplace-offer-deploy/internal/model"
"github.com/microsoft/commercial-marketplace-offer-deploy/internal/model/operation"
"github.com/microsoft/commercial-marketplace-offer-deploy/sdk"
)
const deploymenIdParameterName = "deploymentId"
type invokeDeploymentOperation struct {
repository operation.Repository
}
func (h *invokeDeploymentOperation) Handle(c echo.Context) error {
deploymentId, request, err := h.getParameters(c)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
operation, err := h.createOperation(deploymentId, request)
if err != nil {
return err
}
err = operation.Schedule()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
response := sdk.InvokedDeploymentOperationResponse{
InvokedOperation: &sdk.InvokedOperation{
ID: to.Ptr(operation.ID.String()),
InvokedOn: to.Ptr(time.Now().UTC()),
Name: request.Name,
Parameters: request.Parameters,
Result: nil,
Status: to.Ptr(sdk.StatusScheduled.String()),
},
}
return c.JSON(http.StatusOK, response)
}
func (h *invokeDeploymentOperation) createOperation(deploymentId uint, request *sdk.InvokeDeploymentOperationRequest) (*operation.Operation, error) {
operationType, configure, err := h.getConfigurator(deploymentId, request)
if err != nil {
return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
operation, err := h.repository.New(operationType, configure)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return operation, nil
}
func (h *invokeDeploymentOperation) getConfigurator(deploymentId uint, request *sdk.InvokeDeploymentOperationRequest) (sdk.OperationType, operation.Configure, error) {
if request == nil {
return sdk.OperationUnknown, nil, fmt.Errorf("request is nil")
}
operationType, err := sdk.Type(*request.Name)
if err != nil {
return sdk.OperationUnknown, nil, err
}
parameters, ok := request.Parameters.(map[string]interface{})
if !ok {
parameters = make(map[string]interface{})
}
configure := func(i *model.InvokedOperation) error {
retries := uint(*request.Retries)
if retries <= 0 {
retries = 1
}
i.Retries = retries
i.DeploymentId = deploymentId
i.Name = operationType.String()
i.Parameters = parameters
return nil
}
return operationType, configure, nil
}
func (h *invokeDeploymentOperation) getParameters(c echo.Context) (uint, *sdk.InvokeDeploymentOperationRequest, error) {
deploymentId, err := strconv.Atoi(c.Param(deploymenIdParameterName))
if err != nil {
return uint(deploymentId), nil, fmt.Errorf("%s invalid", deploymenIdParameterName)
}
var request *sdk.InvokeDeploymentOperationRequest
err = c.Bind(&request)
if err != nil {
return uint(deploymentId), nil, err
}
return uint(deploymentId), request, nil
}
func NewInvokeDeploymentOperationHandler(appConfig *config.AppConfig, credential azcore.TokenCredential) echo.HandlerFunc {
return func(c echo.Context) error {
db := data.NewDatabase(appConfig.GetDatabaseOptions()).Instance()
scheduler, err := operation.NewSchedulerFromConfig(appConfig)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
service, err := operation.NewManager(db, scheduler, hook.Notify)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
repository, err := operation.NewRepository(service, nil)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
handler := invokeDeploymentOperation{
repository: repository,
}
return handler.Handle(c)
}
}