2021-04-27 18:24:01 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2021-05-14 19:37:00 +00:00
|
|
|
const awshttp = @import("awshttp.zig");
|
|
|
|
const json = @import("json.zig");
|
2021-06-14 23:12:55 +00:00
|
|
|
const url = @import("url.zig");
|
|
|
|
const case = @import("case.zig");
|
2021-05-30 02:33:08 +00:00
|
|
|
const servicemodel = @import("servicemodel.zig");
|
2021-04-27 18:24:01 +00:00
|
|
|
|
|
|
|
const log = std.log.scoped(.aws);
|
2021-05-14 19:37:00 +00:00
|
|
|
|
2021-06-09 23:25:49 +00:00
|
|
|
pub const Options = struct {
|
|
|
|
region: []const u8 = "aws-global",
|
|
|
|
dualstack: bool = false,
|
|
|
|
};
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2021-06-10 21:35:22 +00:00
|
|
|
/// Using this constant may blow up build times. Recommed using Services()
|
|
|
|
/// function directly, e.g. const services = Services(.{.sts, .ec2, .s3, .ddb}){};
|
2021-05-30 02:33:08 +00:00
|
|
|
pub const services = servicemodel.services;
|
2021-04-27 18:24:01 +00:00
|
|
|
|
2021-06-10 21:35:22 +00:00
|
|
|
/// Get a service model by importing specific services only. As an example:
|
|
|
|
/// const services = Services(.{.sts, .ec2, .s3, .ddb}){};
|
|
|
|
///
|
|
|
|
/// This will give you a constant with service data for sts, ec2, s3 and ddb only
|
|
|
|
pub const Services = servicemodel.Services;
|
|
|
|
|
2021-04-27 18:24:01 +00:00
|
|
|
pub const Aws = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
2021-05-14 19:37:00 +00:00
|
|
|
aws_http: awshttp.AwsHttp,
|
2021-04-27 18:24:01 +00:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Self {
|
|
|
|
return .{
|
|
|
|
.allocator = allocator,
|
2021-05-14 19:37:00 +00:00
|
|
|
.aws_http = awshttp.AwsHttp.init(allocator),
|
2021-04-27 18:24:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
pub fn deinit(self: *Aws) void {
|
2021-05-14 19:37:00 +00:00
|
|
|
self.aws_http.deinit();
|
2021-04-27 18:24:01 +00:00
|
|
|
}
|
2021-05-14 19:37:00 +00:00
|
|
|
|
2021-05-13 22:53:53 +00:00
|
|
|
pub fn call(self: Self, comptime request: anytype, options: Options) !FullResponse(request) {
|
2021-06-09 23:22:44 +00:00
|
|
|
// every codegenned request object includes a metaInfo function to get
|
|
|
|
// pointers to service and action
|
|
|
|
const meta_info = request.metaInfo();
|
|
|
|
const service = meta_info.service;
|
|
|
|
const action = meta_info.action;
|
2021-04-27 18:24:01 +00:00
|
|
|
const R = Response(request);
|
|
|
|
|
2021-06-12 20:39:26 +00:00
|
|
|
log.debug("call: prefix {s}, sigv4 {s}, version {s}, action {s}", .{
|
|
|
|
service.endpoint_prefix,
|
|
|
|
service.sigv4_name,
|
|
|
|
service.version,
|
|
|
|
action.action_name,
|
|
|
|
});
|
|
|
|
log.debug("proto: {s}", .{service.aws_protocol});
|
|
|
|
|
2021-06-18 19:44:43 +00:00
|
|
|
// It seems as though there are 3 major branches of the 6 protocols.
|
|
|
|
// 1. query/ec2_query, which are identical until you get to complex
|
|
|
|
// structures. TBD if the shortcut we're taking for query to make
|
|
|
|
// it return json will work on EC2, but my guess is yes.
|
|
|
|
// 2. *json*: These three appear identical for input (possible difference
|
|
|
|
// for empty body serialization), but differ in error handling.
|
|
|
|
// We're not doing a lot of error handling here, though.
|
|
|
|
// 3. rest_xml: This is a one-off for S3, never used since
|
2021-06-12 20:39:26 +00:00
|
|
|
switch (service.aws_protocol) {
|
2021-06-18 19:44:43 +00:00
|
|
|
.query, .ec2_query => return self.callQuery(request, service, action, options),
|
|
|
|
.rest_json_1, .json_1_0, .json_1_1 => @compileError("REST Json, Json 1.0/1.1 protocol not yet supported"),
|
2021-06-12 20:39:26 +00:00
|
|
|
.rest_xml => @compileError("REST XML protocol not yet supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call using query protocol. This is documented as an XML protocol, but
|
2021-06-18 19:44:43 +00:00
|
|
|
// throwing a JSON accept header seems to work. EC2Query is very simliar to
|
|
|
|
// Query, so we'll handle both here. Realistically we probably don't effectively
|
|
|
|
// handle lists and maps properly anyway yet, so we'll go for it and see
|
|
|
|
// where it breaks. PRs and/or failing test cases appreciated.
|
2021-06-12 20:39:26 +00:00
|
|
|
fn callQuery(self: Self, comptime request: anytype, service: anytype, action: anytype, options: Options) !FullResponse(request) {
|
2021-06-14 23:12:55 +00:00
|
|
|
var buffer = std.ArrayList(u8).init(self.allocator);
|
|
|
|
defer buffer.deinit();
|
|
|
|
const writer = buffer.writer();
|
2021-06-18 19:44:43 +00:00
|
|
|
// TODO: transformation function should be refactored for operation
|
|
|
|
// with a Writer passed in so we don't have to allocate
|
2021-06-14 23:12:55 +00:00
|
|
|
const transformer = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
|
|
|
|
const This = @This();
|
|
|
|
|
|
|
|
pub fn transform(this: This, name: []const u8) ![]const u8 {
|
|
|
|
return try case.snakeToPascal(this.allocator, name);
|
|
|
|
}
|
|
|
|
pub fn transform_deinit(this: This, name: []const u8) void {
|
|
|
|
this.allocator.free(name);
|
|
|
|
}
|
|
|
|
}{ .allocator = self.allocator };
|
|
|
|
try url.encode(request, writer, .{ .field_name_transformer = transformer });
|
|
|
|
const continuation = if (buffer.items.len > 0) "&" else "";
|
|
|
|
|
|
|
|
const body = try std.fmt.allocPrint(self.allocator, "Action={s}&Version={s}{s}{s}\n", .{ action.action_name, service.version, continuation, buffer.items });
|
|
|
|
defer self.allocator.free(body);
|
2021-06-12 20:39:26 +00:00
|
|
|
const FullR = FullResponse(request);
|
2021-06-09 23:25:49 +00:00
|
|
|
const response = try self.aws_http.callApi(
|
|
|
|
service.endpoint_prefix,
|
2021-06-14 23:12:55 +00:00
|
|
|
body,
|
2021-06-09 23:25:49 +00:00
|
|
|
.{
|
|
|
|
.region = options.region,
|
|
|
|
.dualstack = options.dualstack,
|
|
|
|
.sigv4_service_name = service.sigv4_name,
|
|
|
|
},
|
|
|
|
);
|
2021-06-18 19:44:43 +00:00
|
|
|
// TODO: Can response handling be reused?
|
2021-04-27 18:24:01 +00:00
|
|
|
defer response.deinit();
|
2021-06-12 20:38:50 +00:00
|
|
|
if (response.response_code != 200) {
|
|
|
|
log.err("call failed! return status: {d}", .{response.response_code});
|
2021-06-14 23:12:55 +00:00
|
|
|
log.err("Request:\n |{s}\nResponse:\n |{s}", .{ body, response.body });
|
2021-06-12 20:38:50 +00:00
|
|
|
return error.HttpFailure;
|
|
|
|
}
|
2021-04-27 18:24:01 +00:00
|
|
|
// TODO: Check status code for badness
|
2021-05-13 22:53:53 +00:00
|
|
|
var stream = json.TokenStream.init(response.body);
|
|
|
|
|
|
|
|
const parser_options = json.ParseOptions{
|
2021-04-27 18:24:01 +00:00
|
|
|
.allocator = self.allocator,
|
2021-05-13 22:53:53 +00:00
|
|
|
.allow_camel_case_conversion = true, // new option
|
|
|
|
.allow_snake_case_conversion = true, // new option
|
|
|
|
.allow_unknown_fields = true, // new option. Cannot yet handle non-struct fields though
|
2021-04-27 18:24:01 +00:00
|
|
|
};
|
2021-05-13 22:53:53 +00:00
|
|
|
const SResponse = ServerResponse(request);
|
|
|
|
const parsed_response = try json.parse(SResponse, &stream, parser_options);
|
|
|
|
|
|
|
|
// Grab the first (and only) object from the server. Server shape expected to be:
|
|
|
|
// { ActionResponse: {ActionResult: {...}, ResponseMetadata: {...} } }
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
// Next line of code pulls this portion
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// And the response property below will pull whatever is the ActionResult object
|
|
|
|
// We can grab index [0] as structs are guaranteed by zig to be returned in the order
|
|
|
|
// declared, and we're declaring in that order in ServerResponse().
|
|
|
|
const real_response = @field(parsed_response, @typeInfo(SResponse).Struct.fields[0].name);
|
|
|
|
return FullR{
|
|
|
|
.response = @field(real_response, @typeInfo(@TypeOf(real_response)).Struct.fields[0].name),
|
|
|
|
.response_metadata = .{
|
|
|
|
.request_id = real_response.ResponseMetadata.RequestId,
|
|
|
|
},
|
|
|
|
.parser_options = parser_options,
|
|
|
|
.raw_parsed = parsed_response,
|
2021-04-27 18:24:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-13 22:53:53 +00:00
|
|
|
fn ServerResponse(comptime request: anytype) type {
|
|
|
|
const T = Response(request);
|
2021-06-09 23:22:44 +00:00
|
|
|
const action = request.metaInfo().action;
|
2021-05-14 19:37:00 +00:00
|
|
|
// NOTE: The non-standard capitalization here is used as a performance
|
|
|
|
// enhancement and to reduce allocations in json.zig. These fields are
|
|
|
|
// not (nor are they ever intended to be) exposed outside this codebase
|
2021-05-13 22:53:53 +00:00
|
|
|
const ResponseMetadata = struct {
|
|
|
|
RequestId: []u8,
|
|
|
|
};
|
|
|
|
const Result = @Type(.{
|
|
|
|
.Struct = .{
|
|
|
|
.layout = .Auto,
|
|
|
|
.fields = &[_]std.builtin.TypeInfo.StructField{
|
|
|
|
.{
|
|
|
|
.name = action.action_name ++ "Result",
|
|
|
|
.field_type = T,
|
|
|
|
.default_value = null,
|
|
|
|
.is_comptime = false,
|
|
|
|
.alignment = 0,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.name = "ResponseMetadata",
|
|
|
|
.field_type = ResponseMetadata,
|
|
|
|
.default_value = null,
|
|
|
|
.is_comptime = false,
|
|
|
|
.alignment = 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
.decls = &[_]std.builtin.TypeInfo.Declaration{},
|
|
|
|
.is_tuple = false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return @Type(.{
|
|
|
|
.Struct = .{
|
|
|
|
.layout = .Auto,
|
|
|
|
.fields = &[_]std.builtin.TypeInfo.StructField{
|
|
|
|
.{
|
|
|
|
.name = action.action_name ++ "Response",
|
|
|
|
.field_type = Result,
|
|
|
|
.default_value = null,
|
|
|
|
.is_comptime = false,
|
|
|
|
.alignment = 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
.decls = &[_]std.builtin.TypeInfo.Declaration{},
|
|
|
|
.is_tuple = false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fn FullResponse(comptime request: anytype) type {
|
|
|
|
return struct {
|
|
|
|
response: Response(request),
|
|
|
|
response_metadata: struct {
|
|
|
|
request_id: []u8,
|
|
|
|
},
|
|
|
|
parser_options: json.ParseOptions,
|
|
|
|
raw_parsed: ServerResponse(request),
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
pub fn deinit(self: Self) void {
|
|
|
|
json.parseFree(ServerResponse(request), self.raw_parsed, self.parser_options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn Response(comptime request: anytype) type {
|
2021-06-09 23:22:44 +00:00
|
|
|
return request.metaInfo().action.Response;
|
2021-05-13 22:53:53 +00:00
|
|
|
}
|