|
| 1 | +package token |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/ui" |
| 10 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" |
| 11 | + "github.com/spf13/pflag" |
| 12 | +) |
| 13 | + |
| 14 | +// CreateCommand creates the "token create" command |
| 15 | +func CreateCommand() commands.Command { |
| 16 | + return &createCommand{ |
| 17 | + BaseCommand: commands.New( |
| 18 | + "create", |
| 19 | + "Create an API token", |
| 20 | + `upctl account token create --name test --expires-in 1h`, |
| 21 | + `upctl account token create --name test --expires-in 1h --allow-ip-range "0.0.0.0/0" --allow-ip-range "::/0"`, |
| 22 | + ), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +var defaultCreateParams = &createParams{ |
| 27 | + CreateTokenRequest: request.CreateTokenRequest{}, |
| 28 | + name: "", |
| 29 | + expiresIn: 0, |
| 30 | + allowedIPRanges: []string{}, // TODO: should we default to empty or "0.0.0.0/0", "::/0"? |
| 31 | + canCreateTokens: false, |
| 32 | +} |
| 33 | + |
| 34 | +func newCreateParams() createParams { |
| 35 | + return createParams{ |
| 36 | + CreateTokenRequest: request.CreateTokenRequest{}, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +type createParams struct { |
| 41 | + request.CreateTokenRequest |
| 42 | + name string |
| 43 | + expiresIn time.Duration |
| 44 | + expiresAt string |
| 45 | + canCreateTokens bool |
| 46 | + allowedIPRanges []string |
| 47 | +} |
| 48 | + |
| 49 | +func (s *createParams) processParams() error { |
| 50 | + if s.expiresIn == 0 && s.expiresAt == "" { |
| 51 | + return fmt.Errorf("either expires-in or expires-at must be set") |
| 52 | + } |
| 53 | + if s.expiresAt != "" { |
| 54 | + var err error |
| 55 | + s.ExpiresAt, err = time.Parse(time.RFC3339, s.expiresAt) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("invalid expires-at: %w", err) |
| 58 | + } |
| 59 | + } else { |
| 60 | + s.ExpiresAt = time.Now().Add(s.expiresIn) |
| 61 | + } |
| 62 | + s.Name = s.name |
| 63 | + s.CanCreateSubTokens = s.canCreateTokens |
| 64 | + s.AllowedIPRanges = s.allowedIPRanges |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +type createCommand struct { |
| 69 | + *commands.BaseCommand |
| 70 | + params createParams |
| 71 | + flagSet *pflag.FlagSet |
| 72 | +} |
| 73 | + |
| 74 | +func applyCreateFlags(fs *pflag.FlagSet, dst, def *createParams) { |
| 75 | + fs.StringVar(&dst.name, "name", def.name, "Name for the token.") |
| 76 | + fs.StringVar(&dst.expiresAt, "expires-at", def.expiresAt, "Exact time when the token expires in RFC3339 format. e.g. 2025-01-01T00:00:00Z") |
| 77 | + fs.DurationVar(&dst.expiresIn, "expires-in", def.expiresIn, "Duration until the token expires. e.g. 24h") |
| 78 | + fs.BoolVar(&dst.canCreateTokens, "can-create-tokens", def.canCreateTokens, "Allow token to be used to create further tokens.") |
| 79 | + fs.StringArrayVar(&dst.allowedIPRanges, "allow-ip-range", def.allowedIPRanges, "Allowed IP ranges for the token. If not defined, the token can not be used from any IP. To allow access from all IPs, use `0.0.0.0/0` as the value.") |
| 80 | + |
| 81 | + commands.Must(fs.SetAnnotation("name", commands.FlagAnnotationNoFileCompletions, nil)) |
| 82 | + commands.Must(fs.SetAnnotation("expires-at", commands.FlagAnnotationNoFileCompletions, nil)) |
| 83 | + commands.Must(fs.SetAnnotation("expires-in", commands.FlagAnnotationNoFileCompletions, nil)) |
| 84 | + commands.Must(fs.SetAnnotation("allow-ip-range", commands.FlagAnnotationNoFileCompletions, nil)) |
| 85 | +} |
| 86 | + |
| 87 | +// InitCommand implements Command.InitCommand |
| 88 | +func (s *createCommand) InitCommand() { |
| 89 | + s.flagSet = &pflag.FlagSet{} |
| 90 | + s.params = newCreateParams() |
| 91 | + applyCreateFlags(s.flagSet, &s.params, defaultCreateParams) |
| 92 | + |
| 93 | + s.AddFlags(s.flagSet) |
| 94 | + commands.Must(s.Cobra().MarkFlagRequired("name")) |
| 95 | +} |
| 96 | + |
| 97 | +// ExecuteWithoutArguments implements commands.NoArgumentCommand |
| 98 | +func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) { |
| 99 | + svc := exec.Token() |
| 100 | + |
| 101 | + if err := s.params.processParams(); err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + msg := fmt.Sprintf("Creating token %s", s.params.Name) |
| 106 | + exec.PushProgressStarted(msg) |
| 107 | + |
| 108 | + res, err := svc.CreateToken(exec.Context(), &s.params.CreateTokenRequest) |
| 109 | + if err != nil { |
| 110 | + return commands.HandleError(exec, msg, err) |
| 111 | + } |
| 112 | + |
| 113 | + exec.PushProgressSuccess(msg) |
| 114 | + |
| 115 | + return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{ |
| 116 | + {Title: "API Token", Value: res.APIToken, Colour: ui.DefaultNoteColours}, |
| 117 | + {Title: "Name", Value: res.Name}, |
| 118 | + {Title: "ID", Value: res.ID, Colour: ui.DefaultUUUIDColours}, |
| 119 | + {Title: "Type", Value: res.Type}, |
| 120 | + {Title: "Created At", Value: res.CreatedAt.Format(time.RFC3339)}, |
| 121 | + {Title: "Expires At", Value: res.ExpiresAt.Format(time.RFC3339)}, |
| 122 | + {Title: "Allowed IP Ranges", Value: res.AllowedIPRanges}, |
| 123 | + {Title: "Can Create Sub Tokens", Value: res.CanCreateSubTokens}, |
| 124 | + }}, nil |
| 125 | +} |
0 commit comments