~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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "sort"
    "strings"
)

type ListInstancesRequest struct {
    ServiceName string
}

// ListInstances returns a slice of all instances for all deployments for the
// given hosted service name.
func (api *ManagementAPI) ListInstances(request *ListInstancesRequest) ([]RoleInstance, error) {
    instances := []RoleInstance{}
    properties, err := api.GetHostedServiceProperties(request.ServiceName, true)
    if err != nil {
        return nil, err
    }
    for _, deployment := range properties.Deployments {
        instances = append(instances, deployment.RoleInstanceList...)
    }
    return instances, nil
}

// ListAllDeploymentsRequest is a parameter object for ListAllDeployments.
type ListAllDeploymentsRequest struct {
    // ServiceName restricts the listing to the given service.
    ServiceName string
}

// ListAllDeployments returns a slice containing all deployments that match
// the request.
func (api *ManagementAPI) ListAllDeployments(request *ListAllDeploymentsRequest) ([]Deployment, error) {
    properties, err := api.GetHostedServiceProperties(request.ServiceName, true)
    if err != nil {
        return nil, err
    }
    return properties.Deployments, nil
}

// ListDeploymentsRequest is a parameter object for ListDeployments.
type ListDeploymentsRequest struct {
    // ServiceName restricts the listing to the given service.
    ServiceName string
    // DeploymentNames is a set (its value type is ignored) that restricts the
    // listing to those deployments which it contains.
    DeploymentNames []string
}

// ListDeployments returns a slice containing specific deployments, insofar
// as they match the request.
func (api *ManagementAPI) ListDeployments(request *ListDeploymentsRequest) ([]Deployment, error) {
    properties, err := api.GetHostedServiceProperties(request.ServiceName, true)
    if err != nil {
        return nil, err
    }
    // Filter the deployment list according to the given names.
    filter := make(map[string]bool)
    for _, name := range request.DeploymentNames {
        filter[name] = true
    }
    deployments := []Deployment{}
    for _, deployment := range properties.Deployments {
        if _, ok := filter[deployment.Name]; ok {
            deployments = append(deployments, deployment)
        }
    }
    return deployments, nil
}

type ListSpecificHostedServicesRequest struct {
    ServiceNames []string
}

// ListSpecificHostedServices returns a slice containing specific
// HostedServiceDescriptor objects, insofar as they match the request.
func (api *ManagementAPI) ListSpecificHostedServices(request *ListSpecificHostedServicesRequest) ([]HostedServiceDescriptor, error) {
    allServices, err := api.ListHostedServices()
    if err != nil {
        return nil, err
    }
    // Filter the service list according to the given names.
    filter := make(map[string]bool)
    for _, name := range request.ServiceNames {
        filter[name] = true
    }
    services := []HostedServiceDescriptor{}
    for _, service := range allServices {
        if _, ok := filter[service.ServiceName]; ok {
            services = append(services, service)
        }
    }
    return services, nil
}

type ListPrefixedHostedServicesRequest struct {
    ServiceNamePrefix string
}

// ListPrefixedHostedServices returns a slice containing specific
// HostedServiceDescriptor objects, insofar as they match the request.
func (api *ManagementAPI) ListPrefixedHostedServices(request *ListPrefixedHostedServicesRequest) ([]HostedServiceDescriptor, error) {
    services, err := api.ListHostedServices()
    if err != nil {
        return nil, err
    }
    resServices := []HostedServiceDescriptor{}
    for _, service := range services {
        if strings.HasPrefix(service.ServiceName, request.ServiceNamePrefix) {
            resServices = append(resServices, service)
        }
    }
    services = resServices
    return services, nil
}

type DestroyDeploymentRequest struct {
    ServiceName    string
    DeploymentName string
}

// DestroyDeployment brings down all resources within a deployment - running
// instances, disks, etc. - and deletes the deployment itself.
func (api *ManagementAPI) DestroyDeployment(request *DestroyDeploymentRequest) error {
    deployment, err := api.GetDeployment(&GetDeploymentRequest{
        ServiceName:    request.ServiceName,
        DeploymentName: request.DeploymentName,
    })
    if err != nil {
        if IsNotFoundError(err) {
            return nil
        }
        return err
    }
    // 1. Get the list of the VM disks.
    diskNameMap := make(map[string]bool)
    for _, role := range deployment.RoleList {
        for _, osVHD := range role.OSVirtualHardDisk {
            diskNameMap[osVHD.DiskName] = true
        }
    }
    // 2. Delete deployment.  This will delete all the role instances inside
    // this deployment as a side effect.
    err = api.DeleteDeployment(request.ServiceName, request.DeploymentName)
    if err != nil && !IsNotFoundError(err) {
        return err
    }
    // Sort the disk names to aid testing.
    diskNames := []string{}
    for diskName := range diskNameMap {
        diskNames = append(diskNames, diskName)
    }
    sort.Strings(diskNames)
    // 3. Delete the disks.
    for _, diskName := range diskNames {
        err = api.DeleteDisk(diskName)
        if err != nil && !IsNotFoundError(err) {
            return err
        }
    }
    // Done.
    return nil
}

type DestroyHostedServiceRequest struct {
    ServiceName string
}

// DestroyHostedService destroys all of the hosted service's contained
// deployments then deletes the hosted service itself.
func (api *ManagementAPI) DestroyHostedService(request *DestroyHostedServiceRequest) error {
    // 1. Get properties.
    properties, err := api.GetHostedServiceProperties(request.ServiceName, true)
    if err != nil {
        if IsNotFoundError(err) {
            return nil
        }
        return err
    }
    // 2. Delete deployments.
    for _, deployment := range properties.Deployments {
        err := api.DestroyDeployment(&DestroyDeploymentRequest{
            ServiceName:    request.ServiceName,
            DeploymentName: deployment.Name,
        })
        if err != nil {
            return err
        }
    }
    // 3. Delete service.
    err = api.DeleteHostedService(request.ServiceName)
    if err != nil && !IsNotFoundError(err) {
        return err
    }
    // Done.
    return nil
}