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 support for Jira Cloud integration #942

Merged
merged 1 commit into from
Nov 4, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,5 @@ PAGERDUTY_ACC_SCHEDULE_USED_BY_EP_W_1_LAYER=1 make testacc TESTARGS="-run PagerD
| `PAGERDUTY_ACC_INCIDENT_CUSTOM_FIELDS` | Custom Fields |
| `PAGERDUTY_ACC_LICENSE_NAME` | Licenses |
| `PAGERDUTY_ACC_SCHEDULE_USED_BY_EP_W_1_LAYER` | Schedule |
| `PAGERDUTY_ACC_JIRA_ACCOUNT_MAPPING_ID` | Set Jira account-mapping ID to use during acceptance tests |
| `PAGERDUTY_ACC_EXTERNAL_PROVIDER_VERSION` | Modifies the version used to compare plans between sdkv2 and framework implementations. Default `~> 3.6`. |
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/PagerDuty/terraform-provider-pagerduty
go 1.20

require (
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241104145658-2a0050d437ac
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.6.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88 h1:y/icahuphX4xGMW4nLN+Bl4MbFUU4rEA9spwgcPIDJk=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241002154647-8ceedfd04d88/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241025123418-0c3fc7303be4 h1:egkL94FTYFe3GfnJMpY2aAj6tA2wXXYRfGPJPfC90BQ=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241025123418-0c3fc7303be4/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241104145658-2a0050d437ac h1:GtkaaoQH3DSWt9hxC71pAp31pyhKFxU1nBfa5iZWecE=
github.com/PagerDuty/go-pagerduty v1.8.1-0.20241104145658-2a0050d437ac/go.mod h1:ilimTqwHSBjmvKeYA/yayDBZvzf/CX4Pwa9Qbhekzok=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX7IL/m9Y5LO+KQYv+t1CQOiFe6+SV2J7bE=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package pagerduty

import (
"context"
"fmt"
"log"
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

type dataSourceJiraCloudAccountMapping struct{ client *pagerduty.Client }

var _ datasource.DataSourceWithConfigure = (*dataSourceJiraCloudAccountMapping)(nil)

func (*dataSourceJiraCloudAccountMapping) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "pagerduty_jira_cloud_account_mapping"
}

func (*dataSourceJiraCloudAccountMapping) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true},
"base_url": schema.StringAttribute{Computed: true},
"subdomain": schema.StringAttribute{Required: true},
},
}
}

func (d *dataSourceJiraCloudAccountMapping) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...)
}

func (d *dataSourceJiraCloudAccountMapping) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
log.Println("[INFO] Reading PagerDuty jira cloud account mapping")

var searchSubdomain types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("subdomain"), &searchSubdomain)...)
if resp.Diagnostics.HasError() {
return
}

var found *pagerduty.JiraCloudAccountsMapping
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
response, err := d.client.ListJiraCloudAccountsMappings(ctx, pagerduty.ListJiraCloudAccountsMappingsOptions{})
if err != nil {
if util.IsBadRequestError(err) {
return retry.NonRetryableError(err)
}
return retry.RetryableError(err)
}

for _, m := range response.AccountsMappings {
if m.PagerDutyAccount.Subdomain == searchSubdomain.ValueString() {
found = &m
break
}
}
return nil
})
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty jira cloud account mapping with subdomain %s", searchSubdomain),
err.Error(),
)
return
}

if found == nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to locate any jira cloud account mapping with the subdomain %s", searchSubdomain),
"",
)
return
}

model := dataSourceJiraCloudAccountMappingModel{
ID: types.StringValue(found.ID),
BaseURL: types.StringValue(found.JiraCloudAccount.BaseURL),
Subdomain: types.StringValue(found.PagerDutyAccount.Subdomain),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

type dataSourceJiraCloudAccountMappingModel struct {
ID types.String `tfsdk:"id"`
BaseURL types.String `tfsdk:"base_url"`
Subdomain types.String `tfsdk:"subdomain"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pagerduty

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourcePagerDutyJiraCloudAccountMapping_Basic(t *testing.T) {
subdomain := os.Getenv("PAGERDUTY_SUBDOMAIN")
if subdomain == "" {
t.Skip("Missing env variable PAGERDUTY_SUBDOMAIN")
return
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyJiraCloudAccountMappingConfig(subdomain),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.pagerduty_jira_cloud_account_mapping.by_subdomain", "subdomain", subdomain),
resource.TestCheckResourceAttrSet("data.pagerduty_jira_cloud_account_mapping.by_subdomain", "id"),
resource.TestCheckResourceAttrSet("data.pagerduty_jira_cloud_account_mapping.by_subdomain", "base_url"),
),
},
},
})
}

func testAccDataSourcePagerDutyJiraCloudAccountMappingConfig(subdomain string) string {
return fmt.Sprintf(`
data "pagerduty_jira_cloud_account_mapping" "by_subdomain" {
subdomain = "%s"
}
`, subdomain)
}
11 changes: 9 additions & 2 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type Provider struct {
client *pagerduty.Client
client *pagerduty.Client
apiURLOverride string
}

func (p *Provider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -56,8 +57,9 @@ func (p *Provider) DataSources(_ context.Context) [](func() datasource.DataSourc
func() datasource.DataSource { return &dataSourceBusinessService{} },
func() datasource.DataSource { return &dataSourceExtensionSchema{} },
func() datasource.DataSource { return &dataSourceIntegration{} },
func() datasource.DataSource { return &dataSourceLicense{} },
func() datasource.DataSource { return &dataSourceJiraCloudAccountMapping{} },
func() datasource.DataSource { return &dataSourceLicenses{} },
func() datasource.DataSource { return &dataSourceLicense{} },
func() datasource.DataSource { return &dataSourcePriority{} },
func() datasource.DataSource { return &dataSourceService{} },
func() datasource.DataSource { return &dataSourceStandardsResourceScores{} },
Expand All @@ -74,6 +76,7 @@ func (p *Provider) Resources(_ context.Context) [](func() resource.Resource) {
func() resource.Resource { return &resourceBusinessService{} },
func() resource.Resource { return &resourceExtensionServiceNow{} },
func() resource.Resource { return &resourceExtension{} },
func() resource.Resource { return &resourceJiraCloudAccountMappingRule{} },
func() resource.Resource { return &resourceServiceDependency{} },
func() resource.Resource { return &resourceTagAssignment{} },
func() resource.Resource { return &resourceTag{} },
Expand Down Expand Up @@ -122,6 +125,10 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
InsecureTls: insecureTls,
}

if config.APIURLOverride == "" && p.apiURLOverride != "" {
config.APIURLOverride = p.apiURLOverride
}

if !args.UseAppOauthScopedToken.IsNull() {
blockList := []UseAppOauthScopedToken{}
resp.Diagnostics.Append(args.UseAppOauthScopedToken.ElementsAs(ctx, &blockList, false)...)
Expand Down
Loading
Loading