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");
|
2022-01-13 01:01:37 +00:00
|
|
|
const version = @import("git_version.zig");
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2022-01-20 03:29:58 +00:00
|
|
|
var verbose: u8 = 0;
|
2021-06-24 01:23:07 +00:00
|
|
|
|
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 {
|
2022-01-20 03:29:58 +00:00
|
|
|
// Ignore aws_signing messages
|
|
|
|
if (verbose < 2 and scope == .aws_signing and @enumToInt(level) >= @enumToInt(std.log.Level.debug))
|
|
|
|
return;
|
2021-04-27 18:24:01 +00:00
|
|
|
// Ignore awshttp messages
|
2022-01-20 03:29:58 +00:00
|
|
|
if (verbose < 1 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
|
2021-12-23 16:51:48 +00:00
|
|
|
std.debug.getStderrMutex().lock();
|
|
|
|
defer std.debug.getStderrMutex().unlock();
|
2021-04-27 18:24:01 +00:00
|
|
|
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-08-14 01:12:05 +00:00
|
|
|
rest_json_1_query_no_input,
|
|
|
|
rest_json_1_query_with_input,
|
2021-08-25 00:02:28 +00:00
|
|
|
rest_json_1_work_with_lambda,
|
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 {
|
2022-01-12 17:18:16 +00:00
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
2021-06-24 01:23:07 +00:00
|
|
|
defer _ = gpa.deinit();
|
2021-12-23 16:51:48 +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();
|
2022-01-13 01:01:37 +00:00
|
|
|
var first = true;
|
2021-06-24 01:23:07 +00:00
|
|
|
while (args.next(allocator)) |arg_or_error| {
|
|
|
|
const arg = try arg_or_error;
|
|
|
|
defer allocator.free(arg);
|
2022-01-13 01:01:37 +00:00
|
|
|
if (first)
|
|
|
|
std.log.info("{s} {s}", .{ arg, version.pretty_version });
|
|
|
|
first = false;
|
2021-06-24 01:23:07 +00:00
|
|
|
if (std.mem.eql(u8, "-v", arg)) {
|
2022-01-20 03:29:58 +00:00
|
|
|
verbose += 1;
|
2021-06-24 01:23:07 +00:00
|
|
|
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
|
|
|
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("Start\n", .{});
|
2022-01-20 05:17:44 +00:00
|
|
|
var client = try aws.Client.init(allocator, .{});
|
2021-04-27 18:24:01 +00:00
|
|
|
const options = aws.Options{
|
|
|
|
.region = "us-west-2",
|
2021-08-25 00:02:28 +00:00
|
|
|
.client = client,
|
2021-04-27 18:24:01 +00:00
|
|
|
};
|
|
|
|
defer client.deinit();
|
|
|
|
|
2022-01-21 14:15:48 +00:00
|
|
|
const services = aws.Services(.{ .sts, .ec2, .dynamo_db, .ecs, .lambda, .sqs }){};
|
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 => {
|
2021-08-25 00:02:28 +00:00
|
|
|
const call = try aws.Request(services.sts.get_caller_identity).call(.{}, options);
|
|
|
|
// const call = try client.call(services.sts.get_caller_identity.Request{}, options);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("arn: {s}", .{call.response.arn});
|
|
|
|
std.log.info("id: {s}", .{call.response.user_id});
|
|
|
|
std.log.info("account: {s}", .{call.response.account});
|
|
|
|
std.log.info("requestId: {s}", .{call.response_metadata.request_id});
|
2021-06-24 01:23:07 +00:00
|
|
|
},
|
|
|
|
.query_with_input => {
|
2022-01-21 14:15:48 +00:00
|
|
|
const call = try client.call(services.sqs.list_queues.Request{
|
|
|
|
.queue_name_prefix = "s",
|
2021-06-24 01:23:07 +00:00
|
|
|
}, options);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer call.deinit();
|
2022-01-21 14:15:48 +00:00
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
|
|
|
std.log.info("account has queues with prefix 's': {b}", .{call.response.queue_urls != null});
|
2021-08-12 21:24:24 +00:00
|
|
|
},
|
|
|
|
.json_1_0_query_with_input => {
|
2021-08-14 01:12:05 +00:00
|
|
|
const call = try client.call(services.dynamo_db.list_tables.Request{
|
2021-08-12 21:24:24 +00:00
|
|
|
.limit = 1,
|
|
|
|
}, options);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("account has tables: {b}", .{call.response.table_names.?.len > 0});
|
2021-08-12 21:24:24 +00:00
|
|
|
},
|
|
|
|
.json_1_0_query_no_input => {
|
2021-08-14 01:12:05 +00:00
|
|
|
const call = try client.call(services.dynamo_db.describe_limits.Request{}, options);
|
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("account read capacity limit: {d}", .{call.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 => {
|
2021-08-14 01:12:05 +00:00
|
|
|
const call = try client.call(services.ecs.list_clusters.Request{
|
2021-08-13 00:51:47 +00:00
|
|
|
.max_results = 1,
|
|
|
|
}, options);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("account has clusters: {b}", .{call.response.cluster_arns.?.len > 0});
|
2021-08-13 00:51:47 +00:00
|
|
|
},
|
|
|
|
.json_1_1_query_no_input => {
|
2021-08-14 01:12:05 +00:00
|
|
|
const call = try client.call(services.ecs.list_clusters.Request{}, options);
|
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("account has clusters: {b}", .{call.response.cluster_arns.?.len > 0});
|
2021-08-14 01:12:05 +00:00
|
|
|
},
|
|
|
|
.rest_json_1_query_with_input => {
|
|
|
|
const call = try client.call(services.lambda.list_functions.Request{
|
|
|
|
.max_items = 1,
|
|
|
|
}, options);
|
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("account has functions: {b}", .{call.response.functions.?.len > 0});
|
2021-08-14 01:12:05 +00:00
|
|
|
},
|
|
|
|
.rest_json_1_query_no_input => {
|
|
|
|
const call = try client.call(services.lambda.list_functions.Request{}, options);
|
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("account has functions: {b}", .{call.response.functions.?.len > 0});
|
|
|
|
},
|
|
|
|
.rest_json_1_work_with_lambda => {
|
|
|
|
const call = try client.call(services.lambda.list_functions.Request{}, options);
|
|
|
|
defer call.deinit();
|
|
|
|
std.log.info("list request id: {s}", .{call.response_metadata.request_id});
|
|
|
|
if (call.response.functions) |fns| {
|
|
|
|
if (fns.len > 0) {
|
|
|
|
const func = fns[0];
|
|
|
|
const arn = func.function_arn.?;
|
2021-09-01 14:13:42 +00:00
|
|
|
// This is a bit ugly. Maybe a helper function in the library would help?
|
2021-08-25 00:02:28 +00:00
|
|
|
var tags = try std.ArrayList(@typeInfo(try typeForField(services.lambda.tag_resource.Request, "tags")).Pointer.child).initCapacity(allocator, 1);
|
|
|
|
defer tags.deinit();
|
|
|
|
tags.appendAssumeCapacity(.{ .key = "Foo", .value = "Bar" });
|
|
|
|
const req = services.lambda.tag_resource.Request{ .resource = arn, .tags = tags.items };
|
|
|
|
const addtag = try aws.Request(services.lambda.tag_resource).call(req, options);
|
2021-09-06 22:48:10 +00:00
|
|
|
defer addtag.deinit();
|
2021-08-25 00:02:28 +00:00
|
|
|
// const addtag = try client.call(services.lambda.tag_resource.Request{ .resource = arn, .tags = &.{.{ .key = "Foo", .value = "Bar" }} }, options);
|
|
|
|
std.log.info("add tag request id: {s}", .{addtag.response_metadata.request_id});
|
2021-09-01 14:13:42 +00:00
|
|
|
var keys = [_][]const u8{"Foo"}; // Would love to have a way to express this without burning a var here
|
|
|
|
const deletetag = try aws.Request(services.lambda.untag_resource).call(.{ .tag_keys = keys[0..], .resource = arn }, options);
|
2021-09-06 22:48:10 +00:00
|
|
|
defer deletetag.deinit();
|
2021-08-25 00:02:28 +00:00
|
|
|
std.log.info("delete tag request id: {s}", .{deletetag.response_metadata.request_id});
|
|
|
|
} else {
|
|
|
|
std.log.err("no functions to work with", .{});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std.log.err("no functions to work with", .{});
|
|
|
|
}
|
2021-08-13 00:51:47 +00:00
|
|
|
},
|
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-08-25 00:02:28 +00:00
|
|
|
fn typeForField(comptime T: type, field_name: []const u8) !type {
|
|
|
|
const ti = @typeInfo(T);
|
|
|
|
switch (ti) {
|
|
|
|
.Struct => {
|
|
|
|
inline for (ti.Struct.fields) |field| {
|
|
|
|
if (std.mem.eql(u8, field.name, field_name))
|
|
|
|
return field.field_type;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => return error.TypeIsNotAStruct, // should not hit this
|
|
|
|
}
|
|
|
|
return error.FieldNotFound;
|
|
|
|
}
|
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});
|
|
|
|
}
|