Skip to content

Commit 4a4f2ad

Browse files
committed
provider/openstack: Add Instance Personality
This commit adds the "personality" attribute which is used to provision destination files on the instance.
1 parent 84014ed commit 4a4f2ad

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

builtin/providers/openstack/resource_openstack_compute_instance_v2.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ func resourceComputeInstanceV2() *schema.Resource {
275275
},
276276
Set: resourceComputeSchedulerHintsHash,
277277
},
278+
"personality": &schema.Schema{
279+
Type: schema.TypeSet,
280+
Optional: true,
281+
ForceNew: true,
282+
Elem: &schema.Resource{
283+
Schema: map[string]*schema.Schema{
284+
"file": &schema.Schema{
285+
Type: schema.TypeString,
286+
Required: true,
287+
},
288+
"content": &schema.Schema{
289+
Type: schema.TypeString,
290+
Required: true,
291+
},
292+
},
293+
},
294+
Set: resourceComputeInstancePersonalityHash,
295+
},
278296
},
279297
}
280298
}
@@ -334,6 +352,7 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e
334352
ConfigDrive: d.Get("config_drive").(bool),
335353
AdminPass: d.Get("admin_pass").(string),
336354
UserData: []byte(d.Get("user_data").(string)),
355+
Personality: resourceInstancePersonalityV2(d),
337356
}
338357

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

12381257
return nil
12391258
}
1259+
1260+
func resourceComputeInstancePersonalityHash(v interface{}) int {
1261+
var buf bytes.Buffer
1262+
m := v.(map[string]interface{})
1263+
buf.WriteString(fmt.Sprintf("%s-", m["file"].(string)))
1264+
1265+
return hashcode.String(buf.String())
1266+
}
1267+
1268+
func resourceInstancePersonalityV2(d *schema.ResourceData) servers.Personality {
1269+
var personalities servers.Personality
1270+
1271+
if v := d.Get("personality"); v != nil {
1272+
personalityList := v.(*schema.Set).List()
1273+
if len(personalityList) > 0 {
1274+
for _, p := range personalityList {
1275+
rawPersonality := p.(map[string]interface{})
1276+
file := servers.File{
1277+
Path: rawPersonality["file"].(string),
1278+
Contents: []byte(rawPersonality["content"].(string)),
1279+
}
1280+
1281+
log.Printf("[DEBUG] OpenStack Compute Instance Personality: %+v", file)
1282+
1283+
personalities = append(personalities, &file)
1284+
}
1285+
}
1286+
}
1287+
1288+
return personalities
1289+
}

builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,38 @@ func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
325325
})
326326
}
327327

328+
// TODO: verify the personality really exists on the instance.
329+
func TestAccComputeV2Instance_personality(t *testing.T) {
330+
var instance servers.Server
331+
var testAccComputeV2Instance_personality = fmt.Sprintf(`
332+
resource "openstack_compute_instance_v2" "foo" {
333+
name = "terraform-test"
334+
security_groups = ["default"]
335+
personality {
336+
file = "/tmp/foobar.txt"
337+
content = "happy"
338+
}
339+
personality {
340+
file = "/tmp/barfoo.txt"
341+
content = "angry"
342+
}
343+
}`)
344+
345+
resource.Test(t, resource.TestCase{
346+
PreCheck: func() { testAccPreCheck(t) },
347+
Providers: testAccProviders,
348+
CheckDestroy: testAccCheckComputeV2InstanceDestroy,
349+
Steps: []resource.TestStep{
350+
resource.TestStep{
351+
Config: testAccComputeV2Instance_personality,
352+
Check: resource.ComposeTestCheckFunc(
353+
testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
354+
),
355+
},
356+
},
357+
})
358+
}
359+
328360
func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
329361
config := testAccProvider.Meta().(*Config)
330362
computeClient, err := config.computeV2Client(OS_REGION_NAME)

website/source/docs/providers/openstack/r/compute_instance_v2.html.markdown

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ The following arguments are supported:
8585
* `volume` - (Optional) Attach an existing volume to the instance. The volume
8686
structure is described below.
8787

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

91+
* `personality` - (Optional) Customize the personality of an instance by
92+
defining one or more files and their contents. The personality structure
93+
is described below.
94+
9195
The `network` block supports:
9296

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

150+
The `personality` block supports:
151+
152+
* `file` - (Required) The absolute path of the destination file.
153+
154+
* `contents` - (Required) The contents of the file. Limited to 255 bytes.
155+
146156
## Attributes Reference
147157

148158
The following attributes are exported:

0 commit comments

Comments
 (0)