-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update README with docs regarding App Oauth token usage
- Loading branch information
1 parent
6040eda
commit 950afbd
Showing
5 changed files
with
229 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,44 +40,197 @@ func TestProviderImpl(t *testing.T) { | |
var _ *schema.Provider = Provider() | ||
} | ||
|
||
func TestAccPagerDutyProviderScopedOauthTokenAuthentication_Basic(t *testing.T) { | ||
team := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
func TestAccPagerDutyProviderAuthMethods_Basic(t *testing.T) { | ||
username := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
email := fmt.Sprintf("%[email protected]", username) | ||
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
service := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
serviceUpdated := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckProviderScopedOauthTokenAuthentication(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPagerDutyTeamDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPagerDutyProviderAuthenticationConfig(team, "scoped_oauth_token"), | ||
Config: testAccCheckPagerDutyProviderAuthWithAPITokenConfig(username, email, escalationPolicy, service), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamExists("pagerduty_team.foo"), | ||
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "name", service), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "description", "foo"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "auto_resolve_timeout", "1800"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "acknowledgement_timeout", "1800"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "alert_creation", "create_incidents"), | ||
resource.TestCheckNoResourceAttr( | ||
"pagerduty_service.foo", "alert_grouping"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "alert_grouping_timeout", "null"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.#", "1"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), | ||
resource.TestCheckResourceAttrSet( | ||
"pagerduty_service.foo", "html_url"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "type", "service"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckPagerDutyProviderAuthenticationConfig(team, "use_app_credentials"), | ||
Config: testAccCheckPagerDutyProviderAuthWithAppOauthScopedTokenConfig(username, email, escalationPolicy, serviceUpdated), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyTeamExists("pagerduty_team.foo"), | ||
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "name", serviceUpdated), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "description", "bar"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "auto_resolve_timeout", "3600"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "acknowledgement_timeout", "3600"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "alert_creation", "create_incidents"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.#", "1"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"), | ||
resource.TestCheckResourceAttr( | ||
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPagerDutyProviderAuthenticationConfig(team, apiTokenType string) string { | ||
func testAccCheckPagerDutyProviderAuthWithAPITokenConfig(username, email, escalationPolicy, service string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_user" "foo" { | ||
name = "%s" | ||
email = "%s" | ||
color = "green" | ||
role = "user" | ||
job_title = "foo" | ||
description = "foo" | ||
} | ||
resource "pagerduty_escalation_policy" "foo" { | ||
name = "%s" | ||
description = "bar" | ||
num_loops = 2 | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.foo.id | ||
} | ||
} | ||
} | ||
resource "pagerduty_service" "foo" { | ||
name = "%s" | ||
description = "foo" | ||
auto_resolve_timeout = 1800 | ||
acknowledgement_timeout = 1800 | ||
escalation_policy = pagerduty_escalation_policy.foo.id | ||
alert_creation = "create_incidents" | ||
} | ||
`, username, email, escalationPolicy, service) | ||
} | ||
|
||
func testAccCheckPagerDutyProviderAuthWithAppOauthScopedTokenConfig(username, email, escalationPolicy, service string) string { | ||
return fmt.Sprintf(` | ||
provider "pagerduty" { | ||
api_token_type = "%[2]s" | ||
token = "" | ||
use_app_oauth_scoped_token {} | ||
} | ||
resource "pagerduty_team" "foo" { | ||
name = "%[1]s" | ||
description = "foo created with api token type of %[2]s" | ||
}`, team, apiTokenType) | ||
resource "pagerduty_user" "foo" { | ||
name = "%s" | ||
email = "%s" | ||
color = "green" | ||
role = "user" | ||
job_title = "foo" | ||
description = "foo" | ||
} | ||
resource "pagerduty_escalation_policy" "foo" { | ||
name = "%s" | ||
description = "bar" | ||
num_loops = 2 | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.foo.id | ||
} | ||
} | ||
} | ||
resource "pagerduty_service" "foo" { | ||
name = "%s" | ||
description = "bar" | ||
auto_resolve_timeout = 3600 | ||
acknowledgement_timeout = 3600 | ||
escalation_policy = pagerduty_escalation_policy.foo.id | ||
incident_urgency_rule { | ||
type = "constant" | ||
urgency = "high" | ||
} | ||
} | ||
`, username, email, escalationPolicy, service) | ||
} | ||
func testAccCheckPagerDutyProviderAuthWithMultipleMethodsConfig(username, email, escalationPolicy, service string) string { | ||
return fmt.Sprintf(` | ||
provider "pagerduty" { | ||
use_app_oauth_scoped_token {} | ||
} | ||
resource "pagerduty_user" "foo" { | ||
name = "%s" | ||
email = "%s" | ||
color = "green" | ||
role = "user" | ||
job_title = "foo" | ||
description = "foo" | ||
} | ||
resource "pagerduty_escalation_policy" "foo" { | ||
name = "%s" | ||
description = "bar" | ||
num_loops = 2 | ||
rule { | ||
escalation_delay_in_minutes = 10 | ||
target { | ||
type = "user_reference" | ||
id = pagerduty_user.foo.id | ||
} | ||
} | ||
} | ||
resource "pagerduty_service" "foo" { | ||
name = "%s" | ||
description = "bar" | ||
auto_resolve_timeout = 3600 | ||
acknowledgement_timeout = 3600 | ||
escalation_policy = pagerduty_escalation_policy.foo.id | ||
incident_urgency_rule { | ||
type = "constant" | ||
urgency = "high" | ||
} | ||
} | ||
`, username, email, escalationPolicy, service) | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
|
@@ -94,12 +247,6 @@ func testAccPreCheck(t *testing.T) { | |
} | ||
} | ||
|
||
func testAccPreCheckProviderScopedOauthTokenAuthentication(t *testing.T) { | ||
if v := os.Getenv("PAGERDUTY_ACC_PROVIDER_SCOPED_OAUTH"); v == "" { | ||
t.Skip("PAGERDUTY_ACC_PROVIDER_SCOPED_OAUTH not set. Skipping Provider Scoped Oauth-related test") | ||
} | ||
} | ||
|
||
// timeNowInLoc returns the current time in the given location. | ||
// If an error occurs when trying to load the location, we just return the current local time. | ||
func timeNowInLoc(name string) time.Time { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ terraform { | |
required_providers { | ||
pagerduty = { | ||
source = "pagerduty/pagerduty" | ||
version = "2.2.1" | ||
version = ">= 2.2.1" | ||
} | ||
} | ||
} | ||
|
@@ -52,12 +52,60 @@ resource "pagerduty_team_membership" "earline_engineering" { | |
|
||
The following arguments are supported: | ||
|
||
* `token` - (Required) The v2 authorization token. It can also be sourced from the PAGERDUTY_TOKEN environment variable. See [API Documentation](https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTUx-authentication)for more information. | ||
* `user_token` - (Optional) The v2 user level authorization token. It can also be sourced from the PAGERDUTY_USER_TOKEN environment variable. See [API Documentation](https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTUx-authentication) for more information. | ||
* `token` - (Optional) The v2 authorization token. It can also be sourced from the `PAGERDUTY_TOKEN` environment variable. See [API Documentation](https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTUx-authentication)for more information. | ||
* `user_token` - (Optional) The v2 user level authorization token. It can also be sourced from the `PAGERDUTY_USER_TOKEN` environment variable. See [API Documentation](https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTUx-authentication) for more information. | ||
* `use_app_oauth_scoped_token` - (Optional) Defines the configuration needed for making use of [App Oauth Scoped API token](https://developer.pagerduty.com/docs/e518101fde5f3-obtaining-an-app-o-auth-token) for authenticating API calls. | ||
* `skip_credentials_validation` - (Optional) Skip validation of the token against the PagerDuty API. | ||
* `service_region` - (Optional) The PagerDuty service region to use. Default to empty (uses US region). Supported value: `eu`. | ||
* `service_region` - (Optional) The PagerDuty service region to use. Default to empty (uses US region). Supported value: `eu`. This setting also affects configuration of `use_app_oauth_scoped_token` for setting Region of *App Oauth token credentials*. | ||
* `api_url_override` - (Optional) It can be used to set a custom proxy endpoint as PagerDuty client api url overriding `service_region` setup. | ||
|
||
The `use_app_oauth_scoped_token` block contains the following arguments: | ||
|
||
* `pd_client_id` - (Required) An identifier issued when the Scoped OAuth client was added to a PagerDuty App. It can also be sourced from the `PAGERDUTY_CLIENT_ID` environment variable. | ||
* `pd_client_secret` - (Required) A secret issued when the Scoped OAuth client was added to a PagerDuty App. It can also be sourced from the `PAGERDUTY_CLIENT_SECRET` environment variable. | ||
* `pd_subdomain` - (Required) Your PagerDuty account subdomain; i.e: If the *URL* shown by the Browser when you are in your PagerDuty account is some like: https://acme.pagerudty.com, then your PagerDuty subdomain is `acme`. It can also be sourced from the `PAGERDUTY_SUBDOMAIN` environment variable. | ||
|
||
## Example using App Oauth scoped token | ||
|
||
```hcl | ||
# Configure the PagerDuty provider | ||
terraform { | ||
required_providers { | ||
pagerduty = { | ||
source = "pagerduty/pagerduty" | ||
version = ">= 3.0.0" # Mind the supported Provider version | ||
} | ||
} | ||
} | ||
provider "pagerduty" { | ||
# Configure use of App Oauth scoped token | ||
use_app_oauth_scoped_token { | ||
pd_client_id = var.pd_client_id | ||
pd_client_secret = var.pd_client_secret | ||
pd_subdomain = var.pd_subdomain | ||
} | ||
} | ||
# Create a PagerDuty team | ||
resource "pagerduty_team" "engineering" { | ||
name = "Engineering" | ||
description = "All engineering" | ||
} | ||
# Create a PagerDuty user | ||
resource "pagerduty_user" "earline" { | ||
name = "Earline Greenholt" | ||
email = "[email protected]" | ||
} | ||
# Create a team membership | ||
resource "pagerduty_team_membership" "earline_engineering" { | ||
user_id = pagerduty_user.earline.id | ||
team_id = pagerduty_team.engineering.id | ||
} | ||
``` | ||
|
||
## Debugging Provider Output Using Logs | ||
|
||
In addition to the [log levels provided by Terraform](https://developer.hashicorp.com/terraform/internals/debugging), namely `TRACE`, `DEBUG`, `INFO`, `WARN`, and `ERROR` (in descending order of verbosity), the PagerDuty Provider introduces an extra level called `SECURE`. This level offers verbosity similar to Terraform's debug logging level, specifically for the output of API calls and HTTP request/response logs. The key difference is that API keys within the request's Authorization header will be obfuscated, revealing only the last four characters. An example is provided below: | ||
|