~wallyworld/gwacl/fix-request-eof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "fmt"
    "math/rand"
    "strings"
    "time"
)

// This should be refactored at some point, it does not belong in here.
// Perhaps we can add it to gocheck, or start a testtools-like project.
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"

func MakeRandomString(length int) string {
    dest := make([]byte, length)
    for i := range dest {
        num := rand.Intn(len(chars))
        rand_char := chars[num]
        dest[i] = rand_char
    }
    return string(dest)
}

func MakeRandomBool() bool {
    v := rand.Intn(2)
    if v == 0 {
        return false
    }
    return true
}

func BoolToString(v bool) string {
    if v {
        return "true"
    }
    return "false"
}

func StringToBool(v string) bool {
    switch strings.ToLower(v) {
    case "true":
        return true
    case "false":
        return false
    }
    panic(fmt.Errorf("can't convert '%s' to a bool", v))
}

type endpointParams struct {
    setname   string
    localport int
    name      string
    port      int
    protocol  string
}

func makeEndpoint(params endpointParams) *InputEndpoint {
    if params.setname == "" {
        params.setname = MakeRandomString(10)
    }
    if params.name == "" {
        params.name = MakeRandomString(10)
    }
    if params.protocol == "" {
        params.protocol = MakeRandomString(10)
    }
    if params.localport == 0 {
        params.localport = rand.Intn(65535)
    }
    if params.port == 0 {
        params.port = rand.Intn(65535)
    }

    return &InputEndpoint{
        LoadBalancedEndpointSetName: params.setname,
        LocalPort:                   params.localport,
        Name:                        params.name,
        Port:                        params.port,
        Protocol:                    params.protocol}
}

func makeLinuxProvisioningConfiguration() *LinuxProvisioningConfiguration {
    hostname := MakeRandomString(10)
    username := MakeRandomString(10)
    password := MakeRandomString(10)
    disable_ssh := MakeRandomBool()

    return &LinuxProvisioningConfiguration{
        ConfigurationSetType: "LinuxProvisioningConfiguration",
        Hostname:             hostname,
        Username:             username,
        Password:             password,
        DisableSshPasswordAuthentication: disable_ssh}
}

func makeOSVirtualHardDisk() *OSVirtualHardDisk {
    HostCaching := BoolToString(MakeRandomBool())
    DiskLabel := MakeRandomString(10)
    DiskName := MakeRandomString(10)
    MediaLink := MakeRandomString(10)
    SourceImageName := MakeRandomString(10)

    return &OSVirtualHardDisk{
        HostCaching:     HostCaching,
        DiskLabel:       DiskLabel,
        DiskName:        DiskName,
        MediaLink:       MediaLink,
        SourceImageName: SourceImageName}
}

func makeRole() *Role {
    RoleSize := "ExtraSmall"
    RoleName := MakeRandomString(10)
    RoleType := "PersistentVMRole"
    config := makeLinuxProvisioningConfiguration()
    configset := []LinuxProvisioningConfiguration{*config}

    return &Role{
        RoleSize:         RoleSize,
        RoleName:         RoleName,
        RoleType:         RoleType,
        ConfigurationSet: configset}
}

func makeDnsServer() *DnsServer {
    name := MakeRandomString(10)
    address := MakeRandomString(10)

    return &DnsServer{
        Name:    name,
        Address: address}
}

func makeDeployment() *Deployment {
    Name := MakeRandomString(10)
    DeploymentSlot := "Staging"
    Label := MakeRandomString(10)
    VirtualNetworkName := MakeRandomString(10)
    role := makeRole()
    RoleList := []Role{*role}
    Dns := []DnsServer{*makeDnsServer()}

    return &Deployment{
        XMLNS:              XMLNS,
        XMLNS_I:            XMLNS_I,
        Name:               Name,
        DeploymentSlot:     DeploymentSlot,
        Label:              Label,
        RoleList:           RoleList,
        VirtualNetworkName: VirtualNetworkName,
        Dns:                Dns}
}

func makeCreateStorageServiceInput() *CreateStorageServiceInput {
    ServiceName := MakeRandomString(10)
    Description := MakeRandomString(10)
    Label := MakeRandomString(10)
    AffinityGroup := MakeRandomString(10)
    Location := MakeRandomString(10)
    GeoReplicationEnabled := BoolToString(MakeRandomBool())
    ExtendedProperties := []ExtendedProperty{ExtendedProperty{
        Name:  MakeRandomString(10),
        Value: MakeRandomString(10)}}

    return &CreateStorageServiceInput{
        XMLNS:                 XMLNS,
        ServiceName:           ServiceName,
        Description:           Description,
        Label:                 Label,
        AffinityGroup:         AffinityGroup,
        Location:              Location,
        GeoReplicationEnabled: GeoReplicationEnabled,
        ExtendedProperties:    ExtendedProperties}
}

func init() {
    // Staggeringly, rand will produce the same numbers from the start of
    // program invocation unless you seed it ...
    rand.Seed(int64(time.Now().Nanosecond()))
}