This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
forked from hashicorp/terraform-provider-tfe
/
resource_tfe_team_access.go
152 lines (126 loc) · 3.69 KB
/
resource_tfe_team_access.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
package tfe
import (
"fmt"
"log"
"strings"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceTFETeamAccess() *schema.Resource {
return &schema.Resource{
Create: resourceTFETeamAccessCreate,
Read: resourceTFETeamAccessRead,
Delete: resourceTFETeamAccessDelete,
Importer: &schema.ResourceImporter{
State: resourceTFETeamAccessImporter,
},
Schema: map[string]*schema.Schema{
"access": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(
[]string{
string(tfe.AccessAdmin),
string(tfe.AccessRead),
string(tfe.AccessWrite),
},
false,
),
},
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"workspace_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceTFETeamAccessCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get access and team ID.
access := d.Get("access").(string)
teamID := d.Get("team_id").(string)
// Get organization and workspace.
organization, workspace, err := unpackWorkspaceID(d.Get("workspace_id").(string))
if err != nil {
return fmt.Errorf("Error unpacking workspace ID: %v", err)
}
// Get the team.
tm, err := tfeClient.Teams.Read(ctx, teamID)
if err != nil {
return fmt.Errorf("Error retrieving team %s: %v", teamID, err)
}
// Get the workspace.
ws, err := tfeClient.Workspaces.Read(ctx, organization, workspace)
if err != nil {
return fmt.Errorf(
"Error retrieving workspace %s from organization %s: %v", workspace, organization, err)
}
// Create a new options struct.
options := tfe.TeamAccessAddOptions{
Access: tfe.Access(tfe.AccessType(access)),
Team: tm,
Workspace: ws,
}
log.Printf("[DEBUG] Give team %s %s access to workspace: %s", tm.Name, access, ws.Name)
tmAccess, err := tfeClient.TeamAccess.Add(ctx, options)
if err != nil {
return fmt.Errorf(
"Error giving team %s %s access to workspace %s: %v", tm.Name, access, ws.Name, err)
}
d.SetId(tmAccess.ID)
return resourceTFETeamAccessRead(d, meta)
}
func resourceTFETeamAccessRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Read configuration of team access: %s", d.Id())
tmAccess, err := tfeClient.TeamAccess.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Team access %s does no longer exist", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of team access %s: %v", d.Id(), err)
}
// Update config.
d.Set("access", string(tmAccess.Access))
if tmAccess.Team != nil {
d.Set("team_id", tmAccess.Team.ID)
} else {
d.Set("team_id", "")
}
return nil
}
func resourceTFETeamAccessDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Delete team access: %s", d.Id())
err := tfeClient.TeamAccess.Remove(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting team access %s: %v", d.Id(), err)
}
return nil
}
func resourceTFETeamAccessImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
s := strings.SplitN(d.Id(), "/", 3)
if len(s) != 3 {
return nil, fmt.Errorf(
"invalid team access import format: %s (expected <ORGANIZATION>/<WORKSPACE>/<TEAM ACCESS ID>)",
d.Id(),
)
}
// Set the fields that are part of the import ID.
d.Set("workspace_id", s[0]+"/"+s[1])
d.SetId(s[2])
return []*schema.ResourceData{d}, nil
}