This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
current.go
81 lines (64 loc) · 2.27 KB
/
current.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
package blobobject
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/resourcecanceledcontext"
"github.com/giantswarm/azure-operator/v8/service/controller/blobclient"
"github.com/giantswarm/azure-operator/v8/service/controller/controllercontext"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
func (r *Resource) GetCurrentState(ctx context.Context, obj interface{}) (interface{}, error) {
cr, err := key.ToCustomResource(obj)
if err != nil {
return nil, microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
if cc.ContainerURL == nil {
r.logger.Debugf(ctx, "containerurl resource is not ready")
resourcecanceledcontext.SetCanceled(ctx)
r.logger.Debugf(ctx, "canceling resource")
return nil, nil
}
containerURL := cc.ContainerURL
containerName := key.BlobContainerName()
storageAccountName := key.StorageAccountName(&cr)
r.logger.Debugf(ctx, "finding blob object's container")
// if here is no container account - return and wait for deployment to finish container operation.
containerExists, err := blobclient.ContainerExists(ctx, containerURL)
if err != nil {
return nil, microerror.Mask(err)
}
if !containerExists {
r.logger.Debugf(ctx, "did not find blob object's container")
resourcecanceledcontext.SetCanceled(ctx)
r.logger.Debugf(ctx, "canceling resource")
return nil, nil
} else if err != nil {
return nil, microerror.Mask(err)
}
r.logger.Debugf(ctx, "found blob object's container")
r.logger.Debugf(ctx, "finding container objects")
listBlobs, err := blobclient.ListBlobs(ctx, containerURL)
if err != nil {
return nil, microerror.Mask(err)
}
output := []ContainerObjectState{}
for _, object := range listBlobs.Segment.BlobItems {
body, err := blobclient.GetBlockBlob(ctx, object.Name, containerURL)
if err != nil {
return nil, microerror.Mask(err)
}
containerObjectState := ContainerObjectState{
Body: string(body),
ContainerName: containerName,
Key: object.Name,
StorageAccountName: storageAccountName,
}
output = append(output, containerObjectState)
}
r.logger.Debugf(ctx, "found container objects")
return output, nil
}