这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for updating Edge Config #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions client/edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ func (c *Client) GetEdgeConfig(ctx context.Context, id, teamID string) (e EdgeCo
return e, err
}

type UpdateEdgeConfigRequest struct {
Slug string `json:"slug"`
TeamID string `json:"-"`
ID string `json:"-"`
}

func (c *Client) UpdateEdgeConfig(ctx context.Context, request UpdateEdgeConfigRequest) (e EdgeConfig, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s", c.baseURL, request.ID)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}

payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating edge config", map[string]interface{}{
"url": url,
"payload": payload,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "PUT",
url: url,
body: payload,
}, &e)
return e, err
}

func (c *Client) DeleteEdgeConfig(ctx context.Context, id, teamID string) error {
url := fmt.Sprintf("%s/v1/edge-config/%s", c.baseURL, id)
if c.teamID(teamID) != "" {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/edge_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ resource "vercel_project_environment_variable" "example" {

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) The ID of the Edge Config.

## Import

Expand Down
24 changes: 5 additions & 19 deletions vercel/resource_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ An Alias allows a ` + "`vercel_deployment` to be accessed through a different UR
Optional: true,
Computed: true,
Description: "The ID of the team the Alias and Deployment exist under. Required when configuring a team resource if a default team has not been set in the provider.",
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured()},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured(), stringplanmodifier.UseStateForUnknown()},
},
"id": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -156,24 +156,10 @@ func (r *aliasResource) Read(ctx context.Context, req resource.ReadRequest, resp

// Update updates the Alias state.
func (r *aliasResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan Alias
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
resp.Diagnostics.AddError(
"Error getting alias plan",
"Error getting alias plan",
)
return
}

var state Alias
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.AddError(
"Updating an Alias is not supported",
"Updating an Alias is not supported",
)
}

// Delete deletes an Alias.
Expand Down
3 changes: 0 additions & 3 deletions vercel/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vercel

import (
"context"
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -314,8 +313,6 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
return
}

thing, _ := json.Marshal(plan.toUpdateRequest())
tflog.Error(ctx, "UPDATE REQUEST", map[string]interface{}{"request": string(thing)})
out, err := r.client.UpdateDNSRecord(
ctx,
plan.TeamID.ValueString(),
Expand Down
54 changes: 45 additions & 9 deletions vercel/resource_edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ Provides an Edge Config resource.
An Edge Config is a global data store that enables experimentation with feature flags, A/B testing, critical redirects, and more.`,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name/slug of the Edge Config.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Description: "The name/slug of the Edge Config.",
Required: true,
Validators: []validator.String{
stringRegex(
regexp.MustCompile(`^[a-z0-9\_\-]{0,32}$`),
Expand All @@ -75,10 +74,12 @@ An Edge Config is a global data store that enables experimentation with feature
Optional: true,
Computed: true,
Description: "The ID of the team the Edge Config should exist under. Required when configuring a team resource if a default team has not been set in the provider.",
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured()},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured(), stringplanmodifier.UseStateForUnknown()},
},
"id": schema.StringAttribute{
Computed: true,
Description: "The ID of the Edge Config.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
},
}
Expand Down Expand Up @@ -175,10 +176,45 @@ func (r *edgeConfigResource) Read(ctx context.Context, req resource.ReadRequest,

// Update does nothing.
func (r *edgeConfigResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError(
"Updating an Edge Config is not supported",
"Updating an Edge Config is not supported",
)
var plan EdgeConfig
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

out, err := r.client.UpdateEdgeConfig(ctx, client.UpdateEdgeConfigRequest{
Slug: plan.Name.ValueString(),
TeamID: plan.TeamID.ValueString(),
ID: plan.ID.ValueString(),
})
if client.NotFound(err) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Error updating Edge Config",
fmt.Sprintf("Could not update Edge Config %s %s, unexpected error: %s",
plan.TeamID.ValueString(),
plan.ID.ValueString(),
err,
),
)
return
}

result := responseToEdgeConfig(out)
tflog.Trace(ctx, "update edge config", map[string]interface{}{
"team_id": result.TeamID.ValueString(),
"edge_config_id": result.ID.ValueString(),
})

diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// Delete deletes an Edge Config.
Expand Down
17 changes: 17 additions & 0 deletions vercel/resource_edge_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func TestAcc_EdgeConfigResource(t *testing.T) {
resource.TestCheckResourceAttrSet("vercel_edge_config.test", "id"),
),
},
{
Config: testAccResourceEdgeConfigUpdated(name, teamIDConfig()),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckEdgeConfigExists(testTeam(), "vercel_edge_config.test"),
resource.TestCheckResourceAttr("vercel_edge_config.test", "name", fmt.Sprintf("%s-updated", name)),
resource.TestCheckResourceAttrSet("vercel_edge_config.test", "id"),
),
},
},
})
}
Expand All @@ -77,3 +85,12 @@ resource "vercel_edge_config" "test" {
}
`, name, team)
}

func testAccResourceEdgeConfigUpdated(name, team string) string {
return fmt.Sprintf(`
resource "vercel_edge_config" "test" {
name = "%[1]s-updated"
%[2]s
}
`, name, team)
}
5 changes: 3 additions & 2 deletions vercel/resource_edge_config_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ An Edge Config token is used to authenticate against an Edge Config's endpoint.
Optional: true,
Computed: true,
Description: "The ID of the team the Edge Config should exist under. Required when configuring a team resource if a default team has not been set in the provider.",
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured()},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured(), stringplanmodifier.UseStateForUnknown()},
},
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured(), stringplanmodifier.UseStateForUnknown()},
Computed: true,
},
"token": schema.StringAttribute{
Description: "A read access token used for authenticating against the Edge Config's endpoint for high volume, low-latency requests.",
Expand Down