2021-04-27 18:24:01 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const aws = @import("aws.zig");
|
2021-05-13 22:53:53 +00:00
|
|
|
const json = @import("json.zig");
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
var verbose = false;
|
|
|
|
|
2021-04-27 18:24:01 +00:00
|
|
|
pub fn log(
|
|
|
|
comptime level: std.log.Level,
|
|
|
|
comptime scope: @TypeOf(.EnumLiteral),
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
|
|
|
// Ignore awshttp messages
|
2021-06-24 01:23:07 +00:00
|
|
|
if (!verbose and scope == .awshttp and @enumToInt(level) >= @enumToInt(std.log.Level.debug))
|
2021-04-27 18:24:01 +00:00
|
|
|
return;
|
|
|
|
const scope_prefix = "(" ++ @tagName(scope) ++ "): ";
|
|
|
|
const prefix = "[" ++ @tagName(level) ++ "] " ++ scope_prefix;
|
|
|
|
|
|
|
|
// Print the message to stderr, silently ignoring any errors
|
|
|
|
const held = std.debug.getStderrMutex().acquire();
|
|
|
|
defer held.release();
|
|
|
|
const stderr = std.io.getStdErr().writer();
|
|
|
|
nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return;
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
const Tests = enum {
|
|
|
|
query_no_input,
|
|
|
|
query_with_input,
|
|
|
|
ec2_query_no_input,
|
2021-08-12 21:24:24 +00:00
|
|
|
json_1_0_query_with_input,
|
|
|
|
json_1_0_query_no_input,
|
2021-08-13 00:51:47 +00:00
|
|
|
json_1_1_query_with_input,
|
|
|
|
json_1_1_query_no_input,
|
2021-06-24 01:23:07 +00:00
|
|
|
};
|
2021-05-13 22:53:53 +00:00
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
pub fn main() anyerror!void {
|
2021-06-18 20:47:27 +00:00
|
|
|
const c_allocator = std.heap.c_allocator;
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){
|
|
|
|
.backing_allocator = c_allocator,
|
|
|
|
};
|
2021-06-24 01:23:07 +00:00
|
|
|
defer _ = gpa.deinit();
|
2021-06-18 20:47:27 +00:00
|
|
|
const allocator = &gpa.allocator;
|
2021-06-24 01:23:07 +00:00
|
|
|
var tests = std.ArrayList(Tests).init(allocator);
|
|
|
|
defer tests.deinit();
|
|
|
|
var args = std.process.args();
|
|
|
|
while (args.next(allocator)) |arg_or_error| {
|
|
|
|
const arg = try arg_or_error;
|
|
|
|
defer allocator.free(arg);
|
|
|
|
if (std.mem.eql(u8, "-v", arg)) {
|
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
inline for (@typeInfo(Tests).Enum.fields) |f| {
|
|
|
|
if (std.mem.eql(u8, f.name, arg)) {
|
|
|
|
try tests.append(@field(Tests, f.name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tests.items.len == 0) {
|
|
|
|
inline for (@typeInfo(Tests).Enum.fields) |f|
|
|
|
|
try tests.append(@field(Tests, f.name));
|
|
|
|
}
|
2021-04-27 18:24:01 +00:00
|
|
|
|
|
|
|
const options = aws.Options{
|
|
|
|
.region = "us-west-2",
|
|
|
|
};
|
2021-06-24 01:23:07 +00:00
|
|
|
std.log.info("Start\n", .{});
|
2021-04-27 18:24:01 +00:00
|
|
|
var client = aws.Aws.init(allocator);
|
|
|
|
defer client.deinit();
|
|
|
|
|
2021-08-13 00:51:47 +00:00
|
|
|
const services = aws.Services(.{ .sts, .ec2, .dynamo_db, .ecs }){};
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
for (tests.items) |t| {
|
|
|
|
std.log.info("===== Start Test: {s} =====", .{@tagName(t)});
|
|
|
|
switch (t) {
|
|
|
|
.query_no_input => {
|
|
|
|
const resp = try client.call(services.sts.get_caller_identity.Request{}, options);
|
|
|
|
defer resp.deinit();
|
|
|
|
std.log.info("arn: {s}", .{resp.response.arn});
|
|
|
|
std.log.info("id: {s}", .{resp.response.user_id});
|
|
|
|
std.log.info("account: {s}", .{resp.response.account});
|
|
|
|
std.log.info("requestId: {s}", .{resp.response_metadata.request_id});
|
|
|
|
},
|
|
|
|
.query_with_input => {
|
|
|
|
// TODO: Find test without sensitive info
|
|
|
|
const access = try client.call(services.sts.get_session_token.Request{
|
|
|
|
.duration_seconds = 900,
|
|
|
|
}, options);
|
|
|
|
defer access.deinit();
|
2021-08-12 21:24:24 +00:00
|
|
|
std.log.info("access key: {s}", .{access.response.credentials.?.access_key_id});
|
|
|
|
},
|
|
|
|
.json_1_0_query_with_input => {
|
|
|
|
const tables = try client.call(services.dynamo_db.list_tables.Request{
|
|
|
|
.limit = 1,
|
|
|
|
}, options);
|
|
|
|
defer tables.deinit();
|
|
|
|
std.log.info("request id: {s}", .{tables.response_metadata.request_id});
|
|
|
|
std.log.info("account has tables: {b}", .{tables.response.table_names.?.len > 0});
|
|
|
|
},
|
|
|
|
.json_1_0_query_no_input => {
|
|
|
|
const limits = try client.call(services.dynamo_db.describe_limits.Request{}, options);
|
|
|
|
defer limits.deinit();
|
|
|
|
std.log.info("account read capacity limit: {d}", .{limits.response.account_max_read_capacity_units});
|
2021-06-24 01:23:07 +00:00
|
|
|
},
|
2021-08-13 00:51:47 +00:00
|
|
|
.json_1_1_query_with_input => {
|
|
|
|
const clusters = try client.call(services.ecs.list_clusters.Request{
|
|
|
|
.max_results = 1,
|
|
|
|
}, options);
|
|
|
|
defer clusters.deinit();
|
|
|
|
std.log.info("request id: {s}", .{clusters.response_metadata.request_id});
|
|
|
|
std.log.info("account has clusters: {b}", .{clusters.response.cluster_arns.?.len > 0});
|
|
|
|
},
|
|
|
|
.json_1_1_query_no_input => {
|
|
|
|
const clusters = try client.call(services.ecs.list_clusters.Request{}, options);
|
|
|
|
defer clusters.deinit();
|
|
|
|
std.log.info("request id: {s}", .{clusters.response_metadata.request_id});
|
|
|
|
std.log.info("account has clusters: {b}", .{clusters.response.cluster_arns.?.len > 0});
|
|
|
|
},
|
2021-06-24 01:23:07 +00:00
|
|
|
.ec2_query_no_input => {
|
2021-08-12 21:24:24 +00:00
|
|
|
std.log.err("EC2 Test disabled due to compiler bug", .{});
|
|
|
|
// const instances = try client.call(services.ec2.describe_instances.Request{}, options);
|
|
|
|
// defer instances.deinit();
|
|
|
|
// std.log.info("reservation count: {d}", .{instances.response.reservations.len});
|
2021-06-24 01:23:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
std.log.info("===== End Test: {s} =====\n", .{@tagName(t)});
|
2021-04-27 18:24:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
// if (test_twice) {
|
|
|
|
// std.time.sleep(1000 * std.time.ns_per_ms);
|
|
|
|
// std.log.info("second request", .{});
|
|
|
|
//
|
|
|
|
// var client2 = aws.Aws.init(allocator);
|
|
|
|
// defer client2.deinit();
|
|
|
|
// const resp2 = try client2.call(services.sts.get_caller_identity.Request{}, options); // catch here and try alloc?
|
|
|
|
// defer resp2.deinit();
|
|
|
|
// }
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
std.log.info("===== Tests complete =====", .{});
|
2021-04-27 18:24:01 +00:00
|
|
|
}
|
2021-05-13 22:53:53 +00:00
|
|
|
|
2021-06-24 01:23:07 +00:00
|
|
|
// TODO: Move into json.zig
|
2021-05-13 22:53:53 +00:00
|
|
|
pub fn jsonFun() !void {
|
|
|
|
// Standard behavior
|
|
|
|
const payload =
|
|
|
|
\\{"GetCallerIdentityResponse":{"GetCallerIdentityResult":{"Account":"0123456789","Arn":"arn:aws:iam::0123456789:user/test","UserId":"MYUSERID"},"ResponseMetadata":{"RequestId":"3b80a99b-7df8-4bcb-96ee-b2759878a5f2"}}}
|
|
|
|
;
|
|
|
|
const Ret3 = struct {
|
|
|
|
getCallerIdentityResponse: struct { getCallerIdentityResult: struct { account: []u8, arn: []u8, user_id: []u8 }, responseMetadata: struct { requestId: []u8 } },
|
|
|
|
};
|
|
|
|
var stream3 = json.TokenStream.init(payload);
|
|
|
|
const res3 = json.parse(Ret3, &stream3, .{
|
|
|
|
.allocator = std.heap.c_allocator,
|
|
|
|
.allow_camel_case_conversion = true, // new option
|
|
|
|
.allow_snake_case_conversion = true, // new option
|
|
|
|
.allow_unknown_fields = true, // new option
|
|
|
|
}) catch unreachable;
|
|
|
|
std.log.info("{}", .{res3});
|
|
|
|
std.log.info("{s}", .{res3.getCallerIdentityResponse.getCallerIdentityResult.user_id});
|
|
|
|
}
|