|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "fmt" |
6 | 4 | "log" |
7 | 5 | "os" |
8 | 6 |
|
9 | | - outpostgo "github.com/hookdeck/outpost/sdks/outpost-go" |
10 | | - "github.com/hookdeck/outpost/sdks/outpost-go/models/components" |
11 | 7 | "github.com/joho/godotenv" |
12 | 8 | ) |
13 | 9 |
|
14 | | -func withJwt(ctx context.Context, jwt string, serverURL string, tenantID string) { |
15 | | - log.Println("--- Running with Tenant JWT ---") |
16 | | - |
17 | | - apiServerURL := fmt.Sprintf("%s/api/v1", serverURL) |
18 | | - |
19 | | - jwtClient := outpostgo.New( |
20 | | - outpostgo.WithSecurity(components.Security{ |
21 | | - TenantJwt: outpostgo.String(jwt), |
22 | | - }), |
23 | | - outpostgo.WithServerURL(apiServerURL), |
24 | | - ) |
25 | | - |
26 | | - destRes, err := jwtClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil) |
27 | | - if err != nil { |
28 | | - log.Fatalf("Failed to list destinations with JWT: %v", err) |
29 | | - } |
30 | | - |
31 | | - if destRes != nil && destRes.Destinations != nil { |
32 | | - log.Printf("Successfully listed %d destinations using JWT.", len(destRes.Destinations)) |
33 | | - } else { |
34 | | - log.Println("List destinations with JWT returned no data or an unexpected response structure.") |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -func withAdminApiKey(ctx context.Context, serverURL string, adminAPIKey string, tenantID string) { |
39 | | - log.Println("--- Running with Admin API Key ---") |
40 | | - |
41 | | - apiServerURL := fmt.Sprintf("%s/api/v1", serverURL) |
42 | | - |
43 | | - adminClient := outpostgo.New( |
44 | | - outpostgo.WithSecurity(components.Security{ |
45 | | - AdminAPIKey: outpostgo.String(adminAPIKey), |
46 | | - }), |
47 | | - outpostgo.WithServerURL(apiServerURL), |
48 | | - ) |
49 | | - |
50 | | - healthRes, err := adminClient.Health.Check(ctx) |
51 | | - if err != nil { |
52 | | - log.Fatalf("Health check failed: %v", err) |
53 | | - } |
54 | | - |
55 | | - if healthRes != nil && healthRes.Res != nil { |
56 | | - log.Printf("Health check successful. Details: %s", *healthRes.Res) |
57 | | - } else { |
58 | | - log.Println("Health check returned no data or an unexpected response structure.") |
59 | | - } |
60 | | - |
61 | | - destRes, err := adminClient.Destinations.List(ctx, outpostgo.String(tenantID), nil, nil) |
62 | | - if err != nil { |
63 | | - log.Fatalf("Failed to list destinations with Admin Key: %v", err) |
64 | | - } |
65 | | - |
66 | | - if destRes != nil && destRes.Destinations != nil { |
67 | | - log.Printf("Successfully listed %d destinations using Admin Key for tenant %s.", len(destRes.Destinations), tenantID) |
68 | | - } else { |
69 | | - log.Println("List destinations with Admin Key returned no data or an unexpected response structure.") |
70 | | - } |
71 | | - |
72 | | - tokenRes, err := adminClient.Tenants.GetToken(ctx, outpostgo.String(tenantID)) |
73 | | - if err != nil { |
74 | | - log.Fatalf("Failed to get tenant token: %v", err) |
75 | | - } |
76 | | - |
77 | | - if tokenRes != nil && tokenRes.TenantToken != nil && tokenRes.TenantToken.Token != nil { |
78 | | - log.Printf("Successfully obtained tenant JWT for tenant %s.", tenantID) |
79 | | - withJwt(ctx, *tokenRes.TenantToken.Token, serverURL, tenantID) |
80 | | - } else { |
81 | | - log.Println("Get tenant token returned no data or an unexpected response structure.") |
82 | | - } |
83 | | -} |
84 | | - |
85 | 10 | func main() { |
86 | 11 | err := godotenv.Load() |
87 | 12 | if err != nil { |
88 | 13 | log.Println("No .env file found, proceeding without it") |
89 | 14 | } |
90 | 15 |
|
91 | | - serverURL := os.Getenv("SERVER_URL") |
92 | 16 | adminAPIKey := os.Getenv("ADMIN_API_KEY") |
93 | | - tenantID := os.Getenv("TENANT_ID") |
94 | | - |
| 17 | + serverURL := os.Getenv("SERVER_URL") |
95 | 18 | if serverURL == "" { |
96 | | - log.Fatal("SERVER_URL environment variable not set") |
| 19 | + serverURL = "http://localhost:3333" |
| 20 | + log.Printf("SERVER_URL not set, defaulting to %s", serverURL) |
97 | 21 | } |
| 22 | + |
98 | 23 | if adminAPIKey == "" { |
99 | | - log.Fatal("ADMIN_API_KEY environment variable not set") |
| 24 | + log.Println("Warning: ADMIN_API_KEY environment variable not set. Some examples might fail.") |
100 | 25 | } |
101 | | - if tenantID == "" { |
102 | | - log.Fatal("TENANT_ID environment variable not set") |
| 26 | + |
| 27 | + if len(os.Args) < 2 { |
| 28 | + log.Println("Usage: go run . <example_name>") |
| 29 | + log.Println("Available examples: manage, auth") |
| 30 | + os.Exit(1) |
103 | 31 | } |
104 | 32 |
|
105 | | - ctx := context.Background() |
106 | | - withAdminApiKey(ctx, serverURL, adminAPIKey, tenantID) |
| 33 | + exampleToRun := os.Args[1] |
107 | 34 |
|
108 | | - log.Println("--- Example finished ---") |
| 35 | + switch exampleToRun { |
| 36 | + case "manage": |
| 37 | + if adminAPIKey == "" { |
| 38 | + log.Fatal("ADMIN_API_KEY environment variable must be set to run the 'manage' example.") |
| 39 | + } |
| 40 | + log.Println("--- Running Manage Outpost Resources Example ---") |
| 41 | + manageOutpostResources(adminAPIKey, serverURL) |
| 42 | + case "auth": |
| 43 | + log.Println("--- Running Auth Example ---") |
| 44 | + runAuthExample() |
| 45 | + default: |
| 46 | + log.Printf("Unknown example: %s\n", exampleToRun) |
| 47 | + log.Println("Available examples: manage, auth") |
| 48 | + os.Exit(1) |
| 49 | + } |
109 | 50 | } |
0 commit comments