Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(aft): Use public models #3727

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 31 additions & 48 deletions packages/aft/lib/src/commands/generate/generate_sdk_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:code_builder/code_builder.dart';
import 'package:collection/collection.dart';
import 'package:git/git.dart';
import 'package:path/path.dart' as p;
import 'package:pubspec_parse/pubspec_parse.dart';
Expand All @@ -26,7 +25,7 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
..addOption(
'models',
abbr: 'm',
help: 'The path to the AWS SDK models',
help: 'The path to the AWS JS V3 repo',
)
..addOption(
'output',
Expand Down Expand Up @@ -75,7 +74,9 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
await runGit(
[
'clone',
'https://github.com/aws/aws-models.git',
// https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
'--filter=tree:0',
'https://github.com/aws/aws-sdk-js-v3.git',
cloneDir.path,
],
echoOutput: verbose,
Expand All @@ -102,30 +103,6 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
return worktreeDir;
}

/// Organizes model files from [baseDir] into a new temporary directory.
///
/// Returns the new directory.
Future<Directory> _organizeModels(Directory baseDir) async {
final modelsDir = await Directory.systemTemp.createTemp('models');
logger.debug('Organizing models in ${modelsDir.path}');
final services = baseDir.list(followLinks: false).whereType<Directory>();
await for (final serviceDir in services) {
final serviceName = p.basename(serviceDir.path);
final artifacts = await serviceDir.list().whereType<Directory>().toList();
final smithyDir = artifacts.firstWhereOrNull(
(dir) => p.basename(dir.path) == 'smithy',
);
if (smithyDir == null) {
continue;
}
final smithyModel = File(p.join(smithyDir.path, 'model.json'));
final copyToPath = p.join(modelsDir.path, '$serviceName.json');
logger.verbose('Copying $serviceName.json to $copyToPath');
await smithyModel.copy(copyToPath);
}
return modelsDir;
}

Future<Directory> _modelsDirForRef(String ref) async {
if (_modelsCache[ref] case final cachedDir?) {
return cachedDir;
Expand All @@ -137,19 +114,20 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
exitError('Model directory ($modelsDir) does not exist');
}
// Checkout models if modelsPath is a git repository.
if (File(p.join(modelsPath, '.git')).existsSync()) {
if (Directory(p.join(modelsPath, '.git')).existsSync()) {
modelsDir = await _checkoutModelsRef(modelsDir, ref);
} else {
logger.warn(
'Unable to check out $ref since $modelsDir is not a git repo',
);
}
modelsDir = await _organizeModels(modelsDir);
} else {
modelsDir = await _downloadModels();
modelsDir = await _checkoutModelsRef(modelsDir, ref);
modelsDir = await _organizeModels(modelsDir);
}
modelsDir = Directory.fromUri(
modelsDir.uri.resolve('codegen/sdk-codegen/aws-models'),
);
return _modelsCache[ref] = modelsDir;
}

Expand Down Expand Up @@ -180,13 +158,13 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
final modelsDir = await _modelsDirForRef(config.ref);
final models = modelsDir.list().whereType<File>();
await for (final model in models) {
final serviceName = p.basenameWithoutExtension(model.path);
if (!config.apis.keys.contains(serviceName)) {
continue;
}
final astJson = await model.readAsString();
final ast = parseAstJson(astJson);

final serviceNamespace =
ast.shapes.values.whereType<ServiceShape>().single.shapeId.namespace;
if (!config.apis.containsKey(serviceNamespace)) {
continue;
}
smithyModel.shapes!.addAll(ast.shapes);
}

Expand All @@ -201,21 +179,26 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
}

final includeShapes = <ShapeId>[];
for (final shapeIdEntries in config.apis.entries) {
final shapeIds = shapeIdEntries.value;
if (shapeIds != null && shapeIds.isNotEmpty) {
includeShapes.addAll(shapeIds);
continue;
}

final apiName = shapeIdEntries.key;
for (final MapEntry(key: namespace, value: operations)
in config.apis.entries) {
final serviceShape = smithyModel.shapes!.values
.whereType<ServiceShape>()
.singleWhere(
(service) =>
service.expectTrait<ServiceTrait>().endpointPrefix == apiName,
.singleWhere((service) => service.shapeId.namespace == namespace);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool function, didn't know this existed.

final allOperations = serviceShape.operations.map((op) => op.target);
final includeOperations = switch (operations) {
List<String> _ when operations.isNotEmpty => operations.map(
(op) => ShapeId(namespace: namespace, shape: op),
),
_ => allOperations,
};
for (final operation in includeOperations) {
if (!allOperations.contains(operation)) {
throw Exception(
'Invalid operation specified for $namespace: $operation',
);
includeShapes.addAll(serviceShape.operations.map((op) => op.target));
}
}
includeShapes.addAll(includeOperations);
Comment on lines +194 to +201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could flatten this logic a little bit.

final isMissingOp = includeOperations.every((e) => !allOperations.contains(operation));
if (isMissingOp) {
   throw  Exception(
     'Invalid operation specified for $namespace: $operation',
    );
}
includeShapes.addAll(includeOperations);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, started with something like that. This turned out to be cleaner - the problem is getting the element which didn't pass the condition, i.e. operation in your snippet.

}

// Generate SDK for combined operations
Expand Down Expand Up @@ -326,7 +309,7 @@ class GenerateSdkCommand extends AmplifyCommand with GlobOptions {
),
]);

final modelsDir = await _modelsDirForRef('master');
final modelsDir = await _modelsDirForRef('main');
final models = modelsDir.list().whereType<File>();
final services = <Field>[];
await for (final model in models) {
Expand Down
6 changes: 3 additions & 3 deletions packages/aft/lib/src/config/raw_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class SdkConfig
AWSSerializable<Map<String, Object?>>,
AWSDebuggable {
const SdkConfig({
this.ref = 'master',
this.ref = 'main',
required this.apis,
this.plugins = const [],
});
Expand All @@ -412,9 +412,9 @@ class SdkConfig

/// The `aws-models` ref to pull.
///
/// Defaults to `master`.
/// Defaults to `main`.
final String ref;
final Map<String, List<ShapeId>?> apis;
final Map<String, List<String>?> apis;
final List<String> plugins;

@override
Expand Down
8 changes: 3 additions & 5 deletions packages/aft/lib/src/config/raw_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 6 additions & 14 deletions packages/analytics/amplify_analytics_pinpoint_dart/sdk.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
apis:
pinpoint:
- com.amazonaws.pinpoint#GetEndpoint
- com.amazonaws.pinpoint#GetInAppMessages
- com.amazonaws.pinpoint#PutEvents
- com.amazonaws.pinpoint#UpdateEndpoint
- com.amazonaws.pinpoint#UpdateEndpointsBatch
- com.amazonaws.pinpoint#GetUserProfile
- com.amazonaws.pinpoint#UpdateUserProfile
- com.amazonaws.pinpoint#DeleteUserProfile
- com.amazonaws.pinpoint#GetUserProfilesBatch
- com.amazonaws.pinpoint#UpdateUserProfilesBatch
- com.amazonaws.pinpoint#PutUserEvents
# patches:
# - tool/patch/user-models.patch
com.amazonaws.pinpoint:
- GetEndpoint
- GetInAppMessages
- PutEvents
- UpdateEndpoint
- UpdateEndpointsBatch
54 changes: 27 additions & 27 deletions packages/auth/amplify_auth_cognito_dart/sdk.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
apis:
cognito-idp:
- com.amazonaws.cognitoidentityprovider#AssociateSoftwareToken
- com.amazonaws.cognitoidentityprovider#ChangePassword
- com.amazonaws.cognitoidentityprovider#ConfirmDevice
- com.amazonaws.cognitoidentityprovider#ConfirmForgotPassword
- com.amazonaws.cognitoidentityprovider#ConfirmSignUp
- com.amazonaws.cognitoidentityprovider#DeleteUser
- com.amazonaws.cognitoidentityprovider#ForgetDevice
- com.amazonaws.cognitoidentityprovider#ForgotPassword
- com.amazonaws.cognitoidentityprovider#GetDevice
- com.amazonaws.cognitoidentityprovider#GetUser
- com.amazonaws.cognitoidentityprovider#GetUserAttributeVerificationCode
- com.amazonaws.cognitoidentityprovider#GlobalSignOut
- com.amazonaws.cognitoidentityprovider#InitiateAuth
- com.amazonaws.cognitoidentityprovider#ListDevices
- com.amazonaws.cognitoidentityprovider#ResendConfirmationCode
- com.amazonaws.cognitoidentityprovider#RespondToAuthChallenge
- com.amazonaws.cognitoidentityprovider#RevokeToken
- com.amazonaws.cognitoidentityprovider#SetUserMFAPreference
- com.amazonaws.cognitoidentityprovider#SignUp
- com.amazonaws.cognitoidentityprovider#UpdateDeviceStatus
- com.amazonaws.cognitoidentityprovider#UpdateUserAttributes
- com.amazonaws.cognitoidentityprovider#VerifySoftwareToken
- com.amazonaws.cognitoidentityprovider#VerifyUserAttribute
cognito-identity:
- com.amazonaws.cognitoidentity#GetCredentialsForIdentity
- com.amazonaws.cognitoidentity#GetId
com.amazonaws.cognitoidentityprovider:
- AssociateSoftwareToken
- ChangePassword
- ConfirmDevice
- ConfirmForgotPassword
- ConfirmSignUp
- DeleteUser
- ForgetDevice
- ForgotPassword
- GetDevice
- GetUser
- GetUserAttributeVerificationCode
- GlobalSignOut
- InitiateAuth
- ListDevices
- ResendConfirmationCode
- RespondToAuthChallenge
- RevokeToken
- SetUserMFAPreference
- SignUp
- UpdateDeviceStatus
- UpdateUserAttributes
- VerifySoftwareToken
- VerifyUserAttribute
com.amazonaws.cognitoidentity:
- GetCredentialsForIdentity
- GetId

plugins:
- tool/generate_sdk_exceptions.dart
18 changes: 9 additions & 9 deletions packages/aws_sdk/smoke_test/sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# of the six AWS protocols
apis:
# API Gateway V1 (restJson1)
apigateway:
com.amazonaws.apigateway:

# Config (awsJson1_1)
config:
com.amazonaws.configservice:

# EC2 (ec2Query)
ec2:
com.amazonaws.ec2:

# DynamoDB (awsJson1_0)
dynamodb:
streams.dynamodb:
com.amazonaws.dynamodb:
com.amazonaws.dynamodbstreams:

# CloudFormation, IAM, STS (awsQuery)
cloudformation:
iam:
sts:
com.amazonaws.cloudformation:
com.amazonaws.iam:
com.amazonaws.sts:

# S3 (restXml)
s3:
com.amazonaws.s3:
32 changes: 16 additions & 16 deletions packages/storage/amplify_storage_s3_dart/sdk.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
apis:
s3:
- com.amazonaws.s3#CopyObject
- com.amazonaws.s3#DeleteObject
- com.amazonaws.s3#DeleteObjects
- com.amazonaws.s3#GetObject
- com.amazonaws.s3#HeadObject
- com.amazonaws.s3#ListObjectsV2
- com.amazonaws.s3#PutObject
- com.amazonaws.s3#AbortMultipartUpload
- com.amazonaws.s3#CompleteMultipartUpload
- com.amazonaws.s3#CreateMultipartUpload
- com.amazonaws.s3#ListParts
- com.amazonaws.s3#UploadPart
- com.amazonaws.s3#ListMultipartUploads
- com.amazonaws.s3#UploadPartCopy
- com.amazonaws.s3#SelectObjectContent
com.amazonaws.s3:
- CopyObject
- DeleteObject
- DeleteObjects
- GetObject
- HeadObject
- ListObjectsV2
- PutObject
- AbortMultipartUpload
- CompleteMultipartUpload
- CreateMultipartUpload
- ListParts
- UploadPart
- ListMultipartUploads
- UploadPartCopy
- SelectObjectContent
4 changes: 2 additions & 2 deletions packages/test/amplify_integration_test/sdk.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
apis:
cognito-idp:
- com.amazonaws.cognitoidentityprovider#AdminListUserAuthEvents
com.amazonaws.cognitoidentityprovider:
- AdminListUserAuthEvents