Skip to content

Commit

Permalink
Fix various crashes when importing from scratch
Browse files Browse the repository at this point in the history
With optional attributes omitted in configuration.
  • Loading branch information
alexhung committed May 23, 2024
1 parent 6a8c79e commit 106c1ba
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
webhookHandler := CustomHandler{
HandlerType: "custom-webhook",
Url: h["url"].(string),
Secrets: unpackKeyValuePair(h["secrets"].(map[string]interface{})),
Proxy: h["proxy"].(string),
HttpHeaders: unpackKeyValuePair(h["http_headers"].(map[string]interface{})),
Payload: h["payload"].(string),
}

if v, ok := h["secrets"]; ok {
webhookHandler.Secrets = unpackKeyValuePair(v.(map[string]interface{}))
}

if v, ok := h["proxy"]; ok {
webhookHandler.Proxy = v.(string)
}

if v, ok := h["http_headers"]; ok {
webhookHandler.HttpHeaders = unpackKeyValuePair(v.(map[string]interface{}))
}

if v, ok := h["payload"]; ok {
webhookHandler.Payload = v.(string)
}

webhookHandlers = append(webhookHandlers, webhookHandler)
}
}
Expand Down Expand Up @@ -208,12 +221,19 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
packedHandlers := make([]interface{}, len(handlers))
for _, handler := range handlers {
packedHandler := map[string]interface{}{
"url": handler.Url,
"secrets": packSecretsCustom(handler.Secrets, d, handler.Url),
"proxy": handler.Proxy,
"http_headers": packKeyValuePair(handler.HttpHeaders),
"payload": handler.Payload,
"url": handler.Url,
"proxy": handler.Proxy,
"payload": handler.Payload,
}

if handler.Secrets != nil {
packedHandler["secrets"] = packSecretsCustom(handler.Secrets, d, handler.Url)
}

if handler.HttpHeaders != nil {
packedHandler["http_headers"] = packKeyValuePair(handler.HttpHeaders)
}

packedHandlers = append(packedHandlers, packedHandler)
}

Expand Down
58 changes: 42 additions & 16 deletions pkg/artifactory/resource/webhook/resource_artifactory_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ const WhUrl = webhooksUrl + "/{webhookKey}"
const currentSchemaVersion = 2

var unpackKeyValuePair = func(keyValuePairs map[string]interface{}) []KeyValuePair {
var KVPairs []KeyValuePair
var kvPairs []KeyValuePair
for key, value := range keyValuePairs {
keyValuePair := KeyValuePair{
Name: key,
Value: value.(string),
}
KVPairs = append(KVPairs, keyValuePair)
kvPairs = append(kvPairs, keyValuePair)
}

return KVPairs
return kvPairs
}

var packKeyValuePair = func(keyValuePairs []KeyValuePair) map[string]interface{} {
KVPairs := make(map[string]interface{})
kvPairs := make(map[string]interface{})
for _, keyValuePair := range keyValuePairs {
KVPairs[keyValuePair.Name] = keyValuePair.Value
kvPairs[keyValuePair.Name] = keyValuePair.Value
}

return KVPairs
return kvPairs
}

var domainCriteriaLookup = map[string]interface{}{
Expand Down Expand Up @@ -166,8 +166,17 @@ var packCriteria = func(d *schema.ResourceData, webhookType string, criteria map
resource := domainSchemaLookup(currentSchemaVersion, false, webhookType)[webhookType]["criteria"].Elem.(*schema.Resource)
packedCriteria := domainPackLookup[webhookType](criteria)

packedCriteria["include_patterns"] = schema.NewSet(schema.HashString, criteria["includePatterns"].([]interface{}))
packedCriteria["exclude_patterns"] = schema.NewSet(schema.HashString, criteria["excludePatterns"].([]interface{}))
includePatterns := []interface{}{}
if v, ok := criteria["includePatterns"]; ok && v != nil {
includePatterns = v.([]interface{})
}
packedCriteria["include_patterns"] = schema.NewSet(schema.HashString, includePatterns)

excludePatterns := []interface{}{}
if v, ok := criteria["excludePatterns"]; ok && v != nil {
excludePatterns = v.([]interface{})
}
packedCriteria["exclude_patterns"] = schema.NewSet(schema.HashString, excludePatterns)

return setValue("criteria", schema.NewSet(schema.HashResource(resource), []interface{}{packedCriteria}))
}
Expand All @@ -190,7 +199,7 @@ var packSecret = func(d *schema.ResourceData, url string) string {
for _, handler := range handlers {
h := handler.(map[string]interface{})
// if urls match, assign the secret value from the state
if h["url"] == url {
if h["url"].(string) == url {
secret = h["secret"].(string)
}
}
Expand All @@ -215,13 +224,26 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
// https://discuss.hashicorp.com/t/using-typeset-in-provider-always-adds-an-empty-element-on-update/18566/2
if h["url"].(string) != "" {
webhookHandler := Handler{
HandlerType: "webhook",
Url: h["url"].(string),
Secret: h["secret"].(string),
UseSecretForSigning: h["use_secret_for_signing"].(bool),
Proxy: h["proxy"].(string),
CustomHttpHeaders: unpackKeyValuePair(h["custom_http_headers"].(map[string]interface{})),
HandlerType: "webhook",
Url: h["url"].(string),
}

if v, ok := h["secret"]; ok {
webhookHandler.Secret = v.(string)
}

if v, ok := h["use_secret_for_signing"]; ok {
webhookHandler.UseSecretForSigning = v.(bool)
}

if v, ok := h["proxy"]; ok {
webhookHandler.Proxy = v.(string)
}

if v, ok := h["custom_http_headers"]; ok {
webhookHandler.CustomHttpHeaders = unpackKeyValuePair(v.(map[string]interface{}))
}

webhookHandlers = append(webhookHandlers, webhookHandler)
}
}
Expand Down Expand Up @@ -255,8 +277,12 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
"secret": packSecret(d, handler.Url),
"use_secret_for_signing": handler.UseSecretForSigning,
"proxy": handler.Proxy,
"custom_http_headers": packKeyValuePair(handler.CustomHttpHeaders),
}

if handler.CustomHttpHeaders != nil {
packedHandler["custom_http_headers"] = packKeyValuePair(handler.CustomHttpHeaders)
}

packedHandlers = append(packedHandlers, packedHandler)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ var repoWebhookSchema = func(webhookType string, version int, isCustom bool) map
}

var packRepoCriteria = func(artifactoryCriteria map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
criteria := map[string]interface{}{
"any_local": artifactoryCriteria["anyLocal"].(bool),
"any_remote": artifactoryCriteria["anyRemote"].(bool),
"any_federated": artifactoryCriteria["anyFederated"].(bool),
"any_federated": false,
"repo_keys": schema.NewSet(schema.HashString, artifactoryCriteria["repoKeys"].([]interface{})),
}

if v, ok := artifactoryCriteria["anyFederated"]; ok {
criteria["any_federated"] = v.(bool)
}

return criteria
}

var unpackRepoCriteria = func(terraformCriteria map[string]interface{}, baseCriteria BaseWebhookCriteria) interface{} {
Expand Down
Loading

0 comments on commit 106c1ba

Please sign in to comment.