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

Aliases: Add aliases deployment #32

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

Closed
wants to merge 6 commits into from

Conversation

WCMinor
Copy link
Contributor

@WCMinor WCMinor commented May 12, 2022

This is a feature that we use and so we implemented it. It is supported by the CLI (docs here) but sadly it is not documented on the Vercel's API specification.

We reverse engineered the API calls from the CLI code and it works like a charm.

If you feel like in adding this feature despite the lack of official API support then we'll create acceptance tests and update the PR.

Thanks to @raymondbutcher for helping out with this.

@WCMinor WCMinor requested a review from dglsparsons as a code owner May 12, 2022 14:40
@dglsparsons
Copy link
Collaborator

dglsparsons commented May 13, 2022

How does this look in the Vercel UI?

And also, what's your use case for it? :)

I think there's the ability to specify aliases on a per deployment basis via the create-deployment endpoint too. But i'd intentionally omitted these as they aren't really used and may not play particularly nicely with existing patterns/practices.

My initial instinct would be to omit this, preferring project domains to be configured (potentially even on a per-branch basis?) instead, as that's more cohesive across the entirety of Vercel, but happy to hear your thoughts.

@WCMinor
Copy link
Contributor Author

WCMinor commented May 13, 2022

Use case

We do certain deployments from a central CICD service, those deployments are not necessarily based on specific branches. For example, we deploy many times from main or master. We use this for both automated full app (backend and frontend) e2e tests and for backend engineers deploying the frontend for testing the backend.

In both cases we need a custom subdomain to point to the deployed preview environment. Such name has to be provided by our CI (terraform) because it has to match the naming convention for the rest of the app.

The name cannot be based on the branch name, which is the automatic alias that Vercel provides.

About create-deployment aliases method

I've tested that, it does nothing. Or at least I could not make it work.

Screeshot of how does it look in the Vercel UI

Screenshot 2022-05-13 at 17 21 36
The first alias is the one we're associating using terraform.

Personal note

I'm well aware that this functionality is not for a standard Vercel use case but I think the Terraform provider should provide all possible use cases. Actually Vercel is typically not deployed using terraform, yet, here we are ;)

Dario Ferrer added 3 commits May 16, 2022 14:00
@WCMinor
Copy link
Contributor Author

WCMinor commented May 25, 2022

Just an update on this. Vercel released the Alias api documentation. And so we adapted our code to embrace it.

@dglsparsons
Copy link
Collaborator

Excellent. Apologies for the slow review on this one. I'm hoping to have a look at it soon :)

@WCMinor
Copy link
Contributor Author

WCMinor commented May 25, 2022

No apologise needed and no rush here at all please, thanks for all your help @dglsparsons

Copy link
Collaborator

@dglsparsons dglsparsons left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this. The code mostly looks perfect! It's missing some acceptance tests, but I'd also like to take a deeper look at the 204 status code if you don't mind me holding off on this change in the meantime.

Comment on lines +28 to +33
// Add query parameters
q := req.URL.Query()
if teamID != "" {
q.Add("teamId", teamID)
}
req.URL.RawQuery = q.Encode()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that the delete endpoint does the teamId parameter differently to the create/get endpoints. 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. I think this file was copied/modified from deployment_delete.go which also uses req.URL.Query to add the parameter. So we picked the 1 inconsistent file to use as our starting point. Every other file uses fmt.Sprintf to do the same thing.

terraform-provider-vercel % grep teamId client/* | grep fmt.Sprintf
client/alias_create.go:         url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/alias_get.go:            url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/deployment_create.go:            url = fmt.Sprintf("%s&teamId=%s", url, teamID)
client/deployment_get.go:               url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/environment_variable_upsert.go:          url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/environment_variables_delete.go:         url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/environment_variables_get.go:            url = fmt.Sprintf("%s&teamId=%s", url, teamID)
client/project_create.go:               url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_delete.go:               url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_domain_create.go:                url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_domain_delete.go:                url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_domain_get.go:           url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_domain_update.go:                url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_get.go:          url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/project_list.go:         url = fmt.Sprintf("%s&teamId=%s", url, teamID)
client/project_update.go:               url = fmt.Sprintf("%s?teamId=%s", url, teamID)
client/team_get.go:             url = fmt.Sprintf("%s?teamId=%s", url, teamID)
terraform-provider-vercel % grep teamId client/* | grep -v fmt.Sprintf
client/alias_delete.go:         q.Add("teamId", teamID)
client/deployment_delete.go:            q.Add("teamId", teamID)

Comment on lines +61 to +64
if resp.StatusCode == 204 {
//204 means "no content", we are treating it as an error
return errors.New("no content")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can do this as other API endpoints definitely return a 204 for certain success criteria. I'm currently looking into our API implementation for the GET alias endpoint though to see if it's something I can change though. It may block this PR in the meantime though, so apologies for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we were discussing this 204 thing internally, It feels quite odd (almost like an API bug) to respond 204 for a not found element. I did not have a proper look at the rest of the endpoints but totally agree with you, if 204 != 404 then we should put there a bit more logic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe here, it behaves as a 404. I think it's a mistake in our API though. I think it's something I'd be able to correct in the API though, so we get a 404, as this makes much more sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be ideal.

docs/index.md Outdated
@@ -24,8 +24,8 @@ Use the navigation to the left to read about the available resources.
terraform {
required_providers {
vercel = {
source = "vercel/vercel"
version = "~> 0.1"
source = "streetteam/vercel"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice try ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D it got there by mistake

Comment on lines +43 to +46
"uid": {
Computed: true,
Type: types.StringType,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for provider consistency (and being in line with what the older terraform SDKs expect), we should expose this as id rather than uid.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at all of the API endpoints:

It is consistently called uid in the responses, so we thought it best to stick with that. Are you sure you want it named differently in the Terraform provider?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the testing framework data_sources have to have an id field, and it's a semi-expected pattern that resources have an id.
For projects, we have id, Deployments - id, Teams - id. I think it's bizarre that this would have uid as the field name.

The aliases API is quite an old API in terms of Vercel's history. I suspect that is why it refers to uid (and the API shouldn't change for backwards compatibility), but we can expose it in a more consistent way.

PlanModifiers: tfsdk.AttributePlanModifiers{tfsdk.RequiresReplace()},
Type: types.StringType,
},
"uid": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a Description field on this, which doesn't look so nice in the generated documentation.

Copy link

@raymondbutcher raymondbutcher May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other resources in this provider don't have a Description field for the id attribute. If I rename this field to id (as you requested in the other comment) the document generator adds the description automatically ("The ID of this resource."). Are you happy to leave the description out here, to stay consistent with the other resources?

Edit: as mentioned in the other comment, I'm not sure if we should rename it. I suppose if we rename to id then we should leave out the description here, but if we keep it as uid then we should add a description.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, i think omitting it would be totally fine, in line with the other resources in the provider :).

@WCMinor
Copy link
Contributor Author

WCMinor commented Jun 6, 2022

@dglsparsons feel free to close this PR in favor of #41

@dglsparsons
Copy link
Collaborator

Closing in favour of #41

@dglsparsons dglsparsons closed this Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants