Skip to content

Commit

Permalink
Add support for Edge Config Schema (#166)
Browse files Browse the repository at this point in the history
- Change TF_LOG levels to Info, rather than Trace (trace is used very
  heavily by the provider protocol, so it's too noisy).
  • Loading branch information
dglsparsons authored Apr 16, 2024
1 parent c1722f2 commit 48910c3
Show file tree
Hide file tree
Showing 65 changed files with 1,057 additions and 109 deletions.
2 changes: 1 addition & 1 deletion client/alias_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Client) CreateAlias(ctx context.Context, request CreateAliasRequest, de
}
payload := string(mustMarshal(request))

tflog.Trace(ctx, "creating alias", map[string]interface{}{
tflog.Info(ctx, "creating alias", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/alias_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *Client) DeleteAlias(ctx context.Context, aliasUID string, teamID string
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "deleting alias", map[string]interface{}{
tflog.Info(ctx, "deleting alias", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/alias_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Client) GetAlias(ctx context.Context, alias, teamID string) (r AliasRes
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "getting alias", map[string]interface{}{
tflog.Info(ctx, "getting alias", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/deployment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (c *Client) CreateDeployment(ctx context.Context, request CreateDeploymentR
}
payload := string(mustMarshal(request))

tflog.Trace(ctx, "creating deployment", map[string]interface{}{
tflog.Info(ctx, "creating deployment", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/deployment_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) DeleteDeployment(ctx context.Context, deploymentID string, team
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "deleting deployment", map[string]interface{}{
tflog.Info(ctx, "deleting deployment", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/deployment_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Client) GetDeployment(ctx context.Context, deploymentID, teamID string)
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "getting deployment", map[string]interface{}{
tflog.Info(ctx, "getting deployment", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/dns_record_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Client) UpdateDNSRecord(ctx context.Context, teamID, recordID string, r
}

payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating DNS record", map[string]interface{}{
tflog.Info(ctx, "updating DNS record", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
22 changes: 19 additions & 3 deletions client/edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *Client) CreateEdgeConfig(ctx context.Context, request CreateEdgeConfigR
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "creating edge config", map[string]interface{}{
tflog.Info(ctx, "creating edge config", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand All @@ -42,7 +42,7 @@ func (c *Client) GetEdgeConfig(ctx context.Context, id, teamID string) (e EdgeCo
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "reading edge config", map[string]interface{}{
tflog.Info(ctx, "reading edge config", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *Client) DeleteEdgeConfig(ctx context.Context, id, teamID string) error
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "deleting edge config", map[string]interface{}{
tflog.Info(ctx, "deleting edge config", map[string]interface{}{
"url": url,
})

Expand All @@ -94,3 +94,19 @@ func (c *Client) DeleteEdgeConfig(ctx context.Context, id, teamID string) error
url: url,
}, nil)
}

func (c *Client) ListEdgeConfigs(ctx context.Context, teamID string) (e []EdgeConfig, err error) {
url := fmt.Sprintf("%s/v1/edge-config", c.baseURL)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "listing edge configs", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &e)
return e, err
}
78 changes: 78 additions & 0 deletions client/edge_config_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package client

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

type EdgeConfigSchema struct {
ID string `json:"-"`
Definition any `json:"definition"`
TeamID string `json:"-"`
}

func (c *Client) UpsertEdgeConfigSchema(ctx context.Context, request EdgeConfigSchema) (e EdgeConfigSchema, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s/schema", 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.Info(ctx, "creating edge config schema", map[string]interface{}{
"url": url,
"payload": payload,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "POST",
url: url,
body: payload,
}, &e)
e.TeamID = c.teamID(request.TeamID)
e.ID = request.ID
return e, err
}

func (c *Client) GetEdgeConfigSchema(ctx context.Context, id, teamID string) (e EdgeConfigSchema, err error) {
url := fmt.Sprintf("%s/v1/edge-config/%s/schema", c.baseURL, id)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "getting edge config schema", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
errorOnNoContent: true,
}, &e)

if noContent(err) {
return e, APIError{
StatusCode: 404,
Message: "Edge Config Schema not found",
Code: "not_found",
}
}

e.TeamID = c.teamID(teamID)
e.ID = id
return e, err
}

func (c *Client) DeleteEdgeConfigSchema(ctx context.Context, id, teamID string) error {
url := fmt.Sprintf("%s/v1/edge-config/%s/schema", c.baseURL, id)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "deleting edge config schema", map[string]interface{}{
"url": url,
})
return c.doRequest(clientRequest{
ctx: ctx,
method: "DELETE",
url: url,
}, nil)
}
6 changes: 3 additions & 3 deletions client/edge_config_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Client) CreateEdgeConfigToken(ctx context.Context, request CreateEdgeCo
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "creating edge config token", map[string]interface{}{
tflog.Info(ctx, "creating edge config token", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *Client) DeleteEdgeConfigToken(ctx context.Context, request EdgeConfigTo
},
))

tflog.Trace(ctx, "deleting edge config token", map[string]interface{}{
tflog.Info(ctx, "deleting edge config token", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand All @@ -88,7 +88,7 @@ func (c *Client) GetEdgeConfigToken(ctx context.Context, request EdgeConfigToken
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}

tflog.Trace(ctx, "getting edge config token", map[string]interface{}{
tflog.Info(ctx, "getting edge config token", map[string]interface{}{
"url": url,
})

Expand Down
4 changes: 2 additions & 2 deletions client/environment_variable_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Client) CreateEnvironmentVariable(ctx context.Context, request CreateEn
}
payload := string(mustMarshal(request.EnvironmentVariable))

tflog.Trace(ctx, "creating environment variable", map[string]interface{}{
tflog.Info(ctx, "creating environment variable", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down Expand Up @@ -59,7 +59,7 @@ func (c *Client) CreateEnvironmentVariables(ctx context.Context, request CreateE
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request.EnvironmentVariables))
tflog.Trace(ctx, "creating environment variables", map[string]interface{}{
tflog.Info(ctx, "creating environment variables", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/environment_variable_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) UpdateEnvironmentVariable(ctx context.Context, request UpdateEn
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating environment variable", map[string]interface{}{
tflog.Info(ctx, "updating environment variable", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/environment_variables_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c *Client) DeleteEnvironmentVariable(ctx context.Context, projectID, teamI
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "deleting environment variable", map[string]interface{}{
tflog.Info(ctx, "deleting environment variable", map[string]interface{}{
"url": url,
})
return c.doRequest(clientRequest{
Expand Down
4 changes: 2 additions & 2 deletions client/environment_variables_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *Client) getEnvironmentVariables(ctx context.Context, projectID, teamID
envResponse := struct {
Env []EnvironmentVariable `json:"envs"`
}{}
tflog.Trace(ctx, "getting environment variables", map[string]interface{}{
tflog.Info(ctx, "getting environment variables", map[string]interface{}{
"url": url,
})
err := c.doRequest(clientRequest{
Expand All @@ -38,7 +38,7 @@ func (c *Client) GetEnvironmentVariable(ctx context.Context, projectID, teamID,
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "getting environment variable", map[string]interface{}{
tflog.Info(ctx, "getting environment variable", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
5 changes: 5 additions & 0 deletions client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ func NotFound(err error) bool {
var apiErr APIError
return err != nil && errors.As(err, &apiErr) && apiErr.StatusCode == 404
}

func noContent(err error) bool {
var apiErr APIError
return err != nil && errors.As(err, &apiErr) && apiErr.StatusCode == 204
}
4 changes: 2 additions & 2 deletions client/file_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func (c *Client) CreateFile(ctx context.Context, request CreateFileRequest) erro
req.Header.Add("x-vercel-digest", request.SHA)
req.Header.Set("Content-Type", "application/octet-stream")

tflog.Trace(ctx, "uploading file", map[string]interface{}{
tflog.Info(ctx, "uploading file", map[string]interface{}{
"url": url,
"sha": request.SHA,
})
err = c._doRequest(req, nil)
err = c._doRequest(req, nil, false)
return err
}
2 changes: 1 addition & 1 deletion client/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) CreateProject(ctx context.Context, teamID string, request Creat
}

payload := string(mustMarshal(request))
tflog.Trace(ctx, "creating project", map[string]interface{}{
tflog.Info(ctx, "creating project", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/project_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Client) DeleteProject(ctx context.Context, projectID, teamID string) er
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "deleting project", map[string]interface{}{
tflog.Info(ctx, "deleting project", map[string]interface{}{
"url": url,
})
return c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/project_domain_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) CreateProjectDomain(ctx context.Context, projectID, teamID stri
}

payload := string(mustMarshal(request))
tflog.Trace(ctx, "creating project domain", map[string]interface{}{
tflog.Info(ctx, "creating project domain", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/project_domain_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Client) DeleteProjectDomain(ctx context.Context, projectID, domain, tea
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "deleting project domain", map[string]interface{}{
tflog.Info(ctx, "deleting project domain", map[string]interface{}{
"url": url,
})
return c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/project_domain_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Client) GetProjectDomain(ctx context.Context, projectID, domain, teamID
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

tflog.Trace(ctx, "getting project domain", map[string]interface{}{
tflog.Info(ctx, "getting project domain", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/project_domain_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Client) UpdateProjectDomain(ctx context.Context, projectID, domain, tea
}

payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating project domain", map[string]interface{}{
tflog.Info(ctx, "updating project domain", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
2 changes: 1 addition & 1 deletion client/project_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Client) GetProject(ctx context.Context, projectID, teamID string, shoul
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Trace(ctx, "getting project", map[string]interface{}{
tflog.Info(ctx, "getting project", map[string]interface{}{
"url": url,
"shouldFetchEnvironment": shouldFetchEnvironmentVariables,
})
Expand Down
2 changes: 1 addition & 1 deletion client/project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *Client) ListProjects(ctx context.Context, teamID string) (r []ProjectRe
pr := struct {
Projects []ProjectResponse `json:"projects"`
}{}
tflog.Trace(ctx, "listing projects", map[string]interface{}{
tflog.Info(ctx, "listing projects", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
Expand Down
2 changes: 1 addition & 1 deletion client/project_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Client) UpdateProject(ctx context.Context, projectID, teamID string, re
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating project", map[string]interface{}{
tflog.Info(ctx, "updating project", map[string]interface{}{
"url": url,
"payload": payload,
"shouldFetchEnvironmentVariables": shouldFetchEnvironmentVariables,
Expand Down
2 changes: 1 addition & 1 deletion client/project_update_production_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *Client) UpdateProductionBranch(ctx context.Context, request UpdateProdu
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Trace(ctx, "updating project production branch", map[string]interface{}{
tflog.Info(ctx, "updating project production branch", map[string]interface{}{
"url": url,
"payload": payload,
})
Expand Down
Loading

0 comments on commit 48910c3

Please sign in to comment.