Skip to content

Commit f1323e8

Browse files
authored
Populate team_id for vercel_integration_project_access when using the team from the provider. (#270)
1 parent ed93f58 commit f1323e8

3 files changed

+86
-56
lines changed

client/integrations.go

+32-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import (
77
"github.com/hashicorp/terraform-plugin-log/tflog"
88
)
99

10-
func (c *Client) GetIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (bool, error) {
10+
type IntegrationProjectAccess struct {
11+
Allowed bool
12+
TeamID string
13+
}
14+
15+
func (c *Client) GetIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (IntegrationProjectAccess, error) {
1116
url := fmt.Sprintf("%s/v1/integrations/configuration/%s/project/%s", c.baseURL, integrationID, projectID)
1217
if c.teamID(teamID) != "" {
1318
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
@@ -28,12 +33,18 @@ func (c *Client) GetIntegrationProjectAccess(ctx context.Context, integrationID,
2833
url: url,
2934
body: "",
3035
}, &e); err != nil {
31-
return false, err
36+
return IntegrationProjectAccess{
37+
Allowed: false,
38+
TeamID: c.teamID(teamID),
39+
}, err
3240
}
33-
return e.Allowed, nil
41+
return IntegrationProjectAccess{
42+
Allowed: e.Allowed,
43+
TeamID: c.teamID(teamID),
44+
}, nil
3445
}
3546

36-
func (c *Client) GrantIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (bool, error) {
47+
func (c *Client) GrantIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (IntegrationProjectAccess, error) {
3748
url := fmt.Sprintf("%s/v1/integrations/configuration/%s/project/%s", c.baseURL, integrationID, projectID)
3849
if c.teamID(teamID) != "" {
3950
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
@@ -54,12 +65,18 @@ func (c *Client) GrantIntegrationProjectAccess(ctx context.Context, integrationI
5465
url: url,
5566
body: `{ "allowed": true }`,
5667
}, &e); err != nil {
57-
return false, err
68+
return IntegrationProjectAccess{
69+
Allowed: false,
70+
TeamID: c.teamID(teamID),
71+
}, err
5872
}
59-
return true, nil
73+
return IntegrationProjectAccess{
74+
Allowed: true,
75+
TeamID: c.teamID(teamID),
76+
}, nil
6077
}
6178

62-
func (c *Client) RevokeIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (bool, error) {
79+
func (c *Client) RevokeIntegrationProjectAccess(ctx context.Context, integrationID, projectID, teamID string) (IntegrationProjectAccess, error) {
6380
url := fmt.Sprintf("%s/v1/integrations/configuration/%s/project/%s", c.baseURL, integrationID, projectID)
6481
if c.teamID(teamID) != "" {
6582
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
@@ -80,7 +97,13 @@ func (c *Client) RevokeIntegrationProjectAccess(ctx context.Context, integration
8097
url: url,
8198
body: `{ "allowed": false }`,
8299
}, &e); err != nil {
83-
return false, err
100+
return IntegrationProjectAccess{
101+
Allowed: false,
102+
TeamID: c.teamID(teamID),
103+
}, err
84104
}
85-
return false, nil
105+
return IntegrationProjectAccess{
106+
Allowed: false,
107+
TeamID: c.teamID(teamID),
108+
}, nil
86109
}

vercel/resource_integration_project_access.go

+10-43
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (r *integrationProjectAccessResource) Create(ctx context.Context, req resou
8888
return
8989
}
9090

91-
_, err := r.client.GrantIntegrationProjectAccess(ctx, plan.IntegrationID.ValueString(), plan.ProjectID.ValueString(), plan.TeamID.ValueString())
91+
ipa, err := r.client.GrantIntegrationProjectAccess(ctx, plan.IntegrationID.ValueString(), plan.ProjectID.ValueString(), plan.TeamID.ValueString())
9292
if err != nil {
9393
resp.Diagnostics.AddError(
9494
"Error granting integration project access",
@@ -98,7 +98,7 @@ func (r *integrationProjectAccessResource) Create(ctx context.Context, req resou
9898
}
9999

100100
result := IntegrationProjectAccess{
101-
TeamID: plan.TeamID,
101+
TeamID: types.StringValue(ipa.TeamID),
102102
IntegrationID: plan.IntegrationID,
103103
ProjectID: plan.ProjectID,
104104
}
@@ -124,7 +124,7 @@ func (r *integrationProjectAccessResource) Read(ctx context.Context, req resourc
124124
return
125125
}
126126

127-
allowed, err := r.client.GetIntegrationProjectAccess(ctx, state.IntegrationID.ValueString(), state.ProjectID.ValueString(), state.TeamID.ValueString())
127+
ipa, err := r.client.GetIntegrationProjectAccess(ctx, state.IntegrationID.ValueString(), state.ProjectID.ValueString(), state.TeamID.ValueString())
128128
if err != nil {
129129
resp.Diagnostics.AddError(
130130
"Error granting integration project access",
@@ -134,18 +134,18 @@ func (r *integrationProjectAccessResource) Read(ctx context.Context, req resourc
134134
}
135135

136136
result := IntegrationProjectAccess{
137-
TeamID: state.TeamID,
137+
TeamID: types.StringValue(ipa.TeamID),
138138
IntegrationID: state.IntegrationID,
139139
ProjectID: state.ProjectID,
140140
}
141141
tflog.Info(ctx, "read integration project access", map[string]interface{}{
142142
"team_id": result.TeamID.ValueString(),
143143
"integration_id": result.IntegrationID.ValueString(),
144144
"project_id": result.ProjectID.ValueString(),
145-
"allowed": allowed,
145+
"allowed": ipa.Allowed,
146146
})
147147

148-
if allowed {
148+
if ipa.Allowed {
149149
diags = resp.State.Set(ctx, result)
150150
resp.Diagnostics.Append(diags...)
151151
if resp.Diagnostics.HasError() {
@@ -157,40 +157,7 @@ func (r *integrationProjectAccessResource) Read(ctx context.Context, req resourc
157157
}
158158

159159
func (r *integrationProjectAccessResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
160-
var plan IntegrationProjectAccess
161-
diags := req.Plan.Get(ctx, &plan)
162-
resp.Diagnostics.Append(diags...)
163-
if resp.Diagnostics.HasError() {
164-
return
165-
}
166-
167-
allowed, err := r.client.GrantIntegrationProjectAccess(ctx, plan.IntegrationID.ValueString(), plan.ProjectID.ValueString(), plan.TeamID.ValueString())
168-
if err != nil {
169-
resp.Diagnostics.AddError(
170-
"Error granting integration project access",
171-
"Could not grant integration project access, unexpected error: "+err.Error(),
172-
)
173-
return
174-
}
175-
176-
result := IntegrationProjectAccess{
177-
TeamID: plan.TeamID,
178-
IntegrationID: plan.IntegrationID,
179-
ProjectID: plan.ProjectID,
180-
}
181-
182-
tflog.Info(ctx, "granted integration project access", map[string]interface{}{
183-
"team_id": result.TeamID.ValueString(),
184-
"integration_id": result.IntegrationID.ValueString(),
185-
"project_id": result.ProjectID.ValueString(),
186-
"allowed": allowed,
187-
})
188-
189-
diags = resp.State.Set(ctx, result)
190-
resp.Diagnostics.Append(diags...)
191-
if resp.Diagnostics.HasError() {
192-
return
193-
}
160+
resp.Diagnostics.AddError("Access should always be recreated", "Something incorrectly caused an Update, this should always be recreated instead of updated.")
194161
}
195162

196163
func (r *integrationProjectAccessResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
@@ -201,7 +168,7 @@ func (r *integrationProjectAccessResource) Delete(ctx context.Context, req resou
201168
return
202169
}
203170

204-
allowed, err := r.client.RevokeIntegrationProjectAccess(ctx, plan.IntegrationID.ValueString(), plan.ProjectID.ValueString(), plan.TeamID.ValueString())
171+
ipa, err := r.client.RevokeIntegrationProjectAccess(ctx, plan.IntegrationID.ValueString(), plan.ProjectID.ValueString(), plan.TeamID.ValueString())
205172
if err != nil {
206173
resp.Diagnostics.AddError(
207174
"Error revoking integration project access",
@@ -211,7 +178,7 @@ func (r *integrationProjectAccessResource) Delete(ctx context.Context, req resou
211178
}
212179

213180
result := IntegrationProjectAccess{
214-
TeamID: plan.TeamID,
181+
TeamID: types.StringValue(ipa.TeamID),
215182
IntegrationID: plan.IntegrationID,
216183
ProjectID: plan.ProjectID,
217184
}
@@ -220,6 +187,6 @@ func (r *integrationProjectAccessResource) Delete(ctx context.Context, req resou
220187
"team_id": result.TeamID.ValueString(),
221188
"integration_id": result.IntegrationID.ValueString(),
222189
"project_id": result.ProjectID.ValueString(),
223-
"allowed": allowed,
190+
"allowed": ipa.Allowed,
224191
})
225192
}

vercel/resource_integration_project_access_test.go

+44-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ func testCheckIntegrationProjectAccessDestroyed(n, teamID string) resource.TestC
1717
return fmt.Errorf("not found: %s", n)
1818
}
1919

20-
allowed, err := testClient().GetIntegrationProjectAccess(context.TODO(), rs.Primary.Attributes["integration_id"], rs.Primary.Attributes["project_id"], teamID)
20+
ipa, err := testClient().GetIntegrationProjectAccess(context.TODO(), rs.Primary.Attributes["integration_id"], rs.Primary.Attributes["project_id"], teamID)
2121
if err != nil {
2222
return err
2323
}
24-
if allowed {
24+
if ipa.Allowed {
2525
return fmt.Errorf("expected project to not allow access to integration")
2626
}
2727

@@ -36,11 +36,11 @@ func testCheckIntegrationProjectAccessExists(n, teamID string) resource.TestChec
3636
return fmt.Errorf("not found: %s", n)
3737
}
3838

39-
allowed, err := testClient().GetIntegrationProjectAccess(context.TODO(), rs.Primary.Attributes["integration_id"], rs.Primary.Attributes["project_id"], teamID)
39+
ipa, err := testClient().GetIntegrationProjectAccess(context.TODO(), rs.Primary.Attributes["integration_id"], rs.Primary.Attributes["project_id"], teamID)
4040
if err != nil {
4141
return err
4242
}
43-
if !allowed {
43+
if !ipa.Allowed {
4444
return fmt.Errorf("expected project to allow access to integration")
4545
}
4646

@@ -59,6 +59,25 @@ func TestAcc_IntegrationProjectAccess(t *testing.T) {
5959
Config: testAccIntegrationProjectAccess(name, teamIDConfig(), testExistingIntegration()),
6060
Check: resource.ComposeAggregateTestCheckFunc(
6161
testCheckIntegrationProjectAccessExists("vercel_integration_project_access.test_integration_access", testTeam()),
62+
resource.TestCheckResourceAttr("vercel_integration_project_access.test_integration_access", "team_id", testTeam()),
63+
),
64+
},
65+
},
66+
})
67+
}
68+
69+
func TestAcc_IntegrationProjectAccessWithoutExplicitTeam(t *testing.T) {
70+
name := acctest.RandString(16)
71+
resource.Test(t, resource.TestCase{
72+
PreCheck: func() { testAccPreCheck(t) },
73+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
74+
CheckDestroy: testCheckIntegrationProjectAccessDestroyed("vercel_integration_project_access.test_integration_access", testTeam()),
75+
Steps: []resource.TestStep{
76+
{
77+
Config: testAccIntegrationProjectAccessUsingProvider(name, testTeam(), testExistingIntegration()),
78+
Check: resource.ComposeAggregateTestCheckFunc(
79+
testCheckIntegrationProjectAccessExists("vercel_integration_project_access.test_integration_access", testTeam()),
80+
resource.TestCheckResourceAttr("vercel_integration_project_access.test_integration_access", "team_id", testTeam()),
6281
),
6382
},
6483
},
@@ -81,5 +100,26 @@ resource "vercel_integration_project_access" "test_integration_access" {
81100
project_id = vercel_project.test.id
82101
%[2]s
83102
}
103+
`, name, team, integration, testTeam())
104+
}
105+
106+
func testAccIntegrationProjectAccessUsingProvider(name, team, integration string) string {
107+
//lintignore:AT004
108+
return fmt.Sprintf(`
109+
provider "vercel" {
110+
team = "%[2]s"
111+
}
112+
113+
data "vercel_endpoint_verification" "test" {
114+
}
115+
116+
resource "vercel_project" "test" {
117+
name = "test-acc-%[1]s"
118+
}
119+
120+
resource "vercel_integration_project_access" "test_integration_access" {
121+
integration_id = "%[3]s"
122+
project_id = vercel_project.test.id
123+
}
84124
`, name, team, integration)
85125
}

0 commit comments

Comments
 (0)