-
Notifications
You must be signed in to change notification settings - Fork 20
/
iam_access_key.go
47 lines (37 loc) · 1.16 KB
/
iam_access_key.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
egoscale "github.com/exoscale/egoscale/v2"
)
var iamAccessKeyCmd = &cobra.Command{
Use: "access-key",
Aliases: []string{"key"},
Short: "IAM access keys management",
}
// parseIAMAccessKeyResource parses a string-encoded IAM access key resource formatted such as
// DOMAIN/TYPE:NAME and deserializes it into an egoscale.IAMAccessKeyResource struct.
func parseIAMAccessKeyResource(v string) (*egoscale.IAMAccessKeyResource, error) {
var iamAccessKeyResource egoscale.IAMAccessKeyResource
parts := strings.SplitN(v, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format")
}
iamAccessKeyResource.ResourceName = parts[1]
parts = strings.SplitN(parts[0], "/", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format")
}
iamAccessKeyResource.Domain = parts[0]
iamAccessKeyResource.ResourceType = parts[1]
if iamAccessKeyResource.Domain == "" ||
iamAccessKeyResource.ResourceType == "" ||
iamAccessKeyResource.ResourceName == "" {
return nil, fmt.Errorf("invalid format")
}
return &iamAccessKeyResource, nil
}
func init() {
iamCmd.AddCommand(iamAccessKeyCmd)
}