forked from aws-samples/aurora-dsql-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
129 lines (104 loc) · 3.05 KB
/
example.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"os"
"strings"
_ "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/dsql/auth"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
_ "github.com/jackc/pgx/v5/stdlib"
)
type Owner struct {
Id string `json:"id"`
Name string `json:"name"`
City string `json:"city"`
Telephone string `json:"telephone"`
}
func GenerateDbConnectAdminAuthToken(clusterEndpoint string, region string, action string) (string, error) {
ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return "", err
}
token, err := auth.GenerateDBConnectAdminAuthToken(ctx, clusterEndpoint, region, cfg.Credentials)
if err != nil {
return "", err
}
return token, nil
}
func getConnection(ctx context.Context, clusterEndpoint string, region string) (*pgx.Conn, error) {
// Build connection URL
var sb strings.Builder
sb.WriteString("postgres://")
sb.WriteString(clusterEndpoint)
sb.WriteString(":5432/postgres?user=admin&sslmode=verify-full")
url := sb.String()
// The token expiration time is optional, and the default value 900 seconds
// If you are not connecting as admin, use DbConnect action instead
token, err := GenerateDbConnectAdminAuthToken(clusterEndpoint, region, "DbConnectAdmin")
if err != nil {
return nil, err
}
connConfig, err := pgx.ParseConfig(url)
// To avoid issues with parse config set the password directly in config
connConfig.Password = token
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
os.Exit(1)
}
conn, err := pgx.ConnectConfig(ctx, connConfig)
return conn, err
}
func example(clusterEndpoint string, region string) error {
ctx := context.Background()
// Establish connection
conn, err := getConnection(ctx, clusterEndpoint, region)
if err != nil {
return err
}
defer conn.Close(ctx)
// Create owner table
_, err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS owner (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255),
city VARCHAR(255),
telephone VARCHAR(255)
)
`)
if err != nil {
return err
}
// insert data
query := `INSERT INTO owner (id, name, city, telephone) VALUES ($1, $2, $3, $4)`
_, err = conn.Exec(ctx, query, uuid.New(), "John Doe", "Anytown", "555-555-0150")
if err != nil {
return err
}
owners := []Owner{}
// Define the SQL query to insert a new owner record.
query = `SELECT id, name, city, telephone FROM owner where name='John Doe'`
rows, err := conn.Query(ctx, query)
defer rows.Close()
owners, err = pgx.CollectRows(rows, pgx.RowToStructByName[Owner])
fmt.Println(owners)
if err != nil || owners[0].Name != "John Doe" || owners[0].City != "Anytown" {
panic("Error retrieving data")
}
_, err = conn.Exec(ctx, `DELETE FROM owner where name='John Doe'`)
if err != nil {
return err
}
return nil
}
// Run example
func main() {
err := example(os.Getenv("CLUSTER_ENDPOINT"), os.Getenv("REGION"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to run example: %v\n", err)
os.Exit(1)
}
}