add aws apis
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.trj.tw/golang/go-ddns-svc/module/config"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/route53"
|
||||
)
|
||||
|
||||
// AWSClient -
|
||||
type AWSClient struct {
|
||||
Session *session.Session
|
||||
Config *aws.Config
|
||||
Services struct {
|
||||
R53 *route53.Route53
|
||||
}
|
||||
}
|
||||
|
||||
var client *AWSClient
|
||||
|
||||
// NewAWS -
|
||||
func NewAWS() error {
|
||||
conf := config.GetConfig()
|
||||
awsConf := &aws.Config{}
|
||||
awsConf.Region = aws.String("us-east-1")
|
||||
var sess *session.Session
|
||||
|
||||
if conf.AWS.SharedConfig {
|
||||
if len(conf.AWS.SharedName) == 0 {
|
||||
return errors.New("aws shared config name empty")
|
||||
}
|
||||
awsConf.Credentials = credentials.NewSharedCredentials("", conf.AWS.SharedName)
|
||||
} else {
|
||||
awsConf.Credentials = credentials.NewStaticCredentials(conf.AWS.AccessKey, conf.AWS.SecretKey, "")
|
||||
}
|
||||
sess = session.New(awsConf)
|
||||
|
||||
client = &AWSClient{}
|
||||
client.Session = sess
|
||||
client.Config = awsConf
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -16,6 +16,7 @@ type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
AWS struct {
|
||||
SharedConfig bool `yaml:"shared_config"`
|
||||
SharedName string `yaml:"shared_name"`
|
||||
AccessKey string `yaml:"access_key"`
|
||||
SecretKey string `yaml:"secret_key"`
|
||||
} `yaml:"aws"`
|
||||
@@ -50,7 +51,7 @@ func LoadConfig(p ...string) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
envOverride()
|
||||
envOverride()
|
||||
|
||||
return
|
||||
}
|
||||
@@ -76,6 +77,11 @@ func envOverride() {
|
||||
}
|
||||
}
|
||||
|
||||
str = os.Getenv("AWS_SHARED_NAME")
|
||||
if len(str) > 0 {
|
||||
conf.AWS.SharedName = str
|
||||
}
|
||||
|
||||
// set aws access key
|
||||
str = os.Getenv("AWS_ACCESS_KEY")
|
||||
if len(str) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user