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

[WIP] Add Triton CNS tag support. #6115

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions builtin/providers/triton/resource_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
"user_script": "user-script",
"user_data": "user-data",
"administrator_pw": "administrator-pw",
"triton_cns_status": "triton.cns.status",
}
)

Expand Down Expand Up @@ -167,6 +168,20 @@ func resourceMachine() *schema.Resource {
Optional: true,
Default: false,
},
"triton_cns_status": {
Description: "up/down flag to control Triton's CNS",
Type: schema.TypeString,
Optional: true,
Default: "up",
},
"domain_names": {
Description: "list of domain names from Triton's CNS",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

// computed resources from metadata
"root_authorized_keys": {
Expand Down Expand Up @@ -229,18 +244,13 @@ func resourceMachineCreate(d *schema.ResourceData, meta interface{}) error {
}
}

tags := map[string]string{}
for k, v := range d.Get("tags").(map[string]interface{}) {
tags[k] = v.(string)
}

machine, err := client.CreateMachine(cloudapi.CreateMachineOpts{
Name: d.Get("name").(string),
Package: d.Get("package").(string),
Image: d.Get("image").(string),
Networks: networks,
Metadata: metadata,
Tags: tags,
Tags: tagsToTritonTags(d.Get("tags").(map[string]interface{})),
FirewallEnabled: d.Get("firewall_enabled").(bool),
})
if err != nil {
Expand Down Expand Up @@ -291,13 +301,14 @@ func resourceMachineRead(d *schema.ResourceData, meta interface{}) error {
d.Set("memory", machine.Memory)
d.Set("disk", machine.Disk)
d.Set("ips", machine.IPs)
d.Set("tags", machine.Tags)
d.Set("tags", tagsFromTritonTags(machine.Tags))
d.Set("created", machine.Created)
d.Set("updated", machine.Updated)
d.Set("package", machine.Package)
d.Set("image", machine.Image)
d.Set("primaryip", machine.PrimaryIP)
d.Set("firewall_enabled", machine.FirewallEnabled)
d.Set("domain_names", machine.DomainNames)

// create and update NICs
var (
Expand Down Expand Up @@ -356,10 +367,7 @@ func resourceMachineUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("tags") {
tags := map[string]string{}
for k, v := range d.Get("tags").(map[string]interface{}) {
tags[k] = v.(string)
}
tags := tagsToTritonTags(d.Get("tags").(map[string]interface{}))

var err error
if len(tags) == 0 {
Expand Down
52 changes: 52 additions & 0 deletions builtin/providers/triton/triton_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package triton

import (
"strings"
)

var (
tritonTags = []string{
// TODO: add cns.disable when joyent/gosdc#31 is fixed.
//"triton.cns.disable",
"triton.cns.services",
"triton.cns.reverse_ptr",
}

fromTritonMap = map[string]string{}
toTritonMap = map[string]string{}
)

func init() {
for _, tritonTag := range tritonTags {
// Transform: _ -> __ followed by . -> _
// Inverse: _ -> . followed by .. -> _
terTag := strings.Replace(strings.Replace(tritonTag, "_", "__", -1),
".", "_", -1)
fromTritonMap[tritonTag] = terTag
toTritonMap[terTag] = tritonTag
}
}

// Returns Terraform's tags from Triton's machine tags
func tagsFromTritonTags(tritonTags map[string]string) map[string]string {
tags := make(map[string]string)
for k, v := range tritonTags {
if new_k, ok := fromTritonMap[k]; ok {
k = new_k
}
tags[k] = v
}
return tags
}

// Returns Triton's machine tags from Terraform tags
func tagsToTritonTags(tags map[string]interface{}) map[string]string {
tritonTags := make(map[string]string)
for k, v := range tags {
if new_k, ok := toTritonMap[k]; ok {
k = new_k
}
tritonTags[k] = v.(string)
}
return tritonTags
}
86 changes: 86 additions & 0 deletions builtin/providers/triton/triton_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package triton

import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccTritonTags_basic(t *testing.T) {
machineName := fmt.Sprintf("acctest-%d", acctest.RandInt())
step_0 := fmt.Sprintf(testAccTritonTags_basic_0, machineName)
step_10 := fmt.Sprintf(testAccTritonTags_basic_10, machineName)

machine := "triton_machine.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: step_0,
Check: resource.ComposeTestCheckFunc(
testCheckTritonMachineExists(machine),
resource.TestCheckResourceAttr(machine,
"triton_cns_status",
"up",
),
resource.TestCheckResourceAttr(machine,
"tags.triton_cns_services",
"bastion",
),
resource.TestCheckResourceAttr(machine,
"tags.triton_cns_reverse__ptr",
"www.joyent.com",
),
),
},
resource.TestStep{
Config: step_10,
Check: resource.ComposeTestCheckFunc(
func(*terraform.State) error {
time.Sleep(10 * time.Second)
return nil
},
resource.TestCheckResourceAttr(machine,
"triton_cns_status",
"down",
),
),
},
},
})
}

var testAccTritonTags_basic_0 = `
resource "triton_machine" "test" {
name = "%s"
package = "t4-standard-128M"
image = "eb9fc1ea-e19a-11e5-bb27-8b954d8c125c"

tags = {
triton_cns_services = "bastion"
triton_cns_reverse__ptr = "www.joyent.com"
}
}
`

var testAccTritonTags_basic_10 = `
resource "triton_machine" "test" {
name = "%s"
package = "t4-standard-128M"
image = "eb9fc1ea-e19a-11e5-bb27-8b954d8c125c"

triton_cns_status = "down"

tags = {
triton_cns_services = "bastion"
triton_cns_reverse__ptr = "www.joyent.com"
}
}
`
1 change: 1 addition & 0 deletions vendor/github.com/joyent/gosdc/cloudapi/machines.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.