Created
November 23, 2016 14:05
-
-
Save hirokazumiyaji/762954f5d56592b42b9dea4cef6e5d88 to your computer and use it in GitHub Desktop.
Amazon SNS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package amzn | |
import ( | |
"errors" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/service/sns" | |
"github.com/aws/aws-sdk-go/service/sns/snsiface" | |
) | |
var ( | |
ApplicationArnEmptyError = errors.New("ApplicationArn should not be empty") | |
TokenEmptyError = errors.New("Token should not be empty") | |
) | |
type SnsData struct { | |
ApplicationArn string | |
Token string | |
EndpointArn string | |
CustomUserData string | |
} | |
func GetClient() { | |
} | |
func CreatePlatformEndpoint(c snsiface.SNSAPI, d SnsData) (*SnsData, error) { | |
if d.ApplicationArn == "" { | |
return &d, ApplicationArnEmptyError | |
} | |
if d.Token == "" { | |
return &d, TokenEmptyError | |
} | |
if d.EndpointArn == "" { | |
res, err := c.CreatePlatformEndpoint( | |
&sns.CreatePlatformEndpointInput{ | |
CustomUserData: aws.String(d.CustomUserData), | |
PlatformApplicationArn: aws.String(d.ApplicationArn), | |
Token: aws.String(d.Token), | |
}, | |
) | |
if err == nil { | |
d.EndpointArn = *res.EndpointArn | |
return &d, nil | |
} | |
} | |
res, err := c.GetEndpointAttributes( | |
&sns.GetEndpointAttributesInput{ | |
EndpointArn: aws.String(d.EndpointArn), | |
}, | |
) | |
if err != nil { | |
res, err := c.CreatePlatformEndpoint( | |
&sns.CreatePlatformEndpointInput{ | |
CustomUserData: aws.String(d.CustomUserData), | |
PlatformApplicationArn: aws.String(d.ApplicationArn), | |
Token: aws.String(d.Token), | |
}, | |
) | |
if err == nil { | |
d.EndpointArn = *res.EndpointArn | |
return &d, nil | |
} | |
return &d, err | |
} | |
if d.Token != *res.Attributes["Token"] || *res.Attributes["Enable"] == "false" { | |
_, err := c.SetEndpointAttributes( | |
&sns.SetEndpointAttributesInput{ | |
Attributes: map[string]*string{ | |
"CustomUserData": aws.String(d.CustomUserData), | |
"Enable": aws.String("true"), | |
"Token": aws.String(d.Token), | |
}, | |
EndpointArn: aws.String(d.EndpointArn), | |
}, | |
) | |
if err != nil { | |
return &d, err | |
} | |
return &d, nil | |
} | |
return &d, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment