Skip to content

Commit 2fc1fc4

Browse files
committed
more commands
1 parent 7f549ea commit 2fc1fc4

File tree

15 files changed

+649
-272
lines changed

15 files changed

+649
-272
lines changed

astra-shell/README.MD

Lines changed: 165 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,212 @@
1-
# Astra Shell
1+
# Getting Started with Astra CLI
22

33
[![License Apache2](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)
44

5-
_This component is in heavy development that can lead to breaking changes._
5+
Astra Shell is a standalone command line interface proposing functions to operate the Datastax Astra platform.
66

7+
## A. Installation
78

8-
## Installation
9+
### Prequisites
910

10-
Installing ASTRA-CLI on UNIX-like platforms is as easy as ever. ASTRA-CLI installs smoothly on macOS, Linux, WSL, Cygwin, Solaris and FreeBSD. We also support Bash and ZSH shells.
11+
On your machine you will need :
12+
- A bash shell
13+
- Commands: `unzip`, `curl`
14+
- A Java JRE or JDK 8+ (temporary)
1115

12-
- Open a new terminal and enter:
16+
You will also need to:
17+
- [Create an Astra account](https://astra.datastax.com)
18+
- [Create an authentication token](https://dtsx.io/create-astra-token)
1319

20+
### Installation
21+
22+
```
23+
curl -Ls "https://dtsx.io/get-astra-cli" | bash
24+
```
25+
26+
### Setup
27+
28+
- 1/3 - Execute the following
29+
```
30+
astra setup
31+
```
32+
33+
- 2/3 When asked provides a token of your choice:
34+
35+
```
36+
• Enter your token (AstraCS...) :
37+
AstraCS:.......
1438
```
15-
curl -Ls curl -s "https://dtsx.io/get-astra-cli" | bash
39+
- 3/3 Open a new TAB to finalize install
40+
41+
```
42+
astra --version
43+
astra help
1644
```
1745

18-
- Follow the instructions on-screen to complete installation.
46+
## B. Working with configurations
1947

20-
- To get autocompletion open a new terminal :
48+
- **List Configurations**
2149

2250
```
23-
source "$HOME/.astra/install/astra-init.sh"
51+
astra config list
2452
```
2553

26-
- Lastly, run the following code snippet to ensure that installation succeeded:
54+
- **Create new configuration**
55+
56+
If you have multiple organizations it could be useful to switch from one to another. To create another config, the latest created org will become the default.
2757

2858
```
29-
astra -v
59+
astra config create dev -t <token_of_org_2>
3060
```
3161

32-
If all went well, the version should be displayed. Something like:
62+
```
63+
astra config list
64+
```
65+
66+
When you issue any command the config is now loaded for you:
3367

3468
```
35-
0.3.2-SNAPSHOT
69+
astra user list
3670
```
3771

38-
## Setup
72+
- **Change default configuration**:
3973

40-
- Run the following command and provide your token when needed
74+
You can change the default org at any time with the following command:
4175

4276
```
43-
astra setup
77+
astra config default dev && astra config list
4478
```
4579

46-
- You should be good to go
80+
- Test again, user list should be different
4781

4882
```
83+
astra user list
84+
```
85+
86+
- Back to original org (change with your email)
87+
88+
```
89+
astra config default [email protected] && astra config list
90+
```
91+
92+
- You can use any organization without switching default configuration with `--config` or `-t`
93+
94+
```
95+
astra user list --config dev
96+
```
97+
98+
- **Delete configuration**
99+
100+
You can delete any organization. I see you.. if you delete the default one you are on your own and you would need to define a new one.
101+
102+
```
103+
astra config delete dev
49104
astra config list
50105
```
51106

107+
## C. Working with Databases
108+
109+
- **List Databases**:
110+
52111
```
53-
+-----------------------------------------+
54-
| configuration |
55-
+-----------------------------------------+
56-
| default ([email protected]) |
57-
| DatastaxDevs |
58-
59-
+-----------------------------------------+
112+
astra db list
60113
```
61114

62-
- And access Astra
115+
- **Create a new database:**
116+
117+
If not provided the region will be the default free region and the keyspace will be the database name but you can change then with `-r` and `-k` respectivitely.
63118

64119
```
120+
astra db create demo
65121
astra db list
66122
```
67123

124+
- **Get information on your database**
125+
126+
Notice how you never provide any the technical ids.
127+
128+
```
129+
astra db get demo
68130
```
69-
+---------------------+--------------------------------------+---------------------+----------------+
70-
| name | id | default-region | status |
71-
+---------------------+--------------------------------------+---------------------+----------------+
72-
| mtg | dde308f5-a8b0-474d-afd6-81e5689e3e25 | eu-central-1 | ACTIVE |
73-
| workshops | 3ed83de7-d97f-4fb6-bf9f-82e9f7eafa23 | eu-west-1 | ACTIVE |
74-
| sdk_tests | 06a9675a-ca62-4cd0-9b94-aefaf395922b | us-east-1 | ACTIVE |
75-
| api_days_dubai_2022 | 53f07b2a-8856-4741-b595-72b03be56bc6 | us-east1 | ACTIVE |
76-
| free_db | 8642763c-8ba7-4ede-a031-82355c7c34a7 | us-east1 | ACTIVE |
77-
+---------------------+--------------------------------------+---------------------+----------------+
131+
132+
- **Change output format**
133+
134+
This CLI wil be used by CI/CD and script, sometimes the output must be parsed. There are 3 outputs: *human,json,csv"
135+
78136
```
137+
astra db list -o json
138+
```
139+
140+
```
141+
astra db list -o csv
142+
```
143+
144+
The return type is also important to check the errors. O mean everything is OK
145+
146+
```
147+
echo $?
148+
```
149+
150+
```
151+
astra db get i_do_not_exist && echo $?
152+
```
153+
154+
Error codes tables
155+
156+
| Error | Code |
157+
|-------------------|-----------|
158+
| SUCCESS | 0 |
159+
| PARSE_ERROR | 1 |
160+
| INVALID_PARAMETER | 10 |
161+
| NOT_FOUND | 20 |
162+
| CONFLICT | 21 |
163+
| ALREADY_EXIST | 22 |
164+
| CANNOT_CONNECT | 30 |
165+
| INTERNAL_ERROR | 40 |
166+
167+
168+
- The cli implemented `If not Exist` for both keyspace and database (convenient for workshops and training)
169+
170+
```
171+
astra help db create
172+
astra db create demo --if-not-exist
173+
```
174+
175+
- All commands can also be in versbose mode anytime with `-v`
176+
177+
```
178+
astra db create demo --if-not-exist -v
179+
```
180+
181+
Make sure the database is now Active or you will get some expected errors.
182+
183+
- **Create a new keyspace**
184+
185+
```
186+
astra db create-keyspace demo -k ks2
187+
```
188+
189+
Need helps ?
190+
```
191+
astra help db create-keyspace
192+
```
193+
194+
- Now let's blow your mind 🥁
195+
196+
```
197+
astra db cqlsh demo
198+
```
199+
200+
201+
## D. Interactive mode
202+
203+
204+
## E. Working with Users
205+
206+
207+
## F. Working with Roles
208+
209+
79210

80211

81212

astra-shell/src/main/java/com/datastax/astra/shell/AstraShell.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.datastax.astra.shell.cmd.ExitCommand;
44
import com.datastax.astra.shell.cmd.HelpCommand;
55
import com.datastax.astra.shell.cmd.QuitCommand;
6+
import com.datastax.astra.shell.cmd.db.DbCqlShellShell;
7+
import com.datastax.astra.shell.cmd.db.DbCreateKeyspaceShell;
68
import com.datastax.astra.shell.cmd.db.DbCreateShell;
79
import com.datastax.astra.shell.cmd.db.DbDeleteShell;
810
import com.datastax.astra.shell.cmd.db.DbGetShell;
@@ -38,21 +40,30 @@
3840
EmptyCommand.class,
3941
HelpCommand.class,
4042
ExitCommand.class,
41-
QuitCommand.class
43+
44+
// With selected db
45+
DbCreateKeyspaceShell.class,
46+
DbGetShell.class,
47+
DbCqlShellShell.class,
48+
QuitCommand.class,
4249
},
4350
groups = {
4451
@Group(name = OperationsDb.DB, description = "Commands acting of database", commands = {
4552
DbCreateShell.class,
4653
DbDeleteShell.class,
4754
DbListShell.class,
4855
DbGetShell.class,
49-
DbUseShell.class
56+
57+
DbUseShell.class,
58+
}),
59+
@Group(name = OperationsDb.CMD_KEYSPACE, description = "Manage keyspaces (db must be selected)", commands = {
60+
DbCreateKeyspaceShell.class,
5061
}),
51-
@Group(name= OperationIam.COMMAND_ROLE, description = "Manage the roles (RBAC)", commands = {
62+
@Group(name= OperationIam.COMMAND_ROLE, description = "Manage roles (RBAC)", commands = {
5263
RoleListShell.class,
5364
RoleGetCli.class
5465
}),
55-
@Group(name= OperationIam.COMMAND_USER, description = "Manage the users permission", commands = {
66+
@Group(name= OperationIam.COMMAND_USER, description = "Manage users permission", commands = {
5667
UserListShell.class,
5768
UserGetShell.class,
5869
UserInviteShell.class,

astra-shell/src/main/java/com/datastax/astra/shell/cmd/BaseShellCommand.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,19 @@ public void run() {
4343
protected ShellContext ctx() {
4444
return ShellContext.getInstance();
4545
}
46+
47+
/**
48+
* Db Selected.
49+
* @return
50+
* if a db is selected or not
51+
*/
52+
protected boolean dbSelected() {
53+
boolean selected = (null != ctx().getDatabase()) ;
54+
if (!selected) {
55+
LoggerShell.error("You must select a DB first with 'db use <dbname>'");
56+
}
57+
return selected;
58+
}
59+
4660

4761
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.datastax.astra.shell.cmd;
22

33
import com.datastax.astra.shell.ExitCode;
4-
import com.datastax.astra.shell.out.LoggerShell;
54
import com.github.rvesse.airline.annotations.Command;
65

76
/**
@@ -15,12 +14,11 @@ public class QuitCommand extends BaseShellCommand {
1514
/** {@inheritDoc} */
1615
@Override
1716
public ExitCode execute() {
18-
if (null != ctx().getDatabase()) {
19-
ctx().exitDatabase();
20-
return ExitCode.SUCCESS;
17+
if (!dbSelected()) {
18+
return ExitCode.CONFLICT;
2119
}
22-
LoggerShell.warning("You have no base selected.");
23-
return ExitCode.NOT_FOUND;
20+
ctx().exitDatabase();
21+
return ExitCode.SUCCESS;
2422
}
2523

2624
}

0 commit comments

Comments
 (0)