-
Notifications
You must be signed in to change notification settings - Fork 769
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
resource/github_repository_webhook: Avoid spurious diff #133
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceGithubRepositoryWebhookMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found GitHub Repository Webhook State v0; migrating to v1") | ||
return migrateGithubRepositoryWebhookStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateGithubRepositoryWebhookStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
log.Printf("[DEBUG] GitHub Repository Webhook Attributes before migration: %#v", is.Attributes) | ||
|
||
prefix := "configuration." | ||
|
||
delete(is.Attributes, prefix+"%") | ||
|
||
// Read & delete old keys | ||
oldKeys := make(map[string]string, 0) | ||
for k, v := range is.Attributes { | ||
if strings.HasPrefix(k, prefix) { | ||
oldKeys[k] = v | ||
|
||
// Delete old keys | ||
delete(is.Attributes, k) | ||
} | ||
} | ||
|
||
// Write new keys | ||
for k, v := range oldKeys { | ||
newKey := "configuration.0." + strings.TrimPrefix(k, prefix) | ||
is.Attributes[newKey] = v | ||
} | ||
|
||
is.Attributes[prefix+"#"] = "1" | ||
|
||
log.Printf("[DEBUG] GitHub Repository Webhook Attributes after State Migration: %#v", is.Attributes) | ||
|
||
return is, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package github | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestMigrateGithubRepositoryWebhookStateV0toV1(t *testing.T) { | ||
oldAttributes := map[string]string{ | ||
"configuration.%": "4", | ||
"configuration.content_type": "form", | ||
"configuration.insecure_ssl": "0", | ||
"configuration.secret": "blablah", | ||
"configuration.url": "https://google.co.uk/", | ||
} | ||
|
||
newState, err := migrateGithubRepositoryWebhookStateV0toV1(&terraform.InstanceState{ | ||
ID: "nonempty", | ||
Attributes: oldAttributes, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedAttributes := map[string]string{ | ||
"configuration.#": "1", | ||
"configuration.0.content_type": "form", | ||
"configuration.0.insecure_ssl": "0", | ||
"configuration.0.secret": "blablah", | ||
"configuration.0.url": "https://google.co.uk/", | ||
} | ||
if !reflect.DeepEqual(newState.Attributes, expectedAttributes) { | ||
t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n", | ||
expectedAttributes, newState.Attributes) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ func resourceGithubRepositoryWebhook() *schema.Resource { | |
}, | ||
}, | ||
|
||
SchemaVersion: 1, | ||
MigrateState: resourceGithubRepositoryWebhookMigrateState, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
|
@@ -46,8 +49,38 @@ func resourceGithubRepositoryWebhook() *schema.Resource { | |
Set: schema.HashString, | ||
}, | ||
"configuration": { | ||
Type: schema.TypeMap, | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"url": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"content_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"secret": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Sensitive: true, | ||
DiffSuppressFunc: func(k, oldV, newV string, d *schema.ResourceData) bool { | ||
maskedSecret := "********" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't really worked with sensitive attributes, but it feels strange to me for a maskedSecret to ALWAYS be 8 asterisks, is this code robust? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GitHub unfortunately doesn't document this, but I assumed it's a static placeholder they just return. Why would/should the API ever return different number of asterisks? btw. this is unrelated to how we treat sensitive data in the schema via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, I think a code comment might be appropriate in this case mentioning it appears to be an undocumented GH api response. |
||
if oldV == maskedSecret { | ||
return true | ||
} | ||
|
||
return oldV == newV | ||
}, | ||
}, | ||
"insecure_ssl": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"url": { | ||
Type: schema.TypeString, | ||
|
@@ -77,7 +110,11 @@ func resourceGithubRepositoryWebhookObject(d *schema.ResourceData) *github.Hook | |
URL: &url, | ||
Events: events, | ||
Active: &active, | ||
Config: d.Get("configuration").(map[string]interface{}), | ||
} | ||
|
||
config := d.Get("configuration").([]interface{}) | ||
if len(config) > 0 { | ||
hook.Config = config[0].(map[string]interface{}) | ||
} | ||
|
||
return hook | ||
|
@@ -115,7 +152,7 @@ func resourceGithubRepositoryWebhookRead(d *schema.ResourceData, meta interface{ | |
d.Set("url", hook.URL) | ||
d.Set("active", hook.Active) | ||
d.Set("events", hook.Events) | ||
d.Set("configuration", hook.Config) | ||
d.Set("configuration", []interface{}{hook.Config}) | ||
|
||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any chance this could be done all in the previous loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was actually the very first version of this PR 😃until I saw the test intermittently failing as the for loop kept ranging over some new elements which were just added.
Why? https://golang.org/ref/spec#For_range
Short answer: No.
Feel free to have a play yourself: https://play.golang.org/p/Tm1FPyjwd0m 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhhh yes I see now. 👍