1package azureutil
2
3import (
4	"fmt"
5	"math/rand"
6	"strings"
7	"time"
8)
9
10/* Utilities */
11
12// randomAzureStorageAccountName generates a valid storage account name prefixed
13// with a predefined string. Availability of the name is not checked. Uses maximum
14// length to maximise randomness.
15func randomAzureStorageAccountName() string {
16	const (
17		maxLen = 24
18		chars  = "0123456789abcdefghijklmnopqrstuvwxyz"
19	)
20	return storageAccountPrefix + randomString(maxLen-len(storageAccountPrefix), chars)
21}
22
23// randomString generates a random string of given length using specified alphabet.
24func randomString(n int, alphabet string) string {
25	r := timeSeed()
26	b := make([]byte, n)
27	for i := range b {
28		b[i] = alphabet[r.Intn(len(alphabet))]
29	}
30	return string(b)
31}
32
33// imageName holds various components of an OS image name identifier
34type imageName struct{ publisher, offer, sku, version string }
35
36// parseImageName parses a publisher:offer:sku:version into those parts.
37func parseImageName(image string) (imageName, error) {
38	l := strings.Split(image, ":")
39	if len(l) != 4 {
40		return imageName{}, fmt.Errorf("image name %q not a valid format", image)
41	}
42	return imageName{l[0], l[1], l[2], l[3]}, nil
43}
44
45func timeSeed() *rand.Rand { return rand.New(rand.NewSource(time.Now().UTC().UnixNano())) }
46