This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource.go
80 lines (61 loc) · 1.93 KB
/
resource.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
package containerurl
import (
"context"
"fmt"
"net/url"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/azure-operator/service/controller/controllercontext"
)
const (
Name = "containerurl"
)
type Config struct {
Logger micrologger.Logger
}
type Resource struct {
logger micrologger.Logger
}
func New(config Config) (*Resource, error) {
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
newResource := &Resource{
logger: config.Logger,
}
return newResource, nil
}
func (r *Resource) Name() string {
return Name
}
func (r *Resource) addContainerURLToContext(ctx context.Context, containerName, storageAccountName, primaryKey string) error {
r.logger.LogCtx(ctx, "level", "debug", "message", "setting containerurl to context")
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
sc, err := azblob.NewSharedKeyCredential(storageAccountName, primaryKey)
if err != nil {
return microerror.Mask(err)
}
p := azblob.NewPipeline(sc, azblob.PipelineOptions{})
u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", storageAccountName))
serviceURL := azblob.NewServiceURL(*u, p)
containerURL := serviceURL.NewContainerURL(containerName)
_, err = containerURL.GetProperties(ctx, azblob.LeaseAccessConditions{})
if err != nil {
return microerror.Mask(err)
}
cc.ContainerURL = &containerURL
r.logger.LogCtx(ctx, "level", "debug", "message", "set containerurl to context")
return nil
}
func (r *Resource) getStorageAccountsClient(ctx context.Context) (*storage.AccountsClient, error) {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
return cc.AzureClientSet.StorageAccountsClient, nil
}