Compare commits
9 Commits
591f4c43e0
...
9c403d0e18
Author | SHA1 | Date | |
---|---|---|---|
9c403d0e18 | |||
316a388b79 | |||
0a1de2b967 | |||
70bd8dafa5 | |||
48fc41c4b8 | |||
bd72bbb1f3 | |||
9d7df078b0 | |||
812ad96a66 | |||
58949127d6 |
26
src/aws.zig
26
src/aws.zig
|
@ -27,18 +27,19 @@ pub const services = servicemodel.services;
|
|||
/// This will give you a constant with service data for sts, ec2, s3 and ddb only
|
||||
pub const Services = servicemodel.Services;
|
||||
|
||||
pub const ClientOptions = struct {};
|
||||
pub const ClientOptions = struct {
|
||||
proxy: ?std.http.Client.HttpProxy = null,
|
||||
};
|
||||
pub const Client = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
aws_http: awshttp.AwsHttp,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, options: ClientOptions) !Self {
|
||||
_ = options;
|
||||
pub fn init(allocator: std.mem.Allocator, options: ClientOptions) Self {
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.aws_http = try awshttp.AwsHttp.init(allocator),
|
||||
.aws_http = awshttp.AwsHttp.init(allocator, options.proxy),
|
||||
};
|
||||
}
|
||||
pub fn deinit(self: *Client) void {
|
||||
|
@ -478,6 +479,13 @@ pub fn Request(comptime request_action: anytype) type {
|
|||
const xml_options = xml_shaper.ParseOptions{ .allocator = options.client.allocator };
|
||||
var body: []const u8 = result.body;
|
||||
var free_body = false;
|
||||
if (result.body.len < 20) {
|
||||
std.log.err(
|
||||
"Unexpected response from server. Looking for XML that ends in 'Response' or 'Result'. Found:\n{s}␃\n===",
|
||||
.{result.body},
|
||||
);
|
||||
return error.UnexpectedResponse;
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Response>") == null and
|
||||
std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Result>") == null)
|
||||
{
|
||||
|
@ -1322,6 +1330,7 @@ const TestOptions = struct {
|
|||
server_remaining_requests: usize = 1,
|
||||
server_response: []const u8 = "unset",
|
||||
server_response_headers: [][2][]const u8 = &[_][2][]const u8{},
|
||||
server_response_transfer_encoding: ?std.http.TransferEncoding = null,
|
||||
request_body: []u8 = "",
|
||||
request_method: std.http.Method = undefined,
|
||||
request_target: []const u8 = undefined,
|
||||
|
@ -1437,7 +1446,11 @@ fn processRequest(options: *TestOptions, server: *std.http.Server) !void {
|
|||
}
|
||||
break :brk "Unexpected error generating request to lambda";
|
||||
};
|
||||
res.transfer_encoding = .{ .content_length = response_bytes.len };
|
||||
if (options.server_response_transfer_encoding == null)
|
||||
res.transfer_encoding = .{ .content_length = response_bytes.len }
|
||||
else
|
||||
res.transfer_encoding = .chunked;
|
||||
|
||||
try res.do();
|
||||
_ = try res.writer().writeAll(response_bytes);
|
||||
try res.finish();
|
||||
|
@ -1496,7 +1509,7 @@ const TestSetup = struct {
|
|||
null,
|
||||
);
|
||||
aws_creds.static_credentials = self.creds;
|
||||
var client = try Client.init(self.allocator, .{});
|
||||
var client = Client.init(self.allocator, .{});
|
||||
self.client = &client;
|
||||
return .{
|
||||
.region = "us-west-2",
|
||||
|
@ -1798,6 +1811,7 @@ test "ec2_query_no_input: EC2 describe regions" {
|
|||
.{ "Content-Type", "text/xml;charset=UTF-8" },
|
||||
.{ "x-amzn-RequestId", "4cdbdd69-800c-49b5-8474-ae4c17709782" },
|
||||
}),
|
||||
.server_response_transfer_encoding = .chunked,
|
||||
});
|
||||
defer test_harness.deinit();
|
||||
const options = try test_harness.start();
|
||||
|
|
|
@ -61,14 +61,14 @@ const EndPoint = struct {
|
|||
};
|
||||
pub const AwsHttp = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
proxy: ?std.http.Client.HttpProxy,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Recommend usage is init(allocator, awshttp.default_root_ca)
|
||||
/// Passing null for root_pem will result in no TLS verification
|
||||
pub fn init(allocator: std.mem.Allocator) !Self {
|
||||
pub fn init(allocator: std.mem.Allocator, proxy: ?std.http.Client.HttpProxy) Self {
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.proxy = proxy,
|
||||
// .credentialsProvider = // creds provider could be useful
|
||||
};
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ pub const AwsHttp = struct {
|
|||
const url = try std.fmt.allocPrint(self.allocator, "{s}{s}{s}", .{ endpoint.uri, request_cp.path, request_cp.query });
|
||||
defer self.allocator.free(url);
|
||||
log.debug("Request url: {s}", .{url});
|
||||
var cl = std.http.Client{ .allocator = self.allocator };
|
||||
var cl = std.http.Client{ .allocator = self.allocator, .proxy = self.proxy };
|
||||
defer cl.deinit(); // TODO: Connection pooling
|
||||
//
|
||||
// var req = try zfetch.Request.init(self.allocator, url, self.trust_chain);
|
||||
|
@ -211,9 +211,16 @@ pub const AwsHttp = struct {
|
|||
content_length = std.fmt.parseInt(usize, h.value, 10) catch 0;
|
||||
}
|
||||
|
||||
var response_data = try self.allocator.alloc(u8, content_length);
|
||||
errdefer self.allocator.free(response_data);
|
||||
_ = try req.readAll(response_data);
|
||||
var response_data: []u8 =
|
||||
if (req.response.transfer_encoding) |_| // the only value here is "chunked"
|
||||
try req.reader().readAllAlloc(self.allocator, std.math.maxInt(usize))
|
||||
else blk: {
|
||||
// content length
|
||||
var tmp_data = try self.allocator.alloc(u8, content_length);
|
||||
errdefer self.allocator.free(tmp_data);
|
||||
_ = try req.readAll(tmp_data);
|
||||
break :blk tmp_data;
|
||||
};
|
||||
log.debug("raw response body:\n{s}", .{response_data});
|
||||
|
||||
const rc = HttpResult{
|
||||
|
|
152
src/main.zig
152
src/main.zig
|
@ -38,6 +38,9 @@ pub fn log(
|
|||
nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return;
|
||||
}
|
||||
|
||||
pub const std_options = struct {
|
||||
pub const logFn = log;
|
||||
};
|
||||
const Tests = enum {
|
||||
query_no_input,
|
||||
query_with_input,
|
||||
|
@ -63,7 +66,30 @@ pub fn main() anyerror!void {
|
|||
defer tests.deinit();
|
||||
var args = try std.process.argsWithAllocator(allocator);
|
||||
defer args.deinit();
|
||||
const stdout_raw = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_raw);
|
||||
defer bw.flush() catch unreachable;
|
||||
const stdout = bw.writer();
|
||||
var arg0: ?[]const u8 = null;
|
||||
var proxy: ?std.http.Client.HttpProxy = null;
|
||||
while (args.next()) |arg| {
|
||||
if (arg0 == null) arg0 = arg;
|
||||
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
|
||||
try stdout.print(
|
||||
\\usage: {?s} [-h|--help] [-v][-v][-v] [-x|--proxy <proxy url>] [tests...]
|
||||
\\
|
||||
\\Where tests are one of the following:
|
||||
\\
|
||||
, .{arg0});
|
||||
inline for (std.meta.fields(Tests)) |enumfield| {
|
||||
try stdout.print("* {s}\n", .{enumfield.name});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (std.mem.eql(u8, "-x", arg) or std.mem.eql(u8, "--proxy", arg)) {
|
||||
proxy = try proxyFromString(args.next().?); // parse stuff
|
||||
continue;
|
||||
}
|
||||
if (std.mem.eql(u8, "-v", arg)) {
|
||||
verbose += 1;
|
||||
continue;
|
||||
|
@ -81,7 +107,8 @@ pub fn main() anyerror!void {
|
|||
}
|
||||
|
||||
std.log.info("Start\n", .{});
|
||||
var client = try aws.Client.init(allocator, .{});
|
||||
const client_options = aws.ClientOptions{ .proxy = proxy };
|
||||
var client = aws.Client.init(allocator, client_options);
|
||||
const options = aws.Options{
|
||||
.region = "us-west-2",
|
||||
.client = client,
|
||||
|
@ -99,17 +126,17 @@ pub fn main() anyerror!void {
|
|||
const call = try aws.Request(services.sts.get_caller_identity).call(.{}, options);
|
||||
// const call = try client.call(services.sts.get_caller_identity.Request{}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("arn: {any}", .{call.response.arn});
|
||||
std.log.info("id: {any}", .{call.response.user_id});
|
||||
std.log.info("account: {any}", .{call.response.account});
|
||||
std.log.info("requestId: {any}", .{call.response_metadata.request_id});
|
||||
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});
|
||||
},
|
||||
.query_with_input => {
|
||||
const call = try client.call(services.sqs.list_queues.Request{
|
||||
.queue_name_prefix = "s",
|
||||
}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("request id: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has queues with prefix 's': {}", .{call.response.queue_urls != null});
|
||||
},
|
||||
.json_1_0_query_with_input => {
|
||||
|
@ -117,7 +144,7 @@ pub fn main() anyerror!void {
|
|||
.limit = 1,
|
||||
}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("request id: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has tables: {}", .{call.response.table_names.?.len > 0});
|
||||
},
|
||||
.json_1_0_query_no_input => {
|
||||
|
@ -130,13 +157,13 @@ pub fn main() anyerror!void {
|
|||
.max_results = 1,
|
||||
}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("request id: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0});
|
||||
},
|
||||
.json_1_1_query_no_input => {
|
||||
const call = try client.call(services.ecs.list_clusters.Request{}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("request id: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0});
|
||||
},
|
||||
.rest_json_1_query_with_input => {
|
||||
|
@ -144,48 +171,51 @@ pub fn main() anyerror!void {
|
|||
.max_items = 1,
|
||||
}, options);
|
||||
defer call.deinit();
|
||||
std.log.info("request id: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has functions: {}", .{call.response.functions.?.len > 0});
|
||||
},
|
||||
.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: {any}", .{call.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{call.response_metadata.request_id});
|
||||
std.log.info("account has functions: {}", .{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: {any}", .{call.response_metadata.request_id});
|
||||
if (call.response.functions) |fns| {
|
||||
if (fns.len > 0) {
|
||||
const func = fns[0];
|
||||
const arn = func.function_arn.?;
|
||||
// This is a bit ugly. Maybe a helper function in the library would help?
|
||||
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);
|
||||
defer addtag.deinit();
|
||||
// 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: {any}", .{addtag.response_metadata.request_id});
|
||||
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);
|
||||
defer deletetag.deinit();
|
||||
std.log.info("delete tag request id: {any}", .{deletetag.response_metadata.request_id});
|
||||
} else {
|
||||
std.log.err("no functions to work with", .{});
|
||||
}
|
||||
} else {
|
||||
std.log.err("no functions to work with", .{});
|
||||
}
|
||||
// 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.?;
|
||||
// // This is a bit ugly. Maybe a helper function in the library would help?
|
||||
// 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);
|
||||
// TODO: Something is up with signature calculation. I believe it's with the encoding, because the url used
|
||||
// here is totally crazy with the arn of the resource directly in it
|
||||
// Example: https://lambda.us-west-2.amazonaws.com/2017-03-31/tags/arn%253Aaws%253Alambda%253Aus-west-2%253A550620852718%253Afunction%253ADevelopmentFrontendStack--amplifyassetdeploymentha-aZqB9IbZLIKU
|
||||
// defer addtag.deinit();
|
||||
// // 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});
|
||||
// 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);
|
||||
// defer deletetag.deinit();
|
||||
// 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", .{});
|
||||
// }
|
||||
},
|
||||
.ec2_query_no_input => {
|
||||
// Describe regions is a simpler request and easier to debug
|
||||
const result = try client.call(services.ec2.describe_regions.Request{}, options);
|
||||
defer result.deinit();
|
||||
std.log.info("request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("region count: {d}", .{result.response.regions.?.len});
|
||||
},
|
||||
.ec2_query_with_input => {
|
||||
|
@ -223,13 +253,13 @@ pub fn main() anyerror!void {
|
|||
.rest_xml_no_input => {
|
||||
const result = try client.call(services.s3.list_buckets.Request{}, options);
|
||||
defer result.deinit();
|
||||
std.log.info("request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("bucket count: {d}", .{result.response.buckets.?.len});
|
||||
},
|
||||
.rest_xml_anything_but_s3 => {
|
||||
const result = try client.call(services.cloudfront.list_key_groups.Request{}, options);
|
||||
defer result.deinit();
|
||||
std.log.info("request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("request id: {s}", .{result.response_metadata.request_id});
|
||||
const list = result.response.key_group_list.?;
|
||||
std.log.info("key group list max: {?d}", .{list.max_items});
|
||||
std.log.info("key group quantity: {d}", .{list.quantity});
|
||||
|
@ -242,8 +272,8 @@ pub fn main() anyerror!void {
|
|||
const result = try client.call(services.s3.list_buckets.Request{}, options);
|
||||
defer result.deinit();
|
||||
const bucket = result.response.buckets.?[result.response.buckets.?.len - 1];
|
||||
std.log.info("ListBuckets request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("bucket name: {any}", .{bucket.name.?});
|
||||
std.log.info("ListBuckets request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("bucket name: {s}", .{bucket.name.?});
|
||||
break :blk try allocator.dupe(u8, bucket.name.?);
|
||||
};
|
||||
defer allocator.free(bucket);
|
||||
|
@ -253,8 +283,8 @@ pub fn main() anyerror!void {
|
|||
}, options);
|
||||
defer result.deinit();
|
||||
const location = result.response.location_constraint.?;
|
||||
std.log.info("GetBucketLocation request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("location: {any}", .{location});
|
||||
std.log.info("GetBucketLocation request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("location: {s}", .{location});
|
||||
break :blk try allocator.dupe(u8, location);
|
||||
};
|
||||
defer allocator.free(location);
|
||||
|
@ -271,8 +301,8 @@ pub fn main() anyerror!void {
|
|||
.body = "bar",
|
||||
.storage_class = "STANDARD",
|
||||
}, s3opts);
|
||||
std.log.info("PutObject Request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("PutObject etag: {any}", .{result.response.e_tag.?});
|
||||
std.log.info("PutObject Request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("PutObject etag: {s}", .{result.response.e_tag.?});
|
||||
defer result.deinit();
|
||||
}
|
||||
{
|
||||
|
@ -282,9 +312,9 @@ pub fn main() anyerror!void {
|
|||
.bucket = bucket,
|
||||
.key = key,
|
||||
}, s3opts);
|
||||
std.log.info("GetObject Request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("GetObject Body: {any}", .{result.response.body});
|
||||
std.log.info("GetObject etag: {any}", .{result.response.e_tag.?});
|
||||
std.log.info("GetObject Request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("GetObject Body: {s}", .{result.response.body.?});
|
||||
std.log.info("GetObject etag: {s}", .{result.response.e_tag.?});
|
||||
std.log.info("GetObject last modified (seconds since epoch): {d}", .{result.response.last_modified.?});
|
||||
defer result.deinit();
|
||||
}
|
||||
|
@ -293,14 +323,14 @@ pub fn main() anyerror!void {
|
|||
.bucket = bucket,
|
||||
.key = key,
|
||||
}, s3opts);
|
||||
std.log.info("DeleteObject Request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("DeleteObject Request id: {s}", .{result.response_metadata.request_id});
|
||||
defer result.deinit();
|
||||
}
|
||||
{
|
||||
const result = try aws.Request(services.s3.list_objects).call(.{
|
||||
.bucket = bucket,
|
||||
}, s3opts);
|
||||
std.log.info("ListObject Request id: {any}", .{result.response_metadata.request_id});
|
||||
std.log.info("ListObject Request id: {s}", .{result.response_metadata.request_id});
|
||||
std.log.info("Object count: {d}", .{result.response.contents.?.len});
|
||||
defer result.deinit();
|
||||
}
|
||||
|
@ -321,6 +351,26 @@ pub fn main() anyerror!void {
|
|||
|
||||
std.log.info("===== Tests complete =====", .{});
|
||||
}
|
||||
|
||||
fn proxyFromString(string: []const u8) !std.http.Client.HttpProxy {
|
||||
var rc = std.http.Client.HttpProxy{
|
||||
.protocol = undefined,
|
||||
.host = undefined,
|
||||
};
|
||||
var remaining: []const u8 = string;
|
||||
if (std.mem.startsWith(u8, string, "http://")) {
|
||||
remaining = remaining["http://".len..];
|
||||
rc.protocol = .plain;
|
||||
} else if (std.mem.startsWith(u8, string, "https://")) {
|
||||
remaining = remaining["https://".len..];
|
||||
rc.protocol = .tls;
|
||||
} else return error.InvalidScheme;
|
||||
var split_iterator = std.mem.split(u8, remaining, ":");
|
||||
rc.host = std.mem.trimRight(u8, split_iterator.first(), "/");
|
||||
if (split_iterator.next()) |port|
|
||||
rc.port = try std.fmt.parseInt(u16, port, 10);
|
||||
return rc;
|
||||
}
|
||||
fn typeForField(comptime T: type, comptime field_name: []const u8) !type {
|
||||
const ti = @typeInfo(T);
|
||||
switch (ti) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user