Skip to content

Commit

Permalink
Merge pull request #4 from david-spaeth/master
Browse files Browse the repository at this point in the history
Add the ability to update sns application if it already exist
  • Loading branch information
Josh Holtz authored Mar 25, 2021
2 parents 2bcaa1e + 05b2321 commit abfec66
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ aws_sns(

# Optional private key password
# platform_apns_private_key_password: 'joshissupercool'
# Optional: updating certificate if platform_name already exists
# update_if_exist: true
)
```

Expand All @@ -40,6 +42,8 @@ aws_sns(
platform: 'GCM',
platform_name: 'your_awesome_android_app',
platform_gcm_api_key: 'your_gcm_api_key'
# Optional: updating key if platform_name already exists
# update_if_exist: true
)
```

Expand Down
Binary file added assets/test_push_cert_2.p12
Binary file not shown.
4 changes: 3 additions & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ platform :ios do
arn = aws_sns(
platform: 'APNS',
platform_name: 'test_app_ios_2',
platform_apns_private_key_path: 'assets/test_push_cert.p12'
platform_apns_private_key_path: 'assets/test_push_cert_2.p12',

update_if_exists: UI.confirm("Update if exists?")
)
puts "test_app_ios ARN: #{arn}"
end
Expand Down
60 changes: 53 additions & 7 deletions lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.run(params)

platform = params[:platform]
platform_name = params[:platform_name]

update_attributes = params[:update_if_exists]
platform_apns_private_key_path = params[:platform_apns_private_key_path]
platform_apns_private_key_password = params[:platform_apns_private_key_password]

Expand Down Expand Up @@ -63,12 +63,52 @@ def self.run(params)
#
UI.crash!("Unable to create any attributes to create platform application") unless attributes
begin
resp = client.create_platform_application({
name: platform_name,
platform: platform,
attributes: attributes,
})
arn = resp.platform_application_arn

arn = nil

#
# Try to find the arn for platform_name
#
if update_attributes

# Loop as long as list platform applications returns next_page or return the desired name
next_token = nil
loop do

resp = client.list_platform_applications({
next_token: next_token,
})

next_token = resp.next_token
# TODO: Must find a best search method !
platform_application = resp.platform_applications.find { |platform_application| platform_application.platform_application_arn.end_with? platform_name }

unless platform_application.nil?
arn = platform_application.platform_application_arn
break
end
break if next_token.nil?
end

end

# Not arn? OK, we create it !
if arn.nil?
resp = client.create_platform_application({
name: platform_name,
platform: platform,
attributes: attributes,
})
arn = resp.platform_application_arn
UI.important("Created #{arn}")
else
# else, updating
client.set_platform_application_attributes({
platform_application_arn: arn,
attributes: attributes,
})
UI.important("Updated #{arn}")
end

Actions.lane_context[SharedValues::AWS_SNS_PLATFORM_APPLICATION_ARN] = arn
ENV[SharedValues::AWS_SNS_PLATFORM_APPLICATION_ARN.to_s] = arn
Expand Down Expand Up @@ -140,6 +180,12 @@ def self.available_options
env_name: "AWS_SNS_PLATFORM_GCM_API_KEY",
description: "AWS Platform GCM API KEY",
deprecated: "Use :platform_fcm_server_key instead",
optional: true),
FastlaneCore::ConfigItem.new(key: :update_if_exists,
env_name: "AWS_SNS_UDPATE_IF_EXISTS",
description: "updating certificate/key if platform_name already exists",
default_value: false,
is_string: false,
optional: true)
]
end
Expand Down

0 comments on commit abfec66

Please sign in to comment.