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

Adding IAM Action name trait to override using the API operation name #1665

Merged
merged 6 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions docs/source-2.0/aws/aws-iam.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,48 @@ deviates from the :ref:`shape name of the shape ID <shape-id>` of the resource.
}
}

.. smithy-trait:: aws.iam#actionName
.. _aws.iam#actionName-trait:

----------------------------
``aws.iam#actionName`` trait
----------------------------

Summary
Provides a custom IAM action name.
Trait selector
``operation``
Value type
``string``

Operations not annotated with the ``actionName`` trait, default to the
:ref:`shape name of the shape ID <shape-id>` of the targeted operation.

The following example defines two operations:

* ``OperationA`` is not annotated with the ``actionName`` trait, and
resolves the action name of ``OperationA``.
* ``OperationB`` has the ``actionName`` trait, so has the action
name ``OverridingActionName``.

.. code-block:: smithy

$version: "2"

namespace smithy.example

use aws.iam#actionName

service MyService {
version: "2020-07-02"
operations: [OperationA, OperationB]
}

operation OperationA {}

@actionName("OverridingActionName")
operation OperationB {}


.. _deriving-condition-keys:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.iam.traits;

import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.StringTrait;

public final class ActionNameTrait extends StringTrait {

public static final ShapeId ID = ShapeId.from("aws.iam#actionName");

private ActionNameTrait(String action) {
super(ID, action, SourceLocation.NONE);
}

private ActionNameTrait(String action, FromSourceLocation sourceLocation) {
super(ID, action, sourceLocation);
}

public static final class Provider extends StringTrait.Provider<ActionNameTrait> {
public Provider() {
super(ID, ActionNameTrait::new);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ software.amazon.smithy.aws.iam.traits.DisableConditionKeyInferenceTrait$Provider
software.amazon.smithy.aws.iam.traits.RequiredActionsTrait$Provider
software.amazon.smithy.aws.iam.traits.SupportedPrincipalTypesTrait$Provider
software.amazon.smithy.aws.iam.traits.IamResourceTrait$Provider
software.amazon.smithy.aws.iam.traits.ActionNameTrait$Provider
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@
"smithy.api#private": {},
"smithy.api#documentation": "An IAM policy principal type."
}
},
"aws.iam#actionName": {
"type": "string",
"member": {
"target": "aws.iam#IamIdentifier"
},
"traits": {
"smithy.api#trait": {
"selector": "operation"
},
"smithy.api#documentation": "Provides a custom IAM action name. By default, the action name is the same as the operation name."
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.iam.traits;

import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class ActionNameTraitTest {
@Test
public void loadsFromModel() {
Model result = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("actionname-override.smithy"))
.assemble()
.unwrap();

Shape shape = result.expectShape(ShapeId.from("smithy.example#Echo"));
ActionNameTrait trait = shape.expectTrait(ActionNameTrait.class);
assertThat(trait.getValue(), equalTo("overridingActionName"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$version: "2.0"
namespace smithy.example

@aws.iam#actionName("overridingActionName")
operation Echo {}

operation GetResource2 {
input: GetResource2Input
}

structure GetResource2Input {
id1: String,

@required
id2: String
}