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

Remove file path trimming for root_directory settings #16

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 20, 2022
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
3 changes: 3 additions & 0 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ This will read a single file, providing metadata for use with a `vercel_deployme
## Example Usage

```terraform
# In this example, we are assuming that a single index.html file
# is being deployed. This file lives directly next to the terraform file.

data "vercel_file" "example" {
path = "index.html"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ output "project_id" {

- **build_command** (String) The build command for this project. If omitted, this value will be automatically detected.
- **dev_command** (String) The dev command for this project. If omitted, this value will be automatically detected.
- **environment** (Attributes List) A list of environment variables that should be configured for the project. (see [below for nested schema](#nestedatt--environment))
- **environment** (Attributes Set) A list of environment variables that should be configured for the project. (see [below for nested schema](#nestedatt--environment))
- **framework** (String) The framework that is being used for this project. If omitted, no framework is selected.
- **git_repository** (Attributes) The Git Repository that will be connected to the project. When this is defined, any pushes to the specified connected Git Repository will be automatically deployed. This requires the corresponding Vercel for [Github](https://vercel.com/docs/concepts/git/vercel-for-github), [Gitlab](https://vercel.com/docs/concepts/git/vercel-for-gitlab) or [Bitbucket](https://vercel.com/docs/concepts/git/vercel-for-bitbucket) plugins to be installed. (see [below for nested schema](#nestedatt--git_repository))
- **id** (String) The ID of this resource.
Expand Down
20 changes: 17 additions & 3 deletions docs/data-sources/project_directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@ This will recursively read files, providing metadata for use with a `vercel_depl
## Example Usage

```terraform
# In this example, we are assuming that a nextjs UI
# exists in a `ui` directory and any terraform exists in a `terraform` directory.
# E.g.
# ```
# ui/
# src/
# index.js
# package.json
# // etc...
# terraform/
# main.tf
# ```

data "vercel_project_directory" "example" {
path = "../ui"
}

data "vercel_project" "example" {
name = "my-project"
name = "my-awesome-project"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
path_prefix = data.vercel_project_directory.example.path
}
```

Expand Down
18 changes: 11 additions & 7 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,31 @@ Once the build step has completed successfully, a new, immutable deployment will

```terraform
# In this example, we are assuming that a nextjs UI
# exists in a `ui` directory alongside any terraform.
# exists in a `ui` directory and any terraform exists in a `terraform` directory.
# E.g.
# ```
# ui/
# src/
# next.config.js
# index.js
# package.json
# // etc...
# main.tf
# terraform/
# main.tf
# ```

data "vercel_project_directory" "example" {
path = "ui"
path = "../ui"
}

data "vercel_project" "example" {
name = "my-awesome-project"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
production = true
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
path_prefix = data.vercel_project_directory.example.path
production = true

environment = {
FOO = "bar"
Expand All @@ -66,6 +69,7 @@ resource "vercel_deployment" "example" {
### Optional

- **environment** (Map of String) A map of environment variable names to values. These are specific to a Deployment, and can also be configured on the `vercel_project` resource.
- **path_prefix** (String) If specified then the `path_prefix` will be stripped from the start of file paths as they are uploaded to Vercel. If this is omitted, then any leading `../`s will be stripped.
- **production** (Boolean) true if the deployment is a production deployment, meaning production aliases will be assigned.
- **project_settings** (Attributes) Project settings that will be applied to the deployment. (see [below for nested schema](#nestedatt--project_settings))
- **team_id** (String) The team ID to add the deployment to.
Expand Down
15 changes: 5 additions & 10 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ description: |-
Provides a Project resource.
A Project groups deployments and custom domains. To deploy on Vercel, you need to create a Project.
For more detailed information, please see the Vercel documentation https://vercel.com/docs/concepts/projects/overview.
-> The root_directory field behaves slightly differently to the Vercel website as
it allows upward path navigation (../). This is deliberately done so a vercel_file or vercel_project_directory
data source's path field can exactly match the root_directory.
~> If you are creating Deployments through terraform and intend to use both preview and production
deployments, you may not want to create a Project within the same terraform workspace as a Deployment.
deployments, you may wish to 'layer' your terraform, creating the Project with a different set of
terraform to your Deployment.
---

# vercel_project (Resource)
Expand All @@ -21,12 +19,9 @@ A Project groups deployments and custom domains. To deploy on Vercel, you need t

For more detailed information, please see the [Vercel documentation](https://vercel.com/docs/concepts/projects/overview).

-> The `root_directory` field behaves slightly differently to the Vercel website as
it allows upward path navigation (`../`). This is deliberately done so a `vercel_file` or `vercel_project_directory`
data source's `path` field can exactly match the `root_directory`.

~> If you are creating Deployments through terraform and intend to use both preview and production
deployments, you may not want to create a Project within the same terraform workspace as a Deployment.
deployments, you may wish to 'layer' your terraform, creating the Project with a different set of
terraform to your Deployment.

## Example Usage

Expand Down Expand Up @@ -80,7 +75,7 @@ resource "vercel_project" "example" {

- **build_command** (String) The build command for this project. If omitted, this value will be automatically detected.
- **dev_command** (String) The dev command for this project. If omitted, this value will be automatically detected.
- **environment** (Attributes List) A list of environment variables that should be configured for the project. (see [below for nested schema](#nestedatt--environment))
- **environment** (Attributes Set) A set of environment variables that should be configured for the project. (see [below for nested schema](#nestedatt--environment))
- **framework** (String) The framework that is being used for this project. If omitted, no framework is selected.
- **git_repository** (Attributes) The Git Repository that will be connected to the project. When this is defined, any pushes to the specified connected Git Repository will be automatically deployed. This requires the corresponding Vercel for [Github](https://vercel.com/docs/concepts/git/vercel-for-github), [Gitlab](https://vercel.com/docs/concepts/git/vercel-for-gitlab) or [Bitbucket](https://vercel.com/docs/concepts/git/vercel-for-bitbucket) plugins to be installed. (see [below for nested schema](#nestedatt--git_repository))
- **install_command** (String) The install command for this project. If omitted, this value will be automatically detected.
Expand Down
3 changes: 3 additions & 0 deletions examples/data-sources/vercel_file/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# In this example, we are assuming that a single index.html file
# is being deployed. This file lives directly next to the terraform file.

data "vercel_file" "example" {
path = "index.html"
}
Expand Down
20 changes: 17 additions & 3 deletions examples/data-sources/vercel_project_directory/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
# In this example, we are assuming that a nextjs UI
# exists in a `ui` directory and any terraform exists in a `terraform` directory.
# E.g.
# ```
# ui/
# src/
# index.js
# package.json
# // etc...
# terraform/
# main.tf
# ```

data "vercel_project_directory" "example" {
path = "../ui"
}

data "vercel_project" "example" {
name = "my-project"
name = "my-awesome-project"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
path_prefix = data.vercel_project_directory.example.path
}
17 changes: 10 additions & 7 deletions examples/resources/vercel_deployment/resource.tf
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# In this example, we are assuming that a nextjs UI
# exists in a `ui` directory alongside any terraform.
# exists in a `ui` directory and any terraform exists in a `terraform` directory.
# E.g.
# ```
# ui/
# src/
# next.config.js
# index.js
# package.json
# // etc...
# main.tf
# terraform/
# main.tf
# ```

data "vercel_project_directory" "example" {
path = "ui"
path = "../ui"
}

data "vercel_project" "example" {
name = "my-awesome-project"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
production = true
project_id = data.vercel_project.example.id
files = data.vercel_project_directory.example.files
path_prefix = data.vercel_project_directory.example.path
production = true

environment = {
FOO = "bar"
Expand Down
2 changes: 1 addition & 1 deletion vercel/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (r dataSourceProject) Read(ctx context.Context, req tfsdk.ReadDataSourceReq
return
}

result := convertResponseToProject(out, config.TeamID, types.String{})
result := convertResponseToProject(out, config.TeamID)
tflog.Trace(ctx, "read project", map[string]interface{}{
"team_id": result.TeamID.Value,
"project_id": result.ID.Value,
Expand Down
18 changes: 0 additions & 18 deletions vercel/file_path.go

This file was deleted.

6 changes: 6 additions & 0 deletions vercel/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ Once the build step has completed successfully, a new, immutable deployment will
Computed: true,
Type: types.StringType,
},
"path_prefix": {
Description: "If specified then the `path_prefix` will be stripped from the start of file paths as they are uploaded to Vercel. If this is omitted, then any leading `../`s will be stripped.",
Optional: true,
Type: types.StringType,
PlanModifiers: tfsdk.AttributePlanModifiers{tfsdk.RequiresReplace()},
},
"url": {
Description: "A unique URL that is automatically generated for a deployment.",
Computed: true,
Expand Down
25 changes: 19 additions & 6 deletions vercel/resource_deployment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Deployment struct {
ID types.String `tfsdk:"id"`
Production types.Bool `tfsdk:"production"`
ProjectID types.String `tfsdk:"project_id"`
PathPrefix types.String `tfsdk:"path_prefix"`
ProjectSettings *ProjectSettings `tfsdk:"project_settings"`
TeamID types.String `tfsdk:"team_id"`
URL types.String `tfsdk:"url"`
Expand Down Expand Up @@ -69,10 +70,6 @@ func (p *ProjectSettings) toRequest() map[string]interface{} {
if p.RootDirectory.Null {
res["rootDirectory"] = nil
}
if p.RootDirectory.Value != "" {
v := trimFilePath(p.RootDirectory.Value)
res["rootDirectory"] = &v
}

return res
}
Expand Down Expand Up @@ -118,13 +115,28 @@ func (d *Deployment) getFiles() ([]client.DeploymentFile, map[string]client.Depl
}
sha := sizeSha[1]

untrimmedFilename := filename
if d.PathPrefix.Unknown || d.PathPrefix.Null {
for strings.HasPrefix(filename, "../") {
filename = strings.TrimPrefix(filename, "../")
}
} else {
filename = strings.TrimPrefix(filename, d.PathPrefix.Value)
}
file := client.DeploymentFile{
File: trimFilePath(filename),
File: filename,
Sha: sha,
Size: size,
}
files = append(files, file)
filesBySha[sha] = file

/* The API can return a set of missing files. When this happens, we want the path name
* complete with the original, untrimmed prefix. */
filesBySha[sha] = client.DeploymentFile{
File: untrimmedFilename,
Sha: sha,
Size: size,
}
}
return files, filesBySha, nil
}
Expand Down Expand Up @@ -171,6 +183,7 @@ func convertResponseToDeployment(response client.DeploymentResponse, plan Deploy
URL: types.String{Value: response.URL},
Production: production,
Files: plan.Files,
PathPrefix: fillStringNull(plan.PathPrefix),
ProjectSettings: plan.ProjectSettings.fillNulls(),
}
}
43 changes: 38 additions & 5 deletions vercel/resource_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ func TestAcc_DeploymentWithProjectSettings(t *testing.T) {
})
}

func TestAcc_DeploymentWithUpwardRootDirectoryPath(t *testing.T) {
func TestAcc_DeploymentWithRootDirectoryOverride(t *testing.T) {
projectSuffix := acctest.RandString(16)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: noopDestroyCheck,
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNonRootDirectory(projectSuffix),
Config: testAccRootDirectoryOverride(projectSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccDeploymentExists("vercel_deployment.test", ""),
resource.TestCheckResourceAttr("vercel_deployment.test", "production", "true"),
Expand All @@ -143,6 +143,23 @@ func TestAcc_DeploymentWithUpwardRootDirectoryPath(t *testing.T) {
})
}

func TestAcc_DeploymentWithPathPrefix(t *testing.T) {
projectSuffix := acctest.RandString(16)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: noopDestroyCheck,
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccRootDirectoryWithPathPrefix(projectSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccDeploymentExists("vercel_deployment.test", ""),
),
},
},
})
}

func testAccDeployment(t *testing.T, tid string) {
projectSuffix := acctest.RandString(16)
extraConfig := ""
Expand Down Expand Up @@ -197,11 +214,10 @@ resource "vercel_deployment" "test" {
`, projectSuffix, projectExtras, deploymentExtras)
}

func testAccNonRootDirectory(projectSuffix string) string {
func testAccRootDirectoryOverride(projectSuffix string) string {
return fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-deployment-%s"
root_directory = "../foo"
}

data "vercel_file" "index" {
Expand All @@ -213,7 +229,24 @@ resource "vercel_deployment" "test" {
files = data.vercel_file.index.file
production = true
project_settings = {
root_directory = "../vercel/example"
root_directory = "vercel/example"
}
}`, projectSuffix)
}

func testAccRootDirectoryWithPathPrefix(projectSuffix string) string {
return fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-deployment-%s"
}

data "vercel_file" "index" {
path = "../vercel/example/index.html"
}

resource "vercel_deployment" "test" {
project_id = vercel_project.test.id
files = data.vercel_file.index.file
path_prefix = "../vercel/example"
}`, projectSuffix)
}
Loading