Merge lp:~rvb/gwacl/random-initializers into lp:gwacl

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: 175
Merged at revision: 174
Proposed branch: lp:~rvb/gwacl/random-initializers
Merge into: lp:gwacl
Diff against target: 75 lines (+40/-2)
3 files modified
example/management/run.go (+2/-2)
names.go (+30/-0)
names_test.go (+8/-0)
To merge this branch: bzr merge lp:~rvb/gwacl/random-initializers
Reviewer Review Type Date Requested Status
Gavin Panella Approve
Review via email: mp+173935@code.launchpad.net

Commit message

Add MakeRandomRoleName and MakeRandomDiskName.

Description of the change

We need random role names and disk names in the provider. The Azure doc isn't very clear about the limitation here so rather than putting the knowledge of what these identifiers should look like inside the Azure provider, it's much better to have methods to get random names in gwacl.

The testing is minimal (I'm basically only checking that the methods return a non-empty string) but it's probably enough given the nature of the tested methods.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) wrote :

Looks good.

[1]

+    // Azure documentation does not say what the maximum size of a disk name
+    // is.  Testing indicate that 50 works.
+    return makeRandomIdentifier(prefix, 50)

That's a lot of randomness. If prefix is, say, 10 chars long, then
this can produce ~1.8e62 different strings. A 128 bit hash (or IPv6
address, for example) only has ~3.4e38 different values.

It would also be good to have a character, like a hyphen, between the
prefix and the random part, although we could just suggest that as a
convention for callers.

review: Approve
Revision history for this message
Raphaël Badin (rvb) wrote :

> Looks good.
>
>
> [1]
>
> +    // Azure documentation does not say what the maximum size of a disk name
> +    // is.  Testing indicate that 50 works.
> +    return makeRandomIdentifier(prefix, 50)
>
> That's a lot of randomness. If prefix is, say, 10 chars long, then
> this can produce ~1.8e62 different strings. A 128 bit hash (or IPv6
> address, for example) only has ~3.4e38 different values.

Well, we can always reduce that later if we feel like it, but the documentation suggests that a role name must be unique *without Azure* so let's get all the randomness we can get :).

> It would also be good to have a character, like a hyphen, between the
> prefix and the random part, although we could just suggest that as a
> convention for callers.

Yeah, I'd rather left that up to the caller.

Revision history for this message
Gavin Panella (allenap) wrote :

> Well, we can always reduce that later if we feel like it, but the
> documentation suggests that a role name must be unique *without
> Azure* so let's get all the randomness we can get :).

I might be wildly off, but I /think/ that:

- using a prefix of 10 characters to create a disk name, thus leaving
  40 characters as random, with 36 choices per character,

- assuming that we're happy with a one in a quadrillion (10e15) chance
  of collision,

- that 10,000 new disks are created each second,

means that it'll take about 2 trillion years until the probability of
a collision rises above 1 in a quadrillion. Or, when the universe is
~140 times older than it already is.

Just sayin' :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'example/management/run.go'
2--- example/management/run.go 2013-07-10 10:18:16 +0000
3+++ example/management/run.go 2013-07-10 13:00:37 +0000
4@@ -166,7 +166,7 @@
5 // security hazard in such a short-lived private instance.
6 password := "Ubuntu123"
7 username := "ubuntu"
8- vhdName := makeRandomIdentifier("gwacldisk", 16)
9+ vhdName := gwacl.MakeRandomDiskName("gwacldisk")
10 linuxConfigurationSet := gwacl.NewLinuxProvisioningConfigurationSet(hostname, username, password, "TEST_USER_DATA", "false")
11 inputendpoint := gwacl.InputEndpoint{LocalPort: 22, Name: "sshport", Port: 22, Protocol: "TCP"}
12 networkConfigurationSet := gwacl.NewNetworkConfigurationSet([]gwacl.InputEndpoint{inputendpoint})
13@@ -189,7 +189,7 @@
14 diskName := makeRandomIdentifier("gwacldisk", 16)
15 diskLabel := makeRandomIdentifier("gwacl", 64)
16 vhd := gwacl.NewOSVirtualHardDisk("", diskLabel, diskName, mediaLink, sourceImageName, "Linux")
17- roleName := makeRandomIdentifier("gwaclrole", 16)
18+ roleName := gwacl.MakeRandomRoleName("gwaclrole")
19 role := gwacl.NewRole("ExtraSmall", roleName, []gwacl.ConfigurationSet{*linuxConfigurationSet, *networkConfigurationSet}, []gwacl.OSVirtualHardDisk{*vhd})
20 machineName := makeRandomIdentifier("gwaclmachine", 20)
21 deployment := gwacl.NewDeploymentForCreateVMDeployment(machineName, "Staging", machineName, []gwacl.Role{*role}, "")
22
23=== modified file 'names.go'
24--- names.go 2013-07-05 14:21:33 +0000
25+++ names.go 2013-07-10 13:00:37 +0000
26@@ -72,3 +72,33 @@
27 // over 55 letters long.
28 return makeRandomIdentifier(prefix, 55)
29 }
30+
31+// MakeRandomDiskName generates a pseudo-random disk name for a virtual machine
32+// with the given prefix.
33+//
34+// The prefix must be as short as possible, be entirely in ASCII, start with
35+// a lower-case letter, and contain only lower-case letters and digits after
36+// that.
37+//
38+// Be sure your random generator is initialized, or this will always produce
39+// the same sequence!
40+func MakeRandomDiskName(prefix string) string {
41+ // Azure documentation does not say what the maximum size of a disk name
42+ // is. Testing indicate that 50 works.
43+ return makeRandomIdentifier(prefix, 50)
44+}
45+
46+// MakeRandomRoleName generates a pseudo-random role name for a virtual machine
47+// with the given prefix.
48+//
49+// The prefix must be as short as possible, be entirely in ASCII, start with
50+// a lower-case letter, and contain only lower-case letters and digits after
51+// that.
52+//
53+// Be sure your random generator is initialized, or this will always produce
54+// the same sequence!
55+func MakeRandomRoleName(prefix string) string {
56+ // Azure documentation does not say what the maximum size of a role name
57+ // is. Testing indicate that 50 works.
58+ return makeRandomIdentifier(prefix, 50)
59+}
60
61=== modified file 'names_test.go'
62--- names_test.go 2013-07-05 10:23:48 +0000
63+++ names_test.go 2013-07-10 13:00:37 +0000
64@@ -52,3 +52,11 @@
65 // In particular, the first character must still be a letter.
66 c.Check(makeRandomIdentifier("", 1), Matches, "[a-z]")
67 }
68+
69+func (*namesSuite) TestMakeRandomDiskName(c *C) {
70+ c.Check(MakeRandomDiskName(""), Not(HasLen), 0)
71+}
72+
73+func (*namesSuite) TestMakeRandomRoleName(c *C) {
74+ c.Check(MakeRandomRoleName(""), Not(HasLen), 0)
75+}

Subscribers

People subscribed via source and target branches

to all changes: