Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iam): replace account on init command #2778

Merged
merged 19 commits into from
Feb 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ To enable beta features, you can set `SCW_ENABLE_BETA=1` in your environment.

| Namespace | Description | Documentation |
|----------------|-----------------------------------------|-----------------------------------------------------------------------------------------------------------------|
| `account` | Account API | [CLI](./docs/commands/account.md) |
| `account` | User related data | [CLI](./docs/commands/account.md) / [API](https://developers.scaleway.com/en/products/account/api/v2/) |
| `applesilicon` | Apple silicon API | [CLI](./docs/commands/apple-silicon.md) / [API](https://developers.scaleway.com/en/products/apple-silicon/api/) |
| `autocomplete` | Autocomplete related commands | [CLI](./docs/commands/autocomplete.md) |
| `baremetal` | Baremetal API | [CLI](./docs/commands/baremetal.md) / [API](https://developers.scaleway.com/en/products/baremetal/api/) |
3 changes: 1 addition & 2 deletions cmd/scw/testdata/test-all-usage-account-usage.golden
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Account API
This API allows you to manage projects.

USAGE:
scw account <command>

AVAILABLE COMMANDS:
project Project management commands
ssh-key SSH keys management commands

FLAGS:
-h, --help help for account
15 changes: 15 additions & 0 deletions cmd/scw/testdata/test-all-usage-iam-ssh-key-init-usage.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Initialize SSH key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to chance the phrasing here.


USAGE:
scw iam ssh-key init

FLAGS:
-h, --help help for init

GLOBAL FLAGS:
-c, --config string The path to the config file
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-all-usage-iam-ssh-key-usage.golden
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ AVAILABLE COMMANDS:
create Create an SSH key
delete Delete an SSH key
get Get an SSH key
init Initialize SSH key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't mean anything. Could you rephrase to something like, scan for ssh keys on your local machine and send them to your scaleway account.

list List SSH keys
update Update an SSH key

2 changes: 1 addition & 1 deletion cmd/scw/testdata/test-main-usage-usage.golden
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ USAGE:
scw <command>

AVAILABLE COMMANDS:
account Account API
account User related data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the phrasing here too.

apple-silicon Apple silicon API
autocomplete Autocomplete related commands
baremetal Elastic metal API
146 changes: 0 additions & 146 deletions docs/commands/account.md
Original file line number Diff line number Diff line change
@@ -8,13 +8,6 @@ This API allows you to manage projects.
- [Get project](#get-project)
- [List projects](#list-projects)
- [Update project](#update-project)
- [SSH keys management commands](#ssh-keys-management-commands)
- [Add an SSH key to your project](#add-an-ssh-key-to-your-project)
- [Get an SSH key from your project](#get-an-ssh-key-from-your-project)
- [Initialize SSH key](#initialize-ssh-key)
- [List all SSH keys of your project](#list-all-ssh-keys-of-your-project)
- [Remove an SSH key from your project](#remove-an-ssh-key-from-your-project)
- [Update an SSH key on your project](#update-an-ssh-key-on-your-project)


## Project management commands
@@ -124,142 +117,3 @@ scw account project update [arg=value ...]



## SSH keys management commands

SSH keys management commands.


### Add an SSH key to your project

Add an SSH key to your project.

**Usage:**

```
scw account ssh-key add [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| name | | The name of the SSH key |
| public-key | Required | SSH public key. Currently ssh-rsa, ssh-dss (DSA), ssh-ed25519 and ecdsa keys with NIST curves are supported |
| project-id | | Project ID to use. If none is passed the default project ID will be used |
| organization-id | | Organization ID to use. If none is passed the default organization ID will be used |


**Examples:**


Add a given ssh key
```
scw account ssh-key add name=foobar public-key="$(cat <path/to/your/public/key>)"
```




### Get an SSH key from your project

Get an SSH key from your project.

**Usage:**

```
scw account ssh-key get <ssh-key-id ...> [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| ssh-key-id | Required | The ID of the SSH key |



### Initialize SSH key

Initialize SSH key.

**Usage:**

```
scw account ssh-key init
```



### List all SSH keys of your project

List all SSH keys of your project.

**Usage:**

```
scw account ssh-key list [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| order-by | One of: `created_at_asc`, `created_at_desc`, `updated_at_asc`, `updated_at_desc`, `name_asc`, `name_desc` | |
| name | | |
| project-id | | |
| organization-id | | |



### Remove an SSH key from your project

Remove an SSH key from your project.

**Usage:**

```
scw account ssh-key remove <ssh-key-id ...> [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| ssh-key-id | Required | |


**Examples:**


Remove a given SSH key
```
scw account ssh-key remove 11111111-1111-1111-1111-111111111111
```




### Update an SSH key on your project

Update an SSH key on your project.

**Usage:**

```
scw account ssh-key update <ssh-key-id ...> [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| ssh-key-id | Required | |
| name | | Name of the SSH key |



13 changes: 13 additions & 0 deletions docs/commands/iam.md
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ IAM API
- [Create an SSH key](#create-an-ssh-key)
- [Delete an SSH key](#delete-an-ssh-key)
- [Get an SSH key](#get-an-ssh-key)
- [Initialize SSH key](#initialize-ssh-key)
- [List SSH keys](#list-ssh-keys)
- [Update an SSH key](#update-an-ssh-key)
- [Users management commands](#users-management-commands)
@@ -736,6 +737,18 @@ scw iam ssh-key get <ssh-key-id ...> [arg=value ...]



### Initialize SSH key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change phrasing


Initialize SSH key.

**Usage:**

```
scw iam ssh-key init
```



### List SSH keys

List SSH keys.
37 changes: 0 additions & 37 deletions internal/account/account.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package account

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

// Token represents a Token
type Token struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is token maintained in this package? Should we use only the one in the IAM package?

ID string `json:"id"`
@@ -31,32 +23,3 @@ type LoginRequest struct {
Description string `json:"description,omitempty"`
Expires bool `json:"expires"`
}

var (
accountURL = "https://api.scaleway.com/account/v1"
)

func GetAPIKey(ctx context.Context, secretKey string) (*Token, error) {
resp, err := extractHTTPClient(ctx).Get(accountURL + "/tokens/" + secretKey)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get token")
}

token := &LoginResponse{}
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

err = json.Unmarshal(b, token)
if err != nil {
return nil, err
}

return token.Token, err
}
8 changes: 0 additions & 8 deletions internal/account/test.go
Original file line number Diff line number Diff line change
@@ -16,11 +16,3 @@ var contextKey = contextKeyType{}
func InjectHTTPClient(ctx context.Context, httpClient *http.Client) context.Context {
return context.WithValue(ctx, contextKey, httpClient)
}

func extractHTTPClient(ctx context.Context) *http.Client {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems weird that we have to delete this code. Why? Why does using IAM instead of accountv2 impact this function?

httpClient, isHTTPClient := ctx.Value(contextKey).(*http.Client)
if httpClient != nil && isHTTPClient {
return httpClient
}
return http.DefaultClient
}
2 changes: 1 addition & 1 deletion internal/core/context.go
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ func ExtractProfileFlag(ctx context.Context) string {
return extractMeta(ctx).ProfileFlag
}

// GetDocGenContext reuturn a minimal context that can be used by scw-doc-gen
// GetDocGenContext returns a minimal context that can be used by scw-doc-gen
func GetDocGenContext() context.Context {
ctx := context.Background()
client, _ := scw.NewClient(
16 changes: 16 additions & 0 deletions internal/core/validate.go
Original file line number Diff line number Diff line change
@@ -172,6 +172,7 @@ func stringExists(strs []string, s string) bool {
return false
}

// ValidateSecretKey validates a secret key ID.
func ValidateSecretKey() ArgSpecValidateFunc {
return func(argSpec *ArgSpec, valueI interface{}) error {
value := valueI.(string)
@@ -186,6 +187,21 @@ func ValidateSecretKey() ArgSpecValidateFunc {
}
}

// ValidateAccessKey validates an access key ID.
func ValidateAccessKey() ArgSpecValidateFunc {
return func(argSpec *ArgSpec, valueI interface{}) error {
value := valueI.(string)
err := DefaultArgSpecValidateFunc()(argSpec, value)
if err != nil {
return err
}
if !validation.IsAccessKey(value) {
return InvalidAccessKeyError(value)
}
return nil
}
}

// ValidateOrganizationID validates a non-required organization ID.
// By default, for most command, the organization ID is not required.
// In that case, we allow the empty-string value "".
2 changes: 0 additions & 2 deletions internal/namespaces/get_commands.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ import (

"github.com/scaleway/scaleway-cli/v2/internal/core"
accountv2 "github.com/scaleway/scaleway-cli/v2/internal/namespaces/account/v2"
account "github.com/scaleway/scaleway-cli/v2/internal/namespaces/account/v2alpha1"
applesilicon "github.com/scaleway/scaleway-cli/v2/internal/namespaces/applesilicon/v1alpha1"
autocompleteNamespace "github.com/scaleway/scaleway-cli/v2/internal/namespaces/autocomplete"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
@@ -54,7 +53,6 @@ func GetCommands(beta ...bool) *core.Commands {
marketplace.GetCommands(),
initNamespace.GetCommands(),
configNamespace.GetCommands(),
account.GetCommands(),
accountv2.GetCommands(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have one account. Rename it to simply account or avoid renaming entirely if possible.

autocompleteNamespace.GetCommands(),
object.GetCommands(),
Loading