Skip to content

Commit

Permalink
Add support for Jira Cloud integration
Browse files Browse the repository at this point in the history
Add data source for Jira Cloud account mapping
Add resource for Jira Cloud account mapping rule
  • Loading branch information
cjgajard committed Oct 22, 2024
1 parent ff3b756 commit fccb67c
Show file tree
Hide file tree
Showing 10 changed files with 1,400 additions and 4 deletions.
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)
}
19 changes: 15 additions & 4 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 All @@ -82,8 +85,12 @@ func (p *Provider) Resources(_ context.Context) [](func() resource.Resource) {
}
}

func New() *Provider {
return &Provider{}
func New(opt ...string) *Provider {
p := &Provider{}
if len(opt) > 0 {
p.apiURLOverride = opt[0]
}
return p
}

func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
Expand Down Expand Up @@ -122,6 +129,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

0 comments on commit fccb67c

Please sign in to comment.