/
asyncoperationsstatus_get.go
76 lines (60 loc) · 2.16 KB
/
asyncoperationsstatus_get.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
package frontend
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"net/http"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/go-chi/chi/v5"
"github.com/sirupsen/logrus"
"github.com/ugorji/go/codec"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
func (f *frontend) getAsyncOperationsStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
b, err := f._getAsyncOperationsStatus(ctx, r)
reply(log, w, nil, b, err)
}
func (f *frontend) _getAsyncOperationsStatus(ctx context.Context, r *http.Request) ([]byte, error) {
operationId := chi.URLParam(r, "operationId")
subscriptionId := chi.URLParam(r, "subscriptionId")
asyncdoc, err := f.dbAsyncOperations.Get(ctx, operationId)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "The entity was not found.")
case err != nil:
return nil, err
}
resource, err := azure.ParseResourceID(asyncdoc.OpenShiftClusterKey)
switch {
case err != nil:
return nil, err
case resource.SubscriptionID != subscriptionId:
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "The entity was not found.")
}
doc, err := f.dbOpenShiftClusters.Get(ctx, asyncdoc.OpenShiftClusterKey)
if err != nil && !cosmosdb.IsErrorStatusCode(err, http.StatusNotFound) {
return nil, err
}
// don't give away the final operation status until it's committed to the
// database
if doc != nil && doc.AsyncOperationID == operationId {
asyncdoc.AsyncOperation.ProvisioningState = asyncdoc.AsyncOperation.InitialProvisioningState
asyncdoc.AsyncOperation.EndTime = nil
asyncdoc.AsyncOperation.Error = nil
}
asyncdoc.AsyncOperation.MissingFields = api.MissingFields{}
asyncdoc.AsyncOperation.InitialProvisioningState = ""
h := &codec.JsonHandle{
Indent: 4,
}
var b []byte
err = codec.NewEncoderBytes(&b, h).Encode(asyncdoc.AsyncOperation)
if err != nil {
return nil, err
}
return b, nil
}