-
Notifications
You must be signed in to change notification settings - Fork 65
/
config.go
55 lines (45 loc) · 1.18 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
package octopusdeploy
import (
"net/url"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)
// Config holds Address and the APIKey of the Octopus Deploy server
type Config struct {
Address string
APIKey string
SpaceID string
SpaceName string
}
// Client returns a new Octopus Deploy client
func (c *Config) Client() (*octopusdeploy.Client, diag.Diagnostics) {
apiURL, err := url.Parse(c.Address)
if err != nil {
return nil, diag.FromErr(err)
}
client, err := octopusdeploy.NewClient(nil, apiURL, c.APIKey, "")
if err != nil {
return nil, diag.FromErr(err)
}
if len(c.SpaceID) > 0 {
space, err := client.Spaces.GetByID(c.SpaceID)
if err != nil {
return nil, diag.FromErr(err)
}
client, err = octopusdeploy.NewClient(nil, apiURL, c.APIKey, space.GetID())
if err != nil {
return nil, diag.FromErr(err)
}
}
if len(c.SpaceName) > 0 {
space, err := client.Spaces.GetByName(c.SpaceName)
if err != nil {
return nil, diag.FromErr(err)
}
client, err = octopusdeploy.NewClient(nil, apiURL, c.APIKey, space.GetID())
if err != nil {
return nil, diag.FromErr(err)
}
}
return client, nil
}