diff --git a/README.md b/README.md index 3a4d9ce..f3a91c1 100644 --- a/README.md +++ b/README.md @@ -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 ) ``` @@ -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 ) ``` diff --git a/assets/test_push_cert_2.p12 b/assets/test_push_cert_2.p12 new file mode 100644 index 0000000..84b8dd3 Binary files /dev/null and b/assets/test_push_cert_2.p12 differ diff --git a/fastlane/Fastfile b/fastlane/Fastfile index b81afbc..4406765 100644 --- a/fastlane/Fastfile +++ b/fastlane/Fastfile @@ -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 diff --git a/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb b/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb index 531f825..2595583 100644 --- a/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb +++ b/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb @@ -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] @@ -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 @@ -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