Skip to content

Commit e4a1004

Browse files
committed
ore azure: add ability to tag images
Add ability to tag images to facilate resource management.
1 parent 4b098a9 commit e4a1004

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

mantle/cmd/ore/azure/tag-blob.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2022 Red Hat
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package azure
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
cmdTagBlob = &cobra.Command{
27+
Use: "tag-blob",
28+
Short: "Tag a Azure storage blob",
29+
Long: "tag a storage blob on Azure.",
30+
Run: runSetMetadata,
31+
}
32+
33+
bmo struct {
34+
tags []string
35+
storageacct string
36+
container string
37+
blob string
38+
resourceGroup string
39+
}
40+
)
41+
42+
func init() {
43+
sv := cmdTagBlob.Flags().StringVar
44+
ssv := cmdTagBlob.Flags().StringSliceVar
45+
sv(&bmo.storageacct, "storage-account", "kola", "storage account name")
46+
sv(&bmo.container, "container", "vhds", "container name")
47+
sv(&bmo.blob, "blob-name", "", "name of the blob")
48+
sv(&bmo.resourceGroup, "resource-group", "kola", "resource group name")
49+
ssv(&bmo.tags, "tags", []string{}, "list of key=value tags to attach to the Azure storage blob")
50+
Azure.AddCommand(cmdTagBlob)
51+
}
52+
53+
func runSetMetadata(cmd *cobra.Command, args []string) {
54+
if bmo.blob == "" {
55+
fmt.Fprintf(os.Stderr, "Provide --blob-name to tag\n")
56+
os.Exit(1)
57+
}
58+
59+
if len(bmo.tags) < 1 {
60+
fmt.Fprintf(os.Stderr, "Provide --tag to tag\n")
61+
os.Exit(1)
62+
}
63+
64+
if err := api.SetupClients(); err != nil {
65+
fmt.Fprintf(os.Stderr, "setting up clients: %v\n", err)
66+
os.Exit(1)
67+
}
68+
69+
kr, err := api.GetStorageServiceKeys(bmo.storageacct, bmo.resourceGroup)
70+
if err != nil {
71+
fmt.Fprintf(os.Stderr, "Fetching storage service keys failed: %v\n", err)
72+
os.Exit(1)
73+
}
74+
75+
if len(kr.Keys) == 0 {
76+
fmt.Fprintf(os.Stderr, "No storage service keys found")
77+
os.Exit(1)
78+
}
79+
k := kr.Keys
80+
key := k[0].Value
81+
82+
tagMap := make(map[string]*string)
83+
for _, tag := range bmo.tags {
84+
splitTag := strings.SplitN(tag, "=", 2)
85+
if len(splitTag) != 2 {
86+
fmt.Fprintf(os.Stderr, "invalid tag format; should be key=value, not %v\n", tag)
87+
os.Exit(1)
88+
}
89+
key, value := splitTag[0], splitTag[1]
90+
tagMap[key] = &value
91+
}
92+
93+
err = api.SetBlobMetadata(bmo.storageacct, *key, bmo.container, bmo.blob, tagMap)
94+
if err != nil {
95+
fmt.Fprintf(os.Stderr, "couldn't create image tags: %v", err)
96+
os.Exit(1)
97+
}
98+
}

mantle/platform/api/azure/storage.go

+10
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ func (a *API) DeleteBlockBlob(storageaccount, key, container, blob string) error
174174
return err
175175
}
176176

177+
func (a *API) SetBlobMetadata(storageaccount, key, container, blobname string, tags map[string]*string) error {
178+
client, err := getPageBlobClient(storageaccount, key, container, blobname)
179+
if err != nil {
180+
return err
181+
}
182+
183+
_, err = client.SetMetadata(context.Background(), tags, nil)
184+
return err
185+
}
186+
177187
func getBlockBlobClient(storageaccount, key string) (*azblob.Client, error) {
178188
serviceURL := fmt.Sprintf("https://%s.blob.core.windows.net/", storageaccount)
179189
cred, err := azblob.NewSharedKeyCredential(storageaccount, key)

0 commit comments

Comments
 (0)