forked from Cardfree/terraform-provider-teamcity
/
config.go
67 lines (56 loc) · 1.46 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
package teamcity
import (
"errors"
"github.com/bestseller-ecom/teamcity-sdk-go/teamcity"
// "os"
)
type Config struct {
User string
Password string
URL string
Version string
Insecure bool
SkipCredsValidation bool
}
func (c *Config) Client() (interface{}, error) {
// if c.User == "" {
// c.User = os.Getenv("TEAMCITY_USERNAME")
// }
if c.User == "" {
return nil, errors.New("Missing TeamCity user and TEAMCITY_USERNAME not defined")
}
// if c.Password == "" {
// c.Password = os.Getenv("TEAMCITY_PASSWORD")
// }
if c.Password == "" {
return nil, errors.New("Missing TeamCity password and TEAMCITY_PASSWORD not defined")
}
// if c.URL == "" {
// c.URL = os.Getenv("TEAMCITY_URL")
// }
if c.URL == "" {
return nil, errors.New("Missing TeamCity URL and TEAMCITY_URL not defined")
}
if c.Version == "" {
return nil, errors.New("Missing TeamCity API Version and TEAMCITY_API_VERSION not defined")
}
client := teamcity.New(c.URL, c.User, c.Password, c.Version)
if !c.SkipCredsValidation {
err := c.ValidateCredentials(client)
if err != nil {
return nil, err
}
}
return client, nil
}
// Validate credentials early and fail before we do any graph walking.
func (c *Config) ValidateCredentials(client *teamcity.Client) error {
server, err := client.Server()
if err != nil {
return err
}
if server == nil {
return errors.New("Received no reply from server")
}
return nil
}