-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider.go
71 lines (62 loc) · 2.04 KB
/
provider.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
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"octopus"
"os"
)
// Provider creates the Octopus Deploy resource provider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
// Provider settings schema
Schema: map[string]*schema.Schema{
"server_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The base URL of the Octopus Deploy server.",
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Default: "",
Description: "The API key used to authenticate to the Octopus Deploy API (if not specified, then the OCTOPUS_API_KEY environment variable will be used).",
},
},
// Provider resource definitions
ResourcesMap: map[string]*schema.Resource{
"octopus_environment": resourceEnvironment(),
"octopus_variable": resourceVariable(),
},
DataSourcesMap: map[string]*schema.Resource{
"octopus_environment": datasourceEnvironment(),
"octopus_machine": datasourceMachine(),
"octopus_project": datasourceProject(),
"octopus_variable": datasourceVariable(),
},
// Provider configuration
ConfigureFunc: configureProvider,
}
}
// Configure the provider.
// Returns the provider's compute API client.
func configureProvider(providerSettings *schema.ResourceData) (interface{}, error) {
server := providerSettings.Get("server_url").(string)
apiKey := providerSettings.Get("api_key").(string)
if isEmpty(apiKey) {
apiKey = os.Getenv("OCTOPUS_API_KEY")
}
if isEmpty(apiKey) {
return nil, fmt.Errorf("The 'api_key' property was not specified for the 'octopus' provider, and the 'OCTOPUS_API_KEY' environment variable is not present. Please supply either one of these to configure the API key used to authenticate to Octopus Deploy.")
}
var (
client *octopus.Client
err error
)
client, err = octopus.NewClientWithAPIKey(server, apiKey)
if err != nil {
return nil, err
}
return client, nil
}