Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ford-at-aws committed Feb 14, 2024
1 parent 397fd48 commit 1449799
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
5 changes: 0 additions & 5 deletions ruby/example_code/cloudtrail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ To learn more about the contributing process, see [CONTRIBUTING.md](../../../CON
- [SDK for Ruby CloudTrail reference](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Cloudtrail.html)

<!--custom.resources.start-->
* [More Ruby CloudTrail code examples](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/cloud-trail-examples.html)
* [SDK for Ruby Developer Guide](https://aws.amazon.com/developer/language/ruby/)
* [SDK for Ruby CloudTrail Module](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CloudTrail.html)
* [CloudTrail User Guide](https://docs.aws.amazon.com/cloudtrail/)
* [CodeTrail API Reference](https://docs.aws.amazon.com/awscloudtrail/latest/APIReference/Welcome.html)
<!--custom.resources.end-->

---
Expand Down
46 changes: 33 additions & 13 deletions ruby/example_code/iam/get_account_password_policy.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX - License - Identifier: Apache - 2.0
require "aws-sdk-iam"
require "logger"

# snippet-start:[ruby.example_code.iam.GetAccountPasswordPolicy]
# Prints the password policy for the account.
def print_account_password_policy
policy = @iam_resource.account_password_policy
policy.load
puts("The account password policy is:")
puts(policy.data.to_h)
rescue Aws::Errors::ServiceError => e
if e.code == "NoSuchEntity"
puts("The account does not have a password policy.")
else
puts("Couldn't print the account password policy. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
# Class to manage IAM account password policies
class PasswordPolicyManager
attr_accessor :iam_client, :logger

def initialize(iam_client)
@iam_client = iam_client
@logger = Logger.new($stdout)
@logger.progname = "IAMPolicyManager"
end

# Retrieves and logs the account password policy
def print_account_password_policy
begin
response = @iam_client.get_account_password_policy
@logger.info("The account password policy is: #{response.password_policy.to_h}")
rescue Aws::IAM::Errors::NoSuchEntity
@logger.info("The account does not have a password policy.")
rescue Aws::Errors::ServiceError => e
@logger.error("Couldn't print the account password policy. Error: #{e.code} - #{e.message}")
raise
end
end
end
# snippet-end:[ruby.example_code.iam.GetAccountPasswordPolicy]

# Example usage:
if $PROGRAM_NAME == __FILE__
iam_client = Aws::IAM::Client.new
iam_policy_manager = PasswordPolicyManager.new(iam_client)
iam_policy_manager.print_account_password_policy
end
22 changes: 22 additions & 0 deletions ruby/example_code/iam/spec/get_account_password_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rspec"
require_relative "../get_account_password_policy"
require "aws-sdk-iam"

describe PasswordPolicyManager do
let(:iam_client) { Aws::IAM::Client.new }
let(:iam_policy_manager) { PasswordPolicyManager.new(iam_client) }

describe "#print_account_password_policy" do
context "when the account has a password policy" do
it "logs the password policy details" do
expect { iam_policy_manager.print_account_password_policy }.not_to raise_error
end
end

context "when the account does not have a password policy" do
it "logs a specific message" do
expect { iam_policy_manager.print_account_password_policy }.not_to raise_error
end
end
end
end

0 comments on commit 1449799

Please sign in to comment.