Skip to content
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

Add service_region support #384

Merged

Conversation

jbfavre
Copy link
Contributor

@jbfavre jbfavre commented Sep 9, 2021

In order to be able to use Pagerduty provider to manage EU platform deployment, we need to be able to define a custom API URL 0

As gar as I can tell, heimweh/go-pagerduty client use baseURL which is already configurable.
I tried to pass this configuration option when setting up heimweh/go-pagerduty.

@stmcallister please be indulgent, this is one of my first contribution in Go language 😉
I surely missed many things.

@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from f4c46f1 to 8aab478 Compare September 9, 2021 13:31
@jbfavre jbfavre changed the title Add support for provider baseurl new configuration option Make PagerDuty api.pagerduty.com and app.pagerduty.com configurable Sep 9, 2021
@jbfavre jbfavre changed the title Make PagerDuty api.pagerduty.com and app.pagerduty.com configurable Make PagerDuty URLs api.pagerduty.com and app.pagerduty.com configurable Sep 9, 2021
@stmcallister
Copy link
Contributor

Well done! This project was my first using the go language as well! Code-wise, I think it's great. However, I think I want to go a slightly different direction. Rather than having folks set the URLs directly I think we should just provide a service_region field to the provider tag. Then the provider will figure out which URLs to use. At least that's my thoughts right now.

From a user perspective, do you think that would be a cleaner approach?

@jbfavre
Copy link
Contributor Author

jbfavre commented Sep 10, 2021

I understand and agree that having only one configuration option would help to avoid misbehaviours.

think we should just provide a service_region field to the provider tag

Do you mean, like:

provider "pagerduty" {
  token          = xXxXxX
  service_region = "EU"
}

The only caveat I can think of is that having "hard coded" URLs would require a provider release for each new service region opened. It's not that I expect many service region opening, but still I think we should look for the easiest solution.

Maybe dynamically building URLs would help. Kind of:

if service_region == "" or service_region == "us"
then
  ApiUrl = "https://api.pagerduty.com"
  AppUrl = "https://app.pagerduty.com"
else
  ApiUrl = "https://api." + service_region + ".pagerduty.com"
  AppUrl = "https://app." + service_region + ".pagerduty.com"

@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from b712657 to 9e926e0 Compare September 13, 2021 07:40
@jbfavre jbfavre changed the title Make PagerDuty URLs api.pagerduty.com and app.pagerduty.com configurable Add service_region support Sep 13, 2021
Copy link
Contributor

@stmcallister stmcallister left a comment

Choose a reason for hiding this comment

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

Great work! Thank you for this! Just a couple changes before approving.

pagerduty/provider.go Outdated Show resolved Hide resolved
@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from 40dc901 to f6fd5f3 Compare September 29, 2021 07:10
@stmcallister
Copy link
Contributor

stmcallister commented Oct 21, 2021

Hey @jbfavre ! When I run the acceptance tests for the SlackConnection it's trying to POST to https://api.pagerduty.com rather than https://app.pagerduty.com.

Will you update your branch with the latest master and let me know if your SlackConnecting acceptance tests pass? Thanks!

@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from f6fd5f3 to 09dcb2b Compare October 21, 2021 08:04
@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 21, 2021

Hey @jbfavre ! When I run the acceptance tests for the SlackConnection it's trying to POST to https://api.pagerduty.com rather than https://app.pagerduty.com.

Will you update your branch with the latest master and let me know if your SlackConnecting acceptance tests pass? Thanks!

Hello @stmcallister
I'm currently trying to run the acceptance tests. Will keep you updated

@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 21, 2021

Hey @jbfavre ! When I run the acceptance tests for the SlackConnection it's trying to POST to https://api.pagerduty.com rather than https://app.pagerduty.com.

Will you update your branch with the latest master and let me know if your SlackConnecting acceptance tests pass? Thanks!

Well, I'm pretty sure I'm missing something, but I can't even run acceptance tests on master 🤔
I'm using:

  • a dev PagerDuty account
  • a free slack account
  • PagerDuty has been installed in Slack
  • Pagerduty and Slack accounts are linked
  • I've a User PagerDuty Token defined with export PAGERDUTY_USER_TOKEN=xxx
  • I've a Global PagerDuty Token defined with export PAGERDUTY_TOKEN=xxx
  • Slack Workspace ID is declared with export SLACK_CONNECTION_WORKSPACE_ID=
  • Slack Channel ID is statically declared in pagerduty/resource_pagerduty_slack_connection_test.go

Any advice welcome 😬

@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 21, 2021

OK, made some progress.

  1. you must enable Incident Priority in Account Settings > Incident Priorities
  2. make testacc runs, slowly, for v2.0.0 tag, with "only" 2 errors related to Slack tests:
=== RUN   TestAccPagerDutySlackConnection_import
    import_pagerduty_slack_connection_test.go:18: Step 1/3 error: Error running apply: exit status 1
        
        Error: POST API call to https://api.pagerduty.com/users failed 400 Bad Request. Code: 2001, Errors: [You have hit your regular user limit], Message: Invalid Input Provided
        
          with pagerduty_user.foo,
          on terraform_plugin_test.tf line 2, in resource "pagerduty_user" "foo":
           2: 	resource "pagerduty_user" "foo" {
        
--- FAIL: TestAccPagerDutySlackConnection_import (2.27s)
=== RUN   TestAccPagerDutySlackConnectionTeam_import
--- PASS: TestAccPagerDutySlackConnectionTeam_import (8.41s)

and

=== RUN   TestAccPagerDutySlackConnection_Basic
    resource_pagerduty_slack_connection_test.go:29: Step 1/2 error: Error running apply: exit status 1
        
        Error: POST API call to https://api.pagerduty.com/users failed 400 Bad Request. Code: 2001, Errors: [You have hit your regular user limit], Message: Invalid Input Provided
        
          with pagerduty_user.foo,
          on terraform_plugin_test.tf line 2, in resource "pagerduty_user" "foo":
           2: 	resource "pagerduty_user" "foo" {
        
--- FAIL: TestAccPagerDutySlackConnection_Basic (2.44s)
=== RUN   TestAccPagerDutySlackConnection_Team
--- PASS: TestAccPagerDutySlackConnection_Team (11.32s)
=== RUN   TestAccPagerDutySlackConnection_Envar
--- PASS: TestAccPagerDutySlackConnection_Envar (6.83s)

However, I got plenty of other errors, like:

Error: POST API call to https://api.pagerduty.com/users failed 400 Bad Request. Code: 2001, Errors: [You have hit your regular user limit], Message: Invalid Input Provided

Don't know whether it's because I'm using a dev account a not a "real" one. But I'm relunctant to use our production setup to do my tests.

Now trying to run tests for my branch

@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 21, 2021

Indeed, I broke some tests in my branch 🤔

=== RUN   TestAccPagerDutySlackConnectionTeam_import
    import_pagerduty_slack_connection_test.go:42: Step 1/2 error: Error running apply: exit status 1
        
        Error: POST API call to https://api.pagerduty.com/integration-slack/workspaces/T02JBV9PANA/connections failed 404 Not Found. Code: 2100, Errors: <nil>, Message: Not Found
        
          with pagerduty_slack_connection.foo,
          on terraform_plugin_test.tf line 5, in resource "pagerduty_slack_connection" "foo":
           5: 		resource "pagerduty_slack_connection" "foo" {
        
--- FAIL: TestAccPagerDutySlackConnectionTeam_import (125.60s)

Ill have an look at it 👀

@stmcallister
Copy link
Contributor

stmcallister commented Oct 21, 2021

@jbfavre Yeah, the errors on users are because you're using a Developer Account. I'm only worried about the TestAccPagerDutySlackConnection tests passing on your end. I can run the other tests on my account. You can run your tests with this command. Thanks for taking a look!

terraform-provider-pagerduty $ TF_ACC=1 go test -run "TestAccPagerDutySlackConnection" ./pagerduty -v -timeout 120m

@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 22, 2021

@jbfavre Yeah, the errors on users are because you're using a Developer Account. I'm only worried about the TestAccPagerDutySlackConnection tests passing on your end. I can run the other tests on my account. You can run your tests with this command. Thanks for taking a look!

terraform-provider-pagerduty $ TF_ACC=1 go test -run "TestAccPagerDutySlackConnection" ./pagerduty -v -timeout 120m

OK, found something. Breaking change has been introduced in b1221fd
I misunderstood how it worked. Will fix this ASAP

@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from 09dcb2b to 1583820 Compare October 22, 2021 18:32
@jbfavre jbfavre force-pushed the make_provider_ready_for_eu_platform branch from 1583820 to 0a89e0c Compare October 22, 2021 18:41
@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 22, 2021

Finaly found a way to do it.

Problem was: provider creates a pagerduty.Client at startup. Multi-region is easy to support.
Slack resources create a pagerduty.Client when needed. At this point, we can't access anymore the initial Config structure in config.go. Therefore, it's impossible to reuse the multi-region implementation.
I choosed to:

  1. Change the way provider get initialized. It now return the Config structure which in turn can be reused later.
  2. Update slack resources to call Config.SlackClient to get a relevant pagerduty.Client (cons: it duplicates most of the config.Client code)
  3. Update all other resources which have now to create a pagerduty client when needed (as slack resources already did).
  4. Update tests

Only remaining question I have, is: creating a new pagerduty client for each resource might not be that efficient in terms of resource consumption. Maybe we should try to initiate only 2 (default API & Slack one).
What do you think @stmcallister ?

I've checked that all acceptance tests run fine (except user's calls)

@stmcallister
Copy link
Contributor

@jbfavre 🤔 I think this is the right way to go, but let me put some thought into it through the weekend. One nitpick that I know I'd want changed at this point is the naming for the token_user field. Will you change those to user_token through out the code? Thanks for your work on this feature!

@jbfavre
Copy link
Contributor Author

jbfavre commented Oct 25, 2021

@jbfavre thinking I think this is the right way to go, but let me put some thought into it through the weekend. One nitpick that I know I'd want changed at this point is the naming for the token_user field. Will you change those to user_token through out the code? Thanks for your work on this feature!

token_user has been renamed to user_token (and TokenUser to UserToken).

@stmcallister stmcallister merged commit 8e9c219 into PagerDuty:master Oct 25, 2021
@jbfavre jbfavre deleted the make_provider_ready_for_eu_platform branch October 26, 2021 06:13
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.

2 participants