Last active
January 19, 2018 21:24
-
-
Save coingraham/ee23948cbca7f615c7db782d9e30086a to your computer and use it in GitHub Desktop.
Terraform Go code for generating the instance attachment IDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Update the variables below from the output of your terraform apply, assuming the attachment name is "attachment". | |
| // Then paste this into https://play.golang.org/ and run it. This example is here https://play.golang.org/p/UgXxMqbdxH | |
| // Copied from the terraform code directly at https://github.com/hashicorp/terraform | |
| // output "attachment_name" { | |
| // value = "${aws_volume_attachment.attachment.device_name}" | |
| // } | |
| // output "attachment_instance_id" { | |
| // value = "${aws_volume_attachment.attachment.instance_id}" | |
| // } | |
| // output "attachment_vol_id" { | |
| // value = "${aws_volume_attachment.attachment.volume_id}" | |
| // } | |
| package main | |
| import ( | |
| "bytes" | |
| "fmt" | |
| "hash/crc32" | |
| ) | |
| func return_tf_hash(s string) int { | |
| v := int(crc32.ChecksumIEEE([]byte(s))) | |
| if v >= 0 { | |
| return v | |
| } | |
| if -v >= 0 { | |
| return -v | |
| } | |
| // v == MinInt | |
| return 0 | |
| } | |
| func volumeAttachmentID(name, volumeID, instanceID string) string { | |
| var buf bytes.Buffer | |
| buf.WriteString(fmt.Sprintf("%s-", name)) | |
| buf.WriteString(fmt.Sprintf("%s-", instanceID)) | |
| buf.WriteString(fmt.Sprintf("%s-", volumeID)) | |
| return fmt.Sprintf("vai-%d", return_tf_hash(buf.String())) | |
| } | |
| func main() { | |
| var name = "sdf" | |
| var vID = "vol-09692af1265f50676" | |
| var iID = "i-04e90f372c3653925" | |
| fmt.Println(volumeAttachmentID(name, vID, iID)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment