Skip to content

Commit

Permalink
Merge pull request #4623 from jtopjian/openstack-instance-personality
Browse files Browse the repository at this point in the history
provider/openstack: Add Instance Personality
  • Loading branch information
jtopjian committed Jan 13, 2016
2 parents e7d20c2 + 4a4f2ad commit 7b4e177
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ func resourceComputeInstanceV2() *schema.Resource {
},
Set: resourceComputeSchedulerHintsHash,
},
"personality": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"file": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
Set: resourceComputeInstancePersonalityHash,
},
},
}
}
Expand Down Expand Up @@ -334,6 +352,7 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e
ConfigDrive: d.Get("config_drive").(bool),
AdminPass: d.Get("admin_pass").(string),
UserData: []byte(d.Get("user_data").(string)),
Personality: resourceInstancePersonalityV2(d),
}

if keyName, ok := d.Get("key_pair").(string); ok && keyName != "" {
Expand Down Expand Up @@ -1237,3 +1256,34 @@ func checkVolumeConfig(d *schema.ResourceData) error {

return nil
}

func resourceComputeInstancePersonalityHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["file"].(string)))

return hashcode.String(buf.String())
}

func resourceInstancePersonalityV2(d *schema.ResourceData) servers.Personality {
var personalities servers.Personality

if v := d.Get("personality"); v != nil {
personalityList := v.(*schema.Set).List()
if len(personalityList) > 0 {
for _, p := range personalityList {
rawPersonality := p.(map[string]interface{})
file := servers.File{
Path: rawPersonality["file"].(string),
Contents: []byte(rawPersonality["content"].(string)),
}

log.Printf("[DEBUG] OpenStack Compute Instance Personality: %+v", file)

personalities = append(personalities, &file)
}
}
}

return personalities
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,38 @@ func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
})
}

// TODO: verify the personality really exists on the instance.
func TestAccComputeV2Instance_personality(t *testing.T) {
var instance servers.Server
var testAccComputeV2Instance_personality = fmt.Sprintf(`
resource "openstack_compute_instance_v2" "foo" {
name = "terraform-test"
security_groups = ["default"]
personality {
file = "/tmp/foobar.txt"
content = "happy"
}
personality {
file = "/tmp/barfoo.txt"
content = "angry"
}
}`)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeV2InstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeV2Instance_personality,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
),
},
},
})
}

func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
computeClient, err := config.computeV2Client(OS_REGION_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ The following arguments are supported:
* `volume` - (Optional) Attach an existing volume to the instance. The volume
structure is described below.

* `scheduler_hints` - (Optional) Provider the Nova scheduler with hints on how
* `scheduler_hints` - (Optional) Provide the Nova scheduler with hints on how
the instance should be launched. The available hints are described below.

* `personality` - (Optional) Customize the personality of an instance by
defining one or more files and their contents. The personality structure
is described below.

The `network` block supports:

* `uuid` - (Required unless `port` or `name` is provided) The network UUID to
Expand Down Expand Up @@ -143,6 +147,12 @@ The `scheduler_hints` block supports:
* `build_near_host_ip` - (Optional) An IP Address in CIDR form. The instance
will be placed on a compute node that is in the same subnet.

The `personality` block supports:

* `file` - (Required) The absolute path of the destination file.

* `contents` - (Required) The contents of the file. Limited to 255 bytes.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 7b4e177

Please sign in to comment.