forked from aliyun/aliyun-odps-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
131 lines (109 loc) · 3.49 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package odps
import (
account2 "github.com/jiuzhiqian/aliyun-odps-go-sdk/odps/account"
"github.com/jiuzhiqian/aliyun-odps-go-sdk/odps/restclient"
"github.com/pkg/errors"
"gopkg.in/ini.v1"
"net/url"
"time"
)
// Config is the basic config for odps. The NewConfig function should be used, which sets default values.
type Config struct {
AccessId string
AccessKey string
StsToken string
Endpoint string
ProjectName string
TcpConnectionTimeout time.Duration
HttpTimeout time.Duration
}
func NewConfig() *Config {
return &Config{
TcpConnectionTimeout: 30 * time.Second,
HttpTimeout: 0,
}
}
func NewConfigFromIni(iniPath string) (*Config, error) {
cfg, err := ini.Load(iniPath)
if err != nil {
return nil, errors.WithStack(err)
}
section := cfg.Section("odps")
conf := NewConfig()
conf.AccessId = section.Key("access_id").String()
conf.AccessKey = section.Key("access_key").String()
conf.StsToken = section.Key("sts_token").String()
conf.Endpoint = section.Key("endpoint").String()
_, err = url.Parse(conf.Endpoint)
if err != nil {
return nil, errors.Wrapf(err, "invalid endpoint: \"%s\"", conf.Endpoint)
}
conf.ProjectName = section.Key("project").String()
connTimeout, err := section.GetKey("tcp_connection_timeout")
if err == nil {
v, err := connTimeout.Int()
if err == nil {
conf.TcpConnectionTimeout = time.Duration(v) * time.Second
}
}
httpTimeout, err := section.GetKey("http_timeout")
if err == nil {
v, err := httpTimeout.Int()
if err == nil {
conf.HttpTimeout = time.Duration(v) * time.Second
}
}
return conf, nil
}
func (c *Config) GenAccount() account2.Account {
var account account2.Account
if c.StsToken == "" {
account = account2.NewAliyunAccount(c.AccessId, c.AccessKey)
} else {
account = account2.NewStsAccount(c.AccessId, c.AccessKey, c.StsToken)
}
return account
}
func (c *Config) GenRestClient() restclient.RestClient {
account := c.GenAccount()
client := restclient.NewOdpsRestClient(account, c.Endpoint)
client.TcpConnectionTimeout = c.TcpConnectionTimeout
client.HttpTimeout = c.HttpTimeout
return client
}
func (c *Config) GenOdps() *Odps {
account := c.GenAccount()
odpsIns := NewOdps(account, c.Endpoint)
odpsIns.SetTcpConnectTimeout(c.TcpConnectionTimeout)
odpsIns.SetHttpTimeout(c.HttpTimeout)
odpsIns.SetDefaultProjectName(c.ProjectName)
return odpsIns
}
func (c *Config) FormatDsn() string {
u, _ := url.Parse(c.Endpoint)
dsn := url.URL{
Scheme: u.Scheme,
Host: u.Host,
Path: u.Path,
}
values := make(url.Values)
values.Set("project", c.ProjectName)
dsn.RawQuery = values.Encode()
dsn.User = url.UserPassword(c.AccessId, c.AccessKey)
return dsn.String()
}