From 3b2944d4afed19229563adbba48ffbc4a353c8ce Mon Sep 17 00:00:00 2001 From: evalphobia Date: Fri, 24 Apr 2020 16:03:40 +0900 Subject: [PATCH] [sns] Add GetPlatformApplicationAttribtutes() (#60) --- README.md | 1 + sns/client.go | 13 +++++ sns/client_test.go | 19 +++++++ sns/type.go | 120 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 sns/type.go diff --git a/README.md b/README.md index 24a32aa..682e4f2 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ At this time, it suports services below, - CreateTopic - DeleteTopic - GetEndpointAttributes + - GetPlatformApplicationAttributes - Publish - SetEndpointAttributes - Subscribe diff --git a/sns/client.go b/sns/client.go index 327426c..559d8db 100644 --- a/sns/client.go +++ b/sns/client.go @@ -330,6 +330,19 @@ func (svc *SNS) GetEndpoint(arn string) (*PlatformEndpoint, error) { return ep, err } +// GetPlatformApplicationAttributes executes `GetPlatformApplicationAttributes`. +func (svc *SNS) GetPlatformApplicationAttributes(arn string) (PlatformAttributes, error) { + resp, err := svc.client.GetPlatformApplicationAttributes(&SDK.GetPlatformApplicationAttributesInput{ + PlatformApplicationArn: pointers.String(arn), + }) + if err != nil { + svc.Errorf("error on `GetPlatformApplicationAttributes` operation; arn=%s; error=%s;", arn, err.Error()) + return PlatformAttributes{}, err + } + + return NewPlatformAttributesFromMap(resp.Attributes), nil +} + func (svc *SNS) newApplicationEndpoint(arn string) *PlatformEndpoint { return &PlatformEndpoint{ svc: svc, diff --git a/sns/client_test.go b/sns/client_test.go index 8ef22f8..b4d0f13 100644 --- a/sns/client_test.go +++ b/sns/client_test.go @@ -175,3 +175,22 @@ func TestBulkPublish(t *testing.T) { err := svc.BulkPublish(tokens, "message") assert.Nil(err) } + +func TestGetPlatformApplicationAttributes(t *testing.T) { + a := assert.New(t) + svc := getTestClient(t) + + list := []string{ + testAppleARN, + testGoogleARN, + } + + t.Skip("fakesns does not implement GetPlatformApplicationAttributes() yet.") + + for _, v := range list { + resp, err := svc.GetPlatformApplicationAttributes(v) + a.NoError(err, v) + a.True(resp.HasEnabled, v) + a.True(resp.Enabled, v) + } +} diff --git a/sns/type.go b/sns/type.go new file mode 100644 index 0000000..081eef3 --- /dev/null +++ b/sns/type.go @@ -0,0 +1,120 @@ +package sns + +import ( + "strconv" + "time" +) + +// PlatformAttributes contains platform application attributes. +type PlatformAttributes struct { + HasPlatformCredential bool + PlatformCredential string + + HasPlatformPrincipal bool + PlatformPrincipal string + + HasEventEndpointCreated bool + EventEndpointCreated string + + HasEventEndpointDeleted bool + EventEndpointDeleted string + + HasEventEndpointUpdated bool + EventEndpointUpdated string + + HasEventDeliveryFailure bool + EventDeliveryFailure string + + HasSuccessFeedbackRoleArn bool + SuccessFeedbackRoleArn string + + HasFailureFeedbackRoleArn bool + FailureFeedbackRoleArn string + + HasSuccessFeedbackSampleRate bool + SuccessFeedbackSampleRate int // 0 - 100 + + HasEnabled bool + Enabled bool + + HasAppleCertificateExpirationDate bool + AppleCertificateExpirationDate time.Time +} + +func NewPlatformAttributesFromMap(attr map[string]*string) PlatformAttributes { + a := PlatformAttributes{} + if len(attr) == 0 { + return a + } + + if v, ok := attr["PlatformCredential"]; ok { + a.HasPlatformCredential = true + if v != nil { + a.PlatformCredential = *v + } + } + if v, ok := attr["PlatformPrincipal"]; ok { + a.HasPlatformPrincipal = true + if v != nil { + a.PlatformPrincipal = *v + } + } + if v, ok := attr["EventEndpointCreated"]; ok { + a.HasEventEndpointCreated = true + if v != nil { + a.EventEndpointCreated = *v + } + } + if v, ok := attr["EventEndpointDeleted"]; ok { + a.HasEventEndpointDeleted = true + if v != nil { + a.EventEndpointDeleted = *v + } + } + if v, ok := attr["EventEndpointUpdated"]; ok { + a.HasEventEndpointUpdated = true + if v != nil { + a.EventEndpointUpdated = *v + } + } + if v, ok := attr["EventDeliveryFailure"]; ok { + a.HasEventDeliveryFailure = true + if v != nil { + a.EventDeliveryFailure = *v + } + } + if v, ok := attr["SuccessFeedbackRoleArn"]; ok { + a.HasSuccessFeedbackRoleArn = true + if v != nil { + a.SuccessFeedbackRoleArn = *v + } + } + if v, ok := attr["FailureFeedbackRoleArn"]; ok { + a.HasFailureFeedbackRoleArn = true + if v != nil { + a.FailureFeedbackRoleArn = *v + } + } + if v, ok := attr["SuccessFeedbackSampleRate"]; ok { + a.HasSuccessFeedbackSampleRate = true + if v != nil { + num, _ := strconv.Atoi(*v) + a.SuccessFeedbackSampleRate = num + } + } + if v, ok := attr["Enabled"]; ok { + a.HasEnabled = true + if v != nil { + ok, _ := strconv.ParseBool(*v) + a.Enabled = ok + } + } + if v, ok := attr["AppleCertificateExpirationDate"]; ok { + a.HasAppleCertificateExpirationDate = true + if v != nil { + dt, _ := time.Parse(time.RFC3339, *v) + a.AppleCertificateExpirationDate = dt + } + } + return a +}