-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_bucket_object_ownership.go
89 lines (72 loc) · 2.75 KB
/
storage_bucket_object_ownership.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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/storage/sos"
"github.com/spf13/cobra"
)
const (
objOwnershipOpArgIndex = 0
objOwnershipBucketArgIndex = 1
objOwnershipStatus = "status"
objOwnershipObjectWriter = "object-writer"
objOwnershipBucketOwnerEnforced = "bucket-owner-enforced"
objOwnershipBucketOwnerPreferred = "bucket-owner-preferred"
)
func init() {
storageBucketObjectOwnershipCmd.Flags().StringP(zoneFlagLong, zoneFlagShort, "", zoneFlagMsg)
storageBucketCmd.AddCommand(storageBucketObjectOwnershipCmd)
}
var storageBucketObjectOwnershipCmd = &cobra.Command{
// TODO
Use: "object-ownership {status,object-writer,bucket-owner-enforced,bucket-owner-preferred} sos://BUCKET",
Aliases: []string{"oo"},
Short: "Manage the Object Ownership setting of a Storage Bucket",
Long: storageBucketObjectOwnershipCmdLongHelp(),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
permittedOps := make(map[string]struct{}, 4)
permittedOps[objOwnershipStatus] = struct{}{}
permittedOps[objOwnershipObjectWriter] = struct{}{}
permittedOps[objOwnershipBucketOwnerEnforced] = struct{}{}
permittedOps[objOwnershipBucketOwnerPreferred] = struct{}{}
if _, ok := permittedOps[args[objOwnershipOpArgIndex]]; !ok {
cmdExitOnUsageError(cmd, "invalid operation")
}
args[objOwnershipBucketArgIndex] = strings.TrimPrefix(args[objOwnershipBucketArgIndex], sos.BucketPrefix)
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{zoneFlagLong})
},
RunE: func(cmd *cobra.Command, args []string) error {
ownershipCommand := args[objOwnershipOpArgIndex]
bucket := args[objOwnershipBucketArgIndex]
zone, err := cmd.Flags().GetString(zoneFlagLong)
if err != nil {
return err
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptWithZone(zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
switch ownershipCommand {
case objOwnershipStatus:
return printOutput(storage.GetBucketObjectOwnership(cmd.Context(), bucket))
case objOwnershipObjectWriter:
return storage.SetBucketObjectOwnership(cmd.Context(), bucket, sos.ObjectOwnershipObjectWriter)
case objOwnershipBucketOwnerPreferred:
return storage.SetBucketObjectOwnership(cmd.Context(), bucket, sos.ObjectOwnershipBucketOwnerPreferred)
case objOwnershipBucketOwnerEnforced:
return storage.SetBucketObjectOwnership(cmd.Context(), bucket, sos.ObjectOwnershipBucketOwnerEnforced)
}
return fmt.Errorf("invalid operation")
},
}
var storageBucketObjectOwnershipCmdLongHelp = func() string {
// TODO
return "Manage the Object Ownership setting of a Storage Bucket"
}