Skip to content

Commit 273629c

Browse files
committed
Adding code and updated readme
1 parent ed00a89 commit 273629c

File tree

8 files changed

+110
-8
lines changed

8 files changed

+110
-8
lines changed

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
## My Project
1+
## Amazon Keyspaces Python Utility Scripts
22

3-
TODO: Fill this README out!
4-
5-
Be sure to:
6-
7-
* Change the title in this README
8-
* Edit your repository description on GitHub
3+
This repo contains utility scripts to help manage users for the [Scalability and familiarity with Amazon Keyspaces](https://aws.amazon.com/getting-started/hands-on/purpose-built-databases/keyspaces/) tutorial. Please see the tutorial for more information.
94

105
## Security
116

@@ -14,4 +9,3 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform
149
## License
1510

1611
This library is licensed under the MIT-0 License. See the LICENSE file.
17-

create_user.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from entities import User
2+
from utils import cluster
3+
4+
session = cluster.connect()
5+
6+
7+
def create_user(user):
8+
print(f"Creating {user.username}...")
9+
command = f"INSERT INTO users.users (username, name, email, address, birthdate) VALUES ('{user.username}', '{user.name}', '{user.email}', '{user.address}', '{user.birthdate}') IF NOT EXISTS"
10+
r = session.execute(command)
11+
results = r.one()
12+
applied = results["[applied]"]
13+
if not applied:
14+
raise Exception(f"User {user.username} already exists")
15+
print(f"User {user.username} created successfully!")
16+
return user
17+
18+
19+
user = User(
20+
username="michelle72",
21+
name="Thomas Davis",
22+
email="jake74@hotmail.com",
23+
address="136 Ryan Knolls, East Tiffany, AL 29438",
24+
birthdate="1986-07-14",
25+
)
26+
27+
create_user(user)

delete_user.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from entities import User
2+
from utils import cluster
3+
4+
session = cluster.connect()
5+
6+
7+
def delete_user(username):
8+
print(f"Deleting {username}...")
9+
command = f"DELETE FROM users.users WHERE username = '{username}'"
10+
r = session.execute(command)
11+
results = r.one()
12+
print(f"User {username} deleted.")
13+
14+
15+
delete_user("michelle72")

entities.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class User:
2+
def __init__(self, **kwargs):
3+
self.username = kwargs["username"]
4+
self.name = kwargs["name"]
5+
self.email = kwargs["email"]
6+
self.address = kwargs["address"]
7+
self.birthdate = kwargs["birthdate"]
8+
9+
def __repr__(self):
10+
return "User<{} -- {}>".format(self.username, self.name)

get_user.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from entities import User
2+
from utils import cluster
3+
4+
session = cluster.connect()
5+
6+
7+
def get_user(username):
8+
print(f"Retrieving {username}...")
9+
command = f"SELECT * FROM users.users WHERE username = '{username}'"
10+
r = session.execute(command)
11+
results = r.one()
12+
if not results:
13+
raise Exception(f"User {username} does not exist")
14+
user = User(**results)
15+
print(user)
16+
return user
17+
18+
19+
user = get_user("michelle72")

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cassandra-driver==3.23.0

test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from entities import User
2+
from utils import cluster
3+
4+
session = cluster.connect()
5+
r = session.execute("SELECT * FROM users.users LIMIT 5")
6+
print(
7+
f"Connected to Keyspaces table. There are {len(r.current_rows)} rows in the results."
8+
)
9+
for row in r.current_rows:
10+
print(User(**row))

utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
3+
from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
4+
from cassandra import ConsistencyLevel
5+
from cassandra.query import dict_factory
6+
from ssl import SSLContext, PROTOCOL_TLSv1, CERT_REQUIRED
7+
from cassandra.auth import PlainTextAuthProvider
8+
9+
USERNAME = os.environ["CASSANDRA_USERNAME"]
10+
PASSWORD = os.environ["CASSANDRA_PASSWORD"]
11+
REGION = os.environ["AWS_REGION"]
12+
13+
ssl_context = SSLContext(PROTOCOL_TLSv1)
14+
ssl_context.load_verify_locations("./AmazonRootCA1.pem")
15+
ssl_context.verify_mode = CERT_REQUIRED
16+
auth_provider = PlainTextAuthProvider(username=USERNAME, password=PASSWORD)
17+
profile = ExecutionProfile(
18+
consistency_level=ConsistencyLevel.LOCAL_QUORUM, row_factory=dict_factory
19+
)
20+
cluster = Cluster(
21+
[f"cassandra.{REGION}.amazonaws.com"],
22+
ssl_context=ssl_context,
23+
auth_provider=auth_provider,
24+
port=9142,
25+
execution_profiles={EXEC_PROFILE_DEFAULT: profile},
26+
)

0 commit comments

Comments
 (0)