This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
forked from hashicorp/terraform-provider-tfe
/
provider.go
218 lines (185 loc) · 6.33 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package tfe
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/hcl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/svchost"
"github.com/hashicorp/terraform/svchost/auth"
"github.com/hashicorp/terraform/svchost/disco"
"github.com/hashicorp/terraform/terraform"
)
const defaultHostname = "app.terraform.io"
var serviceIDs = []string{"tfe.v2.1", "tfe.v2"}
// Config is the structure of the configuration for the Terraform CLI.
type Config struct {
Hosts map[string]*ConfigHost `hcl:"host"`
Credentials map[string]map[string]interface{} `hcl:"credentials"`
}
// ConfigHost is the structure of the "host" nested block within the CLI
// configuration, which can be used to override the default service host
// discovery behavior for a particular hostname.
type ConfigHost struct {
Services map[string]interface{} `hcl:"services"`
}
// ctx is used as default context.Context when making TFE calls.
var ctx = context.Background()
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"hostname": {
Type: schema.TypeString,
Optional: true,
Description: descriptions["hostname"],
Default: defaultHostname,
},
"token": {
Type: schema.TypeString,
Optional: true,
Description: descriptions["token"],
},
},
DataSourcesMap: map[string]*schema.Resource{
"tfe_ssh_key": dataSourceTFESSHKey(),
"tfe_team": dataSourceTFETeam(),
"tfe_team_access": dataSourceTFETeamAccess(),
"tfe_workspace": dataSourceTFEWorkspace(),
"tfe_workspace_ids": dataSourceTFEWorkspaceIDs(),
},
ResourcesMap: map[string]*schema.Resource{
"tfe_oauth_client": resourceTFEOAuthClient(),
"tfe_organization": resourceTFEOrganization(),
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
"tfe_team_access": resourceTFETeamAccess(),
"tfe_team_member": resourceTFETeamMember(),
"tfe_team_members": resourceTFETeamMembers(),
"tfe_team_token": resourceTFETeamToken(),
"tfe_workspace": resourceTFEWorkspace(),
"tfe_variable": resourceTFEVariable(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
// Get the hostname and token.
hostname := d.Get("hostname").(string)
token := d.Get("token").(string)
// Parse the hostname for comparison,
host, err := svchost.ForComparison(hostname)
if err != nil {
return nil, err
}
// Get the Terraform CLI configuration.
config := cliConfig()
// Create a new credential source and service discovery object.
credsSrc := credentialsSource(config)
services := disco.NewWithCredentialsSource(credsSrc)
// Add any static host configurations service discovery object.
for userHost, hostConfig := range config.Hosts {
host, err := svchost.ForComparison(userHost)
if err != nil {
// ignore invalid hostnames.
continue
}
services.ForceHostServices(host, hostConfig.Services)
}
// Discover the full Terraform Enterprise service address.
var address *url.URL
for _, serviceID := range serviceIDs {
address = services.DiscoverServiceURL(host, serviceID)
if address != nil {
break
}
}
// Check if we were able to discover a service address.
if address == nil {
return nil, fmt.Errorf("host %s does not provide a Terraform Enterprise API", host)
}
// Only try to get to the token from the credentials source if no token
// was explicitly set in the provider configuration.
if token == "" {
creds, err := services.CredentialsForHost(host)
if err != nil {
log.Printf("[DEBUG] Failed to get credentials for %s: %s (ignoring)", host, err)
}
if creds != nil {
token = creds.Token()
}
}
// If we still don't have a token at this point, we return an error.
if token == "" {
return nil, fmt.Errorf("required token could not be found")
}
// Create a new TFE client config..
cfg := &tfe.Config{
Address: address.String(),
Token: token,
}
// Create s new TFE client.
return tfe.NewClient(cfg)
}
// cliConfig tries to find and parse the configuration of the Terraform CLI.
// This is an optional step, so any errors are ignored.
func cliConfig() *Config {
config := &Config{}
// Detect the CLI config file path.
configFilePath := os.Getenv("TERRAFORM_CONFIG")
if configFilePath == "" {
filePath, err := configFile()
if err != nil {
log.Printf("[ERROR] Error detecting default CLI config file path: %s", err)
return config
}
configFilePath = filePath
}
// Read the CLI config file content.
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Printf("[ERROR] Error reading the CLI config file %s: %v", configFilePath, err)
return config
}
// Parse the CLI config file content.
obj, err := hcl.Parse(string(content))
if err != nil {
log.Printf("[ERROR] Error parsing the CLI config file %s: %v", configFilePath, err)
return config
}
// Decode the CLI config file content.
if err := hcl.DecodeObject(&config, obj); err != nil {
log.Printf("[ERROR] Error decoding the CLI config file %s: %v", configFilePath, err)
}
return config
}
func credentialsSource(config *Config) auth.CredentialsSource {
creds := auth.NoCredentials
// Add all configured credentials to the credentials source.
if len(config.Credentials) > 0 {
staticTable := map[svchost.Hostname]map[string]interface{}{}
for userHost, creds := range config.Credentials {
host, err := svchost.ForComparison(userHost)
if err != nil {
// We expect the config was already validated by the time we get
// here, so we'll just ignore invalid hostnames.
continue
}
staticTable[host] = creds
}
creds = auth.StaticCredentialsSource(staticTable)
}
return creds
}
var descriptions = map[string]string{
"hostname": "The Terraform Enterprise hostname to connect to. Defaults to app.terraform.io.",
"token": "The token used to authenticate with Terraform Enterprise. We recommend omitting\n" +
"the token which can be set as credentials in the CLI config file.",
}