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

Migrate webhooks to Plugin Framework #1087

Merged
merged 17 commits into from
Oct 1, 2024
Merged
Changes from 1 commit
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
Next Next commit
Refactoring cleanup
And start of migrating artifact webhook
alexhung committed Sep 26, 2024
commit 116e4a2135b1c6f5d1360f0a0b00411d1c10b9bc
2 changes: 2 additions & 0 deletions pkg/artifactory/provider/framework.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import (
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/replication"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/security"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/user"
"github.com/jfrog/terraform-provider-artifactory/v12/pkg/artifactory/resource/webhook"
"github.com/jfrog/terraform-provider-shared/client"
"github.com/jfrog/terraform-provider-shared/util"
validatorfw_string "github.com/jfrog/terraform-provider-shared/validator/fw/string"
@@ -221,6 +222,7 @@ func (p *ArtifactoryProvider) Resources(ctx context.Context) []func() resource.R
replication.NewLocalRepositorySingleReplicationResource,
replication.NewLocalRepositoryMultiReplicationResource,
replication.NewRemoteRepositoryReplicationResource,
webhook.NewArtifactWebhookResource,
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package webhook

import "github.com/hashicorp/terraform-plugin-framework/resource"

var _ resource.Resource = &ArtifactWebhookResource{}

func NewArtifactWebhookResource() resource.Resource {
return &ArtifactWebhookResource{
TypeName: "artifactory_artifact_webhook",
}
}
Original file line number Diff line number Diff line change
@@ -271,7 +271,7 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
SetPathParam("webhookKey", data.Id()).
SetResult(&webhook).
SetError(&artifactoryError).
Get(WhUrl)
Get(WebhookURL)

if err != nil {
return diag.FromErr(err)
@@ -308,7 +308,7 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
SetBody(webhook).
SetError(&artifactoryError).
AddRetryCondition(retryOnProxyError).
Post(webhooksUrl)
Post(webhooksURL)
if err != nil {
return diag.FromErr(err)
}
@@ -336,7 +336,7 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
SetBody(webhook).
SetError(&artifactoryError).
AddRetryCondition(retryOnProxyError).
Put(WhUrl)
Put(WebhookURL)
if err != nil {
return diag.FromErr(err)
}
@@ -357,7 +357,7 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
resp, err := m.(util.ProviderMetadata).Client.R().
SetPathParam("webhookKey", data.Id()).
SetError(&artifactoryError).
Delete(WhUrl)
Delete(WebhookURL)

if err != nil {
return diag.FromErr(err)
202 changes: 108 additions & 94 deletions pkg/artifactory/resource/webhook/resource_artifactory_webhook.go
Original file line number Diff line number Diff line change
@@ -18,34 +18,54 @@ import (
"golang.org/x/exp/slices"
)

const (
webhooksURL = "/event/api/v1/subscriptions"
WebhookURL = "/event/api/v1/subscriptions/{webhookKey}"

ArtifactLifecycleType = "artifact_lifecycle"
ArtifactPropertyType = "artifact_property"
ArtifactType = "artifact"
ArtifactoryReleaseBundleType = "artifactory_release_bundle"
BuildType = "build"
DestinationType = "destination"
DistributionType = "distribution"
DockerType = "docker"
ReleaseBundleType = "release_bundle"
ReleaseBundleV2Type = "release_bundle_v2"
ReleaseBundleV2PromotionType = "release_bundle_v2_promotion"
UserType = "user"
)

const currentSchemaVersion = 2

var TypesSupported = []string{
"artifact",
"artifact_property",
"docker",
"build",
"release_bundle",
"distribution",
"artifactory_release_bundle",
"destination",
"user",
"release_bundle_v2",
"release_bundle_v2_promotion",
"artifact_lifecycle",
ArtifactLifecycleType,
ArtifactPropertyType,
// ArtifactType,
ArtifactoryReleaseBundleType,
BuildType,
DestinationType,
DistributionType,
DockerType,
ReleaseBundleType,
ReleaseBundleV2Type,
ReleaseBundleV2PromotionType,
UserType,
}

var DomainEventTypesSupported = map[string][]string{
"artifact": {"deployed", "deleted", "moved", "copied", "cached"},
"artifact_property": {"added", "deleted"},
"docker": {"pushed", "deleted", "promoted"},
"build": {"uploaded", "deleted", "promoted"},
"release_bundle": {"created", "signed", "deleted"},
"distribution": {"distribute_started", "distribute_completed", "distribute_aborted", "distribute_failed", "delete_started", "delete_completed", "delete_failed"},
"artifactory_release_bundle": {"received", "delete_started", "delete_completed", "delete_failed"},
"destination": {"received", "delete_started", "delete_completed", "delete_failed"},
"user": {"locked"},
"release_bundle_v2": {"release_bundle_v2_started", "release_bundle_v2_failed", "release_bundle_v2_completed"},
"release_bundle_v2_promotion": {"release_bundle_v2_promotion_completed", "release_bundle_v2_promotion_failed", "release_bundle_v2_promotion_started"},
"artifact_lifecycle": {"archive", "restore"},
ArtifactType: {"deployed", "deleted", "moved", "copied", "cached"},
ArtifactPropertyType: {"added", "deleted"},
DockerType: {"pushed", "deleted", "promoted"},
BuildType: {"uploaded", "deleted", "promoted"},
ReleaseBundleType: {"created", "signed", "deleted"},
DistributionType: {"distribute_started", "distribute_completed", "distribute_aborted", "distribute_failed", "delete_started", "delete_completed", "delete_failed"},
ArtifactoryReleaseBundleType: {"received", "delete_started", "delete_completed", "delete_failed"},
DestinationType: {"received", "delete_started", "delete_completed", "delete_failed"},
UserType: {"locked"},
ReleaseBundleV2Type: {"release_bundle_v2_started", "release_bundle_v2_failed", "release_bundle_v2_completed"},
ReleaseBundleV2PromotionType: {"release_bundle_v2_promotion_completed", "release_bundle_v2_promotion_failed", "release_bundle_v2_promotion_started"},
ArtifactLifecycleType: {"archive", "restore"},
}

type BaseParams struct {
@@ -80,12 +100,6 @@ type KeyValuePair struct {
Value string `json:"value"`
}

const webhooksUrl = "/event/api/v1/subscriptions"

const WhUrl = webhooksUrl + "/{webhookKey}"

const currentSchemaVersion = 2

var unpackKeyValuePair = func(keyValuePairs map[string]interface{}) []KeyValuePair {
var kvPairs []KeyValuePair
for key, value := range keyValuePairs {
@@ -109,64 +123,64 @@ var packKeyValuePair = func(keyValuePairs []KeyValuePair) map[string]interface{}
}

var domainCriteriaLookup = map[string]interface{}{
"artifact": RepoWebhookCriteria{},
"artifact_property": RepoWebhookCriteria{},
"docker": RepoWebhookCriteria{},
"build": BuildWebhookCriteria{},
"release_bundle": ReleaseBundleWebhookCriteria{},
"distribution": ReleaseBundleWebhookCriteria{},
"artifactory_release_bundle": ReleaseBundleWebhookCriteria{},
"destination": ReleaseBundleWebhookCriteria{},
"user": EmptyWebhookCriteria{},
"release_bundle_v2": ReleaseBundleV2WebhookCriteria{},
"release_bundle_v2_promotion": ReleaseBundleV2PromotionWebhookCriteria{},
"artifact_lifecycle": EmptyWebhookCriteria{},
ArtifactType: RepoWebhookCriteria{},
ArtifactPropertyType: RepoWebhookCriteria{},
DockerType: RepoWebhookCriteria{},
BuildType: BuildWebhookCriteria{},
ReleaseBundleType: ReleaseBundleWebhookCriteria{},
DistributionType: ReleaseBundleWebhookCriteria{},
ArtifactoryReleaseBundleType: ReleaseBundleWebhookCriteria{},
DestinationType: ReleaseBundleWebhookCriteria{},
UserType: EmptyWebhookCriteria{},
ReleaseBundleV2Type: ReleaseBundleV2WebhookCriteria{},
ReleaseBundleV2PromotionType: ReleaseBundleV2PromotionWebhookCriteria{},
ArtifactLifecycleType: EmptyWebhookCriteria{},
}

var domainPackLookup = map[string]func(map[string]interface{}) map[string]interface{}{
"artifact": packRepoCriteria,
"artifact_property": packRepoCriteria,
"docker": packRepoCriteria,
"build": packBuildCriteria,
"release_bundle": packReleaseBundleCriteria,
"distribution": packReleaseBundleCriteria,
"artifactory_release_bundle": packReleaseBundleCriteria,
"destination": packReleaseBundleCriteria,
"user": packEmptyCriteria,
"release_bundle_v2": packReleaseBundleV2Criteria,
"release_bundle_v2_promotion": packReleaseBundleV2PromotionCriteria,
"artifact_lifecycle": packEmptyCriteria,
ArtifactType: packRepoCriteria,
ArtifactPropertyType: packRepoCriteria,
DockerType: packRepoCriteria,
BuildType: packBuildCriteria,
ReleaseBundleType: packReleaseBundleCriteria,
DistributionType: packReleaseBundleCriteria,
ArtifactoryReleaseBundleType: packReleaseBundleCriteria,
DestinationType: packReleaseBundleCriteria,
UserType: packEmptyCriteria,
ReleaseBundleV2Type: packReleaseBundleV2Criteria,
ReleaseBundleV2PromotionType: packReleaseBundleV2PromotionCriteria,
ArtifactLifecycleType: packEmptyCriteria,
}

var domainUnpackLookup = map[string]func(map[string]interface{}, BaseWebhookCriteria) interface{}{
"artifact": unpackRepoCriteria,
"artifact_property": unpackRepoCriteria,
"docker": unpackRepoCriteria,
"build": unpackBuildCriteria,
"release_bundle": unpackReleaseBundleCriteria,
"distribution": unpackReleaseBundleCriteria,
"artifactory_release_bundle": unpackReleaseBundleCriteria,
"destination": unpackReleaseBundleCriteria,
"user": unpackEmptyCriteria,
"release_bundle_v2": unpackReleaseBundleV2Criteria,
"release_bundle_v2_promotion": unpackReleaseBundleV2PromotionCriteria,
"artifact_lifecycle": unpackEmptyCriteria,
ArtifactType: unpackRepoCriteria,
ArtifactPropertyType: unpackRepoCriteria,
DockerType: unpackRepoCriteria,
BuildType: unpackBuildCriteria,
ReleaseBundleType: unpackReleaseBundleCriteria,
DistributionType: unpackReleaseBundleCriteria,
ArtifactoryReleaseBundleType: unpackReleaseBundleCriteria,
DestinationType: unpackReleaseBundleCriteria,
UserType: unpackEmptyCriteria,
ReleaseBundleV2Type: unpackReleaseBundleV2Criteria,
ReleaseBundleV2PromotionType: unpackReleaseBundleV2PromotionCriteria,
ArtifactLifecycleType: unpackEmptyCriteria,
}

var domainSchemaLookup = func(version int, isCustom bool, webhookType string) map[string]map[string]*schema.Schema {
return map[string]map[string]*schema.Schema{
"artifact": repoWebhookSchema(webhookType, version, isCustom),
"artifact_property": repoWebhookSchema(webhookType, version, isCustom),
"docker": repoWebhookSchema(webhookType, version, isCustom),
"build": buildWebhookSchema(webhookType, version, isCustom),
"release_bundle": releaseBundleWebhookSchema(webhookType, version, isCustom),
"distribution": releaseBundleWebhookSchema(webhookType, version, isCustom),
"artifactory_release_bundle": releaseBundleWebhookSchema(webhookType, version, isCustom),
"destination": releaseBundleWebhookSchema(webhookType, version, isCustom),
"user": userWebhookSchema(webhookType, version, isCustom),
"release_bundle_v2": releaseBundleV2WebhookSchema(webhookType, version, isCustom),
"release_bundle_v2_promotion": releaseBundleV2PromotionWebhookSchema(webhookType, version, isCustom),
"artifact_lifecycle": artifactLifecycleWebhookSchema(webhookType, version, isCustom),
ArtifactType: repoWebhookSchema(webhookType, version, isCustom),
ArtifactPropertyType: repoWebhookSchema(webhookType, version, isCustom),
DockerType: repoWebhookSchema(webhookType, version, isCustom),
BuildType: buildWebhookSchema(webhookType, version, isCustom),
ReleaseBundleType: releaseBundleWebhookSchema(webhookType, version, isCustom),
DistributionType: releaseBundleWebhookSchema(webhookType, version, isCustom),
ArtifactoryReleaseBundleType: releaseBundleWebhookSchema(webhookType, version, isCustom),
DestinationType: releaseBundleWebhookSchema(webhookType, version, isCustom),
UserType: userWebhookSchema(webhookType, version, isCustom),
ReleaseBundleV2Type: releaseBundleV2WebhookSchema(webhookType, version, isCustom),
ReleaseBundleV2PromotionType: releaseBundleV2PromotionWebhookSchema(webhookType, version, isCustom),
ArtifactLifecycleType: artifactLifecycleWebhookSchema(webhookType, version, isCustom),
}
}

@@ -212,18 +226,18 @@ var packCriteria = func(d *schema.ResourceData, webhookType string, criteria map
}

var domainCriteriaValidationLookup = map[string]func(context.Context, map[string]interface{}) error{
"artifact": repoCriteriaValidation,
"artifact_property": repoCriteriaValidation,
"docker": repoCriteriaValidation,
"build": buildCriteriaValidation,
"release_bundle": releaseBundleCriteriaValidation,
"distribution": releaseBundleCriteriaValidation,
"artifactory_release_bundle": releaseBundleCriteriaValidation,
"destination": releaseBundleCriteriaValidation,
"user": emptyCriteriaValidation,
"release_bundle_v2": releaseBundleV2CriteriaValidation,
"release_bundle_v2_promotion": emptyCriteriaValidation,
"artifact_lifecycle": emptyCriteriaValidation,
ArtifactType: repoCriteriaValidation,
ArtifactPropertyType: repoCriteriaValidation,
DockerType: repoCriteriaValidation,
BuildType: buildCriteriaValidation,
ReleaseBundleType: releaseBundleCriteriaValidation,
DistributionType: releaseBundleCriteriaValidation,
ArtifactoryReleaseBundleType: releaseBundleCriteriaValidation,
DestinationType: releaseBundleCriteriaValidation,
UserType: emptyCriteriaValidation,
ReleaseBundleV2Type: releaseBundleV2CriteriaValidation,
ReleaseBundleV2PromotionType: emptyCriteriaValidation,
ArtifactLifecycleType: emptyCriteriaValidation,
}

var emptyCriteriaValidation = func(ctx context.Context, criteria map[string]interface{}) error {
@@ -359,7 +373,7 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
SetPathParam("webhookKey", data.Id()).
SetResult(&webhook).
SetError(&artifactoryError).
Get(WhUrl)
Get(WebhookURL)

if err != nil {
return diag.FromErr(err)
@@ -396,7 +410,7 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
SetBody(webhook).
AddRetryCondition(retryOnProxyError).
SetError(&artifactoryError).
Post(webhooksUrl)
Post(webhooksURL)
if err != nil {
return diag.FromErr(err)
}
@@ -424,7 +438,7 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
SetBody(webhook).
AddRetryCondition(retryOnProxyError).
SetError(&artifactoryError).
Put(WhUrl)
Put(WebhookURL)
if err != nil {
return diag.FromErr(err)
}
@@ -445,7 +459,7 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
resp, err := m.(util.ProviderMetadata).Client.R().
SetPathParam("webhookKey", data.Id()).
SetError(&artifactoryError).
Delete(WhUrl)
Delete(WebhookURL)

if err != nil {
return diag.FromErr(err)
Original file line number Diff line number Diff line change
@@ -479,7 +479,7 @@ func testCheckWebhook(id string, request *resty.Request) (*resty.Response, error
return request.
SetPathParam("webhookKey", id).
AddRetryCondition(client.NeverRetry).
Get(webhook.WhUrl)
Get(webhook.WebhookURL)
}

func TestAccWebhook_GH476WebHookChangeBearerSet0(t *testing.T) {