This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
forked from hashicorp/terraform-provider-tfe
/
data_source_workspace.go
123 lines (101 loc) · 2.59 KB
/
data_source_workspace.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
package tfe
import (
"fmt"
"log"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceTFEWorkspace() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEWorkspaceRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
},
"auto_apply": {
Type: schema.TypeBool,
Computed: true,
},
"ssh_key_id": {
Type: schema.TypeString,
Computed: true,
},
"queue_all_runs": {
Type: schema.TypeBool,
Computed: true,
},
"terraform_version": {
Type: schema.TypeString,
Computed: true,
},
"working_directory": {
Type: schema.TypeString,
Computed: true,
},
"vcs_repo": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Computed: true,
},
"ingress_submodules": {
Type: schema.TypeBool,
Computed: true,
},
"oauth_token_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"external_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the name and organization.
name := d.Get("name").(string)
organization := d.Get("organization").(string)
log.Printf("[DEBUG] Read configuration of workspace: %s", name)
workspace, err := tfeClient.Workspaces.Read(ctx, organization, name)
if err != nil {
if err == tfe.ErrResourceNotFound {
return fmt.Errorf("Could not find workspace %s/%s", organization, name)
}
return fmt.Errorf("Error retrieving workspace: %v", err)
}
// Update the config.
d.Set("auto_apply", workspace.AutoApply)
d.Set("queue_all_runs", workspace.QueueAllRuns)
d.Set("terraform_version", workspace.TerraformVersion)
d.Set("working_directory", workspace.WorkingDirectory)
d.Set("external_id", workspace.ID)
if workspace.SSHKey != nil {
d.Set("ssh_key_id", workspace.SSHKey.ID)
}
var vcsRepo []interface{}
if workspace.VCSRepo != nil {
vcsConfig := map[string]interface{}{
"identifier": workspace.VCSRepo.Identifier,
"ingress_submodules": workspace.VCSRepo.IngressSubmodules,
"oauth_token_id": workspace.VCSRepo.OAuthTokenID,
}
vcsRepo = append(vcsRepo, vcsConfig)
}
d.Set("vcs_repo", vcsRepo)
d.SetId(workspace.ID)
return nil
}